remainder#
- ivy.remainder(x1, x2, /, *, modulus=True, out=None)[source]#
Return the remainder of division for each element
x1_iof the input arrayx1and the respective elementx2_iof the input arrayx2.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_iorx2_iisNaN, the result isNaN.If
x1_iis either+infinityor-infinityandx2_iis either+infinityor-infinity, the result isNaN.If
x1_iis either+0or-0andx2_iis either+0or-0, the result isNaN.If
x1_iis+0andx2_iis greater than0, the result is+0.If
x1_iis-0andx2_iis greater than0, the result is+0.If
x1_iis+0andx2_iis less than0, the result is-0.If
x1_iis-0andx2_iis less than0, the result is-0.If
x1_iis greater than0andx2_iis+0, the result isNaN.If
x1_iis greater than0andx2_iis-0, the result isNaN.If
x1_iis less than0andx2_iis+0, the result isNaN.If
x1_iis less than0andx2_iis-0, the result isNaN.If
x1_iis+infinityandx2_iis a positive (i.e., greater than0) finite number, the result isNaN.If
x1_iis+infinityandx2_iis a negative (i.e., less than0) finite number, the result isNaN.If
x1_iis-infinityandx2_iis a positive (i.e., greater than0) finite number, the result isNaN.If
x1_iis-infinityandx2_iis a negative (i.e., less than0) finite number, the result isNaN.If
x1_iis a positive (i.e., greater than0) finite number andx2_iis+infinity, the result isx1_i. (note: this result matches Python behavior.)If
x1_iis a positive (i.e., greater than0) finite number andx2_iis-infinity, the result isx2_i. (note: this result matches Python behavior.)If
x1_iis a negative (i.e., less than0) finite number andx2_iis+infinity, the result isx2_i. (note: this results matches Python behavior.)If
x1_iis a negative (i.e., less than0) finite number andx2_iis-infinity, the result isx1_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 withx1(see ref:Broadcasting). Should have a numeric data type.modulus (
bool, default:True) – whether to compute the modulus instead of the remainder. Default isTrue.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:
- 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.Containerinstances in place of any of the argumentsExamples
With
ivy.Arrayinputs:>>> 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.Arrayandivy.NativeArrayinputs:>>> 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.Containerinputs:>>> 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 withself(see broadcasting). Should have a real-valued data type.modulus (
bool, default:True) – whether to compute the modulus instead of the remainder. Default isTrue.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.Arrayinputs:>>> 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.Arrayandivy.NativeArrayinputs:>>> 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 withself(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 isTrue.key_chains (
Optional[Union[List[str],Dict[str,str],Container]], default:None) – The key-chains to apply or not apply the method to. Default isNone.to_apply (
Union[bool,Container], default:True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue.prune_unapplied (
Union[bool,Container], default:False) – Whether to prune key_chains for which the function was not applied. Default isFalse.map_sequences (
Union[bool,Container], default:False) – Whether to also map method to sequences (lists, tuples). Default isFalse.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.Containerinputs:>>> 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.Containerand 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.Containerand 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.]) }