# Tensor contraction

How do I let mathematica compute a tensor contraction like

$$\delta_{ab}\delta_{bc}$$

with an output

$$\delta_{ac}$$

efficiently?

I tried TensorContract and TensorReduce but they were not helpful.

• They are helpful see e.g. Contracting with Levi-Civita (totally antisymmetric) tensor. Commented Feb 26, 2020 at 13:43
• It depends on what you want to do: For numerical tensors Dot[ ] whould be the tool of choice. Commented Feb 26, 2020 at 13:43

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:

Then

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

(* KroneckerDelta[i, k] *)


Looking as

on the screen.

Have fun!

• 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? Commented Feb 26, 2020 at 14:39
• 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. Commented Feb 26, 2020 at 16:01