TensorContract[tensor,{{s11,s12},{s21,s22},…}]
yields the contraction of tensor in the pairs {si1,si2} of slots.
TensorContract
TensorContract[tensor,{{s11,s12},{s21,s22},…}]
yields the contraction of tensor in the pairs {si1,si2} of slots.
Details
- The first argument of TensorContract[tensor,pairs] can be any tensorial object, in particular explicit or symbolic arrays, or combinations like tensor products, transpositions, etc.
- The slots sij must all be different positive integers, not larger than the rank of the contracted tensor.
- The slots contracted in each pair must all have the same dimensions, but different pairs might be associated to different dimensions.
- For a symbolic tensor in the first argument of TensorContract, the contractions in the second argument are sorted, after sorting the slots in each contraction.
- TensorContract[tensor,{}] returns tensor.
Examples
open all close allBasic Examples (3)
Contract slots 2 and 3 of a rank-3 array:
TensorContract[{{{Subscript[a, 1, 1, 1], Subscript[a, 1, 1, 2]}, {Subscript[a, 1, 2, 1], Subscript[a, 1, 2, 2]}}, {{Subscript[a, 2, 1, 1], Subscript[a, 2, 1, 2]}, {Subscript[a, 2, 2, 1], Subscript[a, 2, 2, 2]}}}, {{2, 3}}]Perform several contractions in an array:
A = Array[Subscript[a, ##]&, {2, 3, 2, 3}];TensorContract[A, {{1, 3}, {2, 4}}]$Assumptions = A∈Arrays[{3, 2, 3}];Slots 1 and 3 can be contracted:
TensorContract[A, {{1, 3}}]Slots 1 and 2 cannot be contracted:
TensorContract[A, {{1, 2}}]Scope (3)
A1 = {{{a, b}, {b, c}}, {{f, g}, {h, i}}};
TensorContract[A1, {{2, 3}}]A2 = SparseArray[A1];
TensorContract[A2, {{2, 3}}]A3 = SymmetrizedArray[A1];
TensorContract[A3, {{2, 3}}]Contraction of symbolic arrays:
$Assumptions = A∈Arrays[{d1, d1, d2, d2}];TensorContract[A, {{3, 4}, {2, 1}}]TensorDimensions[%]Report invalid contractions for symbolic arrays:
TensorContract[A, {{1, 3}}]Combining successive contractions:
TensorContract[TensorContract[A, {{1, 3}}], {{1, 2}}]Generalizations & Extensions (1)
With arrays it is possible to use contractions of more than two slots, using the function SymbolicTensors`ArrayContract:
A = Array[Subscript[a, ##]&, {3, 2, 3, 2, 3}];SymbolicTensors`ArrayContract[A, {{1, 3, 5}}]It is also possible to specify an arbitrary head, generalizing the role of Plus:
SymbolicTensors`ArrayContract[A, {{1, 3, 5}}, g]The contraction can be performed in such a way that levels stay inside the contraction head g:
SymbolicTensors`ArrayContract[A, {{2, 4}}, g, 4]Properties & Relations (8)
Contraction of the first levels of an array is equivalent to Tr:
A = Array[a, {3, 3, 3}];TensorContract[A, {{1, 2}}] === Tr[A, Plus, 2]Contraction of a tensor product of arrays is equivalent to Dot and Inner:
A = Array[a, {2, 2, 2}];
B = Array[b, {2, 2}];
F = Array[f, {2, 3, 2}];Inner performs one contraction between two arrays:
TensorContract[AB, {{3, 4}}] === Inner[Times, A, B]Dot with
arguments performs
contractions among them:
TensorContract[ABF, {{3, 4}, {5, 6}}] === Expand@Dot[A, B, F]Arbitrary contractions can be also performed with a combination of Transpose and Apply:
A = Array[a, {2, 3, 2, 3, 2}];TensorContract[A, {{1, 5}, {2, 4}}] === Apply[Plus, Transpose[A, {2, 3, 1, 3, 2}], 2]Contraction of antisymmetric pairs of array levels gives zero:
A = SymmetrizedArray[pos_ :> a@@pos, {3, 3, 3, 3}, {Antisymmetric[{1, 2}], Antisymmetric[{3, 4}]}]TensorContract[A, {{1, 2}}]Normal[%]Since levels one and three are not an antisymmetric pair, the result is not zero:
TensorContract[A, {{1, 3}}]Normal[%]Contraction of antisymmetric symbolic arrays gives zero:
$Assumptions = A∈Arrays[{3, 3, 3, 3}, Complexes, Antisymmetric[{1, 2, 3}]];TensorContract[A, {{1, 3}}]//TensorReduceIn other cases, the expression is canonicalized, moving contractions to the last possible slots:
TensorContract[A, {{2, 4}}]//TensorReduceTensorContract[tensor,{}] returns tensor, irrespectively of what tensor is:
TensorContract[I, {}]TensorContract, in combination with TensorProduct, can be used to implement Dot:
v = Array[x, {3}];
a = Array[y, {3, 3, 3}];
m = Array[z, {3, 3}];v.a.m == TensorContract[vam, {{1, 2}, {4, 5}}]//SimplifySee Also
Tr Transpose Inner TensorProduct Dot ArrayReduce
Function Repository: ArrayContract ArrayContractThread EinsteinSummation FromTensor ToTensor
Tech Notes
Related Guides
History
Text
Wolfram Research (2012), TensorContract, Wolfram Language function, https://reference.wolfram.com/language/ref/TensorContract.html.
CMS
Wolfram Language. 2012. "TensorContract." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/TensorContract.html.
APA
Wolfram Language. (2012). TensorContract. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TensorContract.html
BibTeX
@misc{reference.wolfram_2026_tensorcontract, author="Wolfram Research", title="{TensorContract}", year="2012", howpublished="\url{https://reference.wolfram.com/language/ref/TensorContract.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_tensorcontract, organization={Wolfram Research}, title={TensorContract}, year={2012}, url={https://reference.wolfram.com/language/ref/TensorContract.html}, note=[Accessed: 13-June-2026]}