# Sum and differentiate over multiple indices

Consider, I have a function $\phi: R^N \to R$, denote its i'th argument with $x_i$. Consider also a vector in $R^N$: $W$. Denote a multiindex $\alpha = (\alpha_1, \dots, \alpha_n)$ (for each $\alpha_n$ integer). Then denote

$$W^\alpha = W_1^{\alpha_1} \cdot W_2^{\alpha_2} \cdots W_n ^{\alpha_n},$$

$$\alpha ! = \alpha_1 ! \cdot \alpha_2 ! \cdots \alpha_N ! ,$$

$$\lvert \alpha \rvert = \alpha_1 + \alpha_2 + \cdots + \alpha _N$$

and (assuming everyhing exists and is well-behaved)

$$D^{ \alpha} = \frac{\partial^{\alpha_1} }{\partial x_1^{\alpha_1}} \cdot \frac{\partial^{\alpha_2} }{\partial x_2^{\alpha_2}} \cdots \frac{\partial^{\alpha_n} }{\partial x_n^{\alpha_n}} \, \phi$$

(i.e. differentiate $\alpha_n$ times with respect to $x_n$ then $\alpha_{n-1}$ times wrt. $x_{n-1}$ etc).

Then I seek to calculate (symbolically!)

$$\sum_{\lvert \alpha \rvert =k} D^{\alpha} \phi \cdot W^{\alpha} \, \frac{k!}{\alpha !}$$

Can anyone help? I am aware of this question: Other Question but honestly I'm having a hard time deciphering the answers. Mathematica isn't my best language, but it seems like it would be very convenient at this particular problem (especially since the symbolic math is so strong).

Oh, and readability is more important to me than speed (to me, in this particular situation).

• @MMM Thanks for the help! (Maybe you can kill the note? - I'm still not allowed to it appears). – Henrik Mar 2 '17 at 13:43
• Are you seeking a completely general result independent of phi, or do you have a function phi in mind? Do you have a way of generating the alpha indices? Are you sure that this is even a Mathematica question, and not still a math question? We can help with implementation, but this seems to require more than that. – MarcoB Mar 2 '17 at 15:14
• @MarcoB Thank you for your interest! I was hoping a general function. Generating the alpha indices is adressed very nicely in the question I is referring to, I just don't understand the implementation. So what I am basically asking is how to get "Derivative" and "Power" to work in the proper way and insert into the place of "b" in that question. – Henrik Mar 2 '17 at 15:33
• I see you've used $n$ and $N$ interchangeably. You have also not set restrictions for the values of $\alpha_i$. Are they >=0 or >0? If the former, the set of $|\alpha| = k$ is quickly rather huge. – LLlAMnYP Mar 8 '17 at 14:39
• By the way, I'm quite convinced, that your desired expression is quite simply the sum of all k-th order terms of the Taylor expansion of $\phi$ in the vicinity of $\vec{0}$ at $W$ – LLlAMnYP Mar 8 '17 at 14:45

As I commented, these are the k'th-order terms of the multivariable Taylor expansion. After some redefinitions of Derivative I came up with a solution:

kOrderTerms[f_[args__], k_] :=                                   (* 1 *)
Block[{d, expr}, d[x___][d[y___][g_]] := (d @@ ({x} + {y}))[g]; (* 2 *)
Block[{Derivative = d},                                      (* 3 *)
expr = Series @@ ({f[args]}~Join~Thread[{{args}, 0, k}]);   (* 4 *)
d[x___ /; Plus[x] != k][g_] = 0 &;                          (* 5 *)
expr = expr];                                               (* 6 *)
d = Derivative;                                              (* 7 *)
expr = expr] // Normal // Expand                             (* 8 *)

1. Usage: $\mathrm{kOrderTerms}[\phi[W_1, W_2, ..., W_n], k]$
2. Get a variable d to emulate the behavior of chained derivative, e.g. d[0, 1, 2][d[3, 0, 1][f]] -> d[3, 1, 3][f], just like Derivative would behave in place of d.
3. Tell Mathematica to use d in place of Derivative.
4. Calculate the series expansion of the given function up to the desired order k in each variable. At this step the chained derivatives properly fold.
5. Introduce a new rule: if the total order of our "derivative" is not equal to k, that summand will be zero.
6. Update the expression, given the new rule.
7. Forget about all the rules for d, instead just switch it back to the proper Derivative.
8. Update the expression once more, remove all big-O terms, expand into a sum. Note that Series caches its results. While coding, I, e.g., ran into a bug that kOrderTerms[f[x, y, z], 3] retained f[0,0,0] in the returned expression. This was fixed by ClearSystemCache[].