Hi @DNF,
I took me a while to create a MWE, but I figured out the part of the code that was allocating:
using LinearAlgebra
function simulate()
k_max = 6000
I18 = Matrix{Float64}(I,18,18)
O3 = zeros(3,3)
P0 = copy(I18)
Q = 1e-20I18
R = diagm(0 => [3e-2, 3e-2, 3e-2, 5e7, 5e7, 5e7])
Fk_1 = I18
Hk = [ -I O3 -I I O3 O3;
-I O3 O3 O3 -I I; ]
Pu = copy(P0)
Pp = similar(P0)
K = zeros(18,6)
aux = zeros(18,18)
result = Vector{Float64}(undef,0)
push!(result, tr(Pu))
@inbounds for k = 2:k_max
Pp .= Fk_1*Pu*Fk_1' .+ Q
K .= Pp*Hk'*pinv(R .+ Hk*Pp*Hk')
aux .= I - K*Hk
Pu .= aux*Pp*aux' + K*R*K'
# Compute and store the result.
push!(result, tr(Pu))
end
result
end
which leads to:
julia> @btime simulate()
116.695 ms (246202 allocations: 242.91 MiB)
EDIT: At least in my attempts, using mul!
did not help to reduce the allocation here either.
P.S.: I cannot pre-allocate the vector result
because in the original code the loop can be interrupted if a condition impossible to simulate here is met.