One-time assignment means something essentially equal to this:
function Mf(t)
M=[t t^2;t^3 t^4] #assignment, just list all its entries in the bracket
end
but not this:
function Mf(t)
M=zeros(2,2) #assignment
M[1,1]=t #mutation
M[1,2]=t^2
M[2,1]=t^3
M[2,2]=t^4
M
end
When a large parametric matrix(e.g. M(t) is 1000x1000) is needed, I can’t assign it by the first way(list all 1000x1000 entries), but to use the second way
function Mf(t)
M=zeros(1000,1000) #assignment
for i=1:1000
for j=1:1000
M[i,j]=m_ij(t) #mutation, m_ij(t) is an explicit expression depends on i,j,t
end
end
M
end
However, if I need to compute gradient of a loss function of M(t) by Zygote, e.g. L(t)=det(M(t))
gradient(t->det(Mf(t)), t0)[1]
an error will occur since mutating array in differetiated function is not allowed, means that I shouldn’t adopt the second way
ERROR: LoadError: Mutating arrays is not supported -- called setindex!(::Matrix{Float64}, _...)
So I wonder if there is a method of assigning a large parametric matrix at one-time without mutating it (the explicit expression of m_ij(t) is known)
I failed to find the answer by searching and watching tutorials. I would be grateful if someone tells the solution.