fn

einsum

Tensor
einsum(equation: str, operands: Tensor = ())
source

Evaluate an Einstein-summation expression over the given tensors.

Implements the general tensor contraction operator: each input is indexed by labels declared in equation and the output retains only the labels appearing on the right-hand side. Repeated labels are summed; matching labels broadcast.

Parameters

equationstr
Einsum equation of the form "<lhs1>,<lhs2>,... -> <rhs>".
*operandsTensor= ()
Tensors corresponding to the comma-separated factors on the left-hand side.

Returns

Tensor

Tensor whose axes correspond to the labels on the right-hand side of equation.

Notes

Matrix multiplication is the canonical example: "ij,jk->ik" realises

Cik=jAijBjk.C_{ik} = \sum_{j} A_{ij} \, B_{jk}.

More generally, einsum expresses traces ("ii->"), batched contractions ("bij,bjk->bik"), and outer products ("i,j->ij") within one consistent notation.

Examples

>>> import lucid
>>> a = lucid.randn(2, 3)
>>> b = lucid.randn(3, 4)
>>> lucid.einops.einsum("ij,jk->ik", a, b).shape
(2, 4)