I have a bunch of unstructured points in 3D and a function value at each one. I want to calculate the gradient of the function (first and second order derivatives) at each point. Is there somewhere (scipy, numpy,...) an implemented function for doing this?
Example (I'm looking for some_function
):
import numpy as np
import random
xx, yy = np.meshgrid(np.linspace(-50, 50, 50), np.linspace(-50, 50, 50))
x = np.array([i + random.random() for i in xx.flatten()])
y = np.array([i + random.random() for i in yy.flatten()])
z = x**2 + x*y
dzdx_analytic = 2*x + y
dzdy_analytic = x
dzdx_numeric, dzdy_numeric = some_function(z, [x, y])
I've found numpy.gradient but don't understand the documentation enough to know if I can use it for this - I think it only works with structured data.
I could implement something like this (least squares linear regression using all neighbouring points), but would prefer to use an already implemented function in a standard library, if possible.