Repository navigation
Make the diagonal of Diagonal tangents projected onto Hermitian real - #723
Merged
Merged
Conversation
ProjectTo{Hermitian} kept the imaginary part of a Diagonal tangent, so
its result was not Hermitian and disagreed with the dense path. Take the
real part with oftype, like LinearAlgebra.hermitian(::Number), so the
element type is kept also for e.g. quaternions and Unitful quantities.
Fixes #722.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #723 +/- ##
==========================================
+ Coverage 93.50% 93.54% +0.03%
==========================================
Files 15 15
Lines 1016 1022 +6
==========================================
+ Hits 950 956 +6
Misses 66 66 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A real input diagonal is already real after projection, so this avoids a second copy. Also check the value for a real tangent, that Symmetric keeps a complex diagonal, and the allocations for a real tangent onto a complex Hermitian. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
added a commit
to JuliaDiff/ChainRules.jl
that referenced
this pull request
Oct 28, 2026
ChainRulesCore 1.26.7 (JuliaDiff/ChainRulesCore.jl#723) makes the projection of a Diagonal tangent onto Hermitian real. Without it, the Matrix(::Hermitian) rrule would keep the imaginary part of a Diagonal cotangent, which the old hand-written rule dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
added a commit
to JuliaDiff/ChainRules.jl
that referenced
this pull request
Oct 29, 2026
* Use natural tangents in Matrix(::HermOrSym) rules The frule and rrule for `Matrix`/`Array` of a `Symmetric`/`Hermitian` now project with `ProjectTo(A)`, giving the natural tangent `(Δ + Δ')/2` instead of the structural one that double-counted off-diagonals. This matches FiniteDifferences 0.12.35, whose `to_vec(::Symmetric/Hermitian)` now yields natural tangents. Re-enables the `prod` tests on `Symmetric` (#475). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Require ChainRulesCore 1.26.7 and test Diagonal cotangents ChainRulesCore 1.26.7 (JuliaDiff/ChainRulesCore.jl#723) makes the projection of a Diagonal tangent onto Hermitian real. Without it, the Matrix(::Hermitian) rrule would keep the imaginary part of a Diagonal cotangent, which the old hand-written rule dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Rename result of Diagonal cotangent test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
ProjectTo{Hermitian}kept the imaginary part of aDiagonaltangent, so the result was not Hermitian and differed from projecting the same tangent as a denseMatrix(#722).The
Diagonalmethod now takes the real part of the projected diagonal withoftype(y, real(y)), the same asLinearAlgebra.hermitian(::Number). Usingoftypeinstead ofcomplexkeeps the element type for any number type. I checked locally that quaternions (Quaternions.jl) and complex Unitful quantities give the same result as the dense path; they are not test dependencies, so that check is not in the test suite.Hermitian{Float64}still projects aDiagonal{Float64}without allocating, and a real tangent onto a complexHermitianis copied only once (new@ballocatedtests).Symmetricmethod is unchanged; a new test checks that it keeps a complex diagonal.ComplexF64andComplexF32, a real tangent for a complexHermitian, a realHermitianwith a complex tangent, agreement with the dense path, and the size check.Found while reviewing JuliaDiff/ChainRules.jl#856, which switched the
Matrix(::Hermitian)rules toProjectTo.Fixes #722.
🤖 Generated with Claude Code