Add a generic matrix exponential, for `exp` and `cis` of `BigFloat`, quaternion and block matrices #1789
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 574 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.380
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1789"
general
weight: 0.35
mikmoore
cis(::Matrix{BigFloat})throwsMethodError: no method matching exp!(::Matrix{Complex{BigFloat}}). The cause is broader thancis:exp(A::AbstractMatrix)callsexp!, which only has a method forStridedMatrix{<:BlasFloat}. So for matrices of element types likeBigFloatorComplex{BigFloat}(andRational, whoseeigtypeisBigFloat), all of these threw:exp;cis,sin,cos,tan,sinh,cosh,tanh, which go throughexp.This PR adds a generic, duck-typed
exp!(::AbstractMatrix). The BLAS method is more specific and stays unchanged. The algorithm is scaling and squaring with a diagonal Padé approximant (Golub and Van Loan, Matrix Computations, 4th ed., Algorithm 9.3.1):1. Scale
Aby2^-sso thatnorm(A) ≤ 1/2. The Frobenius norm bounds the 2-norm and is also defined for block matrices.2. Choose the smallest degree
qfor which the error bound2^(3-2q) (q!)^2/((2q)!(2q+1)!)is at mostepsof the real type of the norm. This givesq = 7at Float64 precision (53 bits),q = 22at 256 bits, and grows withprecision(BigFloat).3. Solve
D \ N, and squarestimes.Choosing the degree from the working precision makes the result accurate to that precision, not just to Float64 precision.
Supported types
The method needs only a norm,
+,,one/zeroof the elements and the generic LU solve. So it works for:FixedSizeMatrix,SMatrix,SizedMatrixorImmutableArrayofBigFloat;Numbers, such as quaternions;Matrix{SMatrix{2,2,Float64}}orMatrix{SizedMatrix{2,2,BigFloat}}. Their element type is kept, rather than promoted byeigtype.For elements that are not numbers,
Dis factorized without pivoting, as blocks can't be compared for pivoting. This is stable becauseDis close to the identity.Blocks whose size is not part of their type, like
MatrixorFixedSizeMatrixblocks, need the generic triangular solves to support them, which is #1792. With it, they also work with blocks of different sizes; the tests for that are in #1792.A diagonal matrix takes the exponential of its diagonal elements, for any element type that has an
expmethod: real and complex numbers, but also quaternions, and square blocks. Element types without anexpmethod, such as a minimal quaternion type, take the Padé path.As in the BLAS method:
ArgumentError("matrix contains Infs or NaNs"). On the BLAS path,gebal!throws this.Unlike the BLAS method, there is no balancing, and no
Hermitianshortcut, since a genericeigenis not available.Accuracy
Errors relative to the machine precision of
BigFloat:| Test | 256 bits | 512 bits | 2048 bits |
|---|---|---|---|
|
exp([0 -t; t 0])vs. the rotation matrix,t = 3/2| 2.3 eps | 5.0 eps | 3.0 eps ||
exp([10 1; 0 10])vs.e^10 [1 1; 0 1](relative) | 2.5 eps | 32 eps | 76 eps ||
sin(A)^2 + cos(A)^2 - I| 8.9 eps | 27 eps | 8.1 eps |The results for
BigFloatandComplex{BigFloat}agree with theFloat64path to within about1e-15. Quaternion and block matrices agree with the corresponding matrices of numbers to within about4e-15.Tests
A new testset in
test/dense.jlcovers:expforBigFloat,Complex{BigFloat}andRational{BigInt};cis,sinandcos;ArgumentErrorfor NaNs;SizedArrayandImmutableArraycontainers;SizedArrayblocks ofFloat64andBigFloat, against the flattened matrix;SizedArrayblocks, which gives exactly the exponentials of the blocks, and for quaternions without anexpmethod, which take the Padé path.test/dense.jl,generic.jl,symmetric.jl,triangular.jl,diagonal.jl,unitful.jlandambiguous_exec.jlpass.Matrix functions that need
eigenorschur(exp(::Hermitian{BigFloat}),log,sqrt, non-integer powers) still throw forBigFloat, as there is no genericeigen!/schur!. That is unchanged by this PR. I did not add aHISTORY.mdentry, as it has not been updated since v1.12; I can add one if wanted. Found while adding coverage tests in #1786.---
_Generated by Claude Code_