multinomial function

17 views
Skip to first unread message

Kjetil brinchmann Halvorsen

unread,
May 3, 2012, 7:45:43 PM5/3/12
to sy...@googlegroups.com
sympy does not seem to have a multinomial() function, in style of binomial(), giving binomial coefs, only multinomial()
shoud give multinomial coefficients.  We should have!  Should/can I try to write such a function, it should be in file
factorials.py, where binomial() resides.

¿Any thoughts?

Kjetil


--
"If you want a picture of the future - imagine a boot stamping on the human face - forever."

George Orwell (1984)



Tom Bachmann

unread,
May 3, 2012, 7:51:34 PM5/3/12
to sy...@googlegroups.com
I agree we should have it, and I agree it should go to factorials.py. Go
ahead and give it a shot!
> --
> You received this message because you are subscribed to the Google
> Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

Kjetil brinchmann Halvorsen

unread,
May 3, 2012, 8:59:57 PM5/3/12
to sy...@googlegroups.com
see below.

On Thu, May 3, 2012 at 7:51 PM, Tom Bachmann <e_m...@web.de> wrote:
I agree we should have it, and I agree it should go to factorials.py. Go ahead and give it a shot!



Will try!

Some questions:   As there is the "generalized binomial coefficient, given by
binom(a,k) = \frac{a(a-1)\dots (a-k+1)}{k!}, we could define in a natural way "generalized multinomial coefficiewnts"
by
multinom(a;k1,k2,     k_s) = \frac{a(a-1)\dots (a-k1-k2 -     -k_s+1)}{k1! k2! \dots k_s!}

which is natural since it can be used to generalize Newton' s binomial theorem tNewton' s multinomial theorem". But I cannot find any refverence for it! Any ideas , references?


second:

which syntax should be used:
--- multinomial(a,k1,k2,.....,k_s)
or
multinomial(a,[k1,k2,...,k_s])
or
   other?

Kjetil



 


On 04.05.2012 00:45, Kjetil brinchmann Halvorsen wrote:
sympy does not seem to have a multinomial() function, in style of
binomial(), giving binomial coefs, only multinomial()
shoud give multinomial coefficients.  We should have!  Should/can I try
to write such a function, it should be in file
factorials.py, where binomial() resides.

żAny thoughts?


Kjetil


--
"If you want a picture of the future - imagine a boot stamping on the
human face - forever."

George Orwell (1984)



--
You received this message because you are subscribed to the Google
Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/sympy?hl=en.

Aaron Meurer

unread,
May 4, 2012, 2:02:14 AM5/4/12
to sy...@googlegroups.com
We do have the multinomial_coefficients() function. Is that the same
thing as what you want?

Aaron Meurer
>>> sympy+un...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/sympy?hl=en.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sympy" group.
>> To post to this group, send email to sy...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> sympy+un...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/sympy?hl=en.
>>
>
>
>
> --
> "If you want a picture of the future - imagine a boot stamping on the human
> face - forever."
>
> George Orwell (1984)
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.

Chris Smith

unread,
May 4, 2012, 3:09:24 AM5/4/12
to sy...@googlegroups.com
On Fri, May 4, 2012 at 11:47 AM, Aaron Meurer <asme...@gmail.com> wrote:
> We do have the multinomial_coefficients() function.  Is that the same
> thing as what you want?

from sympy.functions.combinatorial.factorials
binomial(9, 3) -> 84

from sympy.ntheory import binomial_coefficients
binomial_coefficients(9) ->
{(0, 9): 1, (1, 8): 9, (2, 7): 36, (3, 6): 84, (4, 5): 126, (5, 4):
126, (6, 3): 84, (7, 2): 36, (8, 1): 9, (9, 0): 1}

multinomial_coefficients is the same, returning the dictionary; there
isn't a binomial equivalent that just returns a single coefficient.

I think multinomial could work like this:

```
>>> def multinomial(*expos):
... n = sum(expos)
... k = len(expos)
... return multinomial_coefficients(k, n)[expos]
...
>>> multinomial_coefficients(3,8)
{(0, 5, 3): 56, (4, 2, 2): 420, (0, 3, 5): 56, (1, 1, 6): 56, (3, 1, 4): 280, (4
, 4, 0): 70, (0, 4, 4): 70, (0, 1, 7): 8, (1, 0, 7): 8, (6, 2, 0): 28, (3, 4, 1)
: 280, (1, 2, 5): 168, (1, 7, 0): 8, (1, 4, 3): 280, (6, 0, 2): 28, (4, 3, 1): 2
80, (3, 3, 2): 560, (8, 0, 0): 1, (2, 4, 2): 420, (3, 2, 3): 560, (7, 1, 0): 8,
(6, 1, 1): 56, (2, 2, 4): 420, (5, 1, 2): 168, (0, 8, 0): 1, (0, 2, 6): 28, (0,
6, 2): 28, (3, 0, 5): 56, (7, 0, 1): 8, (2, 5, 1): 168, (0, 0, 8): 1, (2, 0, 6):
28, (2, 6, 0): 28, (5, 0, 3): 56, (2, 1, 5): 168, (1, 6, 1): 56, (4, 1, 3): 280
, (3, 5, 0): 56, (5, 3, 0): 56, (4, 0, 4): 70, (1, 3, 4): 280, (0, 7, 1): 8, (1,
5, 2): 168, (2, 3, 3): 560, (5, 2, 1): 168}
>>> multinomial(0,5,3) # first one in dict above
56
>>> multinomial(5,2,1) # last one in dict above
168
>>> multinomial(1,1,2,3) # coefficient of a*b*c**2*d**3 in (a+b+c+d)**7
420
>>> multinomial(3,6) # coeff of a**3*b**6 in (a+b)**9
84
>>> binomial(9,3)
84
```

Chris Smith

unread,
May 4, 2012, 5:39:36 AM5/4/12
to sy...@googlegroups.com
On Fri, May 4, 2012 at 11:47 AM, Aaron Meurer <asme...@gmail.com> wrote:
> We do have the multinomial_coefficients() function.  Is that the same
> thing as what you want?

The mutinomial function will perhaps be the key to solving the
"coefficient without expanding' problem, e.g. what is the coefficient
of `a**7*b**(6*x+2)` in `(a + b**(2*x) + a**2*b)**4`?

Ans: we want the powers i, j and k such that
`a**i*b**(2*x*j)*(a**2*b)**k == a**7*b**(6*x+2)`.
Expanding the lhs gives `a**(i + 2*k)*b**(2*j*x + k)`.
Equating with the desired coefficients gives `i+2*k == 7 and 2*j*x + k
== 6*x + 2`
Matching coefficient in the 2nd eqn gives `j = 3 and k = 2` hence `i =
3` and the coefficient must be zero since 3 + 3 + 2 > 4

But if we look for the coeff of `a**6*b**(2*x+3)` we find
`i,j,k=0,1,3` with `multinomial(0,1,3) == 4`

```
>>> powsimp(((a + b**(2*x) + a**2*b)**4).expand()).coeff(a**6*b**(2*x+3))
4
```

Are you, Kjetil, interested in implementing that?

/c

smichr

unread,
May 7, 2012, 9:03:57 AM5/7/12
to sy...@googlegroups.com
A first draft of multinomial_coeff is at  https://github.com/sympy/sympy/pull/1280 .
Reply all
Reply to author
Forward
0 new messages