Skip to content

Commit 2a1e65e

Browse files
devmotionclaudegithub-actions[bot]
authored
Handle Symmetric/Hermitian tangents with other uplo in ProjectTo (#721)
* Handle Symmetric/Hermitian tangents with other uplo in ProjectTo Projecting e.g. `Symmetric(B, :L)` with the projector of a `Symmetric(A, :U)` threw `ArgumentError: Cannot construct Symmetric; uplo doesn't match`, since the already-symmetric input was rewrapped with the primal's uplo. Such inputs now go through the symmetrizing fallback, so the result is stored with the primal's uplo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix format Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Test ProjectTo{Symmetric/Hermitian} on already-symmetric plain matrices Covers the generic `_same_uplo` fallback, which is only reached when the input is a plain array that already passes issymmetric/ishermitian. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 0d375e8 commit 2a1e65e

3 files changed

Lines changed: 31 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.5"
3+
version = "1.26.6"
44

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

‎src/projection.jl‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ end
452452
(project::ProjectTo{Diagonal})(dx::Diagonal) = Diagonal(project.diag(dx.diag))
453453

454454
# Symmetric
455+
function _same_uplo(dx::LinearAlgebra.HermOrSym, uplo::Symbol)
456+
return LinearAlgebra.sym_uplo(dx.uplo) === uplo
457+
end
458+
_same_uplo(dx, uplo::Symbol) = true
455459
for (SymHerm, chk, fun) in
456460
((:Symmetric, :issymmetric, :transpose), (:Hermitian, :ishermitian, :adjoint))
457461
@eval begin
@@ -465,7 +469,11 @@ for (SymHerm, chk, fun) in
465469
dy = project.parent(dx)
466470
# Here $chk means this is efficient on same-type.
467471
# If we could mutate dx, then that could speed up action on dx::Matrix.
468-
dz = $chk(dy) ? dy : (dy .+ $fun(dy)) ./ 2
472+
# Separate returns avoid a type-unstable `dz`, which allocates on Julia 1.10.
473+
if $chk(dy) && _same_uplo(dy, project.uplo)
474+
return $SymHerm(project.parent(dy), project.uplo)
475+
end
476+
dz = (dy .+ $fun(dy)) ./ 2
469477
return $SymHerm(project.parent(dz), project.uplo)
470478
end
471479
# This is an example of a subspace which is not a subtype,

‎test/projection.jl‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,27 @@ struct NoSuperType end
284284
@test pherm(pherm(reshape(1:9, 3, 3))) == pherm(reshape(1:9, 3, 3))
285285
@test pherm(rand(ComplexF32, 3, 3, 1)) isa Hermitian{ComplexF64}
286286

287+
@testset "input with other uplo" begin
288+
for dx in (Symmetric(rand(3, 3), :L), Hermitian(rand(3, 3), :L))
289+
@test @inferred(psymm(dx)) == dx
290+
@test psymm(dx).uplo == 'U'
291+
end
292+
dx = Hermitian(rand(ComplexF64, 3, 3), :U)
293+
@test @inferred(pherm(dx)) == dx
294+
@test pherm(dx).uplo == 'L'
295+
dx = Symmetric(rand(ComplexF64, 3, 3), :L)
296+
@test @inferred(ProjectTo(Symmetric(rand(ComplexF64, 3, 3)))(dx)) == dx
297+
end
298+
299+
@testset "plain matrix input that is already symmetric/hermitian" begin
300+
dx = [1.0 2.0 3.0; 2.0 4.0 5.0; 3.0 5.0 6.0]
301+
@test @inferred(psymm(dx)) == dx
302+
@test psymm(dx) isa Symmetric{Float64}
303+
dx = [1.0 2.0+im 3.0; 2.0-im 4.0 5.0im; 3.0 -5.0im 6.0]
304+
@test @inferred(pherm(dx)) == dx
305+
@test pherm(dx) isa Hermitian{ComplexF64}
306+
end
307+
287308
pupp = ProjectTo(UpperTriangular(rand(3, 3)))
288309
@test pupp(reshape(1:9, 3, 3)) == [1.0 4.0 7.0; 0.0 5.0 8.0; 0.0 0.0 9.0]
289310
@test pupp(pupp(reshape(1:9, 3, 3))) == pupp(reshape(1:9, 3, 3))

0 commit comments

Comments
 (0)