Einsum
Shippingtensor_einsumEinstein summation over up to 3 operands via a subscripts string (matmul, trace, transpose, batched)
Signature
Inputs
aSignal|Arrayrequired— First operand (required).bSignal|Array— Second operand (optional) — wire when the subscripts reference a second term.cSignal|Array— Third operand (optional).
Outputs
resultSignal|Array— The result of evaluating the subscripts expression; unit = product of the wired operands' units.
Parameters
| Key | Type | Default | Notes |
|---|---|---|---|
subscripts | text | ij,jk->ik | numpy einsum subscripts, e.g. "ij,jk->ik" (matmul), "ii->i" (diagonal), "ij->ji" (transpose), "ij->" (sum). Implicit output allowed; ellipsis "..." is not. |
Description
Einsum evaluates an Einstein-summation subscripts expression over one to three operands (a required, b and c optional), giving a single node that expresses an enormous range of linear-algebra operations by naming indices.
Repeated index labels are summed (contracted); labels that survive to the right of -> form the output. Classic patterns include "ij,jk->ik" (matrix multiply, the default), "ii->i" (extract the diagonal), "ii->" (trace), "ij->ji" (transpose), "ij->" (sum all elements), and "bij,bjk->bik" (batched matmul over a leading b axis). Implicit output is supported: omitting -> (e.g. "ij,jk") infers the output subscripts (here "ik") by numpy's rule. Ellipsis `...` is not supported.
Unit handling matches the operation: every output term is a product of one element from each operand, so the result unit is the running product of the wired operands' units — while a single-operand op (diagonal, transpose, trace) simply preserves its input's unit. Einsum is stateless.
Mathematics
Examples
Diagonal of a matrix
Wire a matrix into a, leave b/c unwired, and set subscripts = ii->i. For the result is the vector — the diagonal. Change it to ii-> to get the scalar trace instead.
Batched matrix multiply
Feed two rank-3 tensors of shape and and set subscripts = bij,bjk->bik. Einsum performs independent matrix products in one node, contracting j and carrying the batch axis b through untouched.
Applications
- Concisely expressing matmul, trace, transpose, diagonal, and outer products from a single index string.
- Batched linear algebra over a leading axis without writing explicit loops.
- Custom multi-operand contractions in tensor-network, physics, and ML-style computations.
- Reductions and re-orderings (sum over an axis, permute indices) that would otherwise need several separate nodes.
Neat
Operand units are folded in the exact order the ports are walked, so the result unit is a faithful product of only the *wired* inputs — an unwired `b`/`c` contributes nothing.
Single-operand expressions (diagonal, transpose, trace) preserve the input's unit rather than squaring it, matching the physical meaning of those operations.
Implicit-output mode (omitting `->`) infers the free indices numpy-style, so `"ij,jk"` just works as matmul without spelling out `->ik`.
Known issues
Ellipsis broadcasting (`...`) is not implemented — batch axes must be named explicitly (e.g. `b`) in the subscripts string.
A malformed subscripts string, or one whose index sizes disagree across operands, raises a backend error rather than silently truncating.