-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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{(Lower|Upper)Triangular}, A::Bidiagonal) preserve storage structure. Add equivalent methods for Unit(Lower|Upper)Triangular. #17656
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,38 @@ | |
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")) | ||
|
||
# methods for conversion from Bidiagonal to [Unit](Upper|Lower)Triangular | ||
function convert(::Type{LowerTriangular}, A::Bidiagonal) | ||
if A.isupper | ||
throw(ArgumentError("upper Bidiagonal matrices cannot be converted to LowerTriangular")) | ||
end | ||
LowerTriangular(A) | ||
end | ||
function convert(::Type{UpperTriangular}, A::Bidiagonal) | ||
if !A.isupper | ||
throw(ArgumentError("lower Bidiagonal matrices cannot be converted to UpperTriangular")) | ||
end | ||
UpperTriangular(A) | ||
end | ||
function convert(::Type{UnitLowerTriangular}, A::Bidiagonal) | ||
if A.isupper | ||
throw(ArgumentError("upper Bidiagonal matrices cannot be converted to UnitLowerTriangular")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here if all the off-diagonals are zero and all the diagonals are 1? |
||
elseif !all(A.dv .== one(eltype(A))) | ||
throw(ArgumentError(string("Bidiagonal matrices with non-one entries on the ", | ||
"diagonal cannot be converted to UnitLowerTriangular"))) | ||
end | ||
UnitLowerTriangular(A) | ||
end | ||
function convert(::Type{UnitUpperTriangular}, A::Bidiagonal) | ||
if !A.isupper | ||
throw(ArgumentError("lower Bidiagonal matrices cannot be converted to UnitUpperTriangular")) | ||
elseif !all(A.dv .== one(eltype(A))) | ||
throw(ArgumentError(string("Bidiagonal matrices with non-one entries on the ", | ||
"diagonal cannot be converted to UnitUpperTriangular"))) | ||
end | ||
UnitUpperTriangular(A) | ||
end | ||
|
||
function convert(::Type{UnitUpperTriangular}, A::Diagonal) | ||
if !all(A.diag .== one(eltype(A))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it could if all the off-diagonals are zero?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could, which segues to a related issue.
On the one hand, existing {constructors for annotation types} that accept an
A<:AbstractMatrix
as the primary argument do not require thatA
'conform' to the annotation. For example,LowerTriangular(Bidiagonal(rand(5), rand(4), true))
andSymmetric(rand(5,5))
are always valid.On the other hand,
convert(::Type{$(AnnotationType)}, A::$(SpecialMatrixType))
-like methods inconsistently require thatA
'conform' to the annotation type either in storage structure (e.g. as in the code you identified) or in value (e.g. as in your suggestion). And equivalentconvert
methods from other storage types (e.g.Matrix
,SparseMatrixCSC
) to annotation types do not appear to exist. For example,convert(LowerTriangular, rand(4, 4))
throws aMethodError
.Do
convert
methods from special matrix types to annotation types make sense generally? If so, how should they behave? Also if so, shouldn't equivalentconvert
methods from other storage types to annotation types exist? And otherwise, why do these particular methods exist?Thoughts? Best!
Edit: Maybe I should open a dedicated issue?
Edit:
grep -rn 'convert(LowerTriangular
base/` yields nothing, FWIW.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're very much getting into #8001 territory here. If you think of the "annotation wrapper type" as a view into whatever subset of the underlying array happens to be important for that type of annotation, then I guess the structure and the contents of the "inactive" part of the wrapper-type view don't actually matter and could be anything.
Is wrapping around an object always the meaning we should give for
convert
here? Maybe not as that could get confusing. But then since constructors and convert are pretty closely tied together at the moment, I'm not sure what syntax you would use as an alternative to the constructors.