# Overwrite Cholesky factorization

**URL:** https://discourse.julialang.org/t/overwrite-cholesky-factorization/50210
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [November 16, 2020, 5:43am UTC](https://discourse.julialang.org/t/overwrite-cholesky-factorization/50210 "2020-11-16T05:43:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ahwillia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahwillia/32/105_2.png) [@ahwillia](https://discourse.julialang.org/u/ahwillia)
#### Post date: [November 16, 2020, 5:43am UTC](https://discourse.julialang.org/t/overwrite-cholesky-factorization/50210/1 "2020-11-16T05:43:01Z")

</div>

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?

```julia
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…

```julia
"""
    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

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 16, 2020, 9:08am UTC](https://discourse.julialang.org/t/overwrite-cholesky-factorization/50210/2 "2020-11-16T09:08:04Z")

</div>

AFAIK generally, there is no algorithm that is better than simply refactoring, other than rank-1 updates.

> <https://scicomp.stackexchange.com/questions/10630/full-rank-update-to-cholesky-decomposition>

> **[Cholesky decomposition | Updating the decomposition](https://en.wikipedia.org/wiki/Cholesky_decomposition#Updating_the_decomposition)**
>
> A task that often arises in practice is that one needs to update a Cholesky decomposition. In more details, one has already computed the Cholesky decomposition 
>   
>     
>       
>         
> A
>         
> =
>         
> L
>         
>         
>           
> L
>           
>           
> ∗
>           
>         
>       
>     
> {\\displaystyle \\mathbf {A} =\\mathbf {L} \\mathbf {L} ^{\*}}
>   
> of some matrix 
>   
>     
>       
>         
> A
>         
>       
>     
> {\\displays...

---

<div class="post-metadata">

### Author: ![ahwillia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahwillia/32/105_2.png) [@ahwillia](https://discourse.julialang.org/u/ahwillia)
#### Post date: [November 16, 2020, 6:02pm UTC](https://discourse.julialang.org/t/overwrite-cholesky-factorization/50210/3 "2020-11-16T18:02:09Z")

</div>

Sorry, I just meant to ask how to best avoid allocating new memory while doing this. I guess the answer might be that the memory allocation is a small cost compared with computing the factorization itself…

---

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [November 16, 2020, 7:19pm UTC](https://discourse.julialang.org/t/overwrite-cholesky-factorization/50210/4 "2020-11-16T19:19:25Z")

</div>

I think it would be

```julia
cholesky!(Symmetric(copyto!(C.factors, K), :U))

```

The matrix that actually stores the data for the Cholesky object is the `factors` field, either in the upper triangle or lower triangle according to the `uplo` field. If you copy the contents of `K` to `C.factors` and then force it to be considered as `Symmetric` stored in the upper triangle (or lower, if that works better for you) then the `cholesky!` method will decompose it in place.
