# Replacement rule for summation with Kronecker delta

I want to perform some symbolic computations in index notation, without explicit reference to sums, so that in expressions of the type f[i,j]g[j,k] summation over j is implied. I often encounter the need to use Kronecker delta-symbol. Denote our implementation of the delta-symbol by d[i,j]. Its defining property for our purposes is that d[i,j]f[i] equals f[j] for any function f of i. Assume that in the computations only one dummy variable appears which I call 'a'. Then, the replacement pattern that comes to my mind is

aSum := expr_. d[a, i_]:> (expr /.a->i)


This works for some cases, for example

{1, d[a,i], f[a,j,k,l]d[a,i]}/.aSum


returns

{1, 1, f[i, j, k, l]}

but also fails sometimes. The case that bothers me most, practically is the following

f[a]d[a,i]^2 /.aSum


returns

f[a]

instead of f[i]d[i,i] as I would expect. Also, my replacement rule only works fine with monomials in d, for instance

f[a] (d[a, i] + d[a, j]) /. aSum


returns

2 f[a]

instead of f[i] + f[j] that I look for.

What is the proper way to fix this behaviour?

• As for the last one, you can always use Expand on the expression, which makes it work. Alternatively, do aSum := expr_. d[a, i_]:> (Expand@expr /.a->i). For the one that bothers you, I recommend Traceing the evaluation. What happens is that it looks at the inside of d[a, i]^2 first, so d[a, i] matches expr_. d[a, i], which gets replaced with 1 /. a -> i, which evaluates to 1, so that your final expression is f[a]. You might want to consider adding replacement rules for powers of d's. I will do this later in the day, but right now I don't have time. – march Apr 18 '16 at 17:37
• Actually what you've suggested I am already using in my code. For my purposes it seems enough to use Expand and to add additional rule d[i_, j_]^n_:>d[i, j] to work with powers. I was interested whether there is a nicer way. However, after some thinking, these two steps seem necessary. Maybe the question is not posed well enough or does not have a deeper layer. In case you write an answer I will probably mark it as accepted though. – Weather Report Apr 18 '16 at 18:38