Hi!

I need to remove the runtime allocations for a project that is targeting embedding Julia code in a satellite computer. One big problem I have is to create static matrices programmatically. For example, in C++, we can use Eigen and static matrices (which usually are allocated in the stack) and we can change its coefficients to whatever we want given that the type is conserved.

I am trying to replicate this behavior in Julia using StaticArrays.jl and Accessors.jl. However, the code leads to a huge number of allocations and it is considerable slower than just allocating the matrix initially.

Here is one example of an algorithm that I need to use static matrices:

```
function fully_normalized_legendre(
ϕ::T,
n_max::Integer = -1,
m_max::Integer = -1;
ph_term::Bool = false
) where T<:Number
n_max < 0 && throw(ArgumentError("n_max must be positive."))
if (m_max < 0) || (m_max > n_max)
m_max = n_max
end
P = zeros(float(T), n_max + 1, m_max + 1)
# Auxiliary variables to improve code performance.
s, c = sincos(T(ϕ))
# The sine must be always positive. In fact, `s` was previously computed using
# `√(1 - c^2)`. However, we had numerical problems for very small angles that lead to
# `cos(ϕ) = 1`.
if s < 0
s = -s
end
s_fact = !ph_term ? +s : -s
# Get the first indices in `P` to take into account offset arrays.
i₀, j₀ = first.(axes(P))
sq3 = √T(3)
@inbounds for n in 0:n_max
# Starting values.
if n == 0
P[i₀, j₀] = 1
continue
elseif n == 1
P[i₀+1, j₀] = +sq3 * c
if m_max > 0
P[i₀+1, j₀+1] = +sq3 * s_fact
end
continue
end
aux_an = T(2n - 1) * T(2n + 1)
aux_bn = T(2n + 1) / T(2n - 3)
for m in 0:n
if n == m
P[i₀+n, j₀+n] = s_fact * √(T(2n + 1) / T(2n)) * P[i₀+n-1, j₀+n-1]
else
aux_nm = T(n - m) * T(n + m)
a_nm = √(aux_an / aux_nm) * c
b_nm = √(T(n + m - 1) * T(n - m - 1) * aux_bn / aux_nm)
# We assume that the matrix is not initialized. Hence, we must not access
# elements on the upper triangle.
if m != n - 1
P[i₀+n, j₀+m] = a_nm * P[i₀+n-1, j₀+m] - b_nm * P[i₀+n-2, j₀+m]
else
P[i₀+n, j₀+m] = a_nm * P[i₀+n-1, j₀+m]
end
end
# Check if the maximum desired order has been reached.
m == m_max && break
end
end
return P
end
```

Can anyone help me?