Skip to content

Commit 4e9c8aa

Browse files
devmotionclaude
andauthored
Make the diagonal of Diagonal tangents projected onto Hermitian real (#723)
* Make the diagonal of Diagonal tangents projected onto Hermitian real 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> * Skip the real part for real Diagonal tangents onto Hermitian 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> * Rename complex Hermitian projector in allocation test and trim comment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2a1e65e commit 4e9c8aa

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRulesCore"
22
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3-
version = "1.26.6"
3+
version = "1.26.7"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

‎src/projection.jl‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,20 @@ for (SymHerm, chk, fun) in
483483
sub_one = ProjectTo{project_type(sub)}(;
484484
element=sub.element, axes=(sub.axes[1],)
485485
)
486-
return Diagonal(sub_one(dx.diag))
486+
return Diagonal(_project_diag(project, sub_one, dx.diag))
487487
end
488488
end
489489
end
490+
_project_diag(::ProjectTo{Symmetric}, sub_one, diag) = sub_one(diag)
491+
function _project_diag(::ProjectTo{Hermitian}, sub_one, diag)
492+
dy = sub_one(diag)
493+
# The diagonal of a Hermitian matrix is real
494+
if eltype(diag) <: Real || eltype(dy) <: Real
495+
return dy
496+
else
497+
return map(y -> oftype(y, real(y)), dy)
498+
end
499+
end
490500

491501
# Triangular
492502
for UL in (:UpperTriangular, :LowerTriangular, :UnitUpperTriangular, :UnitLowerTriangular) # UpperHessenberg

‎test/projection.jl‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,23 @@ struct NoSuperType end
314314
# an experiment with allowing subspaces which aren't subtypes
315315
@test psymm(Diagonal([1, 2, 3])) isa Diagonal{Float64}
316316
@test pupp(Diagonal([1, 2, 3 + 4im])) isa Diagonal{Float64}
317+
318+
@testset "Diagonal input to Hermitian has real diagonal" begin
319+
dx = Diagonal([1 + 2im, 3im, 4.0])
320+
@test @inferred(pherm(dx)) == Diagonal([1.0, 0.0, 4.0])
321+
@test pherm(dx) isa Diagonal{ComplexF64}
322+
@test pherm(dx) == pherm(Matrix(dx))
323+
@test @inferred(pherm(Diagonal([1.0, 2.0, 3.0]))) == Diagonal([1.0, 2.0, 3.0])
324+
@test pherm(Diagonal([1.0, 2.0, 3.0])) isa Diagonal{ComplexF64}
325+
pherm32 = ProjectTo(Hermitian(rand(ComplexF32, 3, 3)))
326+
@test @inferred(pherm32(dx)) isa Diagonal{ComplexF32}
327+
@test @inferred(ProjectTo(Hermitian(rand(3, 3)))(dx)) ==
328+
Diagonal([1.0, 0.0, 4.0])
329+
@test_throws DimensionMismatch pherm(Diagonal(rand(ComplexF64, 2)))
330+
331+
# Symmetric keeps a complex diagonal
332+
@test @inferred(ProjectTo(Symmetric(rand(ComplexF64, 3, 3)))(dx)) == dx
333+
end
317334
end
318335

319336
@testset "LinearAlgebra: sparse structured matrices" begin
@@ -499,6 +516,14 @@ struct NoSuperType end
499516

500517
psymm = ProjectTo(Symmetric(rand(10^3, 10^3)))
501518
@test 0 == @ballocated $psymm(dx) setup = (dx = Symmetric(rand(10^3, 10^3)))
519+
520+
pherm = ProjectTo(Hermitian(rand(10^3, 10^3)))
521+
@test 0 == @ballocated $pherm(dx) setup = (dx = Diagonal(rand(10^3)))
522+
523+
# a real tangent onto complex Hermitian is copied only once
524+
pherm_c = ProjectTo(Hermitian(rand(ComplexF64, 10^3, 10^3)))
525+
@test 2 * sizeof(ComplexF64) * 10^3 >
526+
@ballocated $pherm_c(dx) setup = (dx = Diagonal(rand(10^3)))
502527
end
503528

504529
@testset "#407" begin

0 commit comments

Comments
 (0)