def _fd_jacobian(self, xi, delta_list):
"""Calculate the Jacobian matrix using central finite difference.
Parameters
----------
xi : array_like
The coordinates to evaluate, with shape (..., ndim)
delta_list : List[float]
list of finite difference step sizes for each input.
Returns
-------
val : np.multiarray.ndarray
The Jacobian matrices at the given coordinates.
"""
xi = np.asarray(xi, dtype=float)
if xi.shape[-1] != self.ndim:
raise ValueError("The requested sample points xi have dimension %d, "
"but this interpolator has dimension %d" % (xi.shape[-1], self.ndim))
# use broadcasting to evaluate all points at once
xtest = np.broadcast_to(xi, (2 * self.ndim,) + xi.shape).copy()
for idx, delta in enumerate(delta_list):
xtest[2 * idx, ..., idx] += delta / 2.0
xtest[2 * idx + 1, ..., idx] -= delta / 2.0
val = self(xtest)
ans = np.empty(xi.shape)
for idx, delta in enumerate(delta_list):
ans[..., idx] = (val[2 * idx, ...] - val[2 * idx + 1, ...]) / delta
return ans
评论列表
文章目录