Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Multinomial Expansion in Mathematica

  1. Aug 14, 2009 #1

    I have the following equation, which is simply the multinomial exapnsion:

    [tex]\left[\sum_{l=1}^L\,x_l\right]^n=\sum_{k_1,k_2,\ldots,k_L}{n\choose k_1,k_2,\ldots,k_L}\prod_{l=1}^L\,x_l^{k_l}[/tex]

    where the summation is taken over all possibilities that the summation of the nonnegative integer indices k1,k2,...,kL equal n.

    How can I program this using Mathematica??

    Thanks in advance
  2. jcsd
  3. Aug 16, 2009 #2
    Any suggestion please? I am really need this. Any help will be greatly appreciated.
  4. Aug 16, 2009 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It really isn't clear what you want to do.
  5. Aug 16, 2009 #4
    Ok, first of all, thank you for replying. What I want to do is to write a function using Mathematica that computes the multinomial expansion, which is the right hand side equation in the first post. The difficulty as I see it, comes from the summation. For example:

    [tex](x_1+x_2+x_3)^2=\underbrace{\sum_{k_1,k_2,k_3}{2\choose k_1,k_2,k_3}\prod_{n=1}^{3}x_n^{k_n}}_{\text{Multinomial Expansion}}[/tex]

    where the summation in the right hand side takes place over all possibilities that [tex]k_1+k_2+k_3=2[/tex], and

    Doing that yields to the final result:


    I hope that I explain the point.

  6. Aug 16, 2009 #5


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    What's wrong with the Expand function?
  7. Aug 16, 2009 #6
    The problem is that, the multinomial I have looks like this:


    I want to exctract the exponentials and combine them with other exponentials and integrate them all over [tex]\gamma[/tex] to do my derivation for the performance metrics.
  8. Aug 17, 2009 #7
    So far I reached to this point:

    Code (Text):
    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]]]]]

    x1^2+2 x1 x2+x2^2+2 x1 x3+2 x2 x3+x3^2
    But how can I write it in general form, when the number of terms and the exponent are variables?
  9. Aug 17, 2009 #8


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I'm still not sure why you think this will result in anything different than the expand function....

    When the number of loops is variable, you essentially have to implement an odometer -- e.g. as a list whose n-th element is the value of the n-th variable. Then you have just one for loop that initializes / steps this odometer.

    It might be easier, however, to construct the m-fold Cartesian product of the set {0, 1, ..., N} with itself. There is surely a builtin function to compute Cartesian products of lists, and it should be easy enough to use that to write a function that iterates it m times. But I don't have mathematica on my home computer to tell you what that function would be.

    (The Cartesian product of A with B is the set AxB whose elements are all length-2 lists whose first element is from A and whose second element is from B)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook