divide#

ivy.divide(x1, x2, /, *, out=None)[source]#

Calculate the division for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.

Special Cases

For real-valued floating-point operands,

  • If either x1_i or x2_i is NaN, the result is NaN.

  • If x1_i is either +infinity or -infinity and x2_i is either +infinity or -infinity, the result is NaN.

  • If x1_i is either +0 or -0 and x2_i is either +0 or -0, the result is NaN.

  • If x1_i is +0 and x2_i is greater than 0, the result is +0.

  • If x1_i is -0 and x2_i is greater than 0, the result is -0.

  • If x1_i is +0 and x2_i is less than 0, the result is -0.

  • If x1_i is -0 and x2_i is less than 0, the result is +0.

  • If x1_i is greater than 0 and x2_i is +0, the result is +infinity.

  • If x1_i is greater than 0 and x2_i is -0, the result is -infinity.

  • If x1_i is less than 0 and x2_i is +0, the result is -infinity.

  • If x1_i is less than 0 and x2_i is -0, the result is +infinity.

  • If x1_i is +infinity and x2_i is a positive (i.e., greater than 0) finite number, the result is +infinity.

  • If x1_i is +infinity and x2_i is a negative (i.e., less than 0) finite number, the result is -infinity.

  • If x1_i is -infinity and x2_i is a positive (i.e., greater than 0) finite number, the result is -infinity.

  • If x1_i is -infinity and x2_i is a negative (i.e., less than 0) finite number, the result is +infinity.

  • If x1_i is a positive (i.e., greater than 0) finite number and x2_i is +infinity, the result is +0.

  • If x1_i is a positive (i.e., greater than 0) finite number and x2_i is -infinity, the result is -0.

  • If x1_i is a negative (i.e., less than 0) finite number and x2_i is +infinity, the result is -0.

  • If x1_i is a negative (i.e., less than 0) finite number and x2_i is -infinity, the result is +0.

  • If x1_i and x2_i have the same mathematical sign and are both nonzero finite numbers, the result has a positive mathematical sign.

  • If x1_i and x2_i 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, nor NaN 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.

For complex floating-point operands, division is defined according to the following table. For real components a and c and imaginary components b and d,

c

dj

c + dj

a

a / c

-(a/d)j

special rules

bj

(b/c)j

b/d

special rules

a + bj

(a/c) + (b/c)j

b/d - (a/d)j

special rules

In general, for complex floating-point operands, real-valued floating-point special cases must independently apply to the real and imaginary component operations involving real numbers as described in the above table.

When a, b, c, or d are all finite numbers (i.e., a value other than NaN, +infinity, or -infinity), division of complex floating-point operands should be computed as if calculated according to the textbook formula for complex number division

\[\frac{a + bj}{c + dj} = \frac{(ac + bd) + (bc - ad)j}{c^2 + d^2}\]

When at least one of a, b, c, or d is NaN, +infinity, or -infinity,

  • If a, b, c, and d are all NaN, the result is NaN + NaN j.

  • In the remaining cases, the result is implementation dependent.

Note

For complex floating-point operands, the results of special cases may be implementation dependent depending on how an implementation chooses to model complex numbers and complex infinity (e.g., complex plane versus Riemann sphere). For those implementations following C99 and its one-infinity model, when at least one component is infinite, even if the other component is NaN, the complex value is infinite, and the usual arithmetic rules do not apply to complex-complex division. In the interest of performance, other implementations may want to avoid the complex branching logic necessary to implement the one-infinity model and choose to implement all complex-complex division according to the textbook formula. Accordingly, special case behavior is unlikely to be consistent across implementations.

This method conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Parameters:
  • x1 (Union[float, Array, NativeArray]) – dividend input array. Should have a numeric data type.

  • x2 (Union[float, Array, NativeArray]) – divisor input array. Must be compatible with x1 (see Broadcasting). Should have a numeric data type.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the element-wise results. The returned array must have a floating-point data type determined by Type Promotion Rules.

Examples

With ivy.Array inputs:

>>> x1 = ivy.array([2., 7., 9.])
>>> x2 = ivy.array([3., 4., 0.6])
>>> y = ivy.divide(x1, x2)
>>> print(y)
ivy.array([0.667, 1.75, 15.])

With mixed ivy.Array and ivy.NativeArray inputs:

>>> x1 = ivy.array([5., 6., 9.])
>>> x2 = ivy.native_array([2., 2., 2.])
>>> y = ivy.divide(x1, x2)
>>> print(y)
ivy.array([2.5, 3., 4.5])

With ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.array([12., 3.5, 6.3]), b=ivy.array([3., 1., 0.9]))
>>> x2 = ivy.Container(a=ivy.array([1., 2.3, 3]), b=ivy.array([2.4, 3., 2.]))
>>> y = ivy.divide(x1, x2)
>>> print(y)
{
    a: ivy.array([12., 1.52, 2.1]),
    b: ivy.array([1.25, 0.333, 0.45])
}

With mixed ivy.Container and ivy.Array inputs:

>>> x1 = ivy.Container(a=ivy.array([12., 3.5, 6.3]), b=ivy.array([3., 1., 0.9]))
>>> x2 = ivy.array([4.3, 3., 5.])
>>> y = ivy.divide(x1, x2)
{
    a: ivy.array([2.79, 1.17, 1.26]),
    b: ivy.array([0.698, 0.333, 0.18])
}
Array.divide(self, x2, /, *, out=None)[source]#

ivy.Array instance method variant of ivy.divide. This method simply wraps the function, and so the docstring for ivy.divide also applies to this method with minimal changes.

Parameters:
  • self (Array) – dividend input array. Should have a real-valued data type.

  • x2 (Union[Array, NativeArray]) – divisor input array. Must be compatible with self (see broadcasting). Should have a real-valued data type.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the element-wise results. The returned array must have a data type determined by type-promotion.

Examples

With ivy.Array inputs:

>>> x1 = ivy.array([2., 7., 9.])
>>> x2 = ivy.array([2., 2., 2.])
>>> y = x1.divide(x2)
>>> print(y)
ivy.array([1., 3.5, 4.5])

With mixed ivy.Array and ivy.NativeArray inputs:

>>> x1 = ivy.array([2., 7., 9.])
>>> x2 = ivy.native_array([2., 2., 2.])
>>> y = x1.divide(x2)
>>> print(y)
ivy.array([1., 3.5, 4.5])
Container.divide(self, x2, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.divide. This method simply wraps the function, and so the docstring for ivy.divide also applies to this method with minimal changes.

Parameters:
  • self (Container) – dividend input array or container. Should have a real-valued data type.

  • x2 (Union[Container, Array, NativeArray]) – divisor input array or container. Must be compatible with self (see broadcasting). Should have a real-valued data type.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – a container containing the element-wise results. The returned container must have a data type determined by type-promotion.

Examples

With ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.array([12., 3.5, 6.3]), b=ivy.array([3., 1., 0.9]))
>>> x2 = ivy.Container(a=ivy.array([1., 2.3, 3]), b=ivy.array([2.4, 3., 2.]))
>>> y = x1.divide(x2)
>>> print(y)
{
    a: ivy.array([12., 1.52, 2.1]),
    b: ivy.array([1.25, 0.333, 0.45])
}

With Number instances at the leaves:

>>> x = ivy.Container(a=1, b=2)
>>> y = ivy.Container(a=5, b=4)
>>> z = x.divide(y)
>>> print(z)
{
    a: 0.2,
    b: 0.5
}