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{alpha} =} multinom_exp (@var{m}, @var{n})
19 %% @deftypefnx {Function File} {@var{alpha} =} multinom_exp (@var{m}, @var{n},@var{sort})
20 %% Returns the exponents of the terms in the multinomial expansion
23 %% (x_1 + x_2 + ... + x_m).^n
29 %% (x1 + x2 + ... + xm).^@var{n}
34 %% For example, for m=2, n=3 the expansion has the terms
37 %% x1^3, x2^3, x1^2*x2, x1*x2^2
43 %% x1^3, x2^3, x1^2*x2, x1*x2^2
48 %% then @code{alpha = [3 0; 2 1; 1 2; 0 3]};
50 %% The optional argument @var{sort} is passed to function @code{sort} to
51 %% sort the exponents by the maximum degree.
52 %% The example above calling @code{ multinom(m,n,"ascend")} produces
54 %% @code{alpha = [2 1; 1 2; 3 0; 0 3]};
56 %% calling @code{ multinom(m,n,"descend")} produces
58 %% @code{alpha = [3 0; 0 3; 2 1; 1 2]};
60 %% @seealso{multinom, multinom_coeff, sort}
63 function alpha = multinom_exp(m, n, sortmethod)
65 %% This is standard stars and bars.
67 stars = nchoosek (1:numsymbols, n);
69 %% Star labels minus their consecutive position becomes their index
71 idx = bsxfun (@minus, stars, [0:n-1]);
73 %% Manipulate indices into the proper shape for accumarray.
75 a = repmat ([1:nr], n, 1);
79 alpha = accumarray (idx, 1);
82 [~, idx] = sort (max (alpha, [], 2), 1, sortmethod);
83 alpha = alpha(idx, :);
90 %! alpha = multinom_exp(m,n)
91 %! alpha_asc = multinom_exp(m,n,'ascend')
92 %! alpha_dec = multinom_exp(m,n,'descend')