Compute the Jacobian matrix of a vector-valued function f: R^n -> R^m. The Jacobian is the m x n matrix of all first-order partial derivatives, where entry (i, j) is df_i/dx_j.
import numpy as np
def jacobian(f, x: np.ndarray, epsilon: float = 1e-5) -> np.ndarray:
x = np.array(x, dtype=float)
f0 = np.array(f(x), dtype=float)
n = len(x)
m = len(f0)
J = np.zeros((m, n))
for j in range(n):
x_plus = x.copy()
x_minus = x.copy()
x_plus[j] += epsilon
x_minus[j] -= epsilon
f_plus = np.array(f(x_plus), dtype=float)
f_minus = np.array(f(x_minus), dtype=float)
J[:, j] = (f_plus - f_minus) / (2 * epsilon)
return Jx to determine the output dimension m.(f(x+e_j*eps) - f(x-e_j*eps)) / (2*eps).