The code’s logic is to have a high-level function where this array is pre-allocated, and then will be re-used in place many times by some internal functions, a bit like the example below,
function update_matrix!(m)
x = [1.2, 3.4]
m[1,1] = exp(1im*x[1]) + sin(x[2])
m[2,1] = exp(1im*x[2]) + sin(x[2])
m[1,2] = exp(1im*x[1]) + sin(x[1])
m[2,2] = exp(1im*x[2]) + sin(x[1])
end
function high_level(a::Int, x::Real)
m = Matrix{Complex{Real}}(undef, 2,2)
for i in 1:a
update_matrix!(m)
end
return(real(m[1,1])*x)
end
high_level(2,1.5)
So in a sense the high-level function has little idea of what the type of m
should be, as it’s left to the update_matrix
function to deal with it.
I could obviously refactor the code and have everything in a monolithic function, but it’s much nicer with the current structure (and I’ve seen a few viable options already in this thread to pre-allocate m
– in fact I think this example is just fine now, with m = Matrix{Complex{typeof(x)}}(undef, 2,2)
and I probably don’t need something more general than that).