How do I let mathematica compute a tensor contraction like


with an output



I tried TensorContract and TensorReduce but they were not helpful.

Thanks for your help!


2 Answers 2


A straightforward way to implement Kronecker Delta is as follows:

SetAttributes[\[Delta], Orderless];
\[Delta][a_, b_] f_[c___, b_, d__] ^:= f[c, a, d] /; ! NumericQ[b];
\[Delta][a_, a_] := dim /; ! NumericQ[a];
\[Delta][a_, b_] := Boole[a == b] /; NumericQ[a] && NumericQ[b];
Format[\[Delta][a_, b_]] := Subscript[\[Delta], a, b];

where dim is the dimension of the vector space. We see that it satisfies required conditions:

In[1]:= {\[Delta][a, b] \[Delta][b, d], \[Delta][a, b] f[b, c], \[Delta][a, b] == \[Delta][b, a], \[Delta][1, 2], \[Delta][1, 1], \[Delta][a, a]}
Out[1]:= {Subscript[\[Delta], a, d], f[a, c], True, 0, 1,dim}

In other words, for the input

$\left\{\delta _{a,b} \delta _{b,d},\delta _{a,b} f(b,c),\delta _{a,b}=\delta _{b,a},\delta _{1,2},\delta _{1,1},\delta _{a,a}\right\}$

we get the expected output:

$\left\{\delta _{a,d},f(a,c),\text{True},0,1,dim\right\}$


Try this:

    Subscript[δ, i_, j_] := KroneckerDelta[i, j];
    ruleDelta = 
      KroneckerDelta[1, i_] KroneckerDelta[1, k_] + 
  KroneckerDelta[2, i_] KroneckerDelta[2, k_] + 
  KroneckerDelta[3, i_] KroneckerDelta[3, 
    k_] -> Subscript[δ, i, k]

On your screen it looks as follows:

enter image description here


Sum[Subscript[δ, i, j]*Subscript[δ, j, k], {j, 1,3}] /. ruleDelta

(* KroneckerDelta[i, k] *)

Looking as

enter image description here

on the screen.

Have fun!

  • $\begingroup$ Thanks! This is useful. However, it does not work if these $\delta$s are multiplied with something else, even with a constant. Is there anything more general available? $\endgroup$
    – Hamurabi
    Commented Feb 26, 2020 at 14:39
  • $\begingroup$ Well, as usually, you may write the rule a bit differently: ruleDelta = a__*KroneckerDelta[1, i_] KroneckerDelta[1, k_] -> a*(Subscript[\[Delta], i, k] - KroneckerDelta[2, i] KroneckerDelta[2, k] - KroneckerDelta[3, i] KroneckerDelta[3, k]) then Sum[b*Subscript[\[Delta], i, j]*Subscript[\[Delta], j, k], {j, 1, 3}] /. ruleDelta // Simplify yields what you expect. $\endgroup$ Commented Feb 26, 2020 at 16:01

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.