I am trying to figure out the best way to overwrite/update a Cholesky factorization. In short, I have a symmetric matrix K
which is periodically updated with new data. Every time I update K
I want to recompute its Cholesky factorization (call it C
). So what I want is a method like:
cholesky!(C::Cholesky, K::Matrix)
But as far as I can tell this doesn’t exist? I do see there is a cholesky!(F::Factor, A::Matrix)
, but I’m not sure if or how to use that.
I think the snippet below works? But I’m not sure and it feels a bit dangerous… Is there a better alternative?
K = # recompute symmetric matrix K
copyto!(C.U.data, K)
cholesky!(C.U.data)
The function below gives a slightly more expanded sketch of what I’m using this for, in case that’s helpful…
"""
update_kernel!(K, C, X)
Overwrite `K` with kernel matrix computed between
columns of `X`. Also compute the Cholesky factorization
of `K` and put the result into `C`.
"""
function update_kernel!(K::Matrix, C::Cholesky, X::Matrix)
# Compute kernel matrix, overwrite K.
d, n = size(X)
for (i, j) in IterTools.product(1:n, 1:n)
K[i, j] = exp(-sum((X[:, i] .- X[:, j]).^2)) # RBF kernel, for example...
end
# Compute Cholesky factorization of K, overwrite C.
cholesky!(C, K) # <--- how to do this?
end