# Make Mathematica apply the same rule exhaustively in simplification

Use Case

Mathematica evaluate the partial derivative as:

$$\frac{\partial}{\partial A_{abc}}\sum _{j=1}^J \sum _{k=1}^K \log \left(\sum _{l=1}^L A_{jkl} B_{jkl}\right) = \sum _{j=1}^J \sum _{k=1}^K \frac{\sum _{l=1}^L\delta _{aj} \delta _{bk} \delta _{cl} B_{jkl}}{\sum _{l=1}^L A_{jkl}B_{jkl}}$$

$$\frac{B_{abc}}{\sum _{l=1}^L A_{abl} B_{abl}}$$

For my case, each summation is over all possible values of an index.

The following code gives the result above:

expr = Sum[
Log[Sum[A[j, k, l]*B[j, k, l], {l, 1, L}]], {j, 1, J}, {k, 1, K}];
expr = Simplify[D[expr, A[a, b, c]]]


Current Solution

In my last question, Chris suggested the following rule to simplify the Kronecker deltas.

expr /. Sum[
y_ KroneckerDelta[s_, r_], {s_, 1, p_}] :> (y /. s -> r) /.
Sum[y_ KroneckerDelta[s_, r_] KroneckerDelta[s1_, r1_], {s_, 1,
p_}, {s1_, 1, p1_}] :> (y /. s -> r /. s1 -> r1)


Potential Improvement

However, the rule can be simpler if Mathematica can automatically apply the following rule for multiple times.

expr = expr /. Sum[y_ KroneckerDelta[r_, s_], {s_, 1, p_}, z__] :>
Sum[(y /. s -> r), z] /.
Sum[y_ KroneckerDelta[r_, s_], {s_, 1, p_}] :>
(y /. s -> r)


Question

How to make Mathematica apply the same rule (or same set of rules) for simplification whenever possible?

Can I make a function call SimplifyKroneckerDelta that would apply this rule exhaustively?

Thanks.

Update:

Merely defining the following function leads to infinite loop.

SimplifyKronecker[expr_] = FixedPoint[expr /. Sum[y_ KroneckerDelta[r_, s_], {s_, 1, p_}, z__] :>
Sum[(y /. s -> r), z] /.
Sum[y_ KroneckerDelta[r_, s_], {s_, 1, p_}] :>
(y /. s -> r), expr];

• Maybe you could use FixedPoint for this? – eyorble Nov 21 '18 at 4:38
• Or use a while loop and define a procedure. While expression is same as old expression, apply rule and update old expression. Return expression. But I don't know how to do that yet. – R zu Nov 21 '18 at 4:40
• Can use //. instead of /. – R zu Nov 21 '18 at 4:53

Use //. to apply a list of rules exhaustively (doc).

SimplifyKroneckerSum[expr_] = expr //. {
Sum[y_ KroneckerDelta[r_, s_], {s_, 1, p_}, z__] :>
Sum[(y /. s -> r), z],
Sum[y_ KroneckerDelta[r_, s_], {s_, 1, p_}] :>
(y /. s -> r)};

expr = Sum[Log[Sum[A[j, k, l]*B[j, k, l], {l, 1, L}]],
{j, 1, J}, {k, 1, K}]

expr = Simplify[D[expr, A[a, b, c]]]

SimplifyKroneckerSum[expr]

• The doc would be better if it has perma-link / anchor to each section / example. – R zu Nov 21 '18 at 4:55