2

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)}$$

Instead of

$$\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.

enter image description here

R zu
  • 349
  • 1
  • 8
  • 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... – Ulrich Neumann 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
  • This may help? https://mathematica.stackexchange.com/a/16378/1089 – chris Nov 21 '18 at 00:23
  • @chris Sorry. I can't quite figure out https://mathematica.stackexchange.com/a/16378/1089 How to use that to simplify the conditional? – R zu Nov 21 '18 at 02:10
  • It didn't work for me. I guess the reason is that Mathematica doesn't even give KroneckerDelta in this case. – R zu Nov 21 '18 at 02:28

1 Answers1

2

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]]]

Mathematica graphics

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)

Mathematica graphics

chris
  • 22,860
  • 5
  • 60
  • 149
  • 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 02: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 03: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 03:10
  • yes that's what the rule mean. – chris Nov 21 '18 at 09:38