Skip to content

Commit 58bf1e1

Browse files
Compute inverse of Cholesky with a single solve (#1326)
Alternative to #1258 that instead of averaging out the errors of the two solves computes the inverse with a single solve and a symmetric matrix multiplication. Checks of symmetry in the tests pass locally, and the example in the tests is even slightly faster: ## This PR ```julia julia> using StaticArrays, LinearAlgebra, Chairmarks julia> @be cholesky((x -> x * x' + I)(@smatrix(randn(3,3)))) inv Benchmark: 3702 samples with 4741 evaluations min 4.561 ns median 5.018 ns mean 5.419 ns max 11.680 ns ``` ## master ```julia julia> using StaticArrays, LinearAlgebra, Chairmarks julia> @be cholesky((x -> x * x' + I)(@smatrix(randn(3,3)))) inv Benchmark: 3756 samples with 3276 evaluations min 6.983 ns median 7.326 ns mean 7.712 ns max 53.406 ns ``` --------- Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent 2060415 commit 58bf1e1

2 files changed

Lines changed: 4 additions & 3 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 = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.9.15"
3+
version = "1.9.16"
44

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

src/cholesky.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ 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-
B = A.U \ (A.U' \ SDiagonal{N}(I))
74-
return (B .+ B') ./ T(2)
73+
# We can't use M = inv(A.U) here since it returns an `UpperTriangular` of a `Matrix`
74+
M = UpperTriangular(A.U \ SDiagonal{N}(I))
75+
return M * M'
7576
end
7677

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

0 commit comments

Comments
 (0)