1

As an example I want to try to find the partial derivatives of

f(x) = \sum_{i=1}^n x_i^2

in Maxima. (The expected output would be \frac{\partial f}{\partial x_k} = 2x_k) I have tried following, but it seems the indexed variables are not handled as I expected, can anyone explain what I am doing wrong?

The same command works if you replace n and k with actual numbers, but not in this form:

f(x) := 1/2 * sum( x[i]^2, i, 1, n);
print(diff(f(x),x[k]));

Try it online!

1
  • Maxima doesn't know how to handle that with builtin functions. I wrote a couple of small packages to handle it -- search for diff_sum.mac and sum_kron_delta.mac. I will post an answer which makes use of those functions in the next day or so. Nov 13, 2017 at 1:27

1 Answer 1

2

Maxima can't handle derivative with respect to a indexed variable by default. I wrote a couple of small packages to handle these problems. Perhaps this is useful to you.

See: https://pastebin.com/MmYJRqCq (sum_kron_delta, summation of Kronecker delta) and: https://pastebin.com/UGCPgvAn (diff_sum, derivative of summation wrt indexed variable)

Here's an example applied to your problem. I'll assume you have downloaded the code above to your computer.

(%i1) load ("sum_kron_delta.mac");
(%o1)                         sum_kron_delta.mac
(%i2) load ("diff_sum.mac");
(%o2)                            diff_sum.mac
(%i3) 'diff ('sum (x[i]^2, i, 1, n), x[j]);
                             n
                            ====
                            \
(%o3)                     2  >    x  kron_delta(i, j)
                            /      i
                            ====
                            i = 1

Note that you have to write 'diff('sum(... that is, with the quote mark ' to indicate that diff and sum are nouns (formal expressions) instead of verbs (functions which are called). This is necessary in the implementation of diff_sum and sum_kron_delta because they work with simplification rules. (It's a long story, which I can explain if there's interest.)

I see we got the kron_delta summation, but we need to cause the simplification rules to be applied. We could also write expand(%, 0, 0) here instead of ''%.

(%i4) ''%;
(%o4) 2 (if (1 <= j) and (j <= n) and %elementp(j, integers) then x  else 0)
                                                                   j

At this point we have the final result, which we can simplify further with additional data.

(%i5) assume (j >= 1, j <= n);
(%o5)                          [j >= 1, n >= j]
(%i6) ''%o4;
(%o6)            2 (if %elementp(j, integers) then x  else 0)
                                                    j
(%i7) declare (j, integer);
(%o7)                                done

(%i8) ''%o6;
(%o8)                                2 x
                                        j

If this seems fruitful to you, I'll be happy to go into details.

1
  • Thanks a lot, this is exactly what I needed! I will look into the details soon, and come back If I have further questions!
    – flawr
    Nov 13, 2017 at 15:18

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.