Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesCore"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.26.5"
version = "1.26.6"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
10 changes: 9 additions & 1 deletion src/projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ end
(project::ProjectTo{Diagonal})(dx::Diagonal) = Diagonal(project.diag(dx.diag))

# Symmetric
function _same_uplo(dx::LinearAlgebra.HermOrSym, uplo::Symbol)
return LinearAlgebra.sym_uplo(dx.uplo) === uplo
end
_same_uplo(dx, uplo::Symbol) = true
for (SymHerm, chk, fun) in
((:Symmetric, :issymmetric, :transpose), (:Hermitian, :ishermitian, :adjoint))
@eval begin
Expand All @@ -465,7 +469,11 @@ for (SymHerm, chk, fun) in
dy = project.parent(dx)
# Here $chk means this is efficient on same-type.
# If we could mutate dx, then that could speed up action on dx::Matrix.
dz = $chk(dy) ? dy : (dy .+ $fun(dy)) ./ 2
# Separate returns avoid a type-unstable `dz`, which allocates on Julia 1.10.
if $chk(dy) && _same_uplo(dy, project.uplo)
return $SymHerm(project.parent(dy), project.uplo)
end
dz = (dy .+ $fun(dy)) ./ 2
return $SymHerm(project.parent(dz), project.uplo)
end
# This is an example of a subspace which is not a subtype,
Expand Down
21 changes: 21 additions & 0 deletions test/projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ struct NoSuperType end
@test pherm(pherm(reshape(1:9, 3, 3))) == pherm(reshape(1:9, 3, 3))
@test pherm(rand(ComplexF32, 3, 3, 1)) isa Hermitian{ComplexF64}

@testset "input with other uplo" begin
for dx in (Symmetric(rand(3, 3), :L), Hermitian(rand(3, 3), :L))
@test @inferred(psymm(dx)) == dx
@test psymm(dx).uplo == 'U'
end
dx = Hermitian(rand(ComplexF64, 3, 3), :U)
@test @inferred(pherm(dx)) == dx
@test pherm(dx).uplo == 'L'
dx = Symmetric(rand(ComplexF64, 3, 3), :L)
@test @inferred(ProjectTo(Symmetric(rand(ComplexF64, 3, 3)))(dx)) == dx
end

@testset "plain matrix input that is already symmetric/hermitian" begin
dx = [1.0 2.0 3.0; 2.0 4.0 5.0; 3.0 5.0 6.0]
@test @inferred(psymm(dx)) == dx
@test psymm(dx) isa Symmetric{Float64}
dx = [1.0 2.0+im 3.0; 2.0-im 4.0 5.0im; 3.0 -5.0im 6.0]
@test @inferred(pherm(dx)) == dx
@test pherm(dx) isa Hermitian{ComplexF64}
end

pupp = ProjectTo(UpperTriangular(rand(3, 3)))
@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]
@test pupp(pupp(reshape(1:9, 3, 3))) == pupp(reshape(1:9, 3, 3))
Expand Down
Loading