2
$\begingroup$

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

$\endgroup$
8
  • $\begingroup$ Mathematica doesn't even give Kronecker delta in this case... $\endgroup$
    – R zu
    Commented Nov 20, 2018 at 15:52
  • $\begingroup$ Mathematica 11.0.1. evaluates Simplify[D[expr, A[a, b, c]]](* 0*) !!! Time for an update... $\endgroup$ Commented Nov 20, 2018 at 16:02
  • $\begingroup$ I am using Mathematica 11.3 $\endgroup$
    – R zu
    Commented Nov 20, 2018 at 16:03
  • $\begingroup$ 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. $\endgroup$
    – R zu
    Commented Nov 20, 2018 at 16:05
  • $\begingroup$ I wish I can put a bounty on this now. $\endgroup$
    – R zu
    Commented Nov 20, 2018 at 23:04

1 Answer 1

2
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – R zu
    Commented Nov 21, 2018 at 2:54
  • $\begingroup$ 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. $\endgroup$
    – R zu
    Commented Nov 21, 2018 at 3:05
  • $\begingroup$ 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. $\endgroup$
    – R zu
    Commented Nov 21, 2018 at 3:10
  • $\begingroup$ yes that's what the rule mean. $\endgroup$
    – chris
    Commented Nov 21, 2018 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.