-
Notifications
You must be signed in to change notification settings - Fork 5
Add generalized eigenvalue decomposition, fix some bugs #39
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1bf4dbf
Add generalized eigenvalue decomposition, fix some bugs
1765566
Fix JET by splitting macro up a bit
f4d37c2
Coverage improvements
f1e713c
More tests
57a448a
Split up the functiondef macro
2ba56fc
Update src/implementations/gen_eig.jl
kshyatt 5725435
Restore eig file
kshyatt 6058d01
Make gaugefixation its own function and cleanup exports
kshyatt e098cff
Update src/implementations/eig.jl
kshyatt 8b966c3
Update src/implementations/gen_eig.jl
kshyatt a0b2b53
Update src/common/gauge.jl
kshyatt 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
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| function gaugefix!(V::AbstractMatrix) | ||
| for j in axes(V, 2) | ||
| v = view(V, :, j) | ||
| s = conj(sign(argmax(abs, v))) | ||
| @inbounds v .*= s | ||
| end | ||
| return V | ||
| 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
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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Inputs | ||
| # ------ | ||
| function copy_input(::typeof(gen_eig_full), A::AbstractMatrix, B::AbstractMatrix) | ||
| return copy!(similar(A, float(eltype(A))), A), copy!(similar(B, float(eltype(B))), B) | ||
| end | ||
| function copy_input(::typeof(gen_eig_vals), A::AbstractMatrix, B::AbstractMatrix) | ||
| return copy_input(gen_eig_full, A, B) | ||
| end | ||
|
|
||
| function check_input(::typeof(gen_eig_full!), A::AbstractMatrix, B::AbstractMatrix, WV) | ||
| ma, na = size(A) | ||
| mb, nb = size(B) | ||
| ma == na || throw(DimensionMismatch("square input matrix A expected")) | ||
| mb == nb || throw(DimensionMismatch("square input matrix B expected")) | ||
| ma == mb || throw(DimensionMismatch("first dimension of input matrices expected to match")) | ||
| na == nb || throw(DimensionMismatch("second dimension of input matrices expected to match")) | ||
| W, V = WV | ||
| @assert W isa Diagonal && V isa AbstractMatrix | ||
| @check_size(W, (ma, ma)) | ||
| @check_scalar(W, A, complex) | ||
| @check_scalar(W, B, complex) | ||
| @check_size(V, (ma, ma)) | ||
| @check_scalar(V, A, complex) | ||
| @check_scalar(V, B, complex) | ||
| return nothing | ||
| end | ||
| function check_input(::typeof(gen_eig_vals!), A::AbstractMatrix, B::AbstractMatrix, W) | ||
| ma, na = size(A) | ||
| mb, nb = size(B) | ||
| ma == na || throw(DimensionMismatch("square input matrix A expected")) | ||
| mb == nb || throw(DimensionMismatch("square input matrix B expected")) | ||
| ma == mb || throw(DimensionMismatch("dimension of input matrices expected to match")) | ||
| @assert W isa AbstractVector | ||
| @check_size(W, (na,)) | ||
| @check_scalar(W, A, complex) | ||
| @check_scalar(W, B, complex) | ||
| return nothing | ||
| end | ||
|
|
||
| # Outputs | ||
| # ------- | ||
| function initialize_output(::typeof(gen_eig_full!), A::AbstractMatrix, B::AbstractMatrix, ::LAPACK_EigAlgorithm) | ||
| n = size(A, 1) # square check will happen later | ||
| Tc = complex(eltype(A)) | ||
| W = Diagonal(similar(A, Tc, n)) | ||
| V = similar(A, Tc, (n, n)) | ||
| return (W, V) | ||
| end | ||
| function initialize_output(::typeof(gen_eig_vals!), A::AbstractMatrix, B::AbstractMatrix, ::LAPACK_EigAlgorithm) | ||
| n = size(A, 1) # square check will happen later | ||
| Tc = complex(eltype(A)) | ||
| D = similar(A, Tc, n) | ||
| return D | ||
| end | ||
|
|
||
| # Implementation | ||
| # -------------- | ||
| # actual implementation | ||
| function gen_eig_full!(A::AbstractMatrix, B::AbstractMatrix, WV, alg::LAPACK_EigAlgorithm) | ||
| check_input(gen_eig_full!, A, B, WV) | ||
| W, V = WV | ||
| if alg isa LAPACK_Simple | ||
| isempty(alg.kwargs) || | ||
| throw(ArgumentError("LAPACK_Simple (ggev) does not accept any keyword arguments")) | ||
| YALAPACK.ggev!(A, B, W.diag, V, similar(W.diag, eltype(A))) | ||
| else # alg isa LAPACK_Expert | ||
| throw(ArgumentError("LAPACK_Expert is not supported for ggev")) | ||
| end | ||
| # TODO: make this controllable using a `gaugefix` keyword argument | ||
| V = gaugefix!(V) | ||
| return W, V | ||
| end | ||
|
|
||
| function gen_eig_vals!(A::AbstractMatrix, B::AbstractMatrix, W, alg::LAPACK_EigAlgorithm) | ||
| check_input(gen_eig_vals!, A, B, W) | ||
| V = similar(A, complex(eltype(A)), (size(A, 1), 0)) | ||
| if alg isa LAPACK_Simple | ||
| isempty(alg.kwargs) || | ||
| throw(ArgumentError("LAPACK_Simple (ggev) does not accept any keyword arguments")) | ||
| YALAPACK.ggev!(A, B, W, V, similar(W, eltype(A))) | ||
| else # alg isa LAPACK_Expert | ||
| throw(ArgumentError("LAPACK_Expert is not supported for ggev")) | ||
| end | ||
| return W | ||
| 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
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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Gen Eig functions | ||
| # ------------- | ||
|
|
||
| # TODO: kwargs for sorting eigenvalues? | ||
|
|
||
| docs_gen_eig_note = """ | ||
| Note that [`gen_eig_full`](@ref) and its variants do not assume additional structure on the inputs, | ||
| and therefore will always return complex eigenvalues and eigenvectors. For the real | ||
| generalized eigenvalue decomposition is not yet supported. | ||
| """ | ||
|
|
||
| # TODO: do we need "full"? | ||
| """ | ||
| gen_eig_full(A, B; kwargs...) -> W, V | ||
| gen_eig_full(A, B, alg::AbstractAlgorithm) -> W, V | ||
| gen_eig_full!(A, B, [WV]; kwargs...) -> W, V | ||
| gen_eig_full!(A, B, [WV], alg::AbstractAlgorithm) -> W, V | ||
|
|
||
| Compute the full generalized eigenvalue decomposition of the square matrices `A` and `B`, | ||
| such that `A * V = B * V * W`, where the invertible matrix `V` contains the generalized eigenvectors | ||
| and the diagonal matrix `W` contains the associated generalized eigenvalues. | ||
|
|
||
| !!! note | ||
| The bang method `gen_eig_full!` optionally accepts the output structure and | ||
| possibly destroys the input matrices `A` and `B`. | ||
| Always use the return value of the function as it may not always be | ||
| possible to use the provided `WV` as output. | ||
|
|
||
| !!! note | ||
| $(docs_gen_eig_note) | ||
|
|
||
| See also [`gen_eig_vals(!)`](@ref eig_vals). | ||
| """ | ||
| @functiondef n_args=2 gen_eig_full | ||
|
|
||
| """ | ||
| gen_eig_vals(A, B; kwargs...) -> W | ||
| gen_eig_vals(A, B, alg::AbstractAlgorithm) -> W | ||
| gen_eig_vals!(A, B, [W]; kwargs...) -> W | ||
| gen_eig_vals!(A, B, [W], alg::AbstractAlgorithm) -> W | ||
|
|
||
| Compute the list of generalized eigenvalues of `A` and `B`. | ||
|
|
||
| !!! note | ||
| The bang method `gen_eig_vals!` optionally accepts the output structure and | ||
| possibly destroys the input matrices `A` and `B`. Always use the return | ||
| value of the function as it may not always be possible to use the | ||
| provided `W` as output. | ||
|
|
||
| !!! note | ||
| $(docs_gen_eig_note) | ||
|
|
||
| See also [`gen_eig_full(!)`](@ref gen_eig_full). | ||
| """ | ||
| @functiondef n_args=2 gen_eig_vals | ||
|
|
||
| # Algorithm selection | ||
| # ------------------- | ||
| default_gen_eig_algorithm(A, B; kwargs...) = default_gen_eig_algorithm(typeof(A), typeof(B); kwargs...) | ||
| default_gen_eig_algorithm(::Type{TA}, ::Type{TB}; kwargs...) where {TA, TB} = throw(MethodError(default_gen_eig_algorithm, (TA,TB))) | ||
| function default_gen_eig_algorithm(::Type{TA}, ::Type{TB}; kwargs...) where {TA<:YALAPACK.BlasMat,TB<:YALAPACK.BlasMat} | ||
| return LAPACK_Simple(; kwargs...) | ||
| end | ||
|
|
||
| for f in (:gen_eig_full!, :gen_eig_vals!) | ||
| @eval function default_algorithm(::typeof($f), ::Tuple{A, B}; kwargs...) where {A, B} | ||
| return default_gen_eig_algorithm(A, B; kwargs...) | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.