Function Repository Resource:

EinsteinSummation

Source Notebook

Given tensors and their indices, sum over repeated indices

Contributed by: Carl Woll

ResourceFunction["EinsteinSummation"][indices,tensors]

sums over repeated indices of the tensors.

ResourceFunction["EinsteinSummation"][indicesout,tensors]

transposes the output if necessary so that the output tensor indices match out.

Details and Options

The indices can be any expression.
ResourceFunction["EinsteinSummation"] accepts any type of tensor, either symbol or explicit, including any type of array.
On explicit rectangular arrays of scalars, ResourceFunction["EinsteinSummation"] will return a rectangular array. On symbolic tensors, ResourceFunction["EinsteinSummation"] will return a TensorContract object, possible wrapped in TensorTranspose.
If assumptions are given for symbolic tensors, then the specified indices must match the rank of the associated symbolic tensor.
Indices repeated more than twice are not supported.
Output indices can be specified, in which case the output tensor indices will be transposed to match the desired output.
If output indices are specified, they must match the tensor rank of the output.

Examples

Basic Examples (4) 

Two explicit matrices:

In[1]:=
SeedRandom[1]
a = RandomReal[1, {3, 3}];
x = RandomReal[1, {3, 3}];

Contract the second indices of the two matrices:

In[2]:=
ResourceFunction["EinsteinSummation"][{{1, 2}, {3, 2}}, {a, x}]
Out[2]=

Check:

In[3]:=
a . x\[Transpose]
Out[3]=

Perform the contraction AikMjk for symbolic tensors A and M:

In[4]:=
ResourceFunction["EinsteinSummation"][{{1, 3}, {2, 3}}, {A, M}]
Out[4]=

Scope (2) 

Perform the contraction AikMjk and transpose the output:

In[5]:=
ResourceFunction[
 "EinsteinSummation"][{{1, 3}, {2, 3}} -> {2, 1}, {A, M}]
Out[5]=

The indices can be any expression:

In[6]:=
ResourceFunction[
 "EinsteinSummation"][{{i, k}, {i, j, k, l}, {j, l}}, {A, M, A}]
Out[6]=

Possible Issues (2) 

The number of index specifications must match the number of tensors:

In[7]:=
ResourceFunction["EinsteinSummation"][{{1, 2}}, {A, M}]
Out[7]=

If $Assumptions is used to define symbolic tensors, the number of indices must match the tensor rank:

In[8]:=
$Assumptions = A \[Element] Arrays[{d, d, d}];
ResourceFunction["EinsteinSummation"][{{1, 2}, {3, 4}}, {A, M}]
Out[9]=

Publisher

Carl Woll

Version History

  • 1.0.0 – 16 October 2019

License Information