Skip to content

Instantly share code, notes, and snippets.

@Khazbs
Created March 12, 2021 14:12
Show Gist options
  • Save Khazbs/b9f9ca5826120282dbdee2bb8271b0b6 to your computer and use it in GitHub Desktop.
Save Khazbs/b9f9ca5826120282dbdee2bb8271b0b6 to your computer and use it in GitHub Desktop.
Calculate function gradient via numpy
import numpy as np
def nabla(f):
n = f.__code__.co_argcount
d = (np.arange(-100, 101, 1) for i in range(n))
x = np.meshgrid(*d)
return np.gradient(f(*x))
x_squared = lambda x: x ** 2
print(nabla(x_squared))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment