2
$\begingroup$

My main problem here is two-fold:

  1. Take symbolic derivative of summation expression
  2. Stop summations from being "unrolled"/expanded, e.g., displayed as the sum of terms

I also have some secondary goals:

  1. Routinely display/print functions without all/some of their arguments (i.e. not using a replacement rule every time)
  2. Treat subscripted variables as their own unique quanitites, distinct from the original subscripted quantity and differently-subscripted versions of the same variable, e.g., get x and Subscript[x, i] and Subscript[x, j] to all be treated as unique variables by Mathematica without a huge amount of replacement/object definition work

Here is a sample expression:

$f \left( x, m \right) = \sum\limits_{i=1}^{c} \sum\limits_{j=1}^{c} m_i m_j x_i x_j$

where $c = \mathrm{length} \left( x \right)$, and $x$ and $m$ are both vectors of length $c$.

I want to differentiate $f$ w.r.t. all the elements of $x$ (effectively the gradient) to obtain the below:

$ \frac{\partial f}{\partial x_k} = \nabla_x f = 2 m_k \sum\limits_{i=1}^{c} m_i x_i$

where derivative index $k$ is independent from $i$ and $j$.

I implement the original expression below in MMa:

f[x_, m_] := Sum[m[[i]]*m[[j]]*x[[i]]*x[[j]], {i, 1, Length[x]}, {j, 1, Length[x]}]

I then define some sample vectors to test the function:

X = {Subscript[x, 1], Subscript[x, 2], Subscript[x, 3]}
M = {Subscript[m, 1], Subscript[m, 2], Subscript[m, 3]}

f[X,M] //TeXForm

$m_1^2 x_1^2+2 m_1 m_2 x_2 x_1+2 m_1 m_3 x_3 x_1+m_2^2 x_2^2+m_3^2 x_3^2+2 m_2 m_3 x_2 x_3$

which is correct.


I can achieve point #1 in code in either of the following ways:

df[x_, m_] := Grad[f[x,m], x]

df[x_, m_] := f'[x,m]

Testing this new function:

df[X,M] //TeXForm

$\left\{2 m_1^2 x_1+2 m_2 m_1 x_2+2 m_3 m_1 x_3,2 m_2^2 x_2+2 m_1 m_2 x_1+2 m_3 m_2 x_3,2 m_3^2 x_3+2 m_1 m_3 x_1+2 m_2 m_3 x_2\right\}$

This is also correct, though I would prefer to display both $f$ and $\nabla_x f$ in their condensed summation/sigma notation (point #2), i.e., the forms in the TeX equations above.


I am still new to MMa, but to that end, I've tried a few approaches I've found, and found that applying HoldForm[] gives me what I want for $f$, sort of:

f[x_, m_] := HoldForm[Sum[m[[i]]*m[[j]]*x[[i]]*x[[j]], {i, 1, Length[x]}, {j, 1, Length[x]}]]

f[X,M] //TeXForm

$\sum _{i=1}^{\text{Length}\left[\left\{x_1,x_2,x_3\right\}\right]} \sum _{j=1}^{\text{Length}\left[\left\{x_1,x_2,x_3\right\}\right]} \left\{m_1,m_2,m_3\right\}[[i]] \left\{m_1,m_2,m_3\right\}[[j]] \left\{x_1,x_2,x_3\right\}[[i]] \left\{x_1,x_2,x_3\right\}[[j]]$

This is mathematically correct, but I would really prefer that the vectors not be printed in full, instead being represented as the vector variable, subscripted if appropriate:

  • $\left\{x_1,x_2,x_3\right\} \rightarrow x$
  • $\left\{x_1,x_2,x_3\right\}[[i]] \rightarrow x_i$
  • $\left\{m_1,m_2,m_3\right\}[[i]] \rightarrow m_i$

I also cannot achieve points #1 and #2 at the same time. Applying Grad[] to the HoldForm[] version of $f$ yields the below errors and output (as img for clarity; plaintext and TeX forms of the raw text are both nearly unreadable): enter image description here

Perhaps there is some clever way to achieve this, but I don't know where to searching beyond what I've already tried. It may also be impossible; after testing MMa/Maple/sympy and others, I've yet to find a satisfactory tool for computationally evaluated symbolic derivatives of vector/summations expressions.

Any help is appreciated!

$\endgroup$
1
  • 1
    $\begingroup$ MMA is intensely case sensitive, create your own function sum MMA will not do most of the things it does with Sum Define a function that takes the derivative of your sum and gives a result in mostly the form you desire. Define all the other functions you need to do what you need to your expressions. If you have to fight more and more of MMA to get exactly the form of results you want then you might start to ask yourself if you are really using almost anything in MMA and perhaps a word processor or a white board might be the better tool for you to choose. $\endgroup$
    – Bill
    Commented Mar 25 at 17:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.