megengine.functional.mod¶
- mod(x, y)[source]¶
Element-wise \(\operatorname{mod}(x, y)\) function.
Returns the remainder of division for each element \(x_i\) of the input tensor \(x\) and the respective element \(y_i\) of the input tensor \(y\).
Note
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.
mod
is an alias ofremainder
in NumPy.
- Parameters
x (
Tensor
) – dividend input tensor. Should have a numeric data type.y (
Tensor
) – divisor input tensor. Must be compatible with \(x\) (see Broadcasting mechanism and rules ). Should have a numeric data type.
- Return type
- Returns
A tensor containing the element-wise results. The returned tensor must have a data type determined by Type promotion rules.
Special cases
For floating-point operands,
If either \(x_i\) or \(y_i\) is
NaN
, the result isNaN
.If \(x_i\) is either
+infinity
or-infinity
and \(y_i\) is either+infinity
or-infinity
, the result isNaN
.If \(x_i\) is either
+0
or-0
and \(y_i\) is either+0
or-0
, the result isNaN
.If \(x_i\) is
+0
and \(y_i\) is greater than 0, the result is+0
.If \(x_i\) is
-0
and \(y_i\) is greater than 0, the result is+0
.If \(x_i\) is
+0
and \(y_i\) is less than 0, the result is-0
.If \(x_i\) is
-0
and \(y_i\) is less than 0, the result is-0
.If \(x_i\) is greater than
0
and \(y_i\) is+0
, the result isNaN
.If \(x_i\) is greater than
0
and \(y_i\) is-0
, the result isNaN
.If \(x_i\) is less than
0
and \(y_i\) is+0
, the result isNaN
.If \(x_i\) is less than
0
and \(y_i\) is-0
, the result isNaN
.If \(x_i\) is
+infinity
and \(y_i\) is a positive (i.e., greater than 0) finite number, the result isNaN
.If \(x_i\) is
+infinity
and \(y_i\) is a negative (i.e., less than 0) finite number, the result isNaN
.If \(x_i\) is
-infinity
and \(y_i\) is a positive (i.e., greater than 0) finite number, the result isNaN
.If \(x_i\) is
-infinity
and \(y_i\) is a negative (i.e., less than 0) finite number, the result isNaN
.If \(x_i\) is a positive (i.e., greater than
0
) finite number and \(y_i\) is+infinity
, the result is \(x_i\). (note: this result matches Python behavior.)If \(x_i\) is a positive (i.e., greater than
0
) finite number and \(y_i\) is-infinity
, the result is \(y_i\). (note: this result matches Python behavior.)If \(x_i\) is a negative (i.e., less than
0
) finite number and \(y_i\) is+infinity
, the result is \(y_i\). (note: this results matches Python behavior.)If \(x_i\) is a negative (i.e., less than
0
) finite number and \(y_i\) is-infinity
, the result is \(x_i\). (note: this result matches Python behavior.)In the remaining cases, the result must match that of the Python
%
operator.
Examples
>>> F.mod(8, 3) Tensor(2, dtype=int32, device=xpux:0)
Element-wise mod:
>>> x = Tensor([1, 2, 3, 4, 5]) >>> y = Tensor([1, 2, 1, 2, 1]) >>> F.mod(x, y) Tensor([0 0 0 0 0], dtype=int32, device=xpux:0)
Broadcasting:
>>> x = Tensor([1, 2, 3, 4, 5]) >>> F.mod(x, 3) Tensor([1 2 0 1 2], dtype=int32, device=xpux:0)