MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / mul

Function mul

imperative/python/megengine/functional/elemwise.py:184–240  ·  view source on GitHub ↗

r"""Element-wise multiplication. Calculates the product for each element :math:`x_i` of the input tensor `x` with the respective element :math:`y_i` of the input tensor :math:`y`. Args: x: first input tensor. Should have a numeric data type. y: second input tensor. Must

(x: Tensor, y: Tensor)

Source from the content-addressed store, hash-verified

182
183
184def mul(x: Tensor, y: Tensor) -> Tensor:
185 r"""Element-wise multiplication.
186
187 Calculates the product for each element :math:`x_i` of the input tensor `x` with the respective element :math:`y_i` of the input tensor :math:`y`.
188
189 Args:
190 x: first input tensor. Should have a numeric data type.
191 y: second input tensor. Must be compatible with :math:`x` (see :ref:`broadcasting-rule` ). Should have a numeric data type.
192
193 Returns:
194 A tensor containing the element-wise products.
195 The returned tensor must have a data type determined by :ref:`dtype-promotion`.
196
197 .. admonition:: Special cases
198
199 For floating-point operands,
200
201 * If either :math:`x_i` or :math:`y_i` is ``NaN``, the result is ``NaN``.
202 * If :math:`x_i` is either ``+infinity`` or ``-infinity`` and :math:`y_i` is either ``+0`` or ``-0``, the result is ``NaN``.
203 * If :math:`x_i` is either ``+0`` or ``-0`` and :math:`y_i` is either ``+infinity`` or ``-infinity``, the result is ``NaN``.
204 * If :math:`x_i` and :math:`y_i` have different mathematical signs, the result has a negative mathematical sign, unless the result is ``NaN``.
205 * If :math:`x_i` is either ``+infinity`` or ``-infinity`` and :math:`y_i` is either ``+infinity`` or ``-infinity``,
206 the result is a signed infinity with the mathematical sign determined by the rule already stated above.
207 * If :math:`x_i` is either ``+infinity`` or ``-infinity`` and :math:`y_i` is a nonzero finite number,
208 the result is a signed infinity with the mathematical sign determined by the rule already stated above.
209 * If :math:`x_i` is a nonzero finite number and :math:`y_i` is either ``+infinity`` or ``-infinity``,
210 the result is a signed infinity with the mathematical sign determined by the rule already stated above.
211 * In the remaining cases, where neither ``infinity`` nor ``NaN`` is involved,
212 the product must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported rounding mode.
213 If the magnitude is too large to represent, the result is an `infinity` of appropriate mathematical sign.
214 If the magnitude is too small to represent, the result is a zero of appropriate mathematical sign.
215
216 .. Note::
217
218 * Floating-point multiplication is not always associative due to finite precision.
219 * The ``*`` operator can be used as a shorthand for ``mul`` on tensors.
220
221 Examples:
222 >>> F.mul(1.0, 4.0)
223 Tensor(4.0, device=xpux:0)
224
225 Element-wise multiplication:
226
227 >>> x = Tensor([[1, 2, 3], [4, 5, 6]])
228 >>> y = Tensor([[1, 1, 1], [2, 2, 2]])
229 >>> F.mul(x, y)
230 Tensor([[ 1 2 3]
231 [ 8 10 12]], dtype=int32, device=xpux:0)
232
233 Boradcasting:
234
235 >>> x = Tensor([[1, 2, 3], [4, 5, 6]])
236 >>> F.mul(x, 2)
237 Tensor([[ 2 4 6]
238 [ 8 10 12]], dtype=int32, device=xpux:0)
239 """
240 return _elwise(x, y, mode=Elemwise.Mode.MUL)
241

Callers 5

_get_tableMethod · 0.50
forwardMethod · 0.50
squareFunction · 0.50
__mul__Method · 0.50
__rmul__Method · 0.50

Calls 1

_elwiseFunction · 0.50

Tested by

no test coverage detected