# Is it possible to have a field CholeskyPivoted and update it?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-have-a-field-choleskypivoted-and-update-it/34946>\
**Category:** General Usage\
**Created:** [February 20, 2020, 11:23pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-field-choleskypivoted-and-update-it/34946 "2020-02-20T23:23:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [February 20, 2020, 11:23pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-field-choleskypivoted-and-update-it/34946/1 "2020-02-20T23:23:49Z")

</div>

My function performs cholesky decomposition on a matrix `V` many many times, so I want to create a field `Vchol` in my model struct `m` to avoid repeated allocation. So my type looks like

```julia
struct m{T <:Real}
    ...
    V::Matrix{T}
    Vchol::CholeskyPivoted{T}
    ...
end

```

Now, inside my function, if I do

```julia
function f(m:m{T})
    m.Vchol = cholesky(m.V)
end

```

then I would get the error `immutable struct of type m cannot be changed`. Sure.  
So my question is, is there any workaround? Thank you.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [February 21, 2020, 12:06am UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-field-choleskypivoted-and-update-it/34946/2 "2020-02-21T00:06:54Z")

</div>

Does switching from `struct` to `mutable struct` help?

---

<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:** [February 21, 2020, 6:43am UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-field-choleskypivoted-and-update-it/34946/3 "2020-02-21T06:43:00Z")

</div>

You may be looking for something like

```julia
copy!(m.Vchol, m.V)
cholesky!(m.Vchol, Val(true))

```

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [February 21, 2020, 4:41pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-field-choleskypivoted-and-update-it/34946/4 "2020-02-21T16:41:15Z")

</div>

> [@Tamas\_Papp](#):
>
> copy!(m.Vchol, m.V)

Won’t that result in repeated allocations?

---

<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:** [February 22, 2020, 6:07am UTC](https://discourse.julialang.org/t/is-it-possible-to-have-a-field-choleskypivoted-and-update-it/34946/5 "2020-02-22T06:07:46Z")

</div>

For the permutated version, yes. Maybe a method could be added that deals with this.

An unpivoted `cholesky!` just wraps the matrix with some information though in a `struct`, so if that does not escape the caller then it could be allocation-free.
