subtract#

ivy.subtract(x1, x2, /, *, alpha=None, out=None)[source]#

Calculate the difference for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.

Parameters:
  • x1 (Union[float, Array, NativeArray]) – first input array. Should have a numeric data type.

  • x2 (Union[float, Array, NativeArray]) – second input array. Must be compatible with x1 (see ref:broadcasting). Should have a numeric data type.

  • alpha (Optional[Union[int, float]], default: None) – optional scalar multiplier for x2.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the element-wise differences.

This method conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

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([3, 6, 3])
>>> y = ivy.array([2, 1, 6])
>>> z = ivy.subtract(x, y)
>>> print(z)
ivy.array([ 1,  5, -3])
>>> x = ivy.array([3, 6, 3])
>>> y = ivy.array([2, 1, 6])
>>> z = ivy.subtract(x, y, alpha=2)
>>> print(z)
ivy.array([-1,  4, -9])
Array.subtract(self, x2, /, *, alpha=None, out=None)[source]#

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

Parameters:
  • self (Array) – first input array. Should have a real-valued data type.

  • x2 (Union[Array, NativeArray]) – second input array. Must be compatible with self (see broadcasting). Should have a real-valued data type.

  • alpha (Optional[Union[int, float]], default: None) – optional scalar multiplier for x2.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the element-wise differences. The returned array must have a data type determined by type-promotion.

Examples

>>> x = ivy.array([5, 2, 3])
>>> y = ivy.array([1, 2, 6])
>>> z = x.subtract(y)
>>> print(z)
ivy.array([4, 0, -3])
>>> x = ivy.array([5., 5, 3])
>>> y = ivy.array([4, 5, 6])
>>> z = x.subtract(y, alpha=2)
>>> print(z)
ivy.array([-3., -5., -9.])
Container.subtract(self, x2, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, alpha=None, out=None)[source]#

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

Parameters:
  • self (Container) – first input array or container. Should have a numeric data type.

  • x2 (Union[Container, Array, NativeArray]) – second input array or container. Must be compatible with self (see broadcasting). Should have a numeric data type.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • alpha (Optional[Union[int, float, Container]], default: None) – optional scalar multiplier for x2.

  • out (Optional[Container], default: None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – a container containing the element-wise sums. The returned container must have a data type determined by type-promotion.

Examples

>>> x = ivy.Container(a=ivy.array([1, 2, 3]),
...                   b=ivy.array([2, 3, 4]))
>>> y = ivy.Container(a=ivy.array([4, 1, 3]),
...                   b=ivy.array([1, -1, 0]))
>>> z = x.subtract(y)
>>> print(z)
{
    a: ivy.array([-3, 1, 0]),
    b: ivy.array([1, 4, 4])
}
>>> z = x.subtract(y, alpha=3)
>>> print(z)
{
    a: ivy.array([-11, -1, -6]),
    b: ivy.array([-1, 6, 4])
}