diff#

ivy.diff(x, /, *, n=1, axis=-1, prepend=None, append=None, out=None)[source]#

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

Parameters:
  • x (Union[Array, NativeArray, list, tuple]) – Array-like input.

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

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

  • prepend (Optional[Union[Array, NativeArray, int, list, tuple]], default: None) – Values to prepend/append to x along given 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 x except along axis.

  • append (Optional[Union[Array, NativeArray, int, list, tuple]], default: None) – Values to prepend/append to x along given 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 x except along axis.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

  • ret – Returns the n-th discrete difference along the given axis.

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

>>> x = ivy.array([1, 2, 4, 7, 0])
>>> ivy.diff(x)
ivy.array([ 1,  2,  3, -7])
Array.diff(self, /, *, n=1, axis=-1, prepend=None, append=None, out=None)[source]#

ivy.Array instance method variant of ivy.diff. This method simply wraps the function, and so the docstring for ivy.diff also applies to this method with minimal changes.

Parameters:
  • self (Array) – array-like input.

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

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

  • prepend (Optional[Union[Array, NativeArray, int, list, tuple]], default: None) – Values to prepend/append to x along given 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 x except along axis.

  • append (Optional[Union[Array, NativeArray, int, list, tuple]], default: None) – Values to prepend/append to x along given 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 x except along axis.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

ret – Returns the n-th discrete difference along the given axis.

Examples

>>> x = ivy.array([1, 2, 4, 7, 0])
>>> x.diff()
ivy.array([ 1,  2,  3, -7])
Container.diff(self, /, *, n=1, axis=-1, prepend=None, append=None, out=None)[source]#

ivy.Container instance method variant of ivy.diff. This method simply wraps the function, and so the docstring for ivy.diff also applies to this method with minimal changes.

Parameters:
  • self (Container) – input container with array-like items.

  • n (Union[int, Container], default: 1) – The number of times values are differenced. If zero, the input is returned as-is.

  • axis (Union[int, Container], default: -1) – The axis along which the difference is taken, default is the last axis.

  • prepend (Optional[Union[Array, NativeArray, int, list, tuple, Container]], default: None) – Values to prepend/append to x along given 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 x except along axis.

  • append (Optional[Union[Array, NativeArray, int, list, tuple, Container]], default: None) – Values to prepend/append to x along given 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 x except along axis.

  • out (Optional[Container], default: None) – optional output container, for writing the result to.

Return type:

Container

Returns:

ret – Container including arrays with the n-th discrete difference along the given axis.

Examples

>>> x = ivy.Container(a=ivy.array([1, 2, 4, 7, 0]),
                      b=ivy.array([1, 2, 4, 7, 0]))
>>> x.diff()
{
    a: ivy.array([1, 2, 3, -7]),
    b: ivy.array([1, 2, 3, -7])
}