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

NumPy · Function

np.einsum

Evaluates Einstein summation expressions over array-like operands. Repeated labels contract dimensions, while explicit output labels control the result’s dimensions and order.

How it works

Pass a subscript expression followed by one or more array-like operands. Labels identify dimensions: repeated labels are contracted by summation, while explicit output labels after -> determine which dimensions remain and their order. Optional arguments control destination storage, dtype, memory order, casting, and contraction optimization.

Syntax

numpy.einsum(*subscripts, **operands, out=None, dtype=None, order='K', casting='safe', optimize=False)

Parameters

NameTypeDescription
subscriptsstrSubscript specification as a comma-separated list of labels, optionally followed by -> and output labels.
operandslist of array_likeArrays participating in the operation.
outndarray, optionalDestination ndarray for the calculation.
dtypedata-type, optionalData type used for the calculation.
order{'C', 'F', 'A', 'K'}, optionalMemory layout of the output.
casting{'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optionalCasting rule for operands and output.
optimizebool or str, optionalWhether and how to optimize contraction order.

Returns

An ndarray containing the evaluated operation; scalar results are returned as zero-dimensional arrays.

Key points

  • einsum expresses multidimensional array operations through Einstein summation notation.
  • Each comma-separated label group describes the dimensions of one corresponding operand.
  • Repeated labels specify contraction over the associated dimension.
  • Use explicit output labels to choose which dimensions remain and their ordering.
  • Labels used once pass through to the output instead of being contracted.
  • The optional out array receives the computed result in place.

Common mistakes

  • Explicit mode requires -> and an output-label list; omitting either makes the expression invalid.
  • A single-use label is retained rather than reduced, and the result can be a view of the input.

Example: Dot product and explicit output

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.einsum('i,i->', a, b))
print(np.einsum('i->i', a))

The first expression computes a dot product; the second explicitly preserves the vector dimension.

When to use it

Linear algebra

Compute dot products, outer products, and matrix multiplication with compact subscript expressions.

Tensor contractions

Contract selected axes of multidimensional arrays without manually reshaping or looping.

Axis manipulation

Use explicit labels to transpose or rearrange dimensions while applying reductions.

Efficient chained operations

Optimize chained contractions by selecting a lower-cost intermediate evaluation order.

Interview questions

QHow does einsum interpret repeated and unrepeated labels?

The subscripts string maps labels to operand dimensions. A label repeated across operands is summed, so 'i,i' performs an inner product; labels appearing once remain in the result unless explicit output labels change the selection.

Follow-up: How would you control output-axis order? Use explicit mode, such as 'ij->ji', which names the output labels after ->.

QWhat distinguishes implicit and explicit mode?

Implicit mode derives the output labels from the expression, while explicit mode specifies them after ->. Explicit mode can therefore force or suppress summation and directly control the output dimensions.

Follow-up: What syntax expresses matrix multiplication? For matrices A and B, use 'ik,kj->ij'.

QWhich optional arguments matter for performance or storage?

The operands are array-like inputs, and out can be supplied as an ndarray receiving the calculation. dtype selects the computation type, order controls output layout, casting sets conversion policy, and optimize controls contraction optimization.

Follow-up: How can contraction order be evaluated separately? Use numpy.einsum_path, which evaluates a low-cost order by considering intermediate arrays.

  • numpy.inner
  • numpy.outer
  • numpy.tensordot
  • numpy.einsum_path

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