-
Notifications
You must be signed in to change notification settings - Fork 7
Exponential #94
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
Open
sanderdemeyer
wants to merge
72
commits into
QuantumKitHub:main
Choose a base branch
from
sanderdemeyer:exponential
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Exponential #94
Changes from all commits
Commits
Show all changes
72 commits
Select commit
Hold shift + click to select a range
eb913cb
implement exponential
sanderdemeyer a3dc04d
update on exponential
sanderdemeyer c4564ee
Merge branch 'QuantumKitHub:main' into exponential
sanderdemeyer 8dc3ecd
remove comment
sanderdemeyer d9fb748
Merge branch 'exponential' of https://github.com/sanderdemeyer/Matrix…
sanderdemeyer 5095cdb
comments
sanderdemeyer 89dfa23
change name of decompositions.jl to matrixfunctions.jl
sanderdemeyer 996ecb5
revert name change
sanderdemeyer dc78eb0
Merge branch 'main' into exponential
sanderdemeyer f220035
general comments
sanderdemeyer c68afad
bug fix
sanderdemeyer 95ddb06
avoid allocation in diagonal case
sanderdemeyer 5d6f4f3
Merge branch 'main' into exponential
sanderdemeyer c8e811c
include exponentiali(tau, A)
sanderdemeyer 0229417
remove simple test case and make the test more general
sanderdemeyer cbbf813
fix formatting
sanderdemeyer d08d545
add docs
sanderdemeyer 720ada5
remove a bunch of allocations and clean up
lkdvos d738c22
Merge branch 'main' into exponential
lkdvos be111ea
introduce `map_diagonal` to simplify and relax types
lkdvos c760a47
rework tests
lkdvos d0d14e1
revert wrong filename changes
lkdvos cf98bd4
avoid running non-GPU tests through buildkite
lkdvos 1536eb4
correct wrong in-place assumptions
lkdvos 349800e
fixes part II
lkdvos 28b5bc5
exponentialr instead of exponentiali
sanderdemeyer c313009
Merge branch 'main' into exponential
sanderdemeyer 55794da
fix default algorithms
sanderdemeyer e04d94c
Merge branch 'exponential' of https://github.com/sanderdemeyer/Matrix…
sanderdemeyer 04f08ab
fix default algorithms 2
sanderdemeyer bfcd6ca
Fix merge
sanderdemeyer 3c3124f
fix default exponential algorithm
sanderdemeyer 04a1436
Merge branch 'main' into exponential
sanderdemeyer 2d736f6
add packages to tests
sanderdemeyer 89f02c3
Update ext/MatrixAlgebraKitGenericSchurExt.jl
sanderdemeyer dba342f
Update test/genericlinearalgebra/exponential.jl
sanderdemeyer ed4228e
Update test/genericlinearalgebra/exponential.jl
sanderdemeyer 329f493
change name of `exponentialr` to `exponential`
sanderdemeyer 62b448b
fix formatting
sanderdemeyer 1cd993e
Fix `check_scalar`
sanderdemeyer cdec4d5
Merge branch 'main' into exponential
sanderdemeyer dc9d48e
Update `check_scalar`
sanderdemeyer 386670d
Merge branch 'exponential' of https://github.com/sanderdemeyer/Matrix…
sanderdemeyer f160b21
remove redundant `ishermitian` checks
sanderdemeyer 08085c9
Update src/implementations/exponential.jl
sanderdemeyer c3ba3be
Update src/implementations/exponential.jl
sanderdemeyer 32670d7
Update src/implementations/exponential.jl
sanderdemeyer 03ce510
Update src/implementations/exponential.jl
sanderdemeyer 70f692f
Update src/implementations/exponential.jl
sanderdemeyer 67b2faf
Update src/implementations/exponential.jl
sanderdemeyer 6589265
change from two-argument to one-argument `check_input`
sanderdemeyer f508b00
Update src/implementations/exponential.jl
sanderdemeyer 1c1c394
Update src/implementations/exponential.jl
sanderdemeyer e28ea8c
Remove undefined exports
sanderdemeyer 414097f
Commit suggestions
sanderdemeyer efd772c
Update src/interface/matrixfunctions.jl
sanderdemeyer a5a724d
Update src/interface/matrixfunctions.jl
sanderdemeyer 49a0857
Update src/interface/matrixfunctions.jl
sanderdemeyer a4c2344
Update src/interface/exponential.jl
sanderdemeyer b571778
Update exponential.jl
sanderdemeyer 0e0d68c
Merge branch 'main' into exponential
sanderdemeyer f8d92fb
Update src/implementations/exponential.jl
sanderdemeyer 888d589
Update src/implementations/exponential.jl
sanderdemeyer 940cbc3
Update src/implementations/exponential.jl
sanderdemeyer 55ecb21
Fix typo in suggested change
sanderdemeyer 8238545
Merge branch 'main' into exponential
sanderdemeyer 6fb3f9c
Update exponential via MatrixFunctionViaEigh
sanderdemeyer f536a2d
Update src/interface/exponential.jl
sanderdemeyer 22ffa75
Update src/interface/exponential.jl
sanderdemeyer a7bdadd
Update src/implementations/exponential.jl
sanderdemeyer 825a072
Update src/implementations/exponential.jl
sanderdemeyer c543b01
Update src/implementations/exponential.jl
sanderdemeyer 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
Some comments aren't visible on the classic Files Changed page.
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
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,109 @@ | ||
| # Inputs | ||
| # ------ | ||
| function copy_input(::typeof(exponential), A::AbstractMatrix) | ||
| return copy!(similar(A, float(eltype(A))), A) | ||
| end | ||
|
|
||
| copy_input(::typeof(exponential), A::Diagonal) = copy(A) | ||
| copy_input(::typeof(exponential), (τ, A)::Tuple{Number, AbstractMatrix}) = (τ, copy!(similar(A, float(eltype(A))), A)) | ||
| copy_input(::typeof(exponential), (τ, A)::Tuple{Number, Diagonal}) = τ, copy(A) | ||
|
|
||
| function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm) | ||
| m = LinearAlgebra.checksquare(A) | ||
| @check_size(expA, (m, m)) | ||
| @check_scalar(expA, A) | ||
| return nothing | ||
| end | ||
|
|
||
| function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, ::DiagonalAlgorithm) | ||
| m = LinearAlgebra.checksquare(A) | ||
| @assert isdiag(A) | ||
| @assert expA isa Diagonal | ||
| @check_size(expA, (m, m)) | ||
| @check_scalar(expA, A) | ||
| return nothing | ||
| end | ||
|
|
||
| function check_input(::typeof(exponential!), (τ, A)::Tuple{Number, AbstractMatrix}, expA::AbstractMatrix, alg::AbstractAlgorithm) | ||
| m = LinearAlgebra.checksquare(A) | ||
| @check_size(expA, (m, m)) | ||
| @check_scalar(expA, A, (τ isa Real) ? identity : complex) | ||
| return nothing | ||
| end | ||
|
|
||
| function check_input(::typeof(exponential!), (τ, A)::Tuple{Number, AbstractMatrix}, expA::AbstractMatrix, ::DiagonalAlgorithm) | ||
| m = LinearAlgebra.checksquare(A) | ||
| @assert isdiag(A) | ||
| @assert expA isa Diagonal | ||
| @check_size(expA, (m, m)) | ||
| @check_scalar(expA, A, (τ isa Real) ? identity : complex) | ||
| return nothing | ||
| end | ||
|
|
||
| # Outputs | ||
| # ------- | ||
| initialize_output(::typeof(exponential!), A::AbstractMatrix, ::AbstractAlgorithm) = A | ||
| initialize_output(::typeof(exponential!), (τ, A)::Tuple{T, AbstractMatrix}, ::AbstractAlgorithm) where {T <: Real} = A | ||
| initialize_output(::typeof(exponential!), (τ, A)::Tuple{Number, AbstractMatrix}, ::AbstractAlgorithm) = complex(A) | ||
|
|
||
| # Implementation | ||
| # -------------- | ||
| function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaLA) | ||
| check_input(exponential!, A, expA, alg) | ||
| A = LinearAlgebra.exp!(A) | ||
| A === expA || copy!(expA, A) | ||
| return expA | ||
| end | ||
|
|
||
| exponential!(A, expA, alg::MatrixFunctionViaEigh) = exponential!((one(eltype(A)), A), expA, alg) | ||
| exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEig) = exponential!((one(eltype(A)), A), expA, alg) | ||
|
|
||
| function exponential!((τ, A)::Tuple{Number, AbstractMatrix}, expA::AbstractMatrix, alg::AbstractAlgorithm) | ||
| expA .= A .* τ | ||
| return exponential!(expA, expA, alg) | ||
| end | ||
|
|
||
| function exponential!((τ, A)::Tuple{Number, AbstractMatrix}, expA::AbstractMatrix, alg::MatrixFunctionViaEigh) | ||
| check_input(exponential!, (τ, A), expA, alg) | ||
| D, V = eigh_full!(A, alg.eigh_alg) | ||
| if eltype(A) <: Real | ||
| if eltype(τ) <: Real | ||
| VexpD = rmul!(V, exponential!((τ / 2, D), D)) | ||
| else | ||
| VexpD = V * exponential((τ / 2, D)) | ||
| end | ||
| return mul!(expA, VexpD, transpose(VexpD)) | ||
| else | ||
| if eltype(τ) <: Real | ||
| VexpD = V * exponential!((τ, D), D) | ||
| else | ||
| VexpD = V * exponential((τ, D)) | ||
| end | ||
| return mul!(expA, VexpD, V') | ||
| end | ||
| end | ||
|
|
||
| function exponential!((τ, A)::Tuple{Number, AbstractMatrix}, expA::AbstractMatrix, alg::MatrixFunctionViaEig) | ||
| check_input(exponential!, (τ, A), expA, alg) | ||
| D, V = eig_full!(A, alg.eig_alg) | ||
| if eltype(A) <: Real && eltype(τ) <: Real | ||
| VexpD = V * exponential!((τ, D), D) | ||
| expAc = rdiv!(VexpD, LinearAlgebra.lu!(V)) | ||
| return expA .= real.(expAc) | ||
| else | ||
| expA .= V .* transpose(diagview(exponential!((τ, D), D))) | ||
| return rdiv!(expA, LinearAlgebra.lu!(V)) | ||
| end | ||
| end | ||
|
|
||
| # Diagonal logic | ||
| # -------------- | ||
| function exponential!(A, expA, alg::DiagonalAlgorithm) | ||
| check_input(exponential!, A, expA, alg) | ||
| return map_diagonal!(exp, expA, A) | ||
| end | ||
|
|
||
| function exponential!((τ, A)::Tuple{Number, Any}, expA, alg::DiagonalAlgorithm) | ||
| check_input(exponential!, (τ, A), expA, alg) | ||
| return map_diagonal!(x -> exp(x * τ), expA, A) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Exponential functions | ||
| # -------------- | ||
|
|
||
| """ | ||
| exponential(A; kwargs...) -> expA | ||
| exponential(A, alg::AbstractAlgorithm) -> expA | ||
| exponential!(A, [expA]; kwargs...) -> expA | ||
| exponential!(A, [expA], alg::AbstractAlgorithm) -> expA | ||
| exponential((τ, A); kwargs...) -> expτA | ||
| exponential((τ, A), alg::AbstractAlgorithm) -> expτA | ||
| exponential!((τ, A), [expA]; kwargs...) -> expτA | ||
| exponential!((τ, A), [expA], alg::AbstractAlgorithm) -> expτA | ||
|
|
||
| Compute the exponential of the square matrix `A` or `τ * A`, | ||
|
|
||
| !!! note | ||
| The bang method `exponential!` optionally accepts the output structure and | ||
| possibly destroys the input matrix `A`. Always use the return value of the function | ||
| as it may not always be possible to use the provided `expA` as output. | ||
| """ | ||
| @functiondef exponential | ||
|
|
||
| # Algorithm selection | ||
| # ------------------- | ||
| default_exponential_algorithm(A; kwargs...) = default_exponential_algorithm(typeof(A); kwargs...) | ||
| function default_exponential_algorithm(T::Type; kwargs...) | ||
| return MatrixFunctionViaLA(; kwargs...) | ||
| end | ||
| function default_exponential_algorithm(::Type{T}; kwargs...) where {T <: Diagonal} | ||
| return DiagonalAlgorithm(; kwargs...) | ||
| end | ||
|
|
||
| function default_algorithm(::typeof(exponential!), ::Type{A}; kwargs...) where {A} | ||
| return default_exponential_algorithm(A; kwargs...) | ||
| end | ||
|
|
||
| function default_algorithm(::typeof(exponential!), ::Tuple{A, B}; kwargs...) where {A, B} | ||
| return default_exponential_algorithm(B; kwargs...) | ||
| end |
|
sanderdemeyer marked this conversation as resolved.
|
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,39 @@ | ||
| # ================================ | ||
| # EXPONENTIAL ALGORITHMS | ||
| # ================================ | ||
| """ | ||
| MatrixFunctionViaLA() | ||
|
|
||
| Algorithm type to denote finding the exponential of `A` via the implementation of `LinearAlgebra`. | ||
| """ | ||
| @algdef MatrixFunctionViaLA | ||
|
|
||
| """ | ||
| MatrixFunctionViaEigh(eigh_alg) | ||
|
|
||
| Algorithm type for computing a function of a matrix by computing its hermitian eigenvalue decomposition and applying the function to the eigenvalues. | ||
| The `eigh_alg` specifies which hermitian eigendecomposition implementation to use. | ||
| """ | ||
| struct MatrixFunctionViaEigh{A <: AbstractAlgorithm} <: AbstractAlgorithm | ||
| eigh_alg::A | ||
| end | ||
| function Base.show(io::IO, alg::MatrixFunctionViaEigh) | ||
| print(io, "MatrixFunctionViaEigh(") | ||
| _show_alg(io, alg.eigh_alg) | ||
| return print(io, ")") | ||
| end | ||
|
|
||
| """ | ||
| MatrixFunctionViaEig(eig_alg) | ||
|
|
||
| Algorithm type for computing a function of a matrix by computing its eigenvalue decomposition and applying the function to the eigenvalues. | ||
| The `eig_alg` specifies which eigendecomposition implementation to use. | ||
| """ | ||
| struct MatrixFunctionViaEig{A <: AbstractAlgorithm} <: AbstractAlgorithm | ||
| eig_alg::A | ||
| end | ||
| function Base.show(io::IO, alg::MatrixFunctionViaEig) | ||
| print(io, "MatrixFunctionViaEig(") | ||
| _show_alg(io, alg.eig_alg) | ||
| return print(io, ")") | ||
| end |
This file was deleted.
Oops, something went wrong.
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,88 @@ | ||
| using MatrixAlgebraKit | ||
| using Test | ||
| using TestExtras | ||
| using StableRNGs | ||
| using MatrixAlgebraKit: diagview | ||
| using LinearAlgebra | ||
| using LinearAlgebra: exp | ||
|
|
||
| BLASFloats = (Float32, Float64, ComplexF32, ComplexF64) | ||
| GenericFloats = (Float16, ComplexF16, BigFloat, Complex{BigFloat}) | ||
|
|
||
| @testset "exponential! for T = $T" for T in BLASFloats | ||
| rng = StableRNG(123) | ||
| m = 54 | ||
|
|
||
| A = LinearAlgebra.normalize!(randn(rng, T, m, m)) | ||
| Ac = copy(A) | ||
| expA = LinearAlgebra.exp(A) | ||
|
|
||
| expA2 = @constinferred exponential(A) | ||
| @test expA ≈ expA2 | ||
| @test A == Ac | ||
|
|
||
| algs = (MatrixFunctionViaLA(), MatrixFunctionViaEig(LAPACK_Simple())) | ||
| @testset "algorithm $alg" for alg in algs | ||
| expA2 = @constinferred exponential(A, alg) | ||
| @test expA ≈ expA2 | ||
| @test A == Ac | ||
| end | ||
|
|
||
| @test_throws DomainError exponential(A; alg = MatrixFunctionViaEigh(LAPACK_QRIteration())) | ||
| end | ||
|
|
||
| @testset "exponential! for T = $T" for T in BLASFloats | ||
| rng = StableRNG(123) | ||
| m = 54 | ||
|
|
||
| A = randn(rng, T, m, m) | ||
| τ = randn(rng, T) | ||
| Ac = copy(A) | ||
|
|
||
| Aτ = A * τ | ||
| expAτ = LinearAlgebra.exp(Aτ) | ||
|
|
||
| expAτ2 = @constinferred exponential((τ, A)) | ||
| @test expAτ ≈ expAτ2 | ||
| @test A == Ac | ||
|
|
||
| algs = (MatrixFunctionViaLA(), MatrixFunctionViaEig(LAPACK_Simple())) | ||
| @testset "algorithm $alg" for alg in algs | ||
| expAτ2 = @constinferred exponential((τ, A), alg) | ||
| @test expAτ ≈ expAτ2 | ||
| @test A == Ac | ||
| end | ||
|
|
||
| @test_throws DomainError exponential((τ, A); alg = MatrixFunctionViaEigh(LAPACK_QRIteration())) | ||
| end | ||
|
|
||
| @testset "exponential! for Diagonal{$T}" for T in (BLASFloats..., GenericFloats...) | ||
| rng = StableRNG(123) | ||
| m = 54 | ||
|
|
||
| A = Diagonal(randn(rng, T, m)) | ||
| τ = randn(rng, T) | ||
| Ac = copy(A) | ||
|
|
||
| expA = LinearAlgebra.exp(A) | ||
|
|
||
| expA2 = @constinferred exponential(A) | ||
| @test expA ≈ expA2 | ||
| @test A == Ac | ||
| end | ||
|
|
||
| @testset "exponential! for Diagonal{$T}" for T in (BLASFloats..., GenericFloats...) | ||
| rng = StableRNG(123) | ||
| m = 1 | ||
|
|
||
| A = Diagonal(randn(rng, T, m)) | ||
| τ = randn(rng, T) | ||
| Ac = copy(A) | ||
|
|
||
| Aτ = A * τ | ||
| expAτ = LinearAlgebra.exp(Aτ) | ||
|
|
||
| expAτ2 = @constinferred exponential((τ, A)) | ||
| @test expAτ ≈ expAτ2 | ||
| @test A == Ac | ||
| 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.