gradient(f, *varargs, axis=None, edge_order=1)¶
Returns the gradient of a N-dimensional array. 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.
Currently we only support edge_order`=1 and uniform spacing of `varargs.
varargs (Union[tuple[number], tuple[tensor scalar]], optional) – Spacing between f values. Default unitary spacing for all dimensions. Spacing can be specified using: 1. single scalar to specify a sample distance for all dimensions. 2. N scalars to specify a constant sample distance for each dimension.
edge_order (int) – Gradient is calculated using N-th order accurate differences at the boundaries. Default: 1.
axis (Union[None, int, tuple(int), list(int)], optional) – Gradient 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 tensor. axis may be negative, in which case it counts from the last to the first axis.
gradient, a list of tensors (or a single tensor if there is only one dimension to be calculated). Each derivative has the same shape as f.
- Supported Platforms:
>>> import mindspore.numpy as np >>> output = np.gradient([[1, 2, 6], [3, 4, 5]], axis=-1) >>> print(output) [[1. 2.5 4. ] [1. 1. 1. ]]