# Simplify sum of unnatural conditional functions generated by derivative

I try to use Mathematica to evaluate:

$$\frac{\partial}{\partial A_{abc}} \sum_{j=1}^{J}\sum_{k=1}^{K}\log\left(\sum_{l=1}^{L}A_{jkl}B_{jkl}\right)$$

I get:

$$\sum_{j=1}^J \sum_{k=1}^K \frac{\begin{array}&\begin{cases} B(j,k,c) & a-j=0\land b-k=0 \\ 0 & \text{True} \\ \end{cases} \\ \end{array}}{\sum_{l=1}^L A(j,k,l) B(j,k,l)}$$

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

How to make Mathematica simplify the sum of conditional functions?

Alternatively, how to avoid the conditional functions in the first place?

The expression is part of a non-linear objective function containing multiple matrices. I am trying to evaluate the gradient and Hessian.

Thanks.

$Assumptions = Element[a | b | c, Integers] && 1 <= a <= J && 1 <= b <= K && 1 <= c <= L; expr = Sum[ Log[Sum[A[j, k, l]*B[j, k, l], {l, 1, L}]], {j, 1, J}, {k, 1, K}] Simplify[D[expr, A[a, b, c]]]  Update: Try to simplify Kronecker delta with rules, as suggested by chris. Simplify[D[expr, A[a, b, c]]]/. 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)  Unfortunately, Mathematica doesn't even give Kronecker delta in this case. • Mathematica doesn't even give Kronecker delta in this case... – R zu Nov 20 '18 at 15:52 • Mathematica 11.0.1. evaluates Simplify[D[expr, A[a, b, c]]](* 0*) !!! Time for an update... Nov 20 '18 at 16:02 • I am using Mathematica 11.3 – R zu Nov 20 '18 at 16:03 • If writing the expression in terms of tensors would help Mathematica solve the problem, I might spend 1-2 days to learn basic tensor algebra. But I don't know if that approach can actually work. Oh well. At least I can use pencil and paper again. – R zu Nov 20 '18 at 16:05 • I wish I can put a bounty on this now. – R zu Nov 20 '18 at 23:04 ## 1 Answer Using Mathematica 11.3 expr = Sum[ Log[Sum[A[j, k, l]*B[j, k, l], {l, 1, L}]], {j, 1, J}, {k, 1, K}]; dexpr = Simplify[D[expr, A[a, b, c]]] So with a bit of help with the Kroneckers dexpr /. 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) • Thanks for writing the rule. How does the rule work? :> means to transform lhs to rhs and then evaluate rhs. I guess (y /. s-> r) means to transform expression y such that each s in y is replaced by r... That is not always correct but works in this case. I don't get the rest of the rule. – R zu Nov 21 '18 at 2:54 • Does the rest of the rule means: within each occurrence of$\sum_{u=1}^{U}\sum_{v=1}^{V} y \delta_{u, i} \delta_{v, j}$in the expression, replace every$u$and$v$in sub-expression$y$by$i$and$j\$ respectively.
– R zu
Nov 21 '18 at 3:05
• If Mathematica can apply the same rule for several times, we only need one rule: Sum[y_ KroneckerDelta[s_, r_], {s_, 1, p_}] :> (y /. s -> r). I will ask a new question about that. Thanks for the answer.
– R zu
Nov 21 '18 at 3:10
• yes that's what the rule mean. Nov 21 '18 at 9:38