7
$\begingroup$

My question is whether Mathematica can represent an expression such as:

$$y_i = \sum_{j=1}^N a_{ij} x_j$$

symbolically as a sum over indefinite bounds (unknown $N$), and differentiate it with respect to an indexed variable such as $a_{ik}$. The answer in this case should be $\partial y_i / \partial a_{ik} = x_k$.

For a more involved example, if we have

$$f_k = x_k \sum_i \sum_j g(a_{ij}, x_j)$$

then

$$\partial f_k / \partial x_n = \delta_{nk} \sum_i \sum_j g(a_{ij}, x_j) + x_k \sum_i \sum_j D_2 g(a_{ij}, x_j) \delta_{nj}$$ $$=\delta_{nk} \sum_i \sum_j g(a_{ij}, x_j) + x_k \sum_i D_2 g(a_{in}, x_n)$$ where $\delta$ is the Kronecker delta and $D_2 g$ is the derivative of $g$ with respect to its second parameter.

Can Mathematica do both of these examples as stated?


With advice from a user on wolfram community, I tried:

Assuming[
 1 <= k <= z && k ∈ Integers
&& 1 <= n <= z && n ∈ Integers,
 ff = D[x[k] * Sum[Sum[g[a[i, j],x[j]], {j, 1, z}], {i, 1, z}], x[n]]
 ]

But this gives the wrong answer: Sum[Derivative[0, 1][g][a[i, n], x[n]], {i, 1, z}]*x[k]. It omits the other term in the answer.

Another user from Wolfram Community suggested trying

Assuming[
 1 <= k <= z && k ∈ Integers
&& 1 <= n <= z && n ∈ Integers,
 ff = D[If[n == k, x[n],x[k]]  * Sum[Sum[g[a[i, j],x[j]], {j, 1, z}], {i, 1, z}], x[n]]
 ]

which gives the result

If[k == n, 1, 0]*Sum[Sum[g[a[i, j], x[j]], {j, 1, z}], {i, 1, z}] + 
  If[k == n, x[n], x[k]]*Sum[Derivative[0, 1][g][a[i, n], x[n]], {i, 1, z}] 

This is technically correct but there are two problems. First, it doesn't convert the first If[] expression to a KroneckerDelta, and it doesn't eliminate the second If[] expression. It doesn't seem to be able to simplify If[] expressions much at all; in other testing I find it will leave If[expr, 0, 0] in that form without simplifying to 0. The second problem is that I need to add those If[] expressions for every variable, and if I have multiple "free" indices (in more complex examples than shown here) that's a lot of cases to handle, requiring me to test for every variable whether or not each of its indices is equal to any of the other indices in the expression. The expression gets so complicated that I'd be better off doing it by hand from the start than manually rewriting and simplifying the Mathematica output.

$\endgroup$
6

2 Answers 2

6
$\begingroup$

OK, though I've done this many times, given that coding this in Mathematica requires one to have a good enough understanding for the core language, let me do a favor:

Clear[myD, mySum]

myD[a_ b_, c_] := a myD[b, c] + b myD[a, c]
(* Add the rule for addition just in case: *)
myD[a_ + b_, c_] := myD[a, c] + myD[b, c]

myD[mySum[a_, b__], x_] := mySum[myD[a, x], b]

myD[(h_ /; Context[h] =!= "System`")[a__], x_] := 
 With[{l = Length@{a}}, 
  Total@MapThread[
    Derivative[Sequence @@ #][h][a] myD[#2, x] &, {IdentityMatrix@
      l, {a}}]]

myD[Subscript[a_, i___, j1_, k___], Subscript[a_, i___, j2_, k___]] :=
  KroneckerDelta[j1, j2]

myD[Subscript[a_, __], Subscript[b_, __]] := 0

mySum[KroneckerDelta@OrderlessPatternSequence[i_, j_] expr_, jL___, 
  j_, jR___] := mySum[expr /. j -> i, jL, jR]

