ForwardDiff and Array of Arrays

maybe using a vector instead of a vector of vectors and reshaping?

function myf(x)
    xvec=reshape(x,3,3)
    return xvec[:,1]⋅cross(xvec[:,2],xvec[:,3]) 
 end

 ∇myf = X->ForwardDiff.gradient(myf,X)
 x0 = randn(9)
 dx = reshape(∇myf(x0),3,3) #here, dx[:,1] ==  X[1]

if you really (emphasis on really) need an array of arrays, just add this code and ∇f should work with your input, returning an array of arrays of the same form as the input.

function _f(x)
    xvec=reshape(x,3,3)
    return xvec[:,1]⋅cross(xvec[:,2],xvec[:,3]) 
 end
function ∇f(X)
    X2 = reduce((x,y)->cat(x,y,dims=1),X)
    ∇X = ForwardDiff.gradient(myf,X2)
    return [∇X[:,j] for j=1:3]
end

why is this necessary? ForwardDiff accepts functions with only 2 types of input: a real number, and a vector of real numbers. any other structure must be transformed to any of those cases to be differentiated via ForwardDiff.

1 Like