Skip to content

Commit 6e61a0a

Browse files
committed
Use Val(true/false) for qrfact(!) and lufact(!)
1 parent 110cc5f commit 6e61a0a

File tree

8 files changed

+55
-51
lines changed

8 files changed

+55
-51
lines changed

NEWS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ Library improvements
8080
* Added `unique!` which is an inplace version of `unique` ([#20549]).
8181

8282
* Uses of `Val{c}` in `Base` has been replaced with `Val{c}()`, which is now easily
83-
accessible via the `@pure` constructor `Val(c)`. Functions are defined as
84-
`f(::Val{c}) = ...` and called by `f(Val(c))`. Notable affected function are:
85-
`ntuple`, `fill_to_length`, `Base.literal_pow`.
83+
accessible via the `@pure` constructor `Val(c)`. Functions are defined as
84+
`f(::Val{c}) = ...` and called by `f(Val(c))`. Notable affected functions include:
85+
`ntuple`, `Base.literal_pow`, `sqrtm`, `lufact`, `lufact!`, `qrfact`, `qrfact!`.
8686

8787
Compiler/Runtime improvements
8888
-----------------------------

base/deprecated.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,10 @@ export conv, conv2, deconv, filt, filt!, xcorr
14901490
@deprecate literal_pow{N}(a,b , ::Type{Val{N}}) literal_pow(f, Val(N))
14911491
#@deprecate IteratorsMD.split(t, V::Type{Val{n}}) IteratorsMD.split(t, Val(n))
14921492
@deprecate sqrtm{T,realmatrix}(A::UpperTriangular{T},::Type{Val{realmatrix}}) sqrtm(A, Val(realmatrix))
1493+
@deprecate lufact(A, pivot::Union{Type{Val{false}}, Type{Val{true}}}) lufact(A, pivot)
1494+
@deprecate lufact!(A, pivot::Union{Type{Val{false}}, Type{Val{true}}}) lufact!(A, pivot)
1495+
@deprecate qrfact(A, pivot::Union{Type{Val{false}}, Type{Val{true}}}) qrfact(A, pivot)
1496+
@deprecate qrfact!(A, pivot::Union{Type{Val{false}}, Type{Val{true}}}) qrfact!(A, pivot)
14931497

14941498
# END 0.7 deprecations
14951499

base/linalg/lu.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ end
1212
LU(factors::AbstractMatrix{T}, ipiv::Vector{BlasInt}, info::BlasInt) where {T} = LU{T,typeof(factors)}(factors, ipiv, info)
1313

1414
# StridedMatrix
15-
function lufact!(A::StridedMatrix{T}, pivot::Union{Type{Val{false}}, Type{Val{true}}} = Val{true}) where T<:BlasFloat
16-
if pivot === Val{false}
15+
function lufact!(A::StridedMatrix{T}, pivot::Union{Val{false}, Val{true}} = Val(true)) where T<:BlasFloat
16+
if pivot === Val(false)
1717
return generic_lufact!(A, pivot)
1818
end
1919
lpt = LAPACK.getrf!(A)
2020
return LU{T,typeof(A)}(lpt[1], lpt[2], lpt[3])
2121
end
2222

2323
"""
24-
lufact!(A, pivot=Val{true}) -> LU
24+
lufact!(A, pivot=Val(true)) -> LU
2525
2626
`lufact!` is the same as [`lufact`](@ref), but saves space by overwriting the
2727
input `A`, instead of creating a copy. An [`InexactError`](@ref)
2828
exception is thrown if the factorization produces a number not representable by the
2929
element type of `A`, e.g. for integer types.
3030
"""
31-
lufact!(A::StridedMatrix, pivot::Union{Type{Val{false}}, Type{Val{true}}} = Val{true}) = generic_lufact!(A, pivot)
32-
function generic_lufact!(A::StridedMatrix{T}, ::Type{Val{Pivot}} = Val{true}) where {T,Pivot}
31+
lufact!(A::StridedMatrix, pivot::Union{Val{false}, Val{true}} = Val(true)) = generic_lufact!(A, pivot)
32+
function generic_lufact!(A::StridedMatrix{T}, ::Val{Pivot} = Val(true)) where {T,Pivot}
3333
m, n = size(A)
3434
minmn = min(m,n)
3535
info = 0
@@ -79,12 +79,12 @@ end
7979

8080
# floating point types doesn't have to be promoted for LU, but should default to pivoting
8181
lufact(A::Union{AbstractMatrix{T}, AbstractMatrix{Complex{T}}},
82-
pivot::Union{Type{Val{false}}, Type{Val{true}}} = Val{true}) where {T<:AbstractFloat} =
82+
pivot::Union{Val{false}, Val{true}} = Val(true)) where {T<:AbstractFloat} =
8383
lufact!(copy(A), pivot)
8484

8585
# for all other types we must promote to a type which is stable under division
8686
"""
87-
lufact(A [,pivot=Val{true}]) -> F::LU
87+
lufact(A [,pivot=Val(true)]) -> F::LU
8888
8989
Compute the LU factorization of `A`.
9090
@@ -135,7 +135,7 @@ julia> F[:L] * F[:U] == A[F[:p], :]
135135
true
136136
```
137137
"""
138-
function lufact(A::AbstractMatrix{T}, pivot::Union{Type{Val{false}}, Type{Val{true}}}) where T
138+
function lufact(A::AbstractMatrix{T}, pivot::Union{Val{false}, Val{true}}) where T
139139
S = typeof(zero(T)/one(T))
140140
AA = similar(A, S, size(A))
141141
copy!(AA, A)
@@ -146,13 +146,13 @@ function lufact(A::AbstractMatrix{T}) where T
146146
S = typeof(zero(T)/one(T))
147147
AA = similar(A, S, size(A))
148148
copy!(AA, A)
149-
F = lufact!(AA, Val{false})
149+
F = lufact!(AA, Val(false))
150150
if F.info == 0
151151
return F
152152
else
153153
AA = similar(A, S, size(A))
154154
copy!(AA, A)
155-
return lufact!(AA, Val{true})
155+
return lufact!(AA, Val(true))
156156
end
157157
end
158158

@@ -162,11 +162,11 @@ lufact(F::LU) = F
162162
lu(x::Number) = (one(x), x, 1)
163163

164164
"""
165-
lu(A, pivot=Val{true}) -> L, U, p
165+
lu(A, pivot=Val(true)) -> L, U, p
166166
167167
Compute the LU factorization of `A`, such that `A[p,:] = L*U`.
168168
By default, pivoting is used. This can be overridden by passing
169-
`Val{false}` for the second argument.
169+
`Val(false)` for the second argument.
170170
171171
See also [`lufact`](@ref).
172172
@@ -185,7 +185,7 @@ julia> A[p, :] == L * U
185185
true
186186
```
187187
"""
188-
function lu(A::AbstractMatrix, pivot::Union{Type{Val{false}}, Type{Val{true}}} = Val{true})
188+
function lu(A::AbstractMatrix, pivot::Union{Val{false}, Val{true}} = Val(true))
189189
F = lufact(A, pivot)
190190
F[:L], F[:U], F[:p]
191191
end
@@ -319,7 +319,7 @@ end
319319
# Tridiagonal
320320

321321
# See dgttrf.f
322-
function lufact!(A::Tridiagonal{T}, pivot::Union{Type{Val{false}}, Type{Val{true}}} = Val{true}) where T
322+
function lufact!(A::Tridiagonal{T}, pivot::Union{Val{false}, Val{true}} = Val(true)) where T
323323
n = size(A, 1)
324324
info = 0
325325
ipiv = Vector{BlasInt}(n)
@@ -334,7 +334,7 @@ function lufact!(A::Tridiagonal{T}, pivot::Union{Type{Val{false}}, Type{Val{true
334334
end
335335
for i = 1:n-2
336336
# pivot or not?
337-
if pivot === Val{false} || abs(d[i]) >= abs(dl[i])
337+
if pivot === Val(false) || abs(d[i]) >= abs(dl[i])
338338
# No interchange
339339
if d[i] != 0
340340
fact = dl[i]/d[i]
@@ -357,7 +357,7 @@ function lufact!(A::Tridiagonal{T}, pivot::Union{Type{Val{false}}, Type{Val{true
357357
end
358358
if n > 1
359359
i = n-1
360-
if pivot === Val{false} || abs(d[i]) >= abs(dl[i])
360+
if pivot === Val(false) || abs(d[i]) >= abs(dl[i])
361361
if d[i] != 0
362362
fact = dl[i]/d[i]
363363
dl[i] = fact

base/linalg/qr.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,26 +194,26 @@ function qrfactPivotedUnblocked!(A::StridedMatrix)
194194
end
195195

196196
# LAPACK version
197-
qrfact!(A::StridedMatrix{<:BlasFloat}, ::Type{Val{false}}) = QRCompactWY(LAPACK.geqrt!(A, min(minimum(size(A)), 36))...)
198-
qrfact!(A::StridedMatrix{<:BlasFloat}, ::Type{Val{true}}) = QRPivoted(LAPACK.geqp3!(A)...)
199-
qrfact!(A::StridedMatrix{<:BlasFloat}) = qrfact!(A, Val{false})
197+
qrfact!(A::StridedMatrix{<:BlasFloat}, ::Val{false}) = QRCompactWY(LAPACK.geqrt!(A, min(minimum(size(A)), 36))...)
198+
qrfact!(A::StridedMatrix{<:BlasFloat}, ::Val{true}) = QRPivoted(LAPACK.geqp3!(A)...)
199+
qrfact!(A::StridedMatrix{<:BlasFloat}) = qrfact!(A, Val(false))
200200

201201
# Generic fallbacks
202202

203203
"""
204-
qrfact!(A, pivot=Val{false})
204+
qrfact!(A, pivot=Val(false))
205205
206206
`qrfact!` is the same as [`qrfact`](@ref) when `A` is a subtype of
207207
`StridedMatrix`, but saves space by overwriting the input `A`, instead of creating a copy.
208208
An [`InexactError`](@ref) exception is thrown if the factorization produces a number not
209209
representable by the element type of `A`, e.g. for integer types.
210210
"""
211-
qrfact!(A::StridedMatrix, ::Type{Val{false}}) = qrfactUnblocked!(A)
212-
qrfact!(A::StridedMatrix, ::Type{Val{true}}) = qrfactPivotedUnblocked!(A)
213-
qrfact!(A::StridedMatrix) = qrfact!(A, Val{false})
211+
qrfact!(A::StridedMatrix, ::Val{false}) = qrfactUnblocked!(A)
212+
qrfact!(A::StridedMatrix, ::Val{true}) = qrfactPivotedUnblocked!(A)
213+
qrfact!(A::StridedMatrix) = qrfact!(A, Val(false))
214214

215215
"""
216-
qrfact(A, pivot=Val{false}) -> F
216+
qrfact(A, pivot=Val(false)) -> F
217217
218218
Compute the QR factorization of the matrix `A`: an orthogonal (or unitary if `A` is
219219
complex-valued) matrix `Q`, and an upper triangular matrix `R` such that
@@ -224,7 +224,7 @@ A = Q R
224224
225225
The returned object `F` stores the factorization in a packed format:
226226
227-
- if `pivot == Val{true}` then `F` is a [`QRPivoted`](@ref) object,
227+
- if `pivot == Val(true)` then `F` is a [`QRPivoted`](@ref) object,
228228
229229
- otherwise if the element type of `A` is a BLAS type ([`Float32`](@ref), [`Float64`](@ref),
230230
`Complex64` or `Complex128`), then `F` is a [`QRCompactWY`](@ref) object,
@@ -283,21 +283,21 @@ end
283283
qrfact(x::Number) = qrfact(fill(x,1,1))
284284

285285
"""
286-
qr(A, pivot=Val{false}; thin::Bool=true) -> Q, R, [p]
286+
qr(A, pivot=Val(false); thin::Bool=true) -> Q, R, [p]
287287
288288
Compute the (pivoted) QR factorization of `A` such that either `A = Q*R` or `A[:,p] = Q*R`.
289289
Also see [`qrfact`](@ref).
290290
The default is to compute a thin factorization. Note that `R` is not
291291
extended with zeros when the full `Q` is requested.
292292
"""
293-
qr(A::Union{Number, AbstractMatrix}, pivot::Union{Type{Val{false}}, Type{Val{true}}}=Val{false}; thin::Bool=true) =
293+
qr(A::Union{Number, AbstractMatrix}, pivot::Union{Val{false}, Val{true}}=Val(false); thin::Bool=true) =
294294
_qr(A, pivot, thin=thin)
295-
function _qr(A::Union{Number, AbstractMatrix}, ::Type{Val{false}}; thin::Bool=true)
296-
F = qrfact(A, Val{false})
295+
function _qr(A::Union{Number, AbstractMatrix}, ::Val{false}; thin::Bool=true)
296+
F = qrfact(A, Val(false))
297297
full(getq(F), thin=thin), F[:R]::Matrix{eltype(F)}
298298
end
299-
function _qr(A::Union{Number, AbstractMatrix}, ::Type{Val{true}}; thin::Bool=true)
300-
F = qrfact(A, Val{true})
299+
function _qr(A::Union{Number, AbstractMatrix}, ::Val{true}; thin::Bool=true)
300+
F = qrfact(A, Val(true))
301301
full(getq(F), thin=thin), F[:R]::Matrix{eltype(F)}, F[:p]::Vector{BlasInt}
302302
end
303303

base/sparse/spqr.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function qmult(method::Integer, QR::Factorization{Tv}, X::Dense{Tv}) where Tv<:V
137137
end
138138

139139

140-
qrfact(A::SparseMatrixCSC, ::Type{Val{true}}) = factorize(ORDERING_DEFAULT, DEFAULT_TOL, Sparse(A, 0))
140+
qrfact(A::SparseMatrixCSC, ::Val{true}) = factorize(ORDERING_DEFAULT, DEFAULT_TOL, Sparse(A, 0))
141141

142142
"""
143143
qrfact(A) -> SPQR.Factorization
@@ -147,7 +147,7 @@ The main application of this type is to solve least squares problems with [`\\`]
147147
calls the C library SPQR and a few additional functions from the library are wrapped but not
148148
exported.
149149
"""
150-
qrfact(A::SparseMatrixCSC) = qrfact(A, Val{true})
150+
qrfact(A::SparseMatrixCSC) = qrfact(A, Val(true))
151151

152152
# With a real lhs and complex rhs with the same precision, we can reinterpret
153153
# the complex rhs as a real rhs with twice the number of columns

test/linalg/cholesky.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted, PosDefException
132132

133133
#pivoted upper Cholesky
134134
if eltya != BigFloat
135-
cz = cholfact(Hermitian(zeros(eltya,n,n)), Val{true})
135+
cz = cholfact(Hermitian(zeros(eltya,n,n)), Val(true))
136136
@test_throws Base.LinAlg.RankDeficientException Base.LinAlg.chkfullrank(cz)
137-
cpapd = cholfact(apdh, Val{true})
137+
cpapd = cholfact(apdh, Val(true))
138138
@test rank(cpapd) == n
139139
@test all(diff(diag(real(cpapd.factors))).<=0.) # diagonal should be non-increasing
140140
if isreal(apd)
@@ -175,11 +175,11 @@ using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted, PosDefException
175175

176176
if eltya != BigFloat && eltyb != BigFloat # Note! Need to implement pivoted Cholesky decomposition in julia
177177

178-
cpapd = cholfact(apdh, Val{true})
178+
cpapd = cholfact(apdh, Val(true))
179179
@test norm(apd * (cpapd\b) - b)/norm(b) <= ε*κ*n # Ad hoc, revisit
180180
@test norm(apd * (cpapd\b[1:n]) - b[1:n])/norm(b[1:n]) <= ε*κ*n
181181

182-
lpapd = cholfact(apdhL, Val{true})
182+
lpapd = cholfact(apdhL, Val(true))
183183
@test norm(apd * (lpapd\b) - b)/norm(b) <= ε*κ*n # Ad hoc, revisit
184184
@test norm(apd * (lpapd\b[1:n]) - b[1:n])/norm(b[1:n]) <= ε*κ*n
185185

@@ -251,7 +251,7 @@ end
251251
0.25336108035924787 + 0.975317836492159im 0.0628393808469436 - 0.1253397353973715im
252252
0.11192755545114 - 0.1603741874112385im 0.8439562576196216 + 1.0850814110398734im
253253
-1.0568488936791578 - 0.06025820467086475im 0.12696236014017806 - 0.09853584666755086im]
254-
cholfact(Hermitian(apd, :L), Val{true}) \ b
254+
cholfact(Hermitian(apd, :L), Val(true)) \ b
255255
r = factorize(apd)[:U]
256256
E = abs.(apd - r'*r)
257257
ε = eps(abs(float(one(Complex64))))
@@ -273,7 +273,7 @@ end
273273
end
274274

275275
@testset "fail for non-BLAS element types" begin
276-
@test_throws ArgumentError cholfact!(Hermitian(rand(Float16, 5,5)), Val{true})
276+
@test_throws ArgumentError cholfact!(Hermitian(rand(Float16, 5,5)), Val(true))
277277
end
278278

279279
@testset "throw for non positive definite matrix" begin

test/linalg/generic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ Base.transpose(a::ModInt{n}) where {n} = a # see Issue 20978
335335
A = [ModInt{2}(1) ModInt{2}(0); ModInt{2}(1) ModInt{2}(1)]
336336
b = [ModInt{2}(1), ModInt{2}(0)]
337337

338-
@test A*(lufact(A, Val{false})\b) == b
338+
@test A*(lufact(A, Val(false))\b) == b
339339

340340
# Needed for pivoting:
341341
Base.abs(a::ModInt{n}) where {n} = a
342342
Base.:<(a::ModInt{n}, b::ModInt{n}) where {n} = a.k < b.k
343343

344-
@test A*(lufact(A, Val{true})\b) == b
344+
@test A*(lufact(A, Val(true))\b) == b

test/linalg/qr.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ debug && println("QR decomposition (without pivoting)")
6565
@test sprint(show,qra) == "$(typeof(qra)) with factors Q and R:\n$qstring\n$rstring"
6666

6767
debug && println("Thin QR decomposition (without pivoting)")
68-
qra = @inferred qrfact(a[:,1:n1], Val{false})
69-
@inferred qr(a[:,1:n1], Val{false})
68+
qra = @inferred qrfact(a[:,1:n1], Val(false))
69+
@inferred qr(a[:,1:n1], Val(false))
7070
q,r = qra[:Q], qra[:R]
7171
@test_throws KeyError qra[:Z]
7272
@test q'*full(q, thin=false) eye(n)
@@ -82,8 +82,8 @@ debug && println("Thin QR decomposition (without pivoting)")
8282
end
8383

8484
debug && println("(Automatic) Fat (pivoted) QR decomposition")
85-
@inferred qrfact(a, Val{true})
86-
@inferred qr(a, Val{true})
85+
@inferred qrfact(a, Val(true))
86+
@inferred qr(a, Val(true))
8787

8888
qrpa = factorize(a[1:n1,:])
8989
q,r = qrpa[:Q], qrpa[:R]
@@ -134,7 +134,7 @@ debug && println("Matmul with QR factorizations")
134134
@test_throws DimensionMismatch Base.LinAlg.A_mul_B!(q,zeros(eltya,n1+1))
135135
@test_throws DimensionMismatch Base.LinAlg.Ac_mul_B!(q,zeros(eltya,n1+1))
136136

137-
qra = qrfact(a[:,1:n1], Val{false})
137+
qra = qrfact(a[:,1:n1], Val(false))
138138
q, r = qra[:Q], qra[:R]
139139
@test A_mul_B!(full(q, thin=false)',q) eye(n)
140140
@test_throws DimensionMismatch A_mul_B!(eye(eltya,n+1),q)
@@ -149,8 +149,8 @@ end
149149
# Because transpose(x) == x
150150
@test_throws ErrorException transpose(qrfact(randn(3,3)))
151151
@test_throws ErrorException ctranspose(qrfact(randn(3,3)))
152-
@test_throws ErrorException transpose(qrfact(randn(3,3), Val{false}))
153-
@test_throws ErrorException ctranspose(qrfact(randn(3,3), Val{false}))
152+
@test_throws ErrorException transpose(qrfact(randn(3,3), Val(false)))
153+
@test_throws ErrorException ctranspose(qrfact(randn(3,3), Val(false)))
154154
@test_throws ErrorException transpose(qrfact(big.(randn(3,3))))
155155
@test_throws ErrorException ctranspose(qrfact(big.(randn(3,3))))
156156

0 commit comments

Comments
 (0)