remainder#

ivy.remainder(x1, x2, /, *, modulus=True, out=None)[source]#

Return the remainder of division for each element x1_i of the input array x1 and the respective element x2_i of the input array x2.

Note

This function is equivalent to the Python modulus operator x1_i % x2_i. For input arrays which promote to an integer data type, the result of division by zero is unspecified and thus implementation-defined. In general, similar to Python’s % operator, this function is not recommended for floating-point operands as semantics do not follow IEEE 754. That this function is specified to accept floating-point operands is primarily for reasons of backward compatibility.

Special Cases

For floating-point operands,

  • If either x1_i or x2_i is NaN, the result is NaN.

  • If x1_i is either +infinity or -infinity and x2_i is either +infinity or -infinity, the result is NaN.

  • If x1_i is either +0 or -0 and x2_i is either +0 or -0, the result is NaN.

  • If x1_i is +0 and x2_i is greater than 0, the result is +0.

  • If x1_i is -0 and x2_i is greater than 0, the result is +0.

  • If x1_i is +0 and x2_i is less than 0, the result is -0.

  • If x1_i is -0 and x2_i is less than 0, the result is -0.

  • If x1_i is greater than 0 and x2_i is +0, the result is NaN.

  • If x1_i is greater than 0 and x2_i is -0, the result is NaN.

  • If x1_i is less than 0 and x2_i is +0, the result is NaN.

  • If x1_i is less than 0 and x2_i is -0, the result is NaN.

  • If x1_i is +infinity and x2_i is a positive (i.e., greater than 0) finite number, the result is NaN.

  • If x1_i is +infinity and x2_i is a negative (i.e., less than 0) finite number, the result is NaN.

  • If x1_i is -infinity and x2_i is a positive (i.e., greater than 0) finite number, the result is NaN.

  • If x1_i is -infinity and x2_i is a negative (i.e., less than 0) finite number, the result is NaN.

  • If x1_i is a positive (i.e., greater than 0) finite number and x2_i is +infinity, the result is x1_i. (note: this result matches Python behavior.)

  • If x1_i is a positive (i.e., greater than 0) finite number and x2_i is -infinity, the result is x2_i. (note: this result matches Python behavior.)

  • If x1_i is a negative (i.e., less than 0) finite number and x2_i is +infinity, the result is x2_i. (note: this results matches Python behavior.)

  • If x1_i is a negative (i.e., less than 0) finite number and x2_i is -infinity, the result is x1_i. (note: this result matches Python behavior.)

  • In the remaining cases, the result must match that of the Python % operator.

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

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

  • modulus (bool, default: True) – whether to compute the modulus instead of the remainder. Default is True.

  • 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 results. Each element-wise result must have the same sign as the respective element x2_i. The returned array must have a data type determined by Type Promotion Rules.

This function 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

With ivy.Array inputs:

>>> x1 = ivy.array([2., 5., 15.])
>>> x2 = ivy.array([3., 2., 4.])
>>> y = ivy.remainder(x1, x2)
>>> print(y)
ivy.array([2., 1., 3.])

With mixed ivy.Array and ivy.NativeArray inputs:

>>> x1 = ivy.array([23., 1., 6.])
>>> x2 = ivy.native_array([11., 2., 4.])
>>> y = ivy.remainder(x1, x2)
>>> print(y)
ivy.array([1., 1., 2.])

With ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.array([2., 3., 5.]), b=ivy.array([2., 2., 4.]))
>>> x2 = ivy.Container(a=ivy.array([1., 3., 4.]), b=ivy.array([1., 3., 3.]))
>>> y = ivy.remainder(x1, x2)
>>> print(y)
{
    a: ivy.array([0., 0., 1.]),
    b: ivy.array([0., 2., 1.])
}
Array.remainder(self, x2, /, *, modulus=True, out=None)[source]#

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

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

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

  • modulus (bool, default: True) – whether to compute the modulus instead of the remainder. Default is True.

  • 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 results. Each element-wise result must have the same sign as the respective element x2_i. The returned array must have a data type determined by type-promotion.

Examples

With ivy.Array inputs:

>>> x1 = ivy.array([2., 5., 15.])
>>> x2 = ivy.array([3., 2., 4.])
>>> y = x1.remainder(x2)
>>> print(y)
ivy.array([2., 1., 3.])

With mixed ivy.Array and ivy.NativeArray inputs:

>>> x1 = ivy.array([11., 4., 18.])
>>> x2 = ivy.native_array([2., 5., 8.])
>>> y = x1.remainder(x2)
>>> print(y)
ivy.array([1., 4., 2.])
Container.remainder(self, x2, /, *, modulus=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
  • self (Container) – input array or container. Should have a real-valued data type.

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

  • modulus (Union[bool, Container], default: True) – whether to compute the modulus instead of the remainder. Default is True.

  • 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.

  • 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 results. The returned container must have the same sign as the respective element x2_i.

Examples

With ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.array([2., 3., 5.]), b=ivy.array([2., 2., 4.]))
>>> x2 = ivy.Container(a=ivy.array([1., 3., 4.]), b=ivy.array([1., 3., 3.]))
>>> y = x1.remainder(x2)
>>> print(y)
{
    a: ivy.array([0., 0., 1.]),
    b: ivy.array([0., 2., 1.])
}

With mixed ivy.Container and ivy.Array inputs:

>>> x1 = ivy.Container(a=ivy.array([2., 3., 5.]), b=ivy.array([2., 2., 4.]))
>>> x2 = ivy.array([1., 2., 3.])
>>> y = x1.remainder(x2)
>>> print(y)
{
    a: ivy.array([0., 1., 2.]),
    b: ivy.array([0., 0., 1.])
}

With mixed ivy.Container and ivy.NativeArray inputs:

>>> x1 = ivy.Container(a=ivy.array([2., 3., 5.]), b=ivy.array([2., 2., 4.]))
>>> x2 = ivy.native_array([1., 2., 3.])
>>> y = x1.remainder(x2)
>>> print(y)
{
    a: ivy.array([0., 1., 2.]),
    b: ivy.array([0., 0., 1.])
}