0

I would like to create a NumPy function that computes the Jacobian of a function at a certain point - with the Jacobian hard coded into the function.

Say I have a vector containing two arbitrary scalars X = np.array([[x],[y]]), and a function f(X) = np.array([[2xy],[3xy]]).

This function has Jacobian J = np.array([[2y, 2x],[3y, 3x]])

How can I write a function that takes in the array X and returns the Jacobian? Of course, I could do this using array indices (e.g. x = X[0,0]), but am wondering if there is a way to do this directly without accessing the individual elements of X.

I am looking for something that works like this:

def foo(x,y):
    return np.array([[2*y, 2*x],[3*y, 3*x]])

X = np.array([[3],[7]])
J = foo(X)

Given that this is possible on 1-dimensional arrays, e.g. the following works:

def foo(x):
    return np.array([x,x,x])

X = np.array([1,2,3,4])
J = foo(X)
hegash
  • 833
  • 1
  • 7
  • 16

1 Answers1

0

You want the jacobian, which is the differential of the function. Is that correct? I'm afraid numpy is not the right tool for that.

Numpy works with fixed numbers not with variables. That is given some number you can calculate the value of a function. The differential is a different function, that has a special relationship to the original function but is not the same. You cannot just calculate the differential but must deduce it from the functional form of the original function using differentiating rules. Numpy cannot do that.

As far as I know you have three options:

  • use a numeric library to calculate the differential at a specific point. However you only will get the jacobian at a specific point (x,y) and no formula for it.
  • take a look at a pythen CAS library like e.g. sympy. There you can define expressions in terms of variables and compute the differential with respect to that variables.
  • Use a library that perform automatic differentiation. Maschine learning toolkits like pytorch or tensorflow have excellent support for automatic differentiation and good integration of numpy arrays. They essentially calculate the differential, by knowing the differential for all basic operation like multiplication or addition. For composed functions, the chain rule is applied and the difderential can be calculated for arbitray complex functions.
Jakob Stark
  • 3,346
  • 6
  • 22