megengine.functional.div¶
- div(x, y)[source]¶
Element-wise division.
Calculates the division for each element
of the input tensor with the respective element of the input tensor .- Parameters
x (
Tensor
) – dividend input tensor. Should have a numeric data type.y (
Tensor
) – divisor input tensor. Must be compatible with (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
or isNaN
, the result isNaN
.If
is either+infinity
or-infinity
and is either+infinity
or-infinity
, the result isNaN
.If
is either+0
or-0
and is either+0
or-0
, the result isNaN
.If
is+0
and is greater than0
, the result is+0
.If
is-0
and is greater than0
, the result is-0
.If
is+0
and is less than0
, the result is-0
.If
is-0
and is less than0
, the result is+0
.If
is greater than0
and is+0
, the result is+infinity
.If
is greater than0
and is-0
, the result is-infinity
.If
is less than0
and is+0
, the result is-infinity
.If
is less than0
and is-0
, the result is+infinity
.If
is+infinity
and is a positive (i.e., greater than0
) finite number, the result is+infinity
.If
is+infinity
and is a negative (i.e., less than0
) finite number, the result is-infinity
.If
is-infinity
and is a positive (i.e., greater than0
) finite number, the result is-infinity
.If
is-infinity
and is a negative (i.e., less than0
) finite number, the result is+infinity
.If
is a positive (i.e., greater than0
) finite number and is+infinity
, the result is+0
.If
is a positive (i.e., greater than0
) finite number and is-infinity
, the result is-0
.If
is a negative (i.e., less than0
) finite number and is+infinity
, the result is-0
.If
is a negative (i.e., less than0
) finite number and is-infinity
, the result is+0
.If
and have the same mathematical sign and are both nonzero finite numbers, the result has a positive mathematical sign.If
and have different mathematical signs and are both nonzero finite numbers, the result has a negative mathematical sign.In the remaining cases, where neither
-infinity
,+0
,-0
, norNaN
is involved, the quotient must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported rounding mode. If the magnitude is too large to represent, the operation overflows and the result is an infinity of appropriate mathematical sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of appropriate mathematical sign.
Note
The
/
operator can be used as a shorthand fordiv
on tensors.See also
In Python,
//
is the floor division operator and/
is the true division operator. Seefloor_div
Examples
>>> F.div(1.0, 4.0) Tensor(0.25, device=xpux:0)
Element-wise division:
>>> x = Tensor([[1, 2, 3], [4, 5, 6]]) >>> y = Tensor([[1, 1, 1], [2, 2, 2]]) >>> F.div(x, y) Tensor([[1. 2. 3. ] [2. 2.5 3. ]], device=xpux:0)
Broadcasting:
>>> x = Tensor([[1, 2, 3], [4, 5, 6]]) >>> F.div(x, 2) Tensor([[0.5 1. 1.5] [2. 2.5 3. ]], device=xpux:0)