Element-wise numeric transformations
Compute products between arrays or between an array and a scalar.
NumPy · Function
Multiplies two inputs element by element, applying NumPy broadcasting when needed. It returns the product, with optional controls for output storage, selection, casting, dtype, layout, and subclasses.
np.multiply applies multiplication independently to corresponding elements of two inputs, using NumPy broadcasting when their shapes differ. Optional controls can direct output storage, select positions to calculate, constrain casting, choose an output dtype, and preserve subclasses.
numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature]) = <ufunc 'multiply'>| Name | Type | Description |
|---|---|---|
x1, x2 | array_like | Input arrays to be multiplied. |
out | ndarray, None, or tuple of ndarray and None, optional | Destination for the result; it may be an ndarray, None, or a tuple of ndarray and None. |
where | array_like of bool, optional | Boolean array or broadcastable condition selecting where computation occurs. |
casting | {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional | Controls the casting policy used during computation. |
order | {‘K’, ‘A’, ‘C’, ‘F’, ‘C’}, optional | Memory-layout order for the result. |
dtype | dtype, optional | Output data type. |
subok | bool, optional | If true, permits subclasses of ndarray to pass through. |
signature | str, tuple of ints, or None, optional | Generalized-ufunc signature, when applicable. |
An ndarray containing the element-wise product; when both operands are scalars, the result is a scalar.
import numpy as np
x = np.arange(9.0).reshape(3, 3)
y = np.arange(3.0)
result = np.multiply(x, y)
print(result)
The one-dimensional operand broadcasts across the rows, so each element is multiplied by the corresponding value in y.
Compute products between arrays or between an array and a scalar.
Combine operands whose dimensions are broadcast-compatible.
Write products into preallocated storage.
Calculate only selected positions with a boolean condition.
It multiplies two array-like inputs element by element and returns their product. If both inputs are scalar, the result is a scalar.
Follow-up: How can differently shaped inputs work? NumPy applies array broadcasting, provided the shapes are broadcast-compatible.
x1 and x2 are the input arrays. out can receive the result, while where controls which locations are evaluated; casting, order, dtype, and subok configure execution and output behavior.
Follow-up: What happens with masked positions when using an uninitialized out array? Positions where where is false are not written, so initialize out when those values matter.
For ndarray inputs, np.multiply follows broadcasting and is equivalent to element-wise multiplication with the * operator. The explicit ufunc form is useful when selecting an output buffer or using where.
Follow-up: When would explicit output storage be useful? It can reduce allocations and permit writing results into a preallocated compatible array.
numpy.addnumpy.subtractnumpy.dividenumpy.powernumpy.multiply.accumulatenumpy.multiply.reduce