mySum[a_] := a

MakeBoxes[mySum[a__], fmt_] ^:= 
 With[{arg = MakeBoxes[#, fmt] & /@ {a}}, 
  With[{funcbody = 
     MakeBoxes[Sum[a], fmt] /. 
      Thread[arg -> (Slot /@ Range@Length@{a})]}, 
   TemplateBox[arg, "mySum", DisplayFunction -> (funcbody &)]]]

Usage:

myD[mySum[Subscript[a, i, j] Subscript[x, j], j], Subscript[a, i, k]]

enter image description here

myD[Subscript[x, k] mySum[g[Subscript[a, i, j], 
                            Subscript[x, j]], i, j], Subscript[x, n]]

enter image description here

You can press Ctrl+Shift+t to transform the output to TraditionalForm, and Ctrl+Shift+n to transform the output to StandardForm.

$\endgroup$
5
  • $\begingroup$ OK, you seem to know what you're doing so I'll take your word that this is necessary. So I'll start with your code and just implement my own symbolic transformation rules for everything in my expressions you didn't already handle. Seems simple enough at least. Thank you! $\endgroup$
    – causative
    Commented Nov 8 at 14:45
  • $\begingroup$ @causative Feel free to continue to ask if you have difficulty in implementing new rules :) . $\endgroup$
    – xzczd
    Commented Nov 8 at 15:08
  • $\begingroup$ It occurs to me that another thing I want to do is set these expressions equal to 0 and solve for xk (separating it out from each sum by writing aik xk + ∑j≠k aij xj, then simplifying and isolating all the xk on the left hand side). That seems more complicated, do you have an example of how I might do something like that? $\endgroup$
    – causative
    Commented Nov 8 at 15:30
  • 1
    $\begingroup$ @causative I can't recall any existing post at the moment. It's not hard to code for this specific task, though: eqn = 0 == mySum[Subscript[a, i, j] Subscript[x, j], j] /. mySum[expr_, j_] /; ! FreeQ[expr, Subscript[x, _]] :> mySum[expr, j != k] + (expr /. j -> k) then Solve[eqn, Subscript[x, k]]. We may need a bit more effort to make this operation more general. $\endgroup$
    – xzczd
    Commented Nov 8 at 15:54
  • 2
    $\begingroup$ @causative Here's a more general rule: myD[Subscript[x, k] mySum[g[Subscript[a, i, j], Subscript[x, j]], i, j], Subscript[x, n]] /. mySum[expr_, jL___, j_, jR___] /; ! FreeQ[expr, Subscript[x, _]] :> mySum[expr, jL, j != k, jR] + (expr /. j -> k) $\endgroup$
    – xzczd
    Commented Nov 8 at 16:05
3
$\begingroup$

By design of Mathematica the D operator differentiates an expression wrt to symbols or expressions, that are identifiable as patterns. So you get

 Subscript[f,j] = Sum[Subscript[a,j,k] x^k,{k,1,n}  
        (*never use N* or other capital letter symbols*)
 D[Subscript[f,j],Subscript[a,p,q]]
 0

for any values of p,q except j,k, simply because the pattern is not present in f. But if you differentiate wrt to the actual pattern and setting assumptions accordingly

Assuming[j \[Element] PositiveIntegers, 
     D[Subscript[f, j], Subscript[a, j, k]]] 

$$\begin{array}{cc} \{ & \begin{array}{cc} x^k & j\geq 1\land j-n\leq 0 \\ 0 & \text{True} \\ \end{array} \\ \end{array}$$

In effect this is use of Einstein's ingenious notation shortcut: working with the symbolic kernels of multilinear constructs like sums, integrals of tensor products and linear forms.

$\endgroup$
1
  • $\begingroup$ But I do want to differentiate with respect to variables with indices not present in the expression, as in the examples. Is there a workaround? $\endgroup$
    – causative
    Commented Nov 8 at 12:50

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.