Skip to content

Commit

Permalink
use symmetrize and symmetrize! where possible – and inplace of Y wher…
Browse files Browse the repository at this point in the history
…e possible.
  • Loading branch information
kellertuer committed Aug 29, 2023
1 parent 87d005b commit 77a3eac
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/manifolds/Rotations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ to map it into the Lie algebra.
"""
riemannian_Hessian(M::Rotations, p, G, H, X)
function riemannian_Hessian!(::Rotations{N}, Y, p, G, H, X) where {N}
project!(SkewSymmetricMatrices(N), Y, p' * H - 1 / 2 .* X * (G' * p + p' * G))
symmetrize!(Y, G' * p)
project!(SkewSymmetricMatrices(N), Y, p' * H - X * Y)
return Y

Check warning on line 402 in src/manifolds/Rotations.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/Rotations.jl#L399-L402

Added lines #L399 - L402 were not covered by tests
end

Expand All @@ -418,7 +419,8 @@ The formula is due to [AbsilMahonyTrumpf:2013](@cite) given by
Weingarten(::Rotations, p, X, V)

function Weingarten!(::Rotations, Y, p, X, V)
Y .= -1 / 2 * p * (V' * X - X' * V)
symmetrize!(Y, V' * X)
Y .= -p * Y
return Y

Check warning on line 424 in src/manifolds/Rotations.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/Rotations.jl#L421-L424

Added lines #L421 - L424 were not covered by tests
end

Expand Down
10 changes: 3 additions & 7 deletions src/manifolds/StiefelCanonicalMetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,8 @@ function riemannian_Hessian!(
H,
X,
) where {n,k,𝔽}
project!(
M,
Y,
p,
H - 1 / 2 .* X * (G' * p + p' * G) -
1 / 2 * ((I - p * p') * G * p' + p * G' * (I - p * p')) * X,
)
Gp = symmetrize(G' * p)
Z = symmetrize((I - p * p') * G * p')
project!(M, Y, p, H - X * Gp - Z * X)
return Y

Check warning on line 220 in src/manifolds/StiefelCanonicalMetric.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/StiefelCanonicalMetric.jl#L217-L220

Added lines #L217 - L220 were not covered by tests
end
6 changes: 4 additions & 2 deletions src/manifolds/StiefelEuclideanMetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ Compared to Eq. (5.6) also the metric conversion simplifies to the identity.
riemannian_Hessian(M::Stiefel, p, G, H, X)

function riemannian_Hessian!(M::Stiefel, Y, p, G, H, X)
project!(M, Y, p, H - 1 / 2 .* X * (G' * p + p' * G))
Z = symmetrize(G' * p)
project!(M, Y, p, H - X * Z)
return Y

Check warning on line 214 in src/manifolds/StiefelEuclideanMetric.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/StiefelEuclideanMetric.jl#L211-L214

Added lines #L211 - L214 were not covered by tests
end

Expand All @@ -232,6 +233,7 @@ The formula is due to [AbsilMahonyTrumpf:2013](@cite) given by
Weingarten(::Stiefel, p, X, V)

function Weingarten!(::Stiefel, Y, p, X, V)
Y .= -X * p' * V - p * (X' * V + V' * X)
Z = symmetrize(X' * V)
Y .= -X * p' * V - p * Z
return Y

Check warning on line 238 in src/manifolds/StiefelEuclideanMetric.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/StiefelEuclideanMetric.jl#L235-L238

Added lines #L235 - L238 were not covered by tests
end
8 changes: 4 additions & 4 deletions src/manifolds/SymmetricPositiveDefiniteAffineInvariant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,10 @@ the symmetrization operator. Then the formula reads
riemannian_Hessian(M::SymmetricPositiveDefinite, p, G, H, X)

function riemannian_Hessian!(::SymmetricPositiveDefinite, Y, p, G, H, X)
# The following formulae even work for complex, H-symmetric positive definite matrices.
# the term within the seconds summands outer \sym:
Y .= X * 0.5 * (G' + G) * p
Y .= p * 1 / 2 * (H' + H) * p + 1 / 2 * (Y' + Y)
symmetrize!(Y, G)
symmetrize!(Y, X * Y * p)
Z = symmetrize(H)
Y .= p * Z * p + Y
return Y

Check warning on line 438 in src/manifolds/SymmetricPositiveDefiniteAffineInvariant.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/SymmetricPositiveDefiniteAffineInvariant.jl#L433-L438

Added lines #L433 - L438 were not covered by tests
end

Expand Down
6 changes: 4 additions & 2 deletions src/manifolds/Unitary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ to map it into the Lie algebra.
"""
riemannian_Hessian(M::UnitaryMatrices, p, G, H, X)
function riemannian_Hessian!(M::UnitaryMatrices, Y, p, G, H, X)
project!(M, Y, p, p' * H - 1 / 2 .* X * (G' * p + p' * G))
symmetrize!(Y, G' * p)
project!(M, Y, p, p' * H - X * Y)
return Y

Check warning on line 133 in src/manifolds/Unitary.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/Unitary.jl#L130-L133

Added lines #L130 - L133 were not covered by tests
end

Expand All @@ -147,7 +148,8 @@ The formula is due to [AbsilMahonyTrumpf:2013](@cite) given by
Weingarten(::UnitaryMatrices, p, X, V)

function Weingarten!(::UnitaryMatrices, Y, p, X, V)
Y .= -1 / 2 * p * (V' * X - X' * V)
symmetrize!(Y, X' * V)
Y .= -p * Y
return Y

Check warning on line 153 in src/manifolds/Unitary.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/Unitary.jl#L150-L153

Added lines #L150 - L153 were not covered by tests
end

Expand Down
19 changes: 19 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ For example `select_from_tuple(("a", "b", "c"), Val((3, 1, 1)))` returns
return Expr(:tuple, [Expr(:ref, :t, k) for k in P]...)
end

@doc raw"""
symmetrize!(Y,X)
Given a quare matrix `X` compute `1/2 .* (X' + X)` in place of `Y`
"""
function symmetrize!(Y, X)
Y .= 1 / 2 .* (X' .+ X)
return Y

Check warning on line 231 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L229-L231

Added lines #L229 - L231 were not covered by tests
end

@doc raw"""
symmetrize(X)
Given a quare matrix `X` compute `1/2 .* (X' + X)`.
"""
function symmetrize(X)
return 1 / 2 .* (X' .+ X)

Check warning on line 240 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L239-L240

Added lines #L239 - L240 were not covered by tests
end

@doc raw"""
vec2skew!(X, v, k)
Expand Down

0 comments on commit 77a3eac

Please sign in to comment.