]> Creatis software - CreaPhase.git/blob - octave_packages/specfun-1.1.0/multinom_coeff.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / specfun-1.1.0 / multinom_coeff.m
1 %% Copyright (c) 2011 Jordi GutiĆ©rrez Hermoso <jordigh@octave.org>
2 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 %%
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
7 %%    any later version.
8 %%
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.
13 %%
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/>.
16
17 %% -*- texinfo -*-
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
21 %% @tex
22 %% $$
23 %% (x1 + x2 + ... + xm).^n
24 %% $$
25 %% @end tex
26 %% @ifnottex
27 %%
28 %% @example
29 %% (x1 + x2 + ... + xm).^n
30 %% @end example
31 %%
32 %% @end ifnottex
33 %%
34 %% For example, for m=3, n=3 the expansion is 
35 %% @tex
36 %% $$
37 %% (x1+x2+x3)^3 = 
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
40 %% $$
41 %% @end tex
42 %% @ifnottex
43 %%
44 %% @example
45 %% @group
46 %% (x1+x2+x3)^3 = 
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
50 %% @end group
51 %% @end example
52 %%
53 %% @end ifnottex
54 %%
55 %% and the coefficients are [6 3 3 3 3 3 3 1 1 1].
56 %%
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
61 %% @tex
62 %% $$
63 %%  {n \choose k} = \frac{n!}{k(1)!k(2)! \cdots k(\text{end})!}
64 %% $$
65 %% @end tex
66 %% @ifnottex
67 %%
68 %% @example
69 %% @group
70 %%  /   \
71 %%  | n |                n!
72 %%  |   |  = ------------------------
73 %%  | k |     k(1)!k(2)! @dots{} k(end)!
74 %%  \   /
75 %% @end group
76 %% @end example
77 %%
78 %% @end ifnottex
79 %%
80 %% @seealso{multinom,multinom_exp}
81 %%
82 %% @end deftypefn
83
84 function [c, alpha] = multinom_coeff(m,n,sortmethod)
85
86     if nargin > 2
87         alpha = multinom_exp(m,n,sortmethod);
88     else
89         alpha = multinom_exp(m,n);
90     end
91
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);
98
99 end
100
101 %!demo
102 %! multinom_coeff(3,3)
103 %! multinom_coeff(3,3,'ascend')
104 %! multinom_coeff(3,3,'descend')