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

Function div

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

r"""Element-wise division. Calculates the division for each element :math:`x_i` of the input tensor :math:`x` with the respective element :math:`y_i` of the input tensor :math:`y`. Args: x: dividend input tensor. Should have a numeric data type. y: divisor input tensor.

(x: Tensor, y: Tensor)

Source from the content-addressed store, hash-verified

241
242
243def div(x: Tensor, y: Tensor) -> Tensor:
244 r"""Element-wise division.
245
246 Calculates the division for each element :math:`x_i` of the input tensor :math:`x` with the respective element :math:`y_i` of the input tensor :math:`y`.
247
248 Args:
249 x: dividend input tensor. Should have a numeric data type.
250 y: divisor input tensor. Must be compatible with :math:`x`` (see :ref:`broadcasting-rule` ). Should have a numeric data type.
251
252 Returns:
253 A tensor containing the element-wise results.
254 The returned tensor must have a data type determined by :ref:`dtype-promotion`.
255
256 .. admonition:: Special cases
257
258 For floating-point operands,
259
260 * If either :math:`x` or :math:`y` is ``NaN``, the result is ``NaN``.
261 * If :math:`x` is either ``+infinity`` or ``-infinity`` and :math:`y` is either ``+infinity`` or ``-infinity``, the result is ``NaN``.
262 * If :math:`x` is either ``+0`` or ``-0`` and :math:`y` is either ``+0`` or ``-0``, the result is ``NaN``.
263 * If :math:`x` is ``+0`` and :math:`y` is greater than ``0``, the result is ``+0``.
264 * If :math:`x` is ``-0`` and :math:`y` is greater than ``0``, the result is ``-0``.
265 * If :math:`x` is ``+0`` and :math:`y` is less than ``0``, the result is ``-0``.
266 * If :math:`x` is ``-0`` and :math:`y` is less than ``0``, the result is ``+0``.
267 * If :math:`x` is greater than ``0`` and :math:`y` is ``+0``, the result is ``+infinity``.
268 * If :math:`x` is greater than ``0`` and :math:`y` is ``-0``, the result is ``-infinity``.
269 * If :math:`x` is less than ``0`` and :math:`y` is ``+0``, the result is ``-infinity``.
270 * If :math:`x` is less than ``0`` and :math:`y` is ``-0``, the result is ``+infinity``.
271 * If :math:`x` is ``+infinity`` and :math:`y` is a positive (i.e., greater than ``0``) finite number, the result is ``+infinity``.
272 * If :math:`x` is ``+infinity`` and :math:`y` is a negative (i.e., less than ``0``) finite number, the result is ``-infinity``.
273 * If :math:`x` is ``-infinity`` and :math:`y` is a positive (i.e., greater than ``0``) finite number, the result is ``-infinity``.
274 * If :math:`x` is ``-infinity`` and :math:`y` is a negative (i.e., less than ``0``) finite number, the result is ``+infinity``.
275 * If :math:`x` is a positive (i.e., greater than ``0``) finite number and :math:`y` is ``+infinity``, the result is ``+0``.
276 * If :math:`x` is a positive (i.e., greater than ``0``) finite number and :math:`y` is ``-infinity``, the result is ``-0``.
277 * If :math:`x` is a negative (i.e., less than ``0``) finite number and :math:`y` is ``+infinity``, the result is ``-0``.
278 * If :math:`x` is a negative (i.e., less than ``0``) finite number and :math:`y` is ``-infinity``, the result is ``+0``.
279 * If :math:`x` and :math:`y` have the same mathematical sign and are both nonzero finite numbers, the result has a positive mathematical sign.
280 * If :math:`x` and :math:`y` have different mathematical signs and are both nonzero finite numbers, the result has a negative mathematical sign.
281 * In the remaining cases, where neither ``-infinity``, ``+0``, ``-0``, nor ``NaN`` is involved,
282 the quotient must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported rounding mode.
283 If the magnitude is too large to represent, the operation overflows and the result is an infinity of appropriate mathematical sign.
284 If the magnitude is too small to represent, the operation underflows and the result is a zero of appropriate mathematical sign.
285
286 .. note::
287
288 The ``/`` operator can be used as a shorthand for ``div`` on tensors.
289
290 .. seealso::
291
292 In Python, ``//`` is the floor division operator and ``/`` is the true division operator.
293 See :func:`~.functional.floor_div`
294
295 Examples:
296 >>> F.div(1.0, 4.0)
297 Tensor(0.25, device=xpux:0)
298
299 Element-wise division:
300

Callers 6

meanFunction · 0.85
floor_divFunction · 0.85
__truediv__Method · 0.85
__rtruediv__Method · 0.85
operator/Method · 0.85
MGB_IMPL_OPR_GRADFunction · 0.85

Calls 1

_elwiseFunction · 0.50

Tested by

no test coverage detected