Skip to content

Add a fast method for diag of Cholesky matrices #53767

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

Merged
merged 12 commits into from
May 20, 2024
13 changes: 13 additions & 0 deletions stdlib/LinearAlgebra/src/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,16 @@ then `CC = cholesky(C.U'C.U - v*v')` but the computation of `CC` only uses
`O(n^2)` operations.
"""
lowrankdowndate(C::Cholesky, v::AbstractVector) = lowrankdowndate!(copy(C), copy(v))

function diag(C::Cholesky{T}) where {T}
N = size(C, 1)
z = Vector{T}(undef, N)
U = C.U
for i=1:N
z[i] = zero(T)
for j=1:i
z[i] += U[j, i]^2
end
end
return z
end
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/test/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,11 @@ end
@test logabsdet(B)[1] == -Inf
end

@testset "diag" begin
A = randn(100, 100)
P = A' * A
C = cholesky(P)
@test diag(P) ≈ diag(C)
end

end # module TestCholesky