I've been looking for a solution to this question for a while. I was wondering if anyone is aware of a way of defining symbolic dummy indices to implement Einstein's convention.

Let me explain with an example. I would like to define, say, an object with one index, defined this way (more or less):


so that, if I call the object multiple times, the variable "something" is always named in a different way. For example, I would like


to return something like


Does anyone know of a way to implement this?


2 Answers 2


I think that this should do the trick.

Module[{idx = 0},
 T[i_] := (idx += 1; a[i, Subscript[s, idx]] b[Subscript[s, idx]])
  • $\begingroup$ This is precisely what I was looking for! Thanks. $\endgroup$
    – Einj
    Commented Sep 1, 2020 at 13:57

You might consider using the symbolic tensor capabilities of Mathematica. For example, define a to be a matrix and b to be a vector:

$Assumptions = a ∈ Matrices[{d,d}] && b ∈ Vectors[d];

Then T is defined as:

T = TensorContract[TensorProduct[a,b],{{2,3}}];

and T.T is:

res = TensorContract[TensorProduct[T, T], {{1, 2}}]

TensorContract[TensorProduct[TensorContract[TensorProduct[a, b], {{2, 3}}], TensorContract[TensorProduct[a, b], {{2, 3}}]], {{1, 2}}]

This can be simplified with TensorReduce:


TensorContract[TensorProduct[a, a, b, b], {{1, 3}, {2, 6}, {4, 5}}]

For you particular example, it is possible to use the ResourceFunctions "ToTensor" and "FromTensor" and avoid TensorContract/TensorProduct:

T = a . b;
TensorReduce[T . T //ResourceFunction["ToTensor"]] //ResourceFunction["FromTensor"]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.