1
$\begingroup$

I try to evaluate $$ \frac{\partial s}{\partial A_{ab}} $$ where $$ s = \sum_{i} \sum_{j} A_{ij}$$

The result should be 1 regardless of $a$ and $b$. But the following code gives zero.

L = 3
A = SymbolicMatrix["A", {L, L}]
s = Sum[Sum[A[i, j], {j, 1, L}], {i, 1, L}]
D[s, A[a, b]]

Result is 0 because Mathematica doesn't know A[a, b] refers to an element of Matrix A.

How to fix this?

Thanks again.


Update:

When I try assumptions, they don't completely solve the problem.

$Assumptions = (a | b) \[Element] Integers && 1 <= a <= L && 
   1 <= b <= L;
s = Sum[A[i, j], {j, 1, L}, {i, 1, L}];
s // TeXForm
D[s, A[a, b]]

enter image description here

Still 0 when I don't assign anything to $L$

enter image description here

$\endgroup$
5
$\begingroup$

Leave the upper limit symbolic (meaning, don't assign a value to L), define assumptions that can be used by Sum, and avoid using your undefined SymbolicMatrix function:

(* the following Clear is just in case you give a value to L *)
Clear[L]
$Assumptions=(a|b) ∈ Integers && 1<=a<=L && 1<=b<=L;
s = Sum[A[i, j], {j, 1, L}, {i, 1, L}];
s //TeXForm

$\sum _{j=1}^L \sum _{i=1}^L A(i,j)$

Then, using D will do what you want:

D[s, A[a, b]]

1

$\endgroup$
  • $\begingroup$ After I cut, paste, and run the 3 + 1 lines of code, I still get 0. $\endgroup$ – R zu Nov 19 '18 at 20:25
  • $\begingroup$ L = 10; $Assumptions = Element[a | b, Integers] && 1 <= a <= L && 1 <= b <= L; s = Sum[A[i, j], {j, 1, L}, {i, 1, L}]; D[s, {A[a, b]}] gives 0 as result. $\endgroup$ – R zu Nov 19 '18 at 20:27
  • $\begingroup$ No... I was trying a dozen of things and the code still gives 0. $\endgroup$ – R zu Nov 19 '18 at 20:44
  • $\begingroup$ Try ClearAll["Global`*"] before evaluating the code in the answer. $\endgroup$ – Rohit Namjoshi Nov 19 '18 at 20:47
  • $\begingroup$ still 0 after opening new notebook and use ClearAll["Global*"] $\endgroup$ – R zu Nov 19 '18 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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