Mathematica code for multinomial expansion

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
nikozm
Messages
51
Reaction score
0
Hello,

I m looking for a mathematica code for the multinomial expansion of the general case of (let's say m sum terms given in a power of n), e.g., the code for the 1st equation in http://en.wikipedia.org/wiki/Multinomial_theorem

Any help would be useful.

Thanks
 
Physics news on Phys.org
I'm not sure I understand what you want. Do you mean you want code that, given ##m## and ##n##, returns the expanded product? Or you want the multinomial coefficients?
 
Hello,

First, thank you for replying. I m interested to deriving the whole expanded formula not just the multinomial coefficient, since the latter can be straightforwardly returned via the corresponding mathematica command "multinomial"

Is there a way to build an efficient code for multinomial expansion for the general case ?
 
I'm still not sure what you want. When you have such a question, it is good to explain clearly what the input to the function is, and what you want to get out.

Is this what you are looking for?
Code:
multi[m_, n_] := Expand[Sum[Subscript[x, i], {i, 1, m}]^n]
 
The expand command does the job, but i want to further elaborate on the product terms (powered to the corresponding multinomial coefficients).

In particular, i want to express the following general form: ∑^{L}_{p1,p2,...,pn=0 && p1+p2+...+pn=L} f(x)^{p1+p2+...+pn}. There is a code for a certain case (see bellow) but how can i generalize it ?

x = 0; For[k1 = 0, k1 <= 2, k1++,
For[k2 = 0, k2 <= 2, k2++,
For[k3 = 0, k3 <= 2, k3++,
If[k1 + k2 + k3 == 2,
x = x + Multinomial[k1, k2, k3]*
x1^k1*x2^k2*x3^k3];
If[k1 == 2 && k2 == 2 && k3 == 2, Print[x]]]]]