-
Notifications
You must be signed in to change notification settings - Fork 5
Sort diagonal eig values and standardize output types
#151
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
+32
−16
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a11835a
diagonal `eig` outputs sorted values
lkdvos b9add60
correct output types
lkdvos ac64d3e
correct sorting order
lkdvos b8327b1
Fix typo
lkdvos a29f73a
standardize `eig` output types
lkdvos a98c688
update changelog
lkdvos 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -30,23 +30,21 @@ end | |
|
|
||
| function check_input(::typeof(eig_full!), A::AbstractMatrix, DV, ::DiagonalAlgorithm) | ||
| m, n = size(A) | ||
| @assert m == n && isdiag(A) | ||
| ((m == n) && isdiag(A)) || throw(DimensionMismatch("diagonal input matrix expected")) | ||
| D, V = DV | ||
| @assert D isa Diagonal && V isa Diagonal | ||
| @assert D isa Diagonal && V isa AbstractMatrix | ||
| @check_size(D, (m, m)) | ||
| @check_scalar(D, A, complex) | ||
| @check_size(V, (m, m)) | ||
| # Diagonal doesn't need to promote to complex scalartype since we know it is diagonalizable | ||
| @check_scalar(D, A) | ||
| @check_scalar(V, A) | ||
| @check_scalar(V, A, complex) | ||
| return nothing | ||
| end | ||
| function check_input(::typeof(eig_vals!), A::AbstractMatrix, D, ::DiagonalAlgorithm) | ||
| m, n = size(A) | ||
| @assert m == n && isdiag(A) | ||
| ((m == n) && isdiag(A)) || throw(DimensionMismatch("diagonal input matrix expected")) | ||
|
Member
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. Same as above |
||
| @assert D isa AbstractVector | ||
| @check_size(D, (n,)) | ||
| # Diagonal doesn't need to promote to complex scalartype since we know it is diagonalizable | ||
| @check_scalar(D, A) | ||
| @check_scalar(D, A, complex) | ||
| return nothing | ||
| end | ||
|
|
||
|
|
@@ -70,10 +68,14 @@ function initialize_output(::Union{typeof(eig_trunc!), typeof(eig_trunc_no_error | |
| end | ||
|
|
||
| function initialize_output(::typeof(eig_full!), A::Diagonal, ::DiagonalAlgorithm) | ||
| return A, similar(A) | ||
| T = eltype(A) | ||
| Tc = complex(T) | ||
| D = T <: Complex ? A : Diagonal(similar(A, Tc, size(A, 1))) | ||
| return D, similar(A, Tc, size(A)) | ||
| end | ||
| function initialize_output(::typeof(eig_vals!), A::Diagonal, ::DiagonalAlgorithm) | ||
| return diagview(A) | ||
| T = eltype(A) | ||
| return T <: Complex ? diagview(A) : similar(A, complex(T), size(A, 1)) | ||
| end | ||
|
|
||
| # Implementation | ||
|
|
@@ -129,17 +131,29 @@ end | |
|
|
||
| # Diagonal logic | ||
| # -------------- | ||
| function eig_full!(A::Diagonal, (D, V)::Tuple{Diagonal, Diagonal}, alg::DiagonalAlgorithm) | ||
| check_input(eig_full!, A, (D, V), alg) | ||
| D === A || copy!(D, A) | ||
| one!(V) | ||
| eig_sortby(x::T) where {T <: Number} = T <: Complex ? (real(x), imag(x)) : x | ||
| function eig_full!(A::Diagonal, DV, alg::DiagonalAlgorithm) | ||
| check_input(eig_full!, A, DV, alg) | ||
| D, V = DV | ||
| diagA = diagview(A) | ||
| I = sortperm(diagA; by = eig_sortby) | ||
| if D === A | ||
| permute!(diagA, I) | ||
| else | ||
| diagview(D) .= view(diagA, I) | ||
| end | ||
| zero!(V) | ||
| n = size(A, 1) | ||
| I .+= (0:(n - 1)) .* n | ||
| V[I] .= Ref(one(eltype(V))) | ||
| return D, V | ||
| end | ||
|
|
||
| function eig_vals!(A::Diagonal, D::AbstractVector, alg::DiagonalAlgorithm) | ||
| check_input(eig_vals!, A, D, alg) | ||
| Ad = diagview(A) | ||
| D === Ad || copy!(D, Ad) | ||
| sort!(D; by = eig_sortby) | ||
| return D | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or 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.
Can we print the actual dimensions here with a lazy string?