How to preallocate a triangular matrix and overwrite it with cholesky!

LinearAlgebra.inv! should be fine

julia> LinearAlgebra.inv!(cholesky(Symmetric(A)))
4×4 Matrix{Float64}:
  0.334048   -0.0773346  -0.0323587  -0.0599437
 -0.0773346   0.211738    0.0864574   0.0753268
 -0.0323587   0.0864574   0.14029     0.125532
 -0.0599437   0.0753268   0.125532    0.467126

julia> inv(A)
4×4 Matrix{Float64}:
  0.334048   -0.0773346  -0.0323587  -0.0599437
 -0.0773346   0.211738    0.0864574   0.0753268
 -0.0323587   0.0864574   0.14029     0.125532
 -0.0599437   0.0753268   0.125532    0.467126

Also:

invR_nk + Diagonal(repeat([1 / deltasq_pick], nk_list[k]))

this could be
invR_nk + (1 / deltasq_pick) * I
to avoid allocating the two vectors from [1 / deltasq_pick] and repeat.

You could also replace all the vcat and hcats with an initial allocation of an array of the final size, and copy data directly into it.

1 Like