Linear algebra
Compute dot products, outer products, and matrix multiplication with compact subscript expressions.
NumPy · Function
Evaluates Einstein summation expressions over array-like operands. Repeated labels contract dimensions, while explicit output labels control the result’s dimensions and order.
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.
numpy.einsum(*subscripts, **operands, out=None, dtype=None, order='K', casting='safe', optimize=False)| Name | Type | Description |
|---|---|---|
subscripts | str | Subscript specification as a comma-separated list of labels, optionally followed by -> and output labels. |
operands | list of array_like | Arrays participating in the operation. |
out | ndarray, optional | Destination ndarray for the calculation. |
dtype | data-type, optional | Data type used for the calculation. |
order | {'C', 'F', 'A', 'K'}, optional | Memory layout of the output. |
casting | {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional | Casting rule for operands and output. |
optimize | bool or str, optional | Whether and how to optimize contraction order. |
An ndarray containing the evaluated operation; scalar results are returned as zero-dimensional arrays.
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.
Compute dot products, outer products, and matrix multiplication with compact subscript expressions.
Contract selected axes of multidimensional arrays without manually reshaping or looping.
Use explicit labels to transpose or rearrange dimensions while applying reductions.
Optimize chained contractions by selecting a lower-cost intermediate evaluation order.
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 ->.
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'.
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.innernumpy.outernumpy.tensordotnumpy.einsum_path