Return the gradient of an 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 h
(f, *varargs, axis=None, edge_order=1)
| 971 | |
| 972 | @array_function_dispatch(_gradient_dispatcher) |
| 973 | def gradient(f, *varargs, axis=None, edge_order=1): |
| 974 | """ |
| 975 | Return the gradient of an N-dimensional array. |
| 976 | |
| 977 | The gradient is computed using second order accurate central differences |
| 978 | in the interior points and either first or second order accurate one-sides |
| 979 | (forward or backwards) differences at the boundaries. |
| 980 | The returned gradient hence has the same shape as the input array. |
| 981 | |
| 982 | Parameters |
| 983 | ---------- |
| 984 | f : array_like |
| 985 | An N-dimensional array containing samples of a scalar function. |
| 986 | varargs : list of scalar or array, optional |
| 987 | Spacing between f values. Default unitary spacing for all dimensions. |
| 988 | Spacing can be specified using: |
| 989 | |
| 990 | 1. single scalar to specify a sample distance for all dimensions. |
| 991 | 2. N scalars to specify a constant sample distance for each dimension. |
| 992 | i.e. `dx`, `dy`, `dz`, ... |
| 993 | 3. N arrays to specify the coordinates of the values along each |
| 994 | dimension of F. The length of the array must match the size of |
| 995 | the corresponding dimension |
| 996 | 4. Any combination of N scalars/arrays with the meaning of 2. and 3. |
| 997 | |
| 998 | If `axis` is given, the number of varargs must equal the number of axes. |
| 999 | Default: 1. |
| 1000 | |
| 1001 | edge_order : {1, 2}, optional |
| 1002 | Gradient is calculated using N-th order accurate differences |
| 1003 | at the boundaries. Default: 1. |
| 1004 | |
| 1005 | .. versionadded:: 1.9.1 |
| 1006 | |
| 1007 | axis : None or int or tuple of ints, optional |
| 1008 | Gradient is calculated only along the given axis or axes |
| 1009 | The default (axis = None) is to calculate the gradient for all the axes |
| 1010 | of the input array. axis may be negative, in which case it counts from |
| 1011 | the last to the first axis. |
| 1012 | |
| 1013 | .. versionadded:: 1.11.0 |
| 1014 | |
| 1015 | Returns |
| 1016 | ------- |
| 1017 | gradient : ndarray or list of ndarray |
| 1018 | A list of ndarrays (or a single ndarray if there is only one dimension) |
| 1019 | corresponding to the derivatives of f with respect to each dimension. |
| 1020 | Each derivative has the same shape as f. |
| 1021 | |
| 1022 | Examples |
| 1023 | -------- |
| 1024 | >>> f = np.array([1, 2, 4, 7, 11, 16], dtype=float) |
| 1025 | >>> np.gradient(f) |
| 1026 | array([1. , 1.5, 2.5, 3.5, 4.5, 5. ]) |
| 1027 | >>> np.gradient(f, 2) |
| 1028 | array([0.5 , 0.75, 1.25, 1.75, 2.25, 2.5 ]) |
| 1029 | |
| 1030 | Spacing can be also specified with an array that represents the coordinates |