← Back to Pull requests

Add a generic matrix exponential, for `exp` and `cis` of `BigFloat`, quaternion and block matrices #1789

Closed
unknown closed this pull request without merging on Nov 09, 2026 10:14 UTC
View on GitHub
unknown unknown commented on Oct 18, 2026 20:14 UTC
Author

cis(::Matrix{BigFloat}) throws MethodError: no method matching exp!(::Matrix{Complex{BigFloat}}). The cause is broader than cis: exp(A::AbstractMatrix) calls exp!, which only has a method for StridedMatrix{<:BlasFloat}. So for matrices of element types like BigFloat or Complex{BigFloat} (and Rational, whose eigtype is BigFloat), all of these threw:

  • exp;

  • cis, sin, cos, tan, sinh, cosh, tanh, which go through exp.
  • 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 A by 2^-s so that norm(A) ≤ 1/2. The Frobenius norm bounds the 2-norm and is also defined for block matrices.
    2. Choose the smallest degree q for which the error bound 2^(3-2q) (q!)^2/((2q)!(2q+1)!) is at most eps of the real type of the norm. This gives q = 7 at Float64 precision (53 bits), q = 22 at 256 bits, and grows with precision(BigFloat).
    3. Solve D \ N, and square s times.

    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/zero of the elements and the generic LU solve. So it works for:
  • other array types, such as FixedSizeMatrix, SMatrix, SizedMatrix or ImmutableArray of BigFloat;

  • element types that are Numbers, such as quaternions;

  • block matrices whose blocks have their size in the type, such as Matrix{SMatrix{2,2,Float64}} or Matrix{SizedMatrix{2,2,BigFloat}}. Their element type is kept, rather than promoted by eigtype.
  • For elements that are not numbers, D is factorized without pivoting, as blocks can't be compared for pivoting. This is stable because D is close to the identity.

    Blocks whose size is not part of their type, like Matrix or FixedSizeMatrix blocks, 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 exp method: real and complex numbers, but also quaternions, and square blocks. Element types without an exp method, such as a minimal quaternion type, take the Padé path.

    As in the BLAS method:

  • a diagonal matrix takes this elementwise shortcut, so a NaN in it gives NaNs;

  • otherwise, Infs and NaNs throw ArgumentError("matrix contains Infs or NaNs"). On the BLAS path, gebal! throws this.
  • Unlike the BLAS method, there is no balancing, and no Hermitian shortcut, since a generic eigen is 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 BigFloat and Complex{BigFloat} agree with the Float64 path to within about 1e-15. Quaternion and block matrices agree with the corresponding matrices of numbers to within about 4e-15.

    Tests


    A new testset in test/dense.jl covers:
  • exp for BigFloat, Complex{BigFloat} and Rational{BigInt};

  • cis, sin and cos;

  • the zero matrix, the diagonal shortcut and the ArgumentError for NaNs;

  • accuracy to 512-bit precision;

  • SizedArray and ImmutableArray containers;

  • quaternion matrices, against their complex 2×2 representation;

  • block matrices with SizedArray blocks of Float64 and BigFloat, against the flattened matrix;

  • the diagonal shortcut for a block matrix with SizedArray blocks, which gives exactly the exponentials of the blocks, and for quaternions without an exp method, which take the Padé path.
  • test/dense.jl, generic.jl, symmetric.jl, triangular.jl, diagonal.jl, unitful.jl and ambiguous_exec.jl pass.

    Matrix functions that need eigen or schur (exp(::Hermitian{BigFloat}), log, sqrt, non-integer powers) still throw for BigFloat, as there is no generic eigen!/schur!. That is unchanged by this PR. I did not add a HISTORY.md entry, 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_

    Diff: 574 lines (+430 / -144) across 7 files Reviewer: mikmoore Review duration: 31080m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%