MCPcopy Index your code
hub / github.com/numpy/numpy / gradient

Function gradient

numpy/lib/_function_base_impl.py:1014–1406  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1012
1013@array_function_dispatch(_gradient_dispatcher)
1014def gradient(f, *varargs, axis=None, edge_order=1):
1015 """
1016 Return the gradient of an N-dimensional array.
1017
1018 The gradient is computed using second order accurate central differences
1019 in the interior points and either first or second order accurate one-sides
1020 (forward or backwards) differences at the boundaries.
1021 The returned gradient hence has the same shape as the input array.
1022
1023 Parameters
1024 ----------
1025 f : array_like
1026 An N-dimensional array containing samples of a scalar function.
1027 varargs : list of scalar or array, optional
1028 Spacing between f values. Default unitary spacing for all dimensions.
1029 Spacing can be specified using:
1030
1031 1. Single scalar to specify a sample distance for all dimensions.
1032 2. N scalars to specify a constant sample distance for each dimension.
1033 i.e. `dx`, `dy`, `dz`, ...
1034 3. N arrays to specify the coordinates of the values along each
1035 dimension of F. The length of the array must match the size of
1036 the corresponding dimension
1037 4. Any combination of N scalars/arrays with the meaning of 2. and 3.
1038
1039 If `axis` is given, the number of varargs must equal the number of axes
1040 specified in the axis parameter.
1041 Default: 1. (see Examples below).
1042
1043 edge_order : {1, 2}, optional
1044 Gradient is calculated using N-th order accurate differences
1045 at the boundaries. Default: 1.
1046 axis : None or int or tuple of ints, optional
1047 Gradient is calculated only along the given axis or axes.
1048 The default (axis = None) is to calculate the gradient for all the axes
1049 of the input array. axis may be negative, in which case it counts from
1050 the last to the first axis.
1051
1052 Returns
1053 -------
1054 gradient : ndarray or tuple of ndarray
1055 A tuple of ndarrays (or a single ndarray if there is only one
1056 dimension) corresponding to the derivatives of f with respect
1057 to each dimension. Each derivative has the same shape as f.
1058
1059 Examples
1060 --------
1061 >>> import numpy as np
1062 >>> f = np.array([1, 2, 4, 7, 11, 16])
1063 >>> np.gradient(f)
1064 array([1. , 1.5, 2.5, 3.5, 4.5, 5. ])
1065 >>> np.gradient(f, 2)
1066 array([0.5 , 0.75, 1.25, 1.75, 2.25, 2.5 ])
1067
1068 Spacing can be also specified with an array that represents the coordinates
1069 of the values F along the dimensions.
1070 For instance a uniform spacing:
1071

Callers 13

test_basicMethod · 0.90
test_argsMethod · 0.90
test_datetime64Method · 0.90
test_maskedMethod · 0.90
test_spacingMethod · 0.90
test_specific_axesMethod · 0.90
test_timedelta64Method · 0.90
test_inexact_dtypesMethod · 0.90
test_valuesMethod · 0.90

Calls 7

sliceFunction · 0.85
astypeMethod · 0.80
replaceMethod · 0.80
reshapeMethod · 0.80
allMethod · 0.45
dtypeMethod · 0.45
viewMethod · 0.45

Tested by 13

test_basicMethod · 0.72
test_argsMethod · 0.72
test_datetime64Method · 0.72
test_maskedMethod · 0.72
test_spacingMethod · 0.72
test_specific_axesMethod · 0.72
test_timedelta64Method · 0.72
test_inexact_dtypesMethod · 0.72
test_valuesMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…