-
Notifications
You must be signed in to change notification settings - Fork 5
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
25
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 6 commits
Commits
Show all changes
25 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # 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) | ||
|
|
||
| function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm) | ||
| m, n = size(A) | ||
| m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)")) | ||
| @check_size(expA, (m, m)) | ||
| return @check_scalar(expA, A) | ||
| end | ||
|
|
||
| function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, ::DiagonalAlgorithm) | ||
| m, n = size(A) | ||
| @assert m == n && isdiag(A) | ||
| @assert expA isa Diagonal | ||
| @check_size(expA, (m, m)) | ||
| @check_scalar(expA, A) | ||
| return nothing | ||
| end | ||
|
|
||
| # Outputs | ||
| # ------- | ||
| function initialize_output(::typeof(exponential!), A::AbstractMatrix, ::AbstractAlgorithm) | ||
| n = size(A, 1) # square check will happen later | ||
| expA = similar(A, (n, n)) | ||
lkdvos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return expA | ||
| end | ||
|
|
||
| function initialize_output(::typeof(exponential!), A::Diagonal, ::DiagonalAlgorithm) | ||
| return similar(A) | ||
| end | ||
lkdvos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Implementation | ||
| # -------------- | ||
| function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaLA) | ||
lkdvos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| copyto!(expA, LinearAlgebra.exp(A)) | ||
lkdvos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return expA | ||
| end | ||
|
|
||
| function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaEigh) | ||
lkdvos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| D, V = eigh_full(A, alg.eigh_alg) | ||
lkdvos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| iV = inv(V) | ||
lkdvos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| map!(exp, diagview(D), diagview(D)) | ||
| mul!(expA, rmul!(V, D), iV) | ||
| return expA | ||
| end | ||
|
|
||
| function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaEig) | ||
| D, V = eig_full(A, alg.eig_alg) | ||
| iV = inv(V) | ||
| map!(exp, diagview(D), diagview(D)) | ||
| mul!(expA, rmul!(V, D), iV) | ||
| return expA | ||
| end | ||
|
|
||
| # Diagonal logic | ||
| # -------------- | ||
| function exponential!(A::Diagonal, expA, alg::DiagonalAlgorithm) | ||
| check_input(exponential!, A, expA, alg) | ||
| map!(exp, diagview(expA), diagview(A)) | ||
| return expA | ||
| 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,16 @@ | ||
| # Exponential functions | ||
| # -------------- | ||
| @functiondef exponential | ||
|
|
||
| # Algorithm selection | ||
| # ------------------- | ||
| default_exponential_algorithm(A; kwargs...) = default_exponential_algorithm(typeof(A); kwargs...) | ||
| function default_exponential_algorithm(T::Type; kwargs...) | ||
| return ExponentialViaLA(; kwargs...) | ||
| end | ||
|
|
||
| for f in (:exponential!,) | ||
| @eval function default_algorithm(::typeof($f), ::Type{A}; kwargs...) where {A} | ||
| return default_exponential_algorithm(A; kwargs...) | ||
| end | ||
| 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,48 @@ | ||
| using MatrixAlgebraKit | ||
| using Test | ||
| using TestExtras | ||
| using StableRNGs | ||
| using MatrixAlgebraKit: diagview | ||
| using LinearAlgebra | ||
|
|
||
| BLASFloats = (Float32, Float64, ComplexF32, ComplexF64) | ||
| GenericFloats = (Float16, ComplexF16, BigFloat, Complex{BigFloat}) | ||
|
|
||
| @testset "exponential! for T = $T" for T in BLASFloats | ||
| rng = StableRNG(123) | ||
| m = 2 | ||
|
|
||
| A = randn(rng, T, m, m) | ||
| A = (A + A') / 2 | ||
| D, V = @constinferred eigh_full(A) | ||
| algs = (ExponentialViaLA(), ExponentialViaEig(LAPACK_Simple()), ExponentialViaEigh(LAPACK_QRIteration())) | ||
| expA_LA = @constinferred exp(A) | ||
| @testset "algorithm $alg" for alg in algs | ||
| expA = similar(A) | ||
|
|
||
| @constinferred exponential!(copy(A), expA) | ||
| expA2 = @constinferred exponential(A; alg = alg) | ||
| @test expA ≈ expA_LA | ||
| @test expA2 ≈ expA | ||
|
|
||
| Dexp, Vexp = @constinferred eigh_full(expA) | ||
| @test diagview(Dexp) ≈ LinearAlgebra.exp.(diagview(D)) | ||
| end | ||
| end | ||
|
|
||
| @testset "exponential! for Diagonal{$T}" for T in (BLASFloats..., GenericFloats...) | ||
| rng = StableRNG(123) | ||
| atol = sqrt(eps(real(T))) | ||
| m = 54 | ||
| Ad = randn(T, m) | ||
| A = Diagonal(Ad) | ||
|
|
||
| expA = similar(A) | ||
| @constinferred exponential!(copy(A), expA) | ||
| expA2 = @constinferred exponential(A; alg = DiagonalAlgorithm()) | ||
| @test expA2 ≈ expA | ||
|
|
||
| D, V = @constinferred eig_full(A) | ||
| Dexp, Vexp = @constinferred eig_full(expA) | ||
| @test diagview(Dexp) ≈ LinearAlgebra.exp.(diagview(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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using MatrixAlgebraKit | ||
| using Test | ||
| using TestExtras | ||
| using StableRNGs | ||
| using MatrixAlgebraKit: diagview | ||
| using LinearAlgebra | ||
|
|
||
| GenericFloats = (BigFloat, Complex{BigFloat}) | ||
|
|
||
| @testset "exp! for T = $T" for T in GenericFloats | ||
| rng = StableRNG(123) | ||
| m = 2 | ||
|
|
||
| A = randn(rng, T, m, m) | ||
| A = (A + A') / 2 | ||
| D, V = @constinferred eigh_full(A) | ||
| algs = (ExponentialViaEigh(GLA_QRIteration()),) | ||
| @testset "algorithm $alg" for alg in algs | ||
| expA = similar(A) | ||
|
|
||
| @constinferred exponential!(copy(A), expA; alg) | ||
| expA2 = @constinferred exponential(A; alg) | ||
| @test expA2 ≈ expA | ||
|
|
||
| Dexp, Vexp = @constinferred eigh_full(expA) | ||
| @test diagview(Dexp) ≈ LinearAlgebra.exp.(diagview(D)) | ||
| end | ||
| 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,29 @@ | ||
| using MatrixAlgebraKit | ||
| using Test | ||
| using TestExtras | ||
| using StableRNGs | ||
| using MatrixAlgebraKit: diagview | ||
| using LinearAlgebra | ||
|
|
||
| GenericFloats = (BigFloat, Complex{BigFloat}) | ||
|
|
||
| @testset "exp! for T = $T" for T in GenericFloats | ||
| rng = StableRNG(123) | ||
| m = 2 | ||
|
|
||
| A = randn(rng, T, m, m) | ||
| D, V = @constinferred eig_full(A) | ||
| algs = (ExponentialViaEig(GS_QRIteration()),) | ||
| expA_LA = @constinferred exponential(A) | ||
| @testset "algorithm $alg" for alg in algs | ||
| expA = similar(A) | ||
|
|
||
| @constinferred exponential!(copy(A), expA) | ||
| expA2 = @constinferred exponential(A; alg = alg) | ||
| @test expA ≈ expA_LA | ||
| @test expA2 ≈ expA | ||
|
|
||
| Dexp, Vexp = @constinferred eig_full(expA) | ||
| @test diagview(Dexp) ≈ LinearAlgebra.exp.(diagview(D)) | ||
| end | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.