Skip to content

Commit

Permalink
Merge pull request #11204 from kshyatt/specialer
Browse files Browse the repository at this point in the history
Conversion tests + bugfix for Unit{Upper/Lower}Triangular
  • Loading branch information
tkelman committed May 9, 2015
2 parents 3ab9af1 + 61376c2 commit 403423c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions base/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ convert{T}(::Type{Bidiagonal}, A::Diagonal{T})=Bidiagonal(A.diag, zeros(T, size(
convert{T}(::Type{SymTridiagonal}, A::Diagonal{T})=SymTridiagonal(A.diag, zeros(T, size(A.diag,1)-1))
convert{T}(::Type{Tridiagonal}, A::Diagonal{T})=Tridiagonal(zeros(T, size(A.diag,1)-1), A.diag, zeros(T, size(A.diag,1)-1))
convert(::Type{UpperTriangular}, A::Diagonal) = UpperTriangular(full(A), :L)
convert(::Type{UnitUpperTriangular}, A::Diagonal) = UnitUpperTriangular(full(A), :L)
convert(::Type{LowerTriangular}, A::Diagonal) = LowerTriangular(full(A), :L)
convert(::Type{UnitLowerTriangular}, A::Diagonal) = UnitLowerTriangular(full(A), :L)
convert(::Type{LowerTriangular}, A::Bidiagonal) = !A.isupper ? LowerTriangular(full(A)) : throw(ArgumentError("Bidiagonal matrix must have lower off diagonal to be converted to LowerTriangular"))
convert(::Type{UpperTriangular}, A::Bidiagonal) = A.isupper ? UpperTriangular(full(A)) : throw(ArgumentError("Bidiagonal matrix must have upper off diagonal to be converted to UpperTriangular"))
convert(::Type{Matrix}, D::Diagonal) = diagm(D.diag)

function convert(::Type{UnitUpperTriangular}, A::Diagonal)
all(A.diag .== one(eltype(A))) || throw(ArgumentError("Matrix cannot be represented as UnitUpperTriangular"))
UnitUpperTriangular(full(A))
end

function convert(::Type{UnitLowerTriangular}, A::Diagonal)
all(A.diag .== one(eltype(A))) || throw(ArgumentError("Matrix cannot be represented as UnitLowerTriangular"))
UnitLowerTriangular(full(A))
end

function convert(::Type{Diagonal}, A::Union(Bidiagonal, SymTridiagonal))
all(A.ev .== 0) || throw(ArgumentError("Matrix cannot be represented as Diagonal"))
Diagonal(A.dv)
Expand Down
6 changes: 6 additions & 0 deletions test/linalg4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ let a=[1.0:n;]
debug && println("newtype is $(newtype)")
@test full(convert(newtype, A)) == full(A)
end
for newtype in [Base.LinAlg.UnitUpperTriangular, Base.LinAlg.UnitLowerTriangular]
@test_throws ArgumentError convert(newtype, A)
@test full(convert(newtype, Diagonal(ones(n)))) == eye(n)
end

for isupper in (true, false)
debug && println("isupper is $(isupper)")
Expand All @@ -38,6 +42,8 @@ let a=[1.0:n;]
for newtype in [Diagonal, Bidiagonal]
@test_throws ArgumentError convert(newtype,A)
end
A = SymTridiagonal(a, zeros(n-1))
@test full(convert(Bidiagonal,A)) == full(A)

A = Tridiagonal(zeros(n-1), [1.0:n;], zeros(n-1)) #morally Diagonal
for newtype in [Diagonal, Bidiagonal, SymTridiagonal, Matrix]
Expand Down

0 comments on commit 403423c

Please sign in to comment.