Skip to content

Instantly share code, notes, and snippets.

@vwxyzjn
Created February 13, 2019 01:20
Show Gist options
  • Save vwxyzjn/f01f2cf096ad2ad271f9b86f179c3d3f to your computer and use it in GitHub Desktop.
Save vwxyzjn/f01f2cf096ad2ad271f9b86f179c3d3f to your computer and use it in GitHub Desktop.
def derivative(func, x):
h = 1e-10
x_d = np.full_like(x, 0) # the derivatives
x_c = x.copy()
for idx, value in np.ndenumerate(x):
x_c[idx] += h
x_d[idx] = (func(x_c) - func(x)) / h
x_c[idx] -= h
return x_d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment