Function Repository Resource:

TensorDiagram

Source Notebook

Display symbolic tensor expressions as a diagram

Contributed by: Nikolay Murzin

ResourceFunction["TensorDiagram"][expr]

gives a graphical representation of a symbolic tensor expression.

Examples

Basic Examples (6) 

Display a rank-3 tensor diagram with indices shown as graph edges:

In[1]:=
ResourceFunction["TensorDiagram"][
 Array[Subscript[a, ##] &, {2, 2, 2}]]
Out[1]=

Given tensors of various rank:

In[2]:=
$Assumptions = s \[Element] Arrays[{}] && v \[Element] Vectors[n] && M \[Element] Matrices[{m, n}] && T \[Element] Arrays[{l, l, n}];

Display them:

In[3]:=
ResourceFunction["TensorDiagram"] /@ {s, v, M, T}
Out[3]=

Contract two indices of a 3-rank tensor:

In[4]:=
ResourceFunction["TensorDiagram"][TensorContract[T, {{1, 3}}]]
Out[4]=

Tensor product:

In[5]:=
ResourceFunction["TensorDiagram"][TensorProduct[s, v, M, T, T], VertexSize -> Tiny]
Out[5]=

Contract tensors:

In[6]:=
ResourceFunction["TensorDiagram"][TensorContract[TensorContract[
TensorProduct[s, v, M, T, T], {}], {{2, 4}}], VertexSize -> Tiny]
Out[6]=

Contract multiple indices:

In[7]:=
ResourceFunction["TensorDiagram"][TensorContract[TensorContract[
TensorProduct[s, v, M, T, T], 
Join[{}, {{2, 4}}]], {{6, 8}, {3, 7}}], VertexSize -> 0.03]
Out[7]=

Scope (3) 

Numeric matrix:

In[8]:=
ResourceFunction["TensorDiagram"][{{1, 2}, {3, 4}}]
Out[8]=

Numeric rank-4 tensor:

In[9]:=
ResourceFunction["TensorDiagram"][RandomInteger[10, {3, 3, 3, 3}]]
Out[9]=

Contraction of a numeric matrix:

In[10]:=
ResourceFunction["TensorDiagram"][
 TensorContract[{{1, 2}, {3, 4}}, {{1, 2}}]]
Out[10]=

Neat Examples (2) 

Multiply 10 matrices:

In[11]:=
Assuming[Table[a[i] \[Element] Arrays[{2, 2}], {i, 10}],
 ResourceFunction["TensorDiagram"][
  TensorContract[TensorProduct @@ Table[a[i], {i, 10}], Partition[Range[2, 19], 2]], VertexSize -> 0.02]
 ]
Out[11]=

Make a ladder, contracting 3-rank tensors in series:

In[12]:=
Assuming[
 Join[Table[a[i] \[Element] Arrays[{2, 2, 2}], {i, 10}], Table[b[i] \[Element] Arrays[{2, 2, 2}], {i, 10}]],
 ResourceFunction["TensorDiagram"][
  TensorContract[
   TensorProduct @@ Join[Table[a[i], {i, 10}], Table[b[i], {i, 10}]], Join[Table[{i, i + 1}, {i, 3, 27, 3}], Table[{i, i + 1}, {i, 33, 57, 3}], Table[{i, 30 + i}, {i, 2, 29, 3}]]], VertexSize -> 0.02]
 ]
Out[12]=

Publisher

N. Murzin

Version History

  • 1.0.0 – 03 August 2020

Related Resources

License Information