Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.multiply

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.

How it works

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.

Syntax

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

Parameters

NameTypeDescription
x1, x2array_likeInput arrays to be multiplied.
outndarray, None, or tuple of ndarray and None, optionalDestination for the result; it may be an ndarray, None, or a tuple of ndarray and None.
wherearray_like of bool, optionalBoolean array or broadcastable condition selecting where computation occurs.
casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optionalControls the casting policy used during computation.
order{‘K’, ‘A’, ‘C’, ‘F’, ‘C’}, optionalMemory-layout order for the result.
dtypedtype, optionalOutput data type.
subokbool, optionalIf true, permits subclasses of ndarray to pass through.
signaturestr, tuple of ints, or None, optionalGeneralized-ufunc signature, when applicable.

Returns

An ndarray containing the element-wise product; when both operands are scalars, the result is a scalar.

Key points

  • The function performs element-wise multiplication rather than matrix multiplication.
  • x1 and x2 are array-like operands supplied to the operation.
  • The optional out argument can store results in a supplied output array.
  • For broadcasting behavior, np.multiply matches multiplying the operands with *.
  • The returned value contains the element-wise product.
  • Scalar inputs produce a scalar result instead of an ndarray.

Common mistakes

  • For ndarray operands, ordinary * is an equivalent shorthand; use np.multiply when you need its ufunc options.

Example: Element-wise multiplication with broadcasting

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.

When to use it

Element-wise numeric transformations

Compute products between arrays or between an array and a scalar.

Broadcasted scaling

Combine operands whose dimensions are broadcast-compatible.

Allocation-aware array processing

Write products into preallocated storage.

Conditional multiplication

Calculate only selected positions with a boolean condition.

Interview questions

QWhat does np.multiply compute?

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.

QWhich important optional arguments does np.multiply provide?

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.

QHow does np.multiply relate to the * operator?

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.add
  • numpy.subtract
  • numpy.divide
  • numpy.power
  • numpy.multiply.accumulate
  • numpy.multiply.reduce

Source: numpy.multiply. Checked 2026-10-15.