Skip to content

Commit acefbd3

Browse files
authored
Force inv(::Cholesky) to be symmetric (#1258)
Fixes #1257
1 parent e7c5e01 commit acefbd3

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/cholesky.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ end
7070
LinearAlgebra.hermitian_type(::Type{SA}) where {T, S, SA<:SArray{S,T}} = Hermitian{T,SA}
7171

7272
function inv(A::Cholesky{T,<:StaticMatrix{N,N,T}}) where {N,T}
73-
return A.U \ (A.U' \ SDiagonal{N}(I))
73+
B = A.U \ (A.U' \ SDiagonal{N}(I))
74+
return (B .+ B') ./ T(2)
7475
end
7576

7677
function Base.:\(A::Cholesky{T,<:StaticMatrix{N,N,T}}, B::StaticVecOrMatLike) where {N,T}

test/chol.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ using LinearAlgebra: PosDefException
5757
c = cholesky(m)
5858
@test (@inferred inv(c)) isa SMatrix{3,3,elty}
5959
@test inv(c) SMatrix{3,3}(inv(m_a))
60+
# Check special case of 2 × 2 inverse that used to trigger an
61+
# error
62+
c2 = cholesky(@SMatrix elty[π 3.1; 3.1 12.0])
63+
d2 = inv(c2)
64+
@test d2[1,2] == d2[2,1]
6065
end
6166

6267
@testset "Division" begin

0 commit comments

Comments
 (0)