diff --git a/base/linalg/special.jl b/base/linalg/special.jl index f70d67130807b..8cd9febbdfb13 100644 --- a/base/linalg/special.jl +++ b/base/linalg/special.jl @@ -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}) diff --git a/test/linalg/special.jl b/test/linalg/special.jl index 89ddde5ee4b00..c7b6e1aba59c6 100644 --- a/test/linalg/special.jl +++ b/test/linalg/special.jl @@ -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