megengine.functional.mul#

mul(x, y)[source]#

Element-wise multiplication.

Calculates the product for each element \(x_i\) of the input tensor x with the respective element \(y_i\) of the input tensor \(y\).

Parameters:
Return type:

Tensor

Returns:

A tensor containing the element-wise products. 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 is NaN.

  • If \(x_i\) is either +infinity or -infinity and \(y_i\) is either +0 or -0, the result is NaN.

  • If \(x_i\) is either +0 or -0 and \(y_i\) is either +infinity or -infinity, the result is NaN.

  • If \(x_i\) and \(y_i\) have different mathematical signs, the result has a negative mathematical sign, unless the result is NaN.

  • If \(x_i\) is either +infinity or -infinity and \(y_i\) is either +infinity or -infinity, the result is a signed infinity with the mathematical sign determined by the rule already stated above.

  • If \(x_i\) is either +infinity or -infinity and \(y_i\) is a nonzero finite number, the result is a signed infinity with the mathematical sign determined by the rule already stated above.

  • If \(x_i\) is a nonzero finite number and \(y_i\) is either +infinity or -infinity, the result is a signed infinity with the mathematical sign determined by the rule already stated above.

  • In the remaining cases, where neither infinity nor NaN is involved, the product 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 result is an infinity of appropriate mathematical sign. If the magnitude is too small to represent, the result is a zero of appropriate mathematical sign.

Note

  • Floating-point multiplication is not always associative due to finite precision.

  • The * operator can be used as a shorthand for mul on tensors.

Examples

>>> F.mul(1.0, 4.0)
Tensor(4.0, device=xpux:0)

Element-wise multiplication:

>>> x = Tensor([[1, 2, 3], [4, 5, 6]])
>>> y = Tensor([[1, 1, 1], [2, 2, 2]])
>>> F.mul(x, y)
Tensor([[ 1  2  3]
 [ 8 10 12]], dtype=int32, device=xpux:0)

Boradcasting:

>>> x = Tensor([[1, 2, 3], [4, 5, 6]])
>>> F.mul(x, 2)
Tensor([[ 2  4  6]
 [ 8 10 12]], dtype=int32, device=xpux:0)