# multiply#

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

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

Note

Floating-point multiplication is not always associative due to finite precision.

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 +0 or -0, the result is NaN.

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

• If x1_i and x2_i have the same mathematical sign, the result has a positive mathematical sign, unless the result is NaN. If the result is NaN, the “sign” of NaN is implementation-defined.

• If x1_i and x2_i have different mathematical signs, the result has a negative mathematical sign, unless the result is NaN. If the result is NaN, the “sign” of NaN is implementation-defined.

• If x1_i is either +infinity or -infinity and x2_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 and x2_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 and x2_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 nor NaN 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 an infinity 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 and c and imaginary components b and d,

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, or d are all finite numbers (i.e., a value other than NaN, +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, 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 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.

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

• x2 (Union[float, Array, NativeArray]) – second input array. Must be compatible with x1 (see :ref’broadcasting). Should have a numeric data type

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

This function 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.

Return type:

Array

Returns:

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

Examples

With ivy.Array inputs:

>>> x1 = ivy.array([3., 5., 7.])
>>> x2 = ivy.array([4., 6., 8.])
>>> y = ivy.multiply(x1, x2)
>>> print(y)
ivy.array([12., 30., 56.])


With ivy.NativeArray inputs:

>>> x1 = ivy.native_array([1., 3., 9.])
>>> x2 = ivy.native_array([4., 7.2, 1.])
>>> y = ivy.multiply(x1, x2)
>>> print(y)
ivy.array([ 4. , 21.6,  9. ])


With mixed ivy.Array and ivy.NativeArray inputs:

>>> x1 = ivy.array([8., 6., 7.])
>>> x2 = ivy.native_array([1., 2., 3.])
>>> y = ivy.multiply(x1, x2)
>>> print(y)
ivy.array([ 8., 12., 21.])


With ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.array([12.,4.,6.]), b=ivy.array([3.,1.,5.]))
>>> x2 = ivy.Container(a=ivy.array([1.,3.,4.]), b=ivy.array([3.,3.,2.]))
>>> y = ivy.multiply(x1, x2)
>>> print(y)
{
a: ivy.array([12.,12.,24.]),
b: ivy.array([9.,3.,10.])
}


With mixed ivy.Container and ivy.Array inputs:

>>> x1 = ivy.Container(a=ivy.array([3., 4., 5.]), b=ivy.array([2., 2., 1.]))
>>> x2 = ivy.array([1.,2.,3.])
>>> y = ivy.multiply(x1, x2)
>>> print(y)
{
a: ivy.array([3.,8.,15.]),
b: ivy.array([2.,4.,3.])
}

Array.multiply(self, x2, /, *, out=None)[source]#

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

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

• x2 (Union[Array, NativeArray]) – second input array. Must be compatible with the first input array. (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 products. The returned array must have a data type determined by type-promotion.

Examples

With ivy.Array inputs:

>>> x1 = ivy.array([3., 5., 7.])
>>> x2 = ivy.array([4., 6., 8.])
>>> y = x1.multiply(x2)
>>> print(y)
ivy.array([12., 30., 56.])


With mixed ivy.Array and ivy.NativeArray inputs:

>>> x1 = ivy.array([8., 6., 7.])
>>> x2 = ivy.native_array([1., 2., 3.])
>>> y = x1.multiply(x2)
>>> print(y)
ivy.array([ 8., 12., 21.])

Container.multiply(self, x2, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
• self (Container) – first input array or container. Should have a numeric data type.

• x2 (Union[Container, Array, NativeArray]) – second input array or container. Must be compatible with self (see broadcasting). Should have a nuneric 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 products. The returned container must have a data type determined by type-promotion.

Examples

With ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.array([15., 4.5, 6.5]),                               b=ivy.array([3.2, 5., 7.5]))
>>> x2 = ivy.Container(a=ivy.array([1.7, 2.8, 3.]),                               b=ivy.array([5.6, 1.2, 4.2]))
>>> y = ivy.Container.multiply(x1, x2)
>>> print(y)
{
a: ivy.array([25.5, 12.6, 19.5]),
b: ivy.array([17.9, 6., 31.5])
}


With mixed ivy.Container and ivy.Array inputs:

>>> x1 = ivy.Container(a=ivy.array([6.2, 4.8, 2.3]),                               b=ivy.array([5., 1.7, 0.1]))
>>> x2 = ivy.array([8.3, 3.2, 6.5])
>>> y = ivy.Container.multiply(x1, x2)
>>> print(y)
{
a: ivy.array([51.5, 15.4, 14.9]),
b: ivy.array([41.5, 5.44, 0.65])
}