multiply¶
- multiply(x1: array, x2: array, /) array ¶
Calculates the product for each element
x1_i
of the input arrayx1
with the respective elementx2_i
of the input arrayx2
.Note
Floating-point multiplication is not always associative due to finite precision.
- Parameters:
x1 (array) – first input array. Should have a numeric data type.
x2 (array) – second input array. Must be compatible with
x1
(see Broadcasting). Should have a numeric data type.
- Returns:
out (array) – an array containing the element-wise products. The returned array must have a data type determined by Type Promotion Rules.
Notes
Special cases
For real-valued floating-point operands,
If either
x1_i
orx2_i
isNaN
, the result isNaN
.If
x1_i
is either+infinity
or-infinity
andx2_i
is either+0
or-0
, the result isNaN
.If
x1_i
is either+0
or-0
andx2_i
is either+infinity
or-infinity
, the result isNaN
.If
x1_i
andx2_i
have the same mathematical sign, the result has a positive mathematical sign, unless the result isNaN
. If the result isNaN
, the “sign” ofNaN
is implementation-defined.If
x1_i
andx2_i
have different mathematical signs, the result has a negative mathematical sign, unless the result isNaN
. If the result isNaN
, the “sign” ofNaN
is implementation-defined.If
x1_i
is either+infinity
or-infinity
andx2_i
is either+infinity
or-infinity
, the result is a signed infinity with the mathematical sign determined by the rule already stated above.If
x1_i
is either+infinity
or-infinity
andx2_i
is a nonzero finite number, the result is a signed infinity with the mathematical sign determined by the rule already stated above.If
x1_i
is a nonzero finite number andx2_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
norNaN
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 aninfinity
of appropriate mathematical sign. If the magnitude is too small to represent, the result is a zero of appropriate mathematical sign.
For complex floating-point operands, multiplication is defined according to the following table. For real components
a
andc
and imaginary componentsb
andd
,c
dj
c + dj
a
a * c
(a*d)j
(a*c) + (a*d)j
bj
(b*c)j
-(b*d)
-(b*d) + (b*c)j
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
, ord
are all finite numbers (i.e., a value other thanNaN
,+infinity
, or-infinity
), multiplication of complex floating-point operands should be computed as if calculated according to the textbook formula for complex number multiplication\[(a + bj) \cdot (c + dj) = (ac - bd) + (bc + ad)j\]When at least one of
a
,b
,c
, ord
isNaN
,+infinity
, or-infinity
,If
a
,b
,c
, andd
are 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 multiplication. 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 multiplication 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.