Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-symmetric inverse of Cholesky decomposition #1257

Open
astrozot opened this issue May 27, 2024 · 0 comments
Open

Non-symmetric inverse of Cholesky decomposition #1257

astrozot opened this issue May 27, 2024 · 0 comments

Comments

@astrozot
Copy link

Because of rounding errors, in some cases explicitly symmetric matrices might produce non-symmetric inverses:

using StaticArrays, LinearAlgebra

c = cholesky(@SMatrix3.1; 3.1 12.0])
d = inv(c)
d[1,2] - d[2,1] # returns -1.3877787807814457e-17

This can be a serious issue and it can break various codes: for example, cholesky(d) would fail. Note that the same code works without problems if one uses standard arrays instead of static ones:

c1 = cholesky([π 3.1; 3.1 12.0])
d1 = inv(c1)
d1[1,2] - d1[2,1] # returns 0.0

A fix is to force, in the definition of inv(::Cholesky)

function inv(A::Cholesky{T,<:StaticMatrix{N,N,T}}) where {N,T}
return A.U \ (A.U' \ SDiagonal{N}(I))
end

the symmetry of the result:

function inv(A::Cholesky{T,<:StaticMatrix{N,N,T}}) where {N,T}
    B = A.U \ (A.U' \ SDiagonal{N}(I))
    return (B .+ B') ./ T(2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant