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.
ArraySymbol
, etc. are related, they're not strong enough at the moment, though. But it's not too hard to code our own, see e.g. mathematica.stackexchange.com/q/226082/1871 mathematica.stackexchange.com/q/226431/1871 mathematica.stackexchange.com/q/245308/1871 mathematica.stackexchange.com/q/245585/1871 You'll find more by checking posts under tag tensors. $\endgroup$If[]
is a programming construct andPiecewise[]
is a functional construct. With version enhancements over the years,If[]
is sometimes converted to or treated likePiecewise[]
, but maybe not perfectly. I don't know anything built-in that converts expression toKroneckerDelta[]
.PiecewiseExpand
will do the opposite. $\endgroup$PiecewiseExpand[]
on yourIf[]
result. I have to run to a meeting, but it seemed to work, I think. $\endgroup$