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

Deprecate convert(::Type{<:AbstractTriangular}, A::(Diagonal|Bidiagonal)) methods #17723

Merged
merged 1 commit into from
Aug 25, 2016
Merged
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
Deprecate methods that convert from Diagonal and Bidiagonal to <:Abst…
…ractTriangular. Remove tests of those convert methods.
  • Loading branch information
Sacha0 committed Aug 23, 2016
commit 1018a55c7fca9e756395b98102f6424cc5fa932d
58 changes: 58 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -795,4 +795,62 @@ end
@deprecate (-)(J::UniformScaling, x::Number) J.λ - x
@deprecate (-)(x::Number, J::UniformScaling) x - J.λ

# Deprecate methods that convert Diagonal and Bidiagonal to <:AbstractTriangular.
function convert(::Type{UpperTriangular}, A::Diagonal)
depwarn(string("`convert(::Type{UpperTriangular}, A::Diagonal)` and other methods ",
"that convert `Diagonal`/`Bidiagonal` to `<:AbstractTriangular` are deprecated. ",
"Consider calling the `UpperTriangular` constructor directly ",
"(`UpperTriangular(A)`) instead."), :convert)
UpperTriangular(A)
end
function convert(::Type{LowerTriangular}, A::Diagonal)
depwarn(string("`convert(::Type{LowerTriangular}, A::Diagonal)` and other methods ",
"that convert `Diagonal`/`Bidiagonal` to `<:AbstractTriangular` are deprecated. ",
"Consider calling the `LowerTriangular` constructor directly ",
"(`LowerTriangular(A)`) instead."), :convert)
LowerTriangular(A)
end
function convert(::Type{Base.LinAlg.UnitUpperTriangular}, A::Diagonal)
depwarn(string("`convert(::Type{UnitUpperTriangular}, A::Diagonal)` and other methods ",
"that convert `Diagonal`/`Bidiagonal` to `<:AbstractTriangular` are deprecated. ",
"Consider calling the `UnitUpperTriangular` constructor directly ",
"(`Base.LinAlg.UnitUpperTriangular(A)`) instead."), :convert)
if !all(A.diag .== one(eltype(A)))
throw(ArgumentError("matrix cannot be represented as UnitUpperTriangular"))
end
Base.LinAlg.UnitUpperTriangular(full(A))
end
function convert(::Type{Base.LinAlg.UnitLowerTriangular}, A::Diagonal)
depwarn(string("`convert(::Type{UnitLowerTriangular}, A::Diagonal)` and other methods ",
"that convert `Diagonal`/`Bidiagonal` to `<:AbstractTriangular` are deprecated. ",
"Consider calling the `UnitLowerTriangular` constructor directly ",
"(`Base.LinAlg.UnitLowerTriangular(A)`) instead."), :convert)
if !all(A.diag .== one(eltype(A)))
throw(ArgumentError("matrix cannot be represented as UnitLowerTriangular"))
end
Base.LinAlg.UnitLowerTriangular(full(A))
end
function convert(::Type{LowerTriangular}, A::Bidiagonal)
depwarn(string("`convert(::Type{LowerTriangular}, A::Bidiagonal)` and other methods ",
"that convert `Diagonal`/`Bidiagonal` to `<:AbstractTriangular` are deprecated. ",
"Consider calling the `LowerTriangular` constructor directly (`LowerTriangular(A)`) ",
"instead."), :convert)
if !A.isupper
LowerTriangular(full(A))
else
throw(ArgumentError("Bidiagonal matrix must have lower off diagonal to be converted to LowerTriangular"))
end
end
function convert(::Type{UpperTriangular}, A::Bidiagonal)
depwarn(string("`convert(::Type{UpperTriangular}, A::Bidiagonal)` and other methods ",
"that convert `Diagoinal`/`Bidiagonal` to `<:AbstractTriangular` are deprecated. ",
"Consider calling the `UpperTriangular` constructor directly (`UpperTriangular(A)`) ",
"instead."), :convert)
if A.isupper
UpperTriangular(full(A))
else
throw(ArgumentError("Bidiagonal matrix must have upper off diagonal to be converted to UpperTriangular"))
end
end

