-
-
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
[LinearAlgebra] Add isdiag/isposdef for Diagonal and UniformScaling #29638
Conversation
There should probably be the same for
Edit: Also |
Thanks @mcognetta for your suggestions. I think I found the right spot to put those methods and went ahead to include them. I hope you don't mind. |
istriu(::UniformScaling) = true | ||
istril(::UniformScaling) = true | ||
issymmetric(::UniformScaling) = true | ||
ishermitian(J::UniformScaling) = isreal(J.λ) | ||
isposdef(J::UniformScaling) = J.λ > zero(J.λ) |
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.
This fails when J.λ
is complex with imaginary part 0 (since there is no comparison operator).
You can simply do isposdef(J::UniformScaling) = isposdef(J.λ)
.
@dkarrasch No problem. I had two other tests that might be useful as well (for the case I mentioned in the comment).
|
@test istriu(I) | ||
@test istril(I) | ||
@test issymmetric(I) | ||
@test issymmetric(UniformScaling(complex(1.0,1.0))) | ||
@test ishermitian(I) | ||
@test !ishermitian(UniformScaling(complex(1.0,1.0))) | ||
@test isposdef(I) | ||
@test !isposdef(-I) |
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.
Maybe test these for complex-valued UniformScaling
s as well?
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.
Yes, I think that's what @mcognetta's suggestion above does, I believe. This is now included.
stdlib/LinearAlgebra/src/diagonal.jl
Outdated
@@ -123,6 +123,7 @@ factorize(D::Diagonal) = D | |||
real(D::Diagonal) = Diagonal(real(D.diag)) | |||
imag(D::Diagonal) = Diagonal(imag(D.diag)) | |||
|
|||
isdiag(D::Diagonal) = true |
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.
Sorry, one more thing. This fails when we have a block diagonal matrix in which not all blocks are diagonal.
MWE:
julia> D=Diagonal([[1 0; 0 1], [1 0; 1 1]])
2×2 Diagonal{Array{Int64,2},Array{Array{Int64,2},1}}:
[1 0; 0 1] ⋅
⋅ [1 0; 1 1]
julia> isdiag(D)
true
I believe it should be
isdiag(D::Diagonal) = all(isdiag, D.diag)
isdiag(D::Diagonal{<:Number}) = true
But I am not so sure on what it means to be a matrix of matrices so maybe @fredrikekre can correct me.
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 see, I only had the Number
case in mind. I'll fix this.
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.
... and add a test.
Cool, I'll include them. Thanks! |
While you are at it, you might as well correct
|
That wasn't me. :-))) Indeed, I included it and will add tests. |
This is looking good. Is it ready to go from your perspective, @dkarrasch? |
Thanks. I think so, yes. |
changes between Julia 1.0 and 1.1, including: - Custom .css-style for compat admonitions. - Information about compat annotations to CONTRIBUTING.md. - NEWS.md entry for PRs #30090, #30035, #30022, #29978, #29969, #29858, #29845, #29754, #29638, #29636, #29615, #29600, #29506, #29469, #29316, #29259, #29178, #29153, #29033, #28902, #28761, #28745, #28708, #28696, #29997, #28790, #29092, #29108, #29782 - Compat annotation for PRs #30090, #30013, #29978, #29890, #29858, #29827, #29754, #29679, #29636, #29623, #29600, #29440, #29316, #29259, #29178, #29157, #29153, #29033, #28902, #28878, #28761, #28708, #28156, #29733, #29670, #29997, #28790, #29092, #29108, #29782, #25278 - Documentation for broadcasting CartesianIndices (#30230). - Documentation for Base.julia_cmd(). - Documentation for colon constructor of CartesianIndices (#29440). - Documentation for ^(::Matrix, ::Number) and ^(::Number, ::Matrix). - Run NEWS-update.jl. Co-authored-by: Morten Piibeleht <morten.piibeleht@gmail.com> Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
changes between Julia 1.0 and 1.1, including: - Custom .css-style for compat admonitions. - Information about compat annotations to CONTRIBUTING.md. - NEWS.md entry for PRs #30090, #30035, #30022, #29978, #29969, #29858, #29845, #29754, #29638, #29636, #29615, #29600, #29506, #29469, #29316, #29259, #29178, #29153, #29033, #28902, #28761, #28745, #28708, #28696, #29997, #28790, #29092, #29108, #29782 - Compat annotation for PRs #30090, #30013, #29978, #29890, #29858, #29827, #29754, #29679, #29636, #29623, #29600, #29440, #29316, #29259, #29178, #29157, #29153, #29033, #28902, #28878, #28761, #28708, #28156, #29733, #29670, #29997, #28790, #29092, #29108, #29782, #25278 - Documentation for broadcasting CartesianIndices (#30230). - Documentation for Base.julia_cmd(). - Documentation for colon constructor of CartesianIndices (#29440). - Documentation for ^(::Matrix, ::Number) and ^(::Number, ::Matrix). - Run NEWS-update.jl. Co-authored-by: Morten Piibeleht <morten.piibeleht@gmail.com> Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
Addition of NEWS and compat admonitions for important changes between Julia 1.0 and 1.1, including: - Custom .css-style for compat admonitions. - Information about compat annotations to CONTRIBUTING.md. - NEWS.md entry for PRs #30090, #30035, #30022, #29978, #29969, #29858, #29845, #29754, #29638, #29636, #29615, #29600, #29506, #29469, #29316, #29259, #29178, #29153, #29033, #28902, #28761, #28745, #28708, #28696, #29997, #28790, #29092, #29108, #29782 - Compat annotation for PRs #30090, #30013, #29978, #29890, #29858, #29827, #29754, #29679, #29636, #29623, #29600, #29440, #29316, #29259, #29178, #29157, #29153, #29033, #28902, #28878, #28761, #28708, #28156, #29733, #29670, #29997, #28790, #29092, #29108, #29782, #25278 - Documentation for broadcasting CartesianIndices (#30230). - Documentation for Base.julia_cmd(). - Documentation for colon constructor of CartesianIndices (#29440). - Documentation for ^(::Matrix, ::Number) and ^(::Number, ::Matrix). - Run NEWS-update.jl. Co-authored-by: Morten Piibeleht <morten.piibeleht@gmail.com> Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
I found a tiny tiny gap, a noop test for diagonality of
Diagonal
matrices. It seems that all other matrix structure tests have specializedDiagonal
-methods:istriu
,istril
,issymmetric
, andisposdef
.Edit: Based on @mcognetta's suggestion, this now also includes
isdiag
andisposdef
forUniformScaling
s (title changed accordingly).