mindspore.numpy.diff

View Source On Gitee
mindspore.numpy.diff(a, n=1, axis=- 1, prepend=None, append=None)[source]

Calculates the n-th discrete difference along the given axis.

The first difference is given by \(out[i] = a[i+1] - a[i]\) along the given axis, higher differences are calculated by using diff iteratively.

Note

Since zero-shaped Tensor is not supported in MindSpore, a value error is raised if an empty Tensor is encountered.

Parameters
  • a (Tensor) – Input tensor.

  • n (int, optional) – The number of times values are differenced. If zero, the input is returned as-is. Default: 1 .

  • axis (int, optional) – The axis along which the difference is taken, default is the last axis. Default: -1 .

  • prepend/append (Tensor, optional) – Values to prepend or append to a along axis prior to performing the difference. Scalar values are expanded to arrays with length 1 in the direction of axis and the shape of the input array in along all other axes. Otherwise the dimension and shape must match a except along axis. Default: None .

Returns

The n-th differences. The shape of the output is the same as a except along axis where the dimension is smaller by n. The type of the output is the same as the type of the difference between any two elements of a. This is the same as the type of a in most cases.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore.numpy as np
>>> arr = np.array([1, 3, -1, 0, 4])
>>> print(np.diff(arr, n=2))
[-6  5  3]