Jacobian of a multi-inputs function

Hello,
I have a function which represents the mapping F(x,p), where x is a vector and p is a scalar parameter.
The function (mapping) output is a vector. I would like to compute the the jacobian (in this case the directional derivative as I have a scalar parameter p) of the function F wrt the parameter p. How can I go around implementing that. I am using

global n
n = x  #vector
 FiniteDifferences.jacobian(central_fdm(3, 1), F, p)[1]

to pass the x vector to the function F, which is not ideal. Is there a way to pass the value of x while taking the derivative wrt to p other than the global variables?

1 Like

Use a closure, i.e. p -> F(x,p)

3 Likes

Calculating the jacobian this way is very expensive. I recently came across your ‘new’ package FiniteDiff. I was wondering how could I calculate the Jacobian with the new package if I have such a mapping. I tried something like

FiniteDiff.finite_difference_jacobian!(p-> Derdp(x,p), p)

but it didn’t work out.

Only use the mutating function if you are mutating. FiniteDiff.finite_difference_jacobian(p-> Derdp(x,p), p) would work.

1 Like

This is actually what I tried, sorry the above quote was wrong. however, it gives the following error:

ERROR: MethodError: no method matching finite_difference_jacobian(::getfield(Main, Symbol("##53#54")){Int64}, ::Float64)

Why is p a Float64? You need to take the Jacobian with respect to a vector.

p is supposed to be a vector of parameters. I started with a simple case where I have only one parameter, so the jacobian <–> directional derivative. Maybe I should pass it as a vector of a single element?

Yes, it’s telling you that the function doesn’t handle scalars.

1 Like

Yes. Unlike in Matlab, Julia, cares about scalar vs array vs matrix. A 1 element vector is not the same as a scalar.

2 Likes

Thanks! It is working now much much faster than before. I appreciate your help.