how to automatically differentiate some function f(x1,x2,x3…) for vector of numbers. I want to use one package preferably , but if I have to mix, I want to mix zygote with ForwardDiff.
I think I have this down in using something like :
I guess if I wanted to take gradient for a vector, where I evaluate each partial derivative at the same point I could do :
X=[1.0,2.0,3.0]
for elem in X
ForwardDiff.gradient(z ->g(z[1], z[2]),[elem,elem])
end
This seems hacky and inefficient , and I know I can use static vectors to speed things up somewhat.
Next:
I want to evaluate a laplacian , a double gradient, for some multivariable function f(x1,x2,x3…) for a vector of numbers.
I guess I could chain the gradient function?
I want to evaluate a gradient f(x1,x2,x3,…) where I only evaluate for one variable, and I set say x2=1.0, x3=3.0
The way I have been doing this is grotesque , I simply recompile my function as a single variable function with a new global variable value put in for the x2,x3…
Thank you! I feel that this thread should be very useful.
I didn’t fully understand all those questions, but here is some code for ForwardDiff that can hopefully be helpful. Generally, I’d say it’s easier to work with functions f(x) where x is a vector, rather than functions f(x1,x2,x3,...) when using ForwardDiff. To make your future posts easier to read, include code in backticks ` (single for inline, triple for multiline).
The code for the Laplacian is admittedly a little gnarly, if you want to do many higher-order directional derivatives, then TaylorDiff.jl is probably a better choice.