mindspore.mint.diff

mindspore.mint.diff(input, n=1, dim=- 1, prepend=None, append=None)[source]

Computes the n-th forward difference along the given dimension.

The first-order differences are given by \(out[i] = input[i+1] - input[i]\). Higher-order differences are calculated by using torch.diff() recursively.

Warning

This is an experimental API that is subject to change or deletion.

Parameters
  • input (Tensor) – the tensor to compute the differences on.

  • n (int, optional) – the number of times to recursively compute the difference. Default: 1 .

  • dim (int, optional) – the dimension to compute the difference along. Default is the last dimension. Default: 0 .

  • prepend (Tensor, optional) – values to prepend or append to input along dim before computing the difference. Their dimensions must be equivalent to that of input, and their shapes must match input's shape except on dim. Default: None .

  • append (Tensor, optional) – values to prepend or append to input along dim before computing the difference. Their dimensions must be equivalent to that of input, and their shapes must match input's shape except on dim. Default: None .

Returns

Tensor, the result of n-th forward difference computation.

Raises
  • TypeError – If input is not a tensor.

  • TypeError – If n is not a scalar or scalar tensor.

  • TypeError – If dim is not a scalar or scalar tensor.

  • TypeError – If input type is complex64, complex128, float64, int16.

Supported Platforms:

Ascend

Examples

>>> from mindspore import Tensor, mint
>>> x = Tensor([1, 3, -1, 0, 4])
>>> out = mint.diff(x)
>>> print(out.asnumpy())
[ 2 -4  1  4]