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. Scalar or array broadcastable to a except along axis. |
append | ArrayLike | undefined | Values to append to a along axis before computing differences. |
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).
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 | number[] | undefined | Spacing between sample points. |
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).