Skip to content

Commit

Permalink
Make conversion from Diagonal to Unit(Upper|Lower)Triangular preserve…
Browse files Browse the repository at this point in the history
… Diagonal storage structure, consistent with conversion to (Upper|Lower)Triangular. Test.
  • Loading branch information
Sacha0 committed Jul 27, 2016
1 parent d0a378d commit 039b780
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
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

0 comments on commit 039b780

Please sign in to comment.