# End deprecations scheduled for 0.6
2 changes: 0 additions & 2 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ Diagonal(V::AbstractVector) = Diagonal(collect(V))
convert{T}(::Type{Diagonal{T}}, D::Diagonal{T}) = D
convert{T}(::Type{Diagonal{T}}, D::Diagonal) = Diagonal{T}(convert(Vector{T}, D.diag))
convert{T}(::Type{AbstractMatrix{T}}, D::Diagonal) = convert(Diagonal{T}, D)
convert{T}(::Type{UpperTriangular}, A::Diagonal{T}) = UpperTriangular(A)
convert{T}(::Type{LowerTriangular}, A::Diagonal{T}) = LowerTriangular(A)
convert(::Type{Matrix}, D::Diagonal) = diagm(D.diag)
convert(::Type{Array}, D::Diagonal) = convert(Matrix, D)
full(D::Diagonal) = convert(Array, D)
Expand Down
20 changes: 2 additions & 18 deletions base/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@
convert{T}(::Type{Bidiagonal}, A::Diagonal{T})=Bidiagonal(A.diag, zeros(T, size(A.diag,1)-1), true)
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{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"))

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

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

function convert(::Type{Diagonal}, A::Union{Bidiagonal, SymTridiagonal})
if !all(A.ev .== 0)
Expand Down Expand Up @@ -147,8 +131,8 @@ for op in (:+, :-)
(:LowerTriangular,:LowerTriangular),
(:UnitLowerTriangular,:LowerTriangular))
@eval begin
($op)(A::($matrixtype1), B::($matrixtype2)) = ($op)(convert(($matrixtype3), A), B)
($op)(A::($matrixtype2), B::($matrixtype1)) = ($op)(A, convert(($matrixtype3), B))
($op)(A::($matrixtype1), B::($matrixtype2)) = ($op)(($matrixtype3)(A), B)
($op)(A::($matrixtype2), B::($matrixtype1)) = ($op)(A, ($matrixtype3)(B))
end
end
end
Expand Down
18 changes: 7 additions & 11 deletions test/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ srand(1)
debug && println("Test interconversion between special matrix types")
let a=[1.0:n;]
A=Diagonal(a)
for newtype in [Diagonal, Bidiagonal, SymTridiagonal, Tridiagonal, LowerTriangular, UpperTriangular, Matrix]
for newtype in [Diagonal, Bidiagonal, SymTridiagonal, Tridiagonal, Matrix]
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)")
A=Bidiagonal(a, [1.0:n-1;], isupper)
for newtype in [Bidiagonal, Tridiagonal, isupper ? UpperTriangular : LowerTriangular, Matrix]
for newtype in [Bidiagonal, Tridiagonal, Matrix]
debug && println("newtype is $(newtype)")
@test full(convert(newtype, A)) == full(A)
@test full(newtype(A)) == full(A)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this line still be tested?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends. Bidiagonal(A)/convert(Bidiagonal, A) and Matrix(A)/convert(Matrix, A) test essentially the same code path, but Tridiagonal(A) tests a different code path from convert(Tridiagonal, A). If you feel exercising the latter path is worthwhile, then this line should remain; if not, it should disappear. Thoughts? Thanks!

end
@test_throws ArgumentError convert(SymTridiagonal, A)
tritype = isupper ? UpperTriangular : LowerTriangular
@test full(tritype(A)) == full(A)

A=Bidiagonal(a, zeros(n-1), isupper) #morally Diagonal
for newtype in [Diagonal, Bidiagonal, SymTridiagonal, Tridiagonal, isupper ? UpperTriangular : LowerTriangular, Matrix]
for newtype in [Diagonal, Bidiagonal, SymTridiagonal, Tridiagonal, Matrix]
debug && println("newtype is $(newtype)")
@test full(convert(newtype, A)) == full(A)
@test full(newtype(A)) == full(A)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the response above. Diagonal(A)/convert(Diagonal, A), SymTridiagonal(A)/convert(SymTridiagonal, A), and Tridiagonal(A)/convert(Tridiagonal, A) appear to test separate code paths as well (which seems a bit odd, the relevant methods would probably benefit from a bit of refactoring). Thoughts? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleting the tests that cover methods we still have seems unfortunate - I agree some refactoring is in order, but I'd say try to preserve test coverage until the methods themselves are actually modified

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! Apologies, I just realized I might've misunderstood your original comments. Did you mean to imply that I missed testing the <:AbstractTriangular constructors that those lines formerly also tested, rather than asking about removal of those lines? If so, you're absolutely right, and I should introduce tests equivalent to those that I accidentally dropped.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few lines mimicking the former <:AbstractTriangular constructor tests. Sorry for the miscommunication, and thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's what I meant - the line I commented on would still be relevant for the types you removed from the loop, though the convert tests no longer would because you're deprecating them

end
@test full(tritype(A)) == full(A)
end

A = SymTridiagonal(a, [1.0:n-1;])
Expand Down Expand Up @@ -78,10 +78,6 @@ let a=[1.0:n;]
for newtype in [Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal]
@test_throws ArgumentError convert(newtype,A)
end
A = Diagonal(a)
for newtype in [UpperTriangular, LowerTriangular]
@test full(convert(newtype,A)) == full(A)
end
end

# Binary ops among special types
Expand Down