# Simplifying an expression with many Kronecker deltas

I have an expression with lots of Kronecker deltas, with terms like: $$\delta_{1,a}v_1+\delta_{2,a}v_2+\delta_{3,a}v_3+\delta_{4,a}v_4$$ where $$v$$ is some four-component vector and the indices $$a,b$$, etc. can take values from 1 to 4.

I want the terms to simplify as follows:

• $$\delta_{1,a}v_1+\delta_{2,a}v_2+\delta_{3,a}v_3+\delta_{4,a}v_4 \longrightarrow v_a$$

• $$\delta_{1,a}v_1^n+\delta_{2,a}v_2^n+\delta_{3,a}v_3^n+\delta_{4,a}v_4^n\longrightarrow v_a^n$$

• $$\delta_{1,a}\delta_{1,b}+\delta_{1,a}\delta_{1,b}+\delta_{1,a}\delta_{1,b}+\delta_{1,a}\delta_{1,b}\longrightarrow \delta_{a,b}$$

• $$\delta_{1,a}+\delta_{2,a}+\delta_{3,a}+\delta_{4,a}\longrightarrow 1$$

• $$\delta_{a,b}\delta_{a,c}\delta_{a,d}+\delta_{a,b}\delta_{b,c}\delta_{c,d}+\delta_{a,b}\delta_{a,c}\delta_{a,d}\delta_{b,d}\longrightarrow 3\delta_{a,b}\delta_{b,c}\delta_{c,d}$$

Currently I'm manually replacing all of these terms, which is messy and takes a while as my expression is very large. Is there a way to tell Mathematica that all indices are in this range 1 to 4, so that this expression can be automatically simplified?

Example: Suppose I am differentiating an expression like $$\frac{1}{k^2-m^2}$$ with respect to $$k_a$$.

k /: D[k[i_], k[j_], NonConstants -> {k}] := KroneckerDelta[i, j]
expr = 1/(k[1]^2 + k[2]^2 + k[3]^2 + k[4]^2 - m^2);
diff=D[expr, k[a], NonConstants -> k]

Result: $$-\frac{2k[1]\delta_{1,a}+2k[2]\delta_{2,a}+2k[3]\delta_{3,a}+2k[4]\delta_{4,a}}{(-m^2+k[1]^2+k[2]^2+k[3]^2+k[4]^2)^2}$$ I am currently doing (for instance)

Expand[
diff/.k[4] KroneckerDelta[4, a] ->
k[a] - k[3] KroneckerDelta[3, a] - k[2] KroneckerDelta[2, a] -
k[1] KroneckerDelta[1, a]
]

which gives $$-\frac{2k[a]}{(-m^2+k[1]^2+k[2]^2+k[3]^2+k[4]^2)^2}.$$ This is fine for a simple expression, but for more complicated cases I need to apply substitutions for the one above for several indices or vectors. Other times the result will have products of multiple Kronecker deltas (see the last bullet) and doing this by hand for all permutations of indices seems needlessly complex. I don't particularly care about the notation used and am happy to represent my vectors in a different way if this makes the simplification of the deltas easier to implement.

Another example:

explicitIndexExpression = k[1]^2 k[2]^3 k[3]^2 k[4]
newExpression = KroneckerDelta[a,b] KroneckerDelta[b,c](1-KroneckerDelta[c,d])*explicitIndexExpression/.{k[1]-> a, k[2]-> b,k[3]-> c,k[4]-> d}

Result: $$\delta_{a,b}\delta_{b,c}(1-\delta_{c,d}) k[a]^2 k[b]^3 k[c]^2 k[d]$$

I have lots of terms like this with various permutations of indices, but Simplify[...] only gives a result with various cases, e.g.

[something]        a==b && a==c && a==d
[something else]   a==b && a!=c && a==d
....
• We need your code---suitably reduced to a minimal example---because there are numerous ways to implement this algebra, and we don't know which way you chose. Please edit your post to include that information! – march Jan 22 '20 at 1:03
• @march I have added an example but am not tied to any particular implementation. What I really want is to be able to write, for example, simplifyKronecker[KroneckerDelta[a,b]KroneckerDelta[b,c]KroneckerDelta[c,d] + KroneckerDelta[a,b]KroneckerDelta[a,c]KroneckerDelta[a,d]] and get out the result 2 KroneckerDelta[a,b]KroneckerDelta[b,c]KroneckerDelta[c,d]. – user366202 Jan 22 '20 at 1:46
• Should the third bullet be $\delta _{1,a} \delta _{1,b}+\delta _{2,a} \delta _{2,b}+\delta _{3,a} \delta _{3,b}+\delta _{4,a} \delta _{4,b} = \delta_{a,b}$? – NonDairyNeutrino Jan 22 '20 at 5:46

Maybe using my answer to Cartesian tensor gradient will suffice:

k /: D[k, k[α_], NonConstants->{k}] := k[α]/k
k /: D[k[α_], k[β_], NonConstants->{k}] := KroneckerDelta[α,β]

k /: MakeBoxes[k[α_], fmt_] := MakeBoxes[Subscript[k,α],fmt]

Couple examples:

D[1/(k^2-m^2), k[a], NonConstants->{k}] //TeXForm

$$-\frac{2 k_a}{\left(k^2-m^2\right)^2}$$

D[1/(k^2-m^2), k[a], k[b], NonConstants->{k}] //TeXForm

$$\frac{8 k_a k_b}{\left(k^2-m^2\right)^3}-\frac{2 \delta _{a,b}}{\left(k^2-m^2\right)^2}$$

• This is a nice solution for the simple example in my question, but unfortunately I have a lot of expressions which explicitly have lots of Kronecker deltas which have to be removed, so I need a solution which simplifies Kronecker deltas rather than preventing them occurring. – user366202 Jan 22 '20 at 4:50