diff
Calculate the n-th discrete difference along the given axis. The first difference is given byout[i] = a[i+1] - a[i] along the specified axis. Higher differences are computed by applying diff recursively.
| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | Input array. |
n | number | 1 | Number of times values are differenced. If zero, the input is returned as-is. |
axis | number | -1 | Axis along which the difference is taken. Defaults to the last axis. |
prepend | ArrayLike | undefined | Values to prepend to a along axis before computing differences. A scalar broadcasts to size 1 along the diff axis with a’s shape on all other axes. An array must match a’s shape on every axis except axis. |
append | ArrayLike | undefined | Values to append to a along axis before computing differences. Same broadcasting rules as prepend. |
NDArray — The n-th differences. The shape is the same as a except along axis where the dimension is reduced by n (adjusted by any prepend/append).
Existing 3-argument callers are unaffected.
ediff1d
Compute the differences between consecutive elements of a flattened array, with optional values prepended or appended.| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | Input array. If multi-dimensional, it is flattened first. |
to_end | number[] | null | undefined | Values to append at the end of the returned differences. |
to_begin | number[] | null | undefined | Values to prepend at the beginning of the returned differences. |
NDArray — 1-D array of differences between consecutive elements, optionally with prepended/appended values.
gradient
Return the gradient of an N-dimensional array. The gradient is computed using second-order central differences in the interior and first-order (forward/backward) differences at the boundaries.| Name | Type | Default | Description |
|---|---|---|---|
f | ArrayLike | — | N-dimensional array containing samples of a scalar function. |
varargs | number | GradientSpacingArg[] | 1 | Spacing between sample points. A single scalar applies uniformly to every axis. A list provides one entry per axis (or per axis entry); each entry is either a scalar (uniform spacing) or a 1-D coordinate array of length axisSize (non-uniform). Mixable: gradient(f, [coords, 2]). |
axis | number | number[] | null | undefined | Axis or axes along which to compute the gradient. |
NDArray when f is 1-D (a single gradient array), NDArray[] when f is N-D with N > 1 (one gradient array per axis).
Coordinate arrays use NumPy’s non-uniform second-order central-difference formula in the interior and first-order differences at the boundaries. Existing scalar callers are unaffected.
Limitation: complex dtypes combined with coordinate arrays throws; pure-scalar spacing still works for complex inputs.