Skip to content

Commit

Permalink
more verbose type names
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarrasch committed May 20, 2021
1 parent 4e8487b commit 4549609
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 45 deletions.
7 changes: 4 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ Standard library changes
* The shape of an `UpperHessenberg` matrix is preserved under certain arithmetic operations, e.g. when multiplying or dividing by an `UpperTriangular` matrix. ([#40039])
* `cis(A)` now supports matrix arguments ([#40194]).
* `dot` now supports `UniformScaling` with `AbstractMatrix` ([#40250]).
* `qr[!]` and `lu[!]` now support `PivotingStrategy` values as their optional `pivot` argument:
defaults are `qr(A, NoPivot())` (vs. `qr(A, ColNorm())` for pivoting) and `lu(A, RowMax())`
(vs. `lu(A, NoPivot())` without pivoting); the former `Val{true/false}`-based calls are deprecated. ([#40623])
* `qr[!]` and `lu[!]` now support `LinearAlgebra.PivotingStrategy` (singleton type) values
as their optional `pivot` argument: defaults are `qr(A, NoPivot())` (vs.
`qr(A, ColumnNorm())` for pivoting) and `lu(A, RowMaximum())` (vs. `lu(A, NoPivot())`
without pivoting); the former `Val{true/false}`-based calls are deprecated. ([#40623])

#### Markdown

Expand Down
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export
BunchKaufman,
Cholesky,
CholeskyPivoted,
ColNorm,
ColumnNorm,
Eigen,
GeneralizedEigen,
GeneralizedSVD,
Expand All @@ -50,7 +50,7 @@ export
Schur,
SVD,
Hermitian,
RowMax,
RowMaximum,
Symmetric,
LowerTriangular,
UpperTriangular,
Expand Down Expand Up @@ -169,8 +169,8 @@ struct QRIteration <: Algorithm end

abstract type PivotingStrategy end
struct NoPivot <: PivotingStrategy end
struct RowMax <: PivotingStrategy end
struct ColNorm <: PivotingStrategy end
struct RowMaximum <: PivotingStrategy end
struct ColumnNorm <: PivotingStrategy end

# Check that stride of matrix/vector is 1
# Writing like this to avoid splatting penalty when called with multiple arguments,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ function factorize(A::StridedMatrix{T}) where T
end
return lu(A)
end
qr(A, ColNorm())
qr(A, ColumnNorm())
end
factorize(A::Adjoint) = adjoint(factorize(parent(A)))
factorize(A::Transpose) = transpose(factorize(parent(A)))
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ size(F::Adjoint{<:Any,<:Factorization}) = reverse(size(parent(F)))
size(F::Transpose{<:Any,<:Factorization}) = reverse(size(parent(F)))

checkpositivedefinite(info) = info == 0 || throw(PosDefException(info))
checknonsingular(info, ::RowMax) = info == 0 || throw(SingularException(info))
checknonsingular(info, ::RowMaximum) = info == 0 || throw(SingularException(info))
checknonsingular(info, ::NoPivot) = info == 0 || throw(ZeroPivotException(info))
checknonsingular(info) = checknonsingular(info, RowMax())
checknonsingular(info) = checknonsingular(info, RowMaximum())

"""
issuccess(F::Factorization)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ function (\)(A::AbstractMatrix, B::AbstractVecOrMat)
end
return lu(A) \ B
end
return qr(A, ColNorm()) \ B
return qr(A, ColumnNorm()) \ B
end

(\)(a::AbstractVector, b::AbstractArray) = pinv(a) * b
Expand Down
32 changes: 12 additions & 20 deletions stdlib/LinearAlgebra/src/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,26 @@ adjoint(F::LU) = Adjoint(F)
transpose(F::LU) = Transpose(F)

# StridedMatrix
lu!(A::StridedMatrix{<:BlasFloat}; check::Bool = true) = lu!(A, RowMax(); check=check)
function lu!(A::StridedMatrix{T}, ::RowMax; check::Bool = true) where {T<:BlasFloat}
lu!(A::StridedMatrix{<:BlasFloat}; check::Bool = true) = lu!(A, RowMaximum(); check=check)
function lu!(A::StridedMatrix{T}, ::RowMaximum; check::Bool = true) where {T<:BlasFloat}
lpt = LAPACK.getrf!(A)
check && checknonsingular(lpt[3])
return LU{T,typeof(A)}(lpt[1], lpt[2], lpt[3])
end
function lu!(A::StridedMatrix{<:BlasFloat}, pivot::NoPivot; check::Bool = true)
return generic_lufact!(A, pivot; check = check)
end
function lu!(A::HermOrSym, pivot::PivotingStrategy = RowMax(); check::Bool = true)
function lu!(A::HermOrSym, pivot::Union{RowMaximum,NoPivot} = RowMaximum(); check::Bool = true)
copytri!(A.data, A.uplo, isa(A, Hermitian))
lu!(A.data, pivot; check = check)
end
# for backward compatibility
# TODO: remove towards Julia v2
@deprecate lu!(A::Union{StridedMatrix,HermOrSym,Tridiagonal}, ::Val{true}; check::Bool = true) lu!(A, RowMax(); check=check)
@deprecate lu!(A::Union{StridedMatrix,HermOrSym,Tridiagonal}, ::Val{true}; check::Bool = true) lu!(A, RowMaximum(); check=check)
@deprecate lu!(A::Union{StridedMatrix,HermOrSym,Tridiagonal}, ::Val{false}; check::Bool = true) lu!(A, NoPivot(); check=check)

"""
lu!(A, pivot = RowMax(); check = true) -> LU
lu!(A, pivot = RowMaximum(); check = true) -> LU
`lu!` is the same as [`lu`](@ref), but saves space by overwriting the
input `A`, instead of creating a copy. An [`InexactError`](@ref)
Expand Down Expand Up @@ -131,26 +131,22 @@ Stacktrace:
[...]
```
"""
lu!(A::StridedMatrix, pivot::PivotingStrategy = RowMax(); check::Bool = true) =
lu!(A::StridedMatrix, pivot::Union{RowMaximum,NoPivot} = RowMaximum(); check::Bool = true) =
generic_lufact!(A, pivot; check = check)
function generic_lufact!(A::StridedMatrix{T}, pivot::PivotingStrategy = RowMax();
function generic_lufact!(A::StridedMatrix{T}, pivot::Union{RowMaximum,NoPivot} = RowMaximum();
check::Bool = true) where {T}
# Extract values
m, n = size(A)
minmn = min(m,n)

if pivot !== RowMax() && pivot !== NoPivot()
throw(ArgumentError("only `RowMax()` and `NoPivot()` are supported as `pivot` argument but you supplied `$pivot`"))
end

# Initialize variables
info = 0
ipiv = Vector{BlasInt}(undef, minmn)
@inbounds begin
for k = 1:minmn
# find index max
kp = k
if pivot === RowMax() && k < m
if pivot === RowMaximum() && k < m
amax = abs(A[k, k])
for i = k+1:m
absi = abs(A[i,k])
Expand Down Expand Up @@ -211,7 +207,7 @@ end

# for all other types we must promote to a type which is stable under division
"""
lu(A, pivot = RowMax(); check = true) -> F::LU
lu(A, pivot = RowMaximum(); check = true) -> F::LU
Compute the LU factorization of `A`.
Expand Down Expand Up @@ -278,12 +274,12 @@ julia> l == F.L && u == F.U && p == F.p
true
```
"""
function lu(A::AbstractMatrix{T}, pivot::PivotingStrategy = RowMax(); check::Bool = true) where {T}
function lu(A::AbstractMatrix{T}, pivot::Union{RowMaximum,NoPivot} = RowMaximum(); check::Bool = true) where {T}
S = lutype(T)
lu!(copy_oftype(A, S), pivot; check = check)
end
# TODO: remove for Julia v2.0
@deprecate lu(A::AbstractMatrix, ::Val{true}; check::Bool = true) lu(A, RowMax(); check=check)
@deprecate lu(A::AbstractMatrix, ::Val{true}; check::Bool = true) lu(A, RowMaximum(); check=check)
@deprecate lu(A::AbstractMatrix, ::Val{false}; check::Bool = true) lu(A, NoPivot(); check=check)


Expand Down Expand Up @@ -495,14 +491,10 @@ inv(A::LU{<:BlasFloat,<:StridedMatrix}) = inv!(copy(A))
# Tridiagonal

# See dgttrf.f
function lu!(A::Tridiagonal{T,V}, pivot::PivotingStrategy = RowMax(); check::Bool = true) where {T,V}
function lu!(A::Tridiagonal{T,V}, pivot::Union{RowMaximum,NoPivot} = RowMaximum(); check::Bool = true) where {T,V}
# Extract values
n = size(A, 1)

if pivot !== RowMax() && pivot !== NoPivot()
throw(ArgumentError("only `RowMax()` and `NoPivot()` are supported as `pivot` argument but you supplied `$pivot`"))
end

# Initialize variables
info = 0
ipiv = Vector{BlasInt}(undef, n)
Expand Down
10 changes: 5 additions & 5 deletions stdlib/LinearAlgebra/src/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ end
# LAPACK version
qr!(A::StridedMatrix{<:BlasFloat}, ::NoPivot; blocksize=36) =
QRCompactWY(LAPACK.geqrt!(A, min(min(size(A)...), blocksize))...)
qr!(A::StridedMatrix{<:BlasFloat}, ::ColNorm) = QRPivoted(LAPACK.geqp3!(A)...)
qr!(A::StridedMatrix{<:BlasFloat}, ::ColumnNorm) = QRPivoted(LAPACK.geqp3!(A)...)

# Generic fallbacks

Expand Down Expand Up @@ -293,10 +293,10 @@ Stacktrace:
```
"""
qr!(A::AbstractMatrix, ::NoPivot) = qrfactUnblocked!(A)
qr!(A::AbstractMatrix, ::ColNorm) = qrfactPivotedUnblocked!(A)
qr!(A::AbstractMatrix, ::ColumnNorm) = qrfactPivotedUnblocked!(A)
qr!(A::AbstractMatrix) = qr!(A, NoPivot())
# TODO: Remove in Julia v2.0
@deprecate qr!(A::AbstractMatrix, ::Val{true}) qr!(A, ColNorm())
@deprecate qr!(A::AbstractMatrix, ::Val{true}) qr!(A, ColumnNorm())
@deprecate qr!(A::AbstractMatrix, ::Val{false}) qr!(A, NoPivot())

_qreltype(::Type{T}) where T = typeof(zero(T)/sqrt(abs2(one(T))))
Expand All @@ -313,7 +313,7 @@ A = Q R
The returned object `F` stores the factorization in a packed format:
- if `pivot == ColNorm()` then `F` is a [`QRPivoted`](@ref) object,
- if `pivot == ColumnNorm()` then `F` is a [`QRPivoted`](@ref) object,
- otherwise if the element type of `A` is a BLAS type ([`Float32`](@ref), [`Float64`](@ref),
`ComplexF32` or `ComplexF64`), then `F` is a [`QRCompactWY`](@ref) object,
Expand Down Expand Up @@ -387,7 +387,7 @@ function qr(A::AbstractMatrix{T}, arg...; kwargs...) where T
end
# TODO: remove in Julia v2.0
@deprecate qr(A::AbstractMatrix, ::Val{false}; kwargs...) qr(A, NoPivot(); kwargs...)
@deprecate qr(A::AbstractMatrix, ::Val{true}; kwargs...) qr(A, ColNorm(); kwargs...)
@deprecate qr(A::AbstractMatrix, ::Val{true}; kwargs...) qr(A, ColumnNorm(); kwargs...)

qr(x::Number) = qr(fill(x,1,1))
function qr(v::AbstractVector)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ end
D = Diagonal(randn(5))
Q = qr(randn(5, 5)).Q
@test D * Q' == Array(D) * Q'
Q = qr(randn(5, 5), ColNorm()).Q
Q = qr(randn(5, 5), ColumnNorm()).Q
@test_throws ArgumentError lmul!(Q, D)
end

Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ LinearAlgebra.Transpose(a::ModInt{n}) where {n} = transpose(a)
Base.abs(a::ModInt{n}) where {n} = a
Base.:<(a::ModInt{n}, b::ModInt{n}) where {n} = a.k < b.k

@test A*(lu(A, RowMax())\b) == b
@test A*(lu(A, RowMaximum())\b) == b
end

@testset "Issue 18742" begin
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/lq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ rectangularQ(Q::LinearAlgebra.LQPackedQ) = convert(Array, Q)
lqa = lq(a)
x = lqa\b
l,q = lqa.L, lqa.Q
qra = qr(a, ColNorm())
qra = qr(a, ColumnNorm())
@testset "Basic ops" begin
@test size(lqa,1) == size(a,1)
@test size(lqa,3) == 1
Expand Down
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/test/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ rectangularQ(Q::LinearAlgebra.AbstractQ) = convert(Array, Q)
@test Base.propertynames(qra) == (:R, :Q)
end
@testset "(Automatic) Fat (pivoted) QR decomposition" begin
@inferred qr(a, ColNorm())
@inferred qr(a, ColumnNorm())

qrpa = factorize(a[1:n1,:])
q,r = qrpa.Q, qrpa.R
Expand Down Expand Up @@ -254,7 +254,7 @@ end
A = zeros(1, 2)
B = zeros(1, 1)
@test A \ B == zeros(2, 1)
@test qr(A, ColNorm()) \ B == zeros(2, 1)
@test qr(A, ColumnNorm()) \ B == zeros(2, 1)
end

@testset "Issue 24107" begin
Expand All @@ -276,7 +276,7 @@ end
@test A \b ldiv!(c, qr(A ), b)
@test b == b0
c0 = copy(c)
@test Ac\c ldiv!(b, qr(Ac, ColNorm()), c)
@test Ac\c ldiv!(b, qr(Ac, ColumnNorm()), c)
@test c0 == c
end

Expand All @@ -293,7 +293,7 @@ end

@testset "det(Q::Union{QRCompactWYQ, QRPackedQ})" begin
# 40 is the number larger than the default block size 36 of QRCompactWY
@testset for n in [1:3; 40], m in [1:3; 40], pivot in (NoPivot(), ColNorm())
@testset for n in [1:3; 40], m in [1:3; 40], pivot in (NoPivot(), ColumnNorm())
@testset "real" begin
@testset for k in 0:min(n, m, 5)
A = cat(Array(I(k)), randn(n - k, m - k); dims=(1, 2))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ end
a = rand(n,n)
atri = typ(a)
b = rand(n,n)
qrb = qr(b, ColNorm())
qrb = qr(b, ColumnNorm())
@test *(atri, adjoint(qrb.Q)) Matrix(atri) * qrb.Q'
@test rmul!(copy(atri), adjoint(qrb.Q)) Matrix(atri) * qrb.Q'
qrb = qr(b, NoPivot())
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ end

@testset "Factorization solutions" begin
J = complex(randn(),randn()) * I
qrp = A -> qr(A, ColNorm())
qrp = A -> qr(A, ColumnNorm())

# thin matrices
X = randn(3,2)
Expand Down

0 comments on commit 4549609

Please sign in to comment.