Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make convert(::Type{<:AbstractTriangular}, A::Diagonal) consistently preserve storage structure #17653

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions base/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ convert(::Type{UpperTriangular}, A::Bidiagonal) = A.isupper ? UpperTriangular(fu

function convert(::Type{UnitUpperTriangular}, A::Diagonal)
if !all(A.diag .== one(eltype(A)))
throw(ArgumentError("matrix cannot be represented as UnitUpperTriangular"))
throw(ArgumentError(string("Diagonal matrices with non-one entries on the ",
"diagonal cannot be converted to UnitUpperTriangular")))
end
UnitUpperTriangular(full(A))
UnitUpperTriangular(A)
end

function convert(::Type{UnitLowerTriangular}, A::Diagonal)
if !all(A.diag .== one(eltype(A)))
throw(ArgumentError("matrix cannot be represented as UnitLowerTriangular"))
throw(ArgumentError(string("Diagonal matrices with non-one entries on the ",
"diagonal cannot be converted to UnitLowerTriangular")))
end
UnitLowerTriangular(full(A))
UnitLowerTriangular(A)
end

function convert(::Type{Diagonal}, A::Union{Bidiagonal, SymTridiagonal})
Expand Down
10 changes: 10 additions & 0 deletions test/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,13 @@ for typ in [UpperTriangular,LowerTriangular,Base.LinAlg.UnitUpperTriangular,Base
@test Base.LinAlg.A_mul_Bc(atri,qrb[:Q]) ≈ full(atri) * qrb[:Q]'
@test Base.LinAlg.A_mul_Bc!(copy(atri),qrb[:Q]) ≈ full(atri) * qrb[:Q]'
end

# Test conversion from Diagonal to <:AbstractTriangular
let
unitdiagmat = Diagonal(ones(Int, 3))
# test that conversion from diagonal to triangular preserves diagonal storage structure
@test typeof(convert(LowerTriangular, unitdiagmat)) == LowerTriangular{Int,Diagonal{Int}}
@test typeof(convert(UpperTriangular, unitdiagmat)) == UpperTriangular{Int,Diagonal{Int}}
@test typeof(convert(Base.LinAlg.UnitLowerTriangular, unitdiagmat)) == Base.LinAlg.UnitLowerTriangular{Int,Diagonal{Int}}
@test typeof(convert(Base.LinAlg.UnitUpperTriangular, unitdiagmat)) == Base.LinAlg.UnitUpperTriangular{Int,Diagonal{Int}}
end