({{, (RefLink[TensorRank, paclet:ref/TensorRank])[tensor]
gives the rank of tensor.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[TensorRank, paclet:ref/TensorRank]) accepts any type of tensor, either symbolic or explicit, including any type of array.

On explicit rectangular arrays of scalars, (RefLink[TensorRank, paclet:ref/TensorRank]) coincides with (RefLink[ArrayDepth, paclet:ref/ArrayDepth]). On symbolic arrays, (RefLink[TensorRank, paclet:ref/TensorRank]) stays unevaluated unless the array has been assigned a rank through any form of assumption.

OpenersPrimaryExamplesSection(Spacer1)Examples  (10)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Rank of an array:

TensorRank[{{1,3},{2,Pi}}]

Rank of a tensor product of two tensors:

TensorRank[T⊗X]

Rank of a contraction:

TensorRank[TensorContract[T,{{1,3}}]]

OpenersExampleSection1(Spacer1)Scope  (3)

Rank or depth of explicit arrays:

A=Array[a,{2,3,4}];
TensorRank[A]

A=SparseArray[{{1,2,3}->a},{2,3,4}];
TensorRank[A]

A=SymmetrizedArray[pos_:>a,{4,4,4},Symmetric[All]];
TensorRank[A]

Rank of symbolic arrays:

$Assumptions={
A∈Arrays[{4,4,5,5},Reals,Symmetric[{1,2}]],
M∈Matrices[{3,4},Reals],
V∈Vectors[5,Reals]
};

TensorRank[A]

TensorRank[M]

TensorRank[V]

Rank of general tensor expressions:

TensorRank[a⊗b⊗c]

TensorRank[TensorContract[a,{{1,5},{2,3}}]]

TensorRank[TensorTranspose[a,{3,4,1,2}]]

OpenersExampleSection2(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)Assumptions  (1)

Specify locally the domain of a symbolic array:

TensorRank[A⊗A,Assumptions->A∈Arrays[{d,d}]]

OpenersExampleSection3(Spacer1)Properties & Relations  (2)

On explicit arrays, (RefLink[TensorRank, paclet:ref/TensorRank]) coincides with (RefLink[ArrayDepth, paclet:ref/ArrayDepth]):

A=Array[a,{2,3,4}];
{TensorRank[A],ArrayDepth[A]}

A=SparseArray[{{1,2,3}->a},{2,3,4}];
{TensorRank[A],ArrayDepth[A]}

A=SymmetrizedArray[pos_:>a,{4,4,4},Symmetric[All]];
{TensorRank[A],ArrayDepth[A]}

For symbolic expressions, there is no default rank assumed:

TensorRank[A]

Use assumptions to assign a rank to the array:

Assuming[A∈Arrays[{3,4,d,d}],TensorRank[A]]

$Assumptions=A∈Arrays[{3,4,5}];
TensorRank[A]

OpenersExampleSection4(Spacer1)Possible Issues  (3)

(RefLink[TensorRank, paclet:ref/TensorRank]) can obtain some information contextually. Expressions without tensor properties inside numeric functions, arrays, or derivatives are considered scalars.

TensorRank[x]

TensorRank[1/x]

TensorRank[{x}]

Grad[x,{a,b}]

It is not possible to mix incompatible local and global assumptions:

$Assumptions=A∈Arrays[{2,2,2,2}];

Assuming[A∈Arrays[{2,2}],TensorRank[A]]

(RefLink[TensorRank, paclet:ref/TensorRank]) does not check for dimensions homogeneity, only rank homogeneity:

$Assumptions={v∈Vectors[2],w∈Vectors[3]};

TensorRank[v+w]

TensorDimensions[v+w]

This alternative construction would check for dimensions homogeneity:

tensorrank[expr_]:=With[{dims=TensorDimensions[expr]},Length@dims/;ListQ[dims]]

tensorrank[v+w]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ArrayDepth, paclet:ref/ArrayDepth])  ▪  (RefLink[Arrays, paclet:ref/Arrays])  ▪  (RefLink[Matrices, paclet:ref/Matrices])  ▪  (RefLink[Vectors, paclet:ref/Vectors])  ▪  (RefLink[TensorDimensions, paclet:ref/TensorDimensions])  ▪  (RefLink[Dimensions, paclet:ref/Dimensions])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Symbolic Tensors, paclet:tutorial/SymbolicTensors])}, {▪(RefLinkPlain[Tensor Symmetries, paclet:tutorial/TensorSymmetries])}, {▪(RefLinkPlain[Symmetrized Arrays, paclet:tutorial/SymmetrizedArrays])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Symbolic Tensors, paclet:guide/SymbolicTensors])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=TensorRank]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})