Created
August 19, 2020 12:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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