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?
Expand
on the expression, which makes it work. Alternatively, doaSum := expr_. d[a, i_]:> (Expand@expr /.a->i)
. For the one that bothers you, I recommendTrace
ing the evaluation. What happens is that it looks at the inside ofd[a, i]^2
first, sod[a, i]
matchesexpr_. d[a, i]
, which gets replaced with1 /. a -> i
, which evaluates to1
, so that your final expression isf[a]
. You might want to consider adding replacement rules for powers ofd
's. I will do this later in the day, but right now I don't have time. $\endgroup$Expand
and to add additional ruled[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. $\endgroup$