Skip to content

Instantly share code, notes, and snippets.

@marmakoide
Created August 19, 2020 12:55
Show Gist options
  • Save marmakoide/5d785ca8c6c6695e4d57f6fdf4b262c2 to your computer and use it in GitHub Desktop.
Save marmakoide/5d785ca8c6c6695e4d57f6fdf4b262c2 to your computer and use it in GitHub Desktop.
Computes the jacobian of a real-valued function, using the complex step method. The result is accurate nearly down to the machine epsilon with a very simple code, at the expense of uncessary computations, and assuming that the function to differentiate is defined in the complex domain
def get_jacobian(func, X, h = 1e-7):
J = []
for i in range(X.shape[0]):
Xp = X.astype(complex)
Xp[i] += h * 1j
J.append([numpy.imag(func(Xp))])
J = numpy.concatenate(J, axis = 0)
J = numpy.transpose(J)
J /= h
return J
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment