Vector of vector of gradients

I am to extend the scalar case of a gradient of a function, i.e.,

foo = Vector{Function}(undef,2)
gnu = Vector{Function}(undef,2)
foo[1] = (x,y) -> x+y 
foo[2] = (x,y) -> x-y 
A = [1. 2.; 3. 4.]
gnu = (x,y) -> B * [foo[1](x,y),foo[2](x,y)]

to the case of a vector of gradient of functions

ndof = 3 
foo = Vector{Vector}(undef,ndof)
gnu = Vector{Vector}(undef,ndof)
for i=1:ndof 
    foo[i] = Vector{Function}(undef,2)
    gnu[i] = Vector{Function}(undef,2)
end 
foo[1][1] = (x,y) -> x+y
foo[1][2] = (x,y) -> x-y
foo[2][1] = (x,y) -> 2*x+2*y  
foo[2][2] = (x,y) -> 2*x-2*y
foo[3][1] = (x,y) -> 3*x+3*y  
foo[3][2] = (x,y) -> 3*x-3*y 
A = [1. 2.; 3. 4.]
for i=1:ndof 
    gnu[i][:] .= (x,y) -> A * [foo[i][1](x,y);foo[i][2](x,y)]
end 

Help is appreciated.

Is there something that doesn’t work with your code? Why do you need help?

As a side note, a Vector{Function} has an abstract element type and so it might be rather slow. Depending on your specific problem, a more appropriate structure can speed things up (for instance if all your functions are linear combinations you can store the coefficients instead of the anonymous function objects)

@gdalle : thx!

The code works indeed. I was testing incorrectly.

I will worry about performance later.

1 Like