NumPy multiply()

The numpy.multiply() function performs element-wise multiplication of two input arrays. If the input arrays have different shapes, they must be broadcastable to a common shape.

Syntax

</>
Copy
numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
x1, x2array_likeInput arrays to be multiplied. If they have different shapes, they must be broadcastable.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store the result. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying where to apply the multiplication. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when performing the multiplication.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalSpecifies the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the element-wise product of x1 and x2. If both inputs are scalars, a scalar is returned.


Examples

1. Multiplying Two Arrays Element-wise

In this example, we perform element-wise multiplication on two arrays of the same shape.

</>
Copy
import numpy as np

# Define two arrays of the same shape
x1 = np.array([1, 2, 3, 4])
x2 = np.array([5, 6, 7, 8])

# Perform element-wise multiplication
result = np.multiply(x1, x2)

# Print the result
print("Element-wise multiplication result:", result)

Output:

Element-wise multiplication result: [ 5 12 21 32]

2. Multiplying Arrays with Broadcasting

When the input arrays have different shapes, broadcasting is applied.

</>
Copy
import numpy as np

# Define an array with shape (3,)
x1 = np.array([1, 2, 3])

# Define a scalar (broadcasted to match x1's shape)
x2 = 10

# Perform element-wise multiplication
result = np.multiply(x1, x2)

# Print the result
print("Broadcasted multiplication result:", result)

Output:

Broadcasted multiplication result: [10 20 30]

3. Using the out Parameter

Storing the result in a preallocated output array instead of creating a new one.

</>
Copy
import numpy as np

# Define two arrays
x1 = np.array([2, 4, 6])
x2 = np.array([3, 3, 3])

# Create an output array with the same shape
output_array = np.empty_like(x1)

# Perform multiplication and store result in output_array
np.multiply(x1, x2, out=output_array)

# Print the result
print("Stored result in output array:", output_array)

Output:

Stored result in output array: [ 6 12 18]

4. Using the where Parameter

The where parameter allows conditional multiplication.

</>
Copy
import numpy as np

# Define two arrays
x1 = np.array([2, 4, 6, 8])
x2 = np.array([1, 2, 3, 4])

# Define a mask (multiply only where mask is True)
mask = np.array([True, False, True, False])

# Perform multiplication with condition
result = np.multiply(x1, x2, where=mask)

# Print the result
print("Conditional multiplication result:", result)

Output:

Conditional multiplication result: [ 2  0 18  0]

Only the elements where mask=True are multiplied. The other values remain unchanged.