1 %% Copyright (c) 2011 Jordi GutiƩrrez Hermoso <jordigh@octave.org>
2 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
4 %% This program is free software: you can redistribute it and/or modify
5 %% it under the terms of the GNU General Public License as published by
6 %% the Free Software Foundation, either version 3 of the License, or
9 %% This program is distributed in the hope that it will be useful,
10 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
11 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 %% GNU General Public License for more details.
14 %% You should have received a copy of the GNU General Public License
15 %% along with this program. If not, see <http://www.gnu.org/licenses/>.
18 %% @deftypefn {Function File} {[@var{c} @var{alpha}] =} multinom_coeff (@var{m}, @var{n})
19 %% @deftypefnx {Function File} {[@var{c} @var{alpha}] =} multinom_coeff (@var{m}, @var{n},@var{order})
20 %% Produces the coefficients of the multinomial expansion
23 %% (x1 + x2 + ... + xm).^n
29 %% (x1 + x2 + ... + xm).^n
34 %% For example, for m=3, n=3 the expansion is
38 %% = x1^3 + x2^3 + x3^3 + 3 x1^2 x2 + 3 x1^2 x3 + 3 x2^2 x1 + 3 x2^2 x3 +
39 %% + 3 x3^2 x1 + 3 x3^2 x2 + 6 x1 x2 x3
47 %% = x1^3 + x2^3 + x3^3 +
48 %% + 3 x1^2 x2 + 3 x1^2 x3 + 3 x2^2 x1 + 3 x2^2 x3 +
49 %% + 3 x3^2 x1 + 3 x3^2 x2 + 6 x1 x2 x3
55 %% and the coefficients are [6 3 3 3 3 3 3 1 1 1].
57 %% The order of the coefficients is defined by the optinal argument @var{order}.
58 %% It is passed to the function @code{multion_exp}. See the help of that
59 %% function for explanation.
60 %% The multinomial coefficients are generated using
63 %% {n \choose k} = \frac{n!}{k(1)!k(2)! \cdots k(\text{end})!}
72 %% | | = ------------------------
73 %% | k | k(1)!k(2)! @dots{} k(end)!
80 %% @seealso{multinom,multinom_exp}
84 function [c, alpha] = multinom_coeff(m,n,sortmethod)
87 alpha = multinom_exp(m,n,sortmethod);
89 alpha = multinom_exp(m,n);
92 %% Multinomial coefficients
93 %% number of ways of depositing n distinct objects into m distinct bins,
94 %% with k1 objects in the first bin, k2 objects in the second bin, and so on
95 %% JPi: I guess it can be improved
96 %% Simplification thanks to Jordi G. H.
97 c = factorial(n)./prod(factorial(alpha),2);
102 %! multinom_coeff(3,3)
103 %! multinom_coeff(3,3,'ascend')
104 %! multinom_coeff(3,3,'descend')