divide¶
- divide(x1: array | int | float | complex, x2: array | int | float | complex, /) array¶
Calculates the division of each element
x1_iof the input arrayx1with the respective elementx2_iof the input arrayx2.- Parameters:
x1 (Union[array, int, float, complex]) – dividend input array. Should have a numeric data type.
x2 (Union[array, int, float, complex]) – divisor input array. Must be compatible with
x1(see Broadcasting). Should have a numeric data type.
- Returns:
out (array) – an array containing the element-wise results. The returned array must have a floating-point data type determined by Type Promotion Rules.
Notes
At least one of
x1orx2must be an array.If one or both of the input arrays have integer data types, the result is implementation-dependent, as type promotion between data type “kinds” (e.g., integer versus floating-point) is unspecified.
Specification-compliant libraries may choose to raise an error or return an array containing the element-wise results. If an array is returned, the array must have a real-valued floating-point data type.
Special cases
For real-valued floating-point operands,
If either
x1_iorx2_iisNaN, the result isNaN.If
x1_iis either+infinityor-infinityandx2_iis either+infinityor-infinity, the result isNaN.If
x1_iis either+0or-0andx2_iis either+0or-0, the result isNaN.If
x1_iis+0andx2_iis greater than0, the result is+0.If
x1_iis-0andx2_iis greater than0, the result is-0.If
x1_iis+0andx2_iis less than0, the result is-0.If
x1_iis-0andx2_iis less than0, the result is+0.If
x1_iis greater than0andx2_iis+0, the result is+infinity.If
x1_iis greater than0andx2_iis-0, the result is-infinity.If
x1_iis less than0andx2_iis+0, the result is-infinity.If
x1_iis less than0andx2_iis-0, the result is+infinity.If
x1_iis+infinityandx2_iis a positive (i.e., greater than0) finite number, the result is+infinity.If
x1_iis+infinityandx2_iis a negative (i.e., less than0) finite number, the result is-infinity.If
x1_iis-infinityandx2_iis a positive (i.e., greater than0) finite number, the result is-infinity.If
x1_iis-infinityandx2_iis a negative (i.e., less than0) finite number, the result is+infinity.If
x1_iis a positive (i.e., greater than0) finite number andx2_iis+infinity, the result is+0.If
x1_iis a positive (i.e., greater than0) finite number andx2_iis-infinity, the result is-0.If
x1_iis a negative (i.e., less than0) finite number andx2_iis+infinity, the result is-0.If
x1_iis a negative (i.e., less than0) finite number andx2_iis-infinity, the result is+0.If
x1_iandx2_ihave the same mathematical sign and are both nonzero finite numbers, the result has a positive mathematical sign.If
x1_iandx2_ihave 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, norNaNis 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 aninfinityof 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
aandcand imaginary componentsbandd,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, ordare all finite numbers (i.e., a value other thanNaN,+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, ordisNaN,+infinity, or-infinity,If
a,b,c, anddare allNaN, the result isNaN + 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.Changed in version 2022.12: Added complex data type support.
Changed in version 2024.12: Added scalar argument support.