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

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

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

Now, inside my function, if I do

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.

Does switching from struct to mutable struct help?

You may be looking for something like

copy!(m.Vchol, m.V)
cholesky!(m.Vchol, Val(true))
1 Like

Won’t that result in repeated allocations?

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.