-
Notifications
You must be signed in to change notification settings - Fork 8
feat: squareroot, logarithm and power
#261
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
Draft
lkdvos
wants to merge
22
commits into
main
Choose a base branch
from
ld-matfun
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.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
17024d6
feat: add `squareroot`, `logarithm` and `power` matrix functions
lkdvos 080ee51
test: add tests for `squareroot`, `logarithm` and `power`
lkdvos ebe7d4c
docs: document new matrix functions and domain handling
lkdvos 793a697
refactor: reuse existing code patterns in matrix-function kernels
lkdvos a40b26c
refactor: delegate eigenvalue transforms to the DiagonalAlgorithm ker…
lkdvos 922948e
refactor: fold the eigenvalue domain scan into a single helper call
lkdvos 22e6b53
refactor: make domain-clamp helpers GPU compatible
lkdvos 759fe50
fix: correct the out-of-domain `DomainError` messages
lkdvos f2110d4
test: cover `squareroot`, `logarithm` and `power` on GPU
lkdvos 2db280b
refactor: build `Diagonal` copies via `map_diagonal`
lkdvos 647c8dd
test: move the matrix-function tests into the TestSuite
lkdvos 6427635
review comment
lkdvos dd70ffb
special case power!(A, 0/1)
lkdvos 3c65bc7
refactor: share input handling and reconstruction between matrix func…
lkdvos 077cc7b
fix: make `exponential` hermitian to the last bit for complex input
lkdvos 4e37944
docs: reference #261 in the changelog and record the behaviour changes
lkdvos a82a76f
docs: update the test README for the matrixfunctions group
lkdvos 234d492
docs: fix the Pkg workspace link in the test README
lkdvos c2750a7
copyto! -> copy!
lkdvos 4849584
code style suggestion
lkdvos 9f527af
tolerance in Float64
lkdvos f94f6bc
matrix function tolerances are hard
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
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
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,65 @@ | ||
| # Inputs | ||
| # ------ | ||
| copy_input(::typeof(logarithm), A::AbstractMatrix) = _matrixfunction_copy_input(A) | ||
|
|
||
| function check_input(::typeof(logarithm!), A::AbstractMatrix, logA, alg::AbstractAlgorithm) | ||
| return _matrixfunction_check_input(A, logA, alg) | ||
| end | ||
|
|
||
| # Algorithm selection | ||
| # ------------------- | ||
| logarithm!(A::AbstractMatrix, alg::DefaultAlgorithm) = logarithm!(A, select_algorithm(logarithm!, A, nothing; alg.kwargs...)) | ||
| logarithm!(A::AbstractMatrix, out, alg::DefaultAlgorithm) = logarithm!(A, out, select_algorithm(logarithm!, A, nothing; alg.kwargs...)) | ||
|
|
||
| # Outputs | ||
| # ------- | ||
| initialize_output(::typeof(logarithm!), A::AbstractMatrix, ::AbstractAlgorithm) = A | ||
|
|
||
| # Implementation | ||
| # -------------- | ||
| function logarithm!(A::AbstractMatrix, logA, alg::MatrixFunctionViaLA) | ||
| check_input(logarithm!, A, logA, alg) | ||
| domain_atol = _la_domain_atol(alg, logarithm!) | ||
| # `LinearAlgebra.log` of a real matrix is real whenever the principal logarithm is. Note that a | ||
| # (numerically) zero eigenvalue goes undetected here, as documented in the manual. | ||
| logAc = LinearAlgebra.log(A) | ||
| if eltype(logAc) <: Complex && !(eltype(logA) <: Complex) | ||
| _la_project_real!(logA, logAc, domain_atol, logarithm!) | ||
| else | ||
| copy!(logA, logAc) | ||
| end | ||
| return logA | ||
| end | ||
|
|
||
| function logarithm!(A::AbstractMatrix, logA, alg::MatrixFunctionViaEigh) | ||
| check_input(logarithm!, A, logA, alg) | ||
| D, V = eigh_full!(A, alg.eigh_alg) | ||
| diag_alg = DiagonalAlgorithm(; domain_atol = _resolve_domain_atol(diagview(D), alg)) | ||
| return _apply_eigh!(logA, V, logarithm!(D, D, diag_alg)) | ||
| end | ||
|
|
||
| function logarithm!(A::AbstractMatrix, logA, alg::MatrixFunctionViaEig) | ||
| check_input(logarithm!, A, logA, alg) | ||
| D, V = eig_full!(A, alg.eig_alg) | ||
| λ = diagview(D) | ||
| atol = _resolve_domain_atol(λ, alg) | ||
| _check_nonzero_eigenvalues(λ, atol) | ||
| # a real result requires the spectrum to stay off the negative real axis | ||
| eltype(A) <: Real && _check_domain_eigenvalues(λ, atol, false) | ||
| diag_alg = DiagonalAlgorithm(; domain_atol = atol) | ||
| return _apply_eig!(logA, V, logarithm!(D, D, diag_alg)) | ||
| end | ||
|
|
||
| # Diagonal logic | ||
| # -------------- | ||
| function logarithm!(A::AbstractMatrix, logA, alg::DiagonalAlgorithm) | ||
| check_input(logarithm!, A, logA, alg) | ||
| λ = diagview(logA) | ||
| copy!(λ, diagview(A)) | ||
| atol = _resolve_domain_atol(λ, alg) | ||
| # `log(0)` does not exist, so the origin is excluded from the domain and nothing is clamped | ||
| _check_nonzero_eigenvalues(λ, atol) | ||
| eltype(λ) <: Real && _check_domain_eigenvalues(λ, atol, false) | ||
| λ .= log.(λ) | ||
| return logA | ||
| 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.