← Back to Pull requests

Make the diagonal of Hermitian matrix functions and generic Bunch-Kaufman inverses exactly real #1994

Merged
dkarrasch merged 6 files (46 lines) on Dec 02, 2026 16:07 UTC
View on GitHub
dkarrasch dkarrasch commented on Nov 30, 2026 15:43 UTC
Author

For a Hermitian A, cos, sin, tan, their hyperbolic and inverse variants, acosh, log and sincos compute V Diagonal(f.(λ)) V', which is Hermitian only up to rounding. The Hermitian result hides that, but the dense methods copy the parent with copytri!, so their results are not Hermitian. The same holds for inv of a generic Hermitian Bunch-Kaufman factorization. These results are now made exactly Hermitian with hermitianpart!.

The generic inv of a Cholesky or CholeskyPivoted factorization (e.g. for Complex{BigFloat} or quaternions) fell back to solving for the identity, so neither its triangles nor its diagonal were exactly Hermitian. It is now made exactly Hermitian with hermitianpart! as well.

julia> A = ComplexF64[2 1+2im 3im; 1-2im -1 1+im; -3im 1-im 1];

julia> ishermitian(sin(A))  # master; true with this PR
false

julia> factorize(sin(A)) isa LU  # master; a BunchKaufman with this PR
true

julia> ishermitian(inv(cholesky(Hermitian(big.(A'A + I)))))  # master; true with this PR
false

This is not a regression (1.10 behaves the same). exp, sqrt and real powers already had an exactly real diagonal.

https://claude.ai/code/session_012douRorupKk8wH4zTpqpCx

---
_Generated by Claude Code_

Diff: 46 lines (+39 / -7) across 6 files Reviewer: stevengj Review duration: 2903m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.38) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.38 · 46 lines diff across 6 files
0.38
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.78 · 5 substantive comment(s)
0.78
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
84%
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.230
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
8 items
codecov[bot] codecov[bot] commented on Nov 30, 2026 16:06 UTC
Bot

Codecov Report


:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 99.58%. Comparing base (bc33f3c) to head (f0fd505).
:warning: Report is 10 commits behind head on master.

<details><summary>Additional details and impacted files</summary>

@@           Coverage Diff           @@
##           master    #1994   +/-   ##
=======================================
  Coverage   99.58%   99.58%           
=======================================
  Files          35       35           
  Lines       18699    18711   +12     
=======================================
+ Hits        18622    18634   +12     
  Misses         77       77           

</details>

:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>

  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

  • </details>

    stevengj stevengj commented on Nov 30, 2026 20:25 UTC
    Contributor

    Should we be calling hermitianpart! instead?

    dkarrasch dkarrasch commented on Dec 01, 2026 08:24 UTC
    Author

    That's extra effort, but probably negligible compared to the eigendecompositions.

    dkarrasch dkarrasch commented on Dec 01, 2026 10:17 UTC
    Author

    It reduced the code a little bit, so hermitianpart is included.

    stevengj stevengj commented on code on Dec 01, 2026 12:59 UTC
    Code Review
    src/symmetric.jl:1
    @@ -840,6 +840,10 @@ function _psd_spectral_product(v, U)
         return Uv * Uv' # often faster than generic matmul by calling BLAS.herk
     end
     
    +# a product V * D * V' is Hermitian only up to rounding; make its parent exactly Hermitian, which
    +# dense matrix functions copy
    +_spectralwrap(A, retmat) = wrappertype(A)(_hermitianpart!(retmat))

    _spectralwrap!(A, retmat) = wrappertype(A)(_hermitianpart!(retmat))
    

    since it mutates an argument?

    dkarrasch dkarrasch commented on code on Dec 01, 2026 18:12 UTC
    Code Review
    src/symmetric.jl:1
    @@ -840,6 +840,10 @@ function _psd_spectral_product(v, U)
         return Uv * Uv' # often faster than generic matmul by calling BLAS.herk
     end
     
    +# a product V * D * V' is Hermitian only up to rounding; make its parent exactly Hermitian, which
    +# dense matrix functions copy
    +_spectralwrap(A, retmat) = wrappertype(A)(_hermitianpart!(retmat))

    Good point.

    stevengj stevengj approved these changes on Dec 02, 2026 14:33 UTC
    Approved Reviewer

    Approved these changes.

    Reviewers
    Assignees
    dkarrasch dkarrasch
    Labels
    risk: low confidence: 84.4% 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: 38.4% (MEDIUM) · Higher = larger blast radius
    38.4%
    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: 84.4% · Lower = rushed or shallow review
    84.4%
    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: 6.0% · Safe to merge
    6.0%