-
Notifications
You must be signed in to change notification settings - Fork 8
Taylor series native exponential implementation
#243
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
17 commits
Select commit
Hold shift + click to select a range
3c92f54
taylorexponentiate part 1
Jutho f9893c5
Integrate MatrixFunctionViaTaylor with the exponential interface
lkdvos 2a78be4
add benchmark script
lkdvos ab625a4
update docs
lkdvos 463fd84
make GPU-compatible
lkdvos 7184550
add DoubleFloat to benchmark
lkdvos fb5a1cf
use `one!`
lkdvos 00ab0d5
micro-optimize
lkdvos 8f13b77
try to be even smarter!
lkdvos fa8a6e1
fix specialization in signature
lkdvos 283f617
remove benchmark artifacts
lkdvos 881a01e
updates from code review
lkdvos ee2927c
simplify radix=2
lkdvos 50cf3c9
improve Paterson-Stockmeyer cost model and blocking
lkdvos 03d1da6
simplify order search loop
lkdvos 40f2c9e
cite Al-Mohy-Higham (2009) for the alpha_p norm bound
lkdvos c3d69f5
remove dead resize! of powers array
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """ | ||
| balance!(A::AbstractMatrix; maxiter=100) -> A, scale | ||
|
|
||
| Balance the square matrix `A` in place through a diagonal similarity `A ← D⁻¹ A D` that reduces its norm. | ||
| Each sweep computes, for every index at once, the power-of-two factor that best equalizes the | ||
| off-diagonal row and column norms (a simultaneous, Osborne-style variant of the Parlett–Reinsch scaling), | ||
| and applies all factors together; sweeps repeat until no index changes or `maxiter` is reached. | ||
|
|
||
| The scaling factors are integer powers of two, the machine radix, so that the transformation is exact even | ||
| in floating point arithmetic. The row and column norms are simultaneously adapted to avoid scalar indexing | ||
| and lots of kernel calls on GPUs. | ||
|
|
||
| The returned `scale` holds the diagonal of `D`, so that a matrix function of the original | ||
| input can be recovered from a matrix function `f` of the balanced matrix through | ||
| `D f(D⁻¹AD) D⁻¹`, i.e. `expA[i, j] = scale[i] * f(B)[i, j] / scale[j]`. | ||
| """ | ||
| function balance!(A::AbstractMatrix{T}; maxiter::Integer = 100) where {T} | ||
| n = LinearAlgebra.checksquare(A) | ||
| R = real(T) | ||
| β = convert(R, 2) | ||
| logβ = log(β) | ||
| scale = fill!(similar(A, R, n), one(R)) | ||
|
|
||
| colnorm = similar(A, R, n) | ||
| rownorm = similar(A, R, n) | ||
| f = similar(A, R, n) | ||
| colsum = reshape(colnorm, 1, n) | ||
| rowsum = reshape(rownorm, n, 1) | ||
| d = abs.(diagview(A)) | ||
| fᵀ = transpose(f) | ||
|
|
||
| for _ in 1:maxiter | ||
| fill!(colsum, zero(R)) | ||
| Base.mapreducedim!(abs, +, colsum, A) | ||
| fill!(rowsum, zero(R)) | ||
| Base.mapreducedim!(abs, +, rowsum, A) | ||
| colnorm .-= d | ||
| rownorm .-= d | ||
| f .= _balance_factor.(colnorm, rownorm, β, logβ) | ||
| all(isone, f) && break | ||
| # apply Aᵢⱼ ← Aᵢⱼ fⱼ / fᵢ (i.e. column j scaled by fⱼ, row i by 1/fᵢ) and accumulate. | ||
| A .= A .* fᵀ ./ f | ||
| scale .*= f | ||
| end | ||
|
|
||
| return A, scale | ||
| end | ||
|
|
||
| # Nearest power-of-`β` factor `f` that equalizes the scaled off-diagonal norms | ||
| # `colnorm·f` and `rownorm/f`, kept only when it reduces their sum (avoids oscillation and | ||
| # leaves degenerate rows/columns untouched). | ||
| @inline function _balance_factor(colnorm::R, rownorm::R, β::R, logβ::R) where {R} | ||
| (colnorm > 0 && rownorm > 0) || return one(R) | ||
| f = β^round(log(rownorm / colnorm) / (2 * logβ)) | ||
|
lkdvos marked this conversation as resolved.
|
||
| return (colnorm * f + rownorm / f) < convert(R, 19 // 20) * (colnorm + rownorm) ? f : one(R) | ||
| 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
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
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.