bundles / numpy latest / numpy / gradient
_ArrayFunctionDispatcher
numpy:gradient
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_function_base_impl.py :1002
Signature
def gradient ( f , * varargs , axis = None , edge_order = 1 ) Summary
Return the gradient of an N-dimensional array.
Extended Summary
The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array.
Parameters
f: array_likeAn N-dimensional array containing samples of a scalar function.
varargs: list of scalar or array, optionalSpacing between f values. Default unitary spacing for all dimensions. Spacing can be specified using:
single scalar to specify a sample distance for all dimensions.
N scalars to specify a constant sample distance for each dimension. i.e.
dx,dy,dz, ...N arrays to specify the coordinates of the values along each dimension of F. The length of the array must match the size of the corresponding dimension
Any combination of N scalars/arrays with the meaning of 2. and 3.
If
axisis given, the number of varargs must equal the number of axes specified in the axis parameter. Default: 1. (see Examples below).edge_order: {1, 2}, optionalGradient is calculated using N-th order accurate differences at the boundaries. Default: 1.
axis: None or int or tuple of ints, optionalGradient is calculated only along the given axis or axes The default (axis = None) is to calculate the gradient for all the axes of the input array. axis may be negative, in which case it counts from the last to the first axis.
Returns
gradient: ndarray or tuple of ndarrayA tuple of ndarrays (or a single ndarray if there is only one dimension) corresponding to the derivatives of f with respect to each dimension. Each derivative has the same shape as f.
Notes
Assuming that (i.e., has at least 3 continuous derivatives) and let be a non-homogeneous stepsize, we minimize the "consistency error" between the true gradient and its estimate from a linear combination of the neighboring grid-points:
By substituting and with their Taylor series expansion, this translates into solving the following the linear system:
The resulting approximation of is the following:
It is worth noting that if (i.e., data are evenly spaced) we find the standard second order approximation:
With a similar procedure the forward/backward approximations used for boundaries can be derived.
Examples
import numpy as np f = np.array([1, 2, 4, 7, 11, 16]) np.gradient(f)✓
np.gradient(f, 2)
✗x = np.arange(f.size)
✓np.gradient(f, x)
✗x = np.array([0., 1., 1.5, 3.5, 4., 6.])
✓np.gradient(f, x)
✗np.gradient(np.array([[1, 2, 6], [3, 4, 5]]))
✗dx = 2. y = [1., 1.5, 3.5]✓
np.gradient(np.array([[1, 2, 6], [3, 4, 5]]), dx, y)
✗x = np.array([0, 1, 2, 3, 4]) f = x**2✓
np.gradient(f, edge_order=1)
✗np.gradient(f, edge_order=2)
✓np.gradient(np.array([[1, 2, 6], [3, 4, 5]]), axis=0)
✓x = np.array([0., 2., 3., 6., 8.]) y = x ** 2 np.gradient(y, x, edge_order=2)✓
dx = 2 x = np.array([0., 2., 4., 6., 8.]) y = x ** 2 np.gradient(y, dx, edge_order=2)✓
dx = 2 dy = 3 x = np.arange(0, 6, dx) y = np.arange(0, 9, dy) xs, ys = np.meshgrid(x, y) zs = xs + 2 * ys✓
np.gradient(zs, dy, dx) # Passing two scalars
✗np.gradient(zs, y, dx) # Passing one array and one scalar
✗Aliases
-
numpy.gradient