# Derivative with respect to any element of a matrix

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]]  Still 0 when I don't assign anything to $$L$$ ## 1 Answer 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

• After I cut, paste, and run the 3 + 1 lines of code, I still get 0. – R zu Nov 19 '18 at 20:25
• 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. – R zu Nov 19 '18 at 20:27
• No... I was trying a dozen of things and the code still gives 0. – R zu Nov 19 '18 at 20:44
• Try ClearAll["Global`*"] before evaluating the code in the answer. – Rohit Namjoshi Nov 19 '18 at 20:47
• still 0 after opening new notebook and use ClearAll["Global*"] – R zu Nov 19 '18 at 20:48