Created
March 12, 2021 14:12
-
-
Save Khazbs/b9f9ca5826120282dbdee2bb8271b0b6 to your computer and use it in GitHub Desktop.
Calculate function gradient via numpy
This file contains 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
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