Repository navigation
Repeated Indices
In numpy and in Summa an index can appear any number of times: twice in one tensor (a diagonal, a trace), once (a sum), in two tensors (a bond), or in three tensors and more (a hyperedge, a batch index). Indices repeated more than twice are the reason Summa exists: you write the formula, and the package finds how to compute it. Every block on this page runs after
Needs["Summa`"]An index repeated in one tensor takes its diagonal; summed, the trace:
m = {{a, b}, {c, d}};
{Einsum["ii->i", m], Einsum["ii->", m]}At any rank, and in any number of its levels: the diagonal of a tensor of rank three, and the trace over its first two indices:
t = Array[x, {2, 2, 2}];
Einsum["iii->i", t]{x[1, 1, 1], x[2, 2, 2]}
Einsum["iij->j", t]{x[1, 1, 1] + x[2, 2, 1], x[1, 1, 2] + x[2, 2, 2]}
An index in one tensor only, and not in the result, is summed: the sums of the rows, of the columns, of everything.
{Einsum["ij->i", m], Einsum["ij->j", m], Einsum["ij->", m]}An index in two tensors is summed if it does not reach the result, and kept otherwise: the scalar product of two matrices, and the scalar products of their rows.
n = {{p, q}, {r, s}};
{Einsum["ij,ij->", m, n], Einsum["ij,ij->i", m, n]}An index can be shared by any number of tensors, here three vectors,
Einsum["i,i,i->", {a, b}, {c, d}, {e, f}]and, kept in the result, their product element by element:
Einsum["i,i,i->i", {a, b}, {c, d}, {e, f}]In a drawing of the network such an index is a hyperedge, a dot joined to all the tensors that share it (Diagrams).
An index in two tensors and in the result is a batch index: one contraction for each of its values. A thousand products of 3×3 matrices:
SeedRandom[1]; {x1, x2} = RandomReal[1, {2, 1000, 3, 3}];
Einsum["bij,bjk->bik", x1, x2] == MapThread[Dot, {x1, x2}]True
and a quadratic form for every row of a matrix,
SeedRandom[2]; {rows, g} = {RandomReal[1, {1000, 3}], RandomReal[1, {3, 3}]};
Einsum["bi,ij,bj->b", rows, g, rows] == (# . g . # & /@ rows)True
A tensor of rank three in its canonical (CP) decomposition, i joins three matrices:
SeedRandom[3]; {u, v, w} = RandomReal[1, {3, 4, 2}];
Einsum["ai,bi,ci->abc", u, v, w] == Table[Sum[u[[a, i]] v[[b, i]] w[[c, i]], {i, 2}], {a, 4}, {b, 4}, {c, 4}]True
EinsumDiagram["ai,bi,ci->abc"]
EinsumPath shows how it is computed, for matrices of 40×8:
EinsumPath["ai,bi,ci->abc", {{40, 8}, {40, 8}, {40, 8}}]["Contractions"] // Columnai,ci->iac
iac,bi->bac
First u and w are joined for every value of i (a product batched over i, which v still needs), then the
product with v sums i, and a transpose puts the result in the order abc: the hyperedge is a batch index in the
first step, a summed index in the last.
TensorContract can sum over three slots and more, on the outer product of the tensors:
{Einsum["i,i,i->", {a, b}, {c, d}, {e, f}], TensorContract[TensorProduct[{a, b}, {c, d}, {e, f}], {{1, 2, 3}}]}but it builds the whole outer product first: for "ai,bi,ci->abc" with three 40×8 matrices that is
Einsum
(Mathematica 15, one machine). It cannot keep a shared index in the result, as a batch index. The resource function
EinsteinSummation refuses an index that appears three times ({{i}, {i}, {i}} -> {}) and a batch index kept in the
result ({{k, a, b}, {k, b, c}} -> {k, a, c}).
An index cannot appear twice in the result ("i->ii" gives Einsum::outrep, as in numpy): to put a vector on a
diagonal, see the FAQ.
Start
Recipes
Reference
Documents