2011/9/17 Juan Pablo Carbajal <[email protected]>:
> I changed the names of the functions. I added an optional argument
> to multinom_exp to make it compatible with the original version,
> since the order of the exponents was not the same.
>
> I added the new functions here
> https://sourceforge.net/tracker/?func=detail&aid=3410842&group_id=2888&atid=352888
I guess you really want this to run in Matlab, which is why you
changed the comment characters to % from #. There are number of
further changes in order to fit this with the more limited Matlab
languagen:
1) rows(x) has to be changed to size(x,1)
2) foo(x)(:) has to be changed to temp = foo(x); temp(:)
Additionally, I made a few stylistic changes while keeping Matlab
compatibility (I hope):
3) Octave style is to write foo (x) when foo is a function and
foo(x) when foo is an array to visually distinguish function
calls from array indexing. The exception is inside matrices,
since the interpreter reads [foo (x)] as [foo, (x)].
4) There was no need to call ones () in the accumarray call, which
seems like undocumented behaviour. Simply "1" instead was
enough. I'll push a documentation fix for that now.
5) A stray "%%s" seems to have happened when you replaced my ##
with %%.
6) I find it visually cleaner to use ndims than varargin for the
optional sorting parameter, since then you can easily use an
explicit for the third optional argument.
7) I changed [dummy, foo] = bar (...) to [~, foo] = bar (...),
which is syntax that works since Octave 3.4 and is more
explicit about discarding output parameters.
8) I added spaces after commas.
Given that I'm now making stylistic editorial decisions, I think I do
want my name on this thing. Thank you for that. ;-)
HTH,
- Jordi G. H.
%% Copyright (c) 2011 Jordi Gutiérrez Hermoso <[email protected]>
%% Copyright (c) 2011 Juan Pablo Carbajal <[email protected]>
%%
%% This program is free software: you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published by
%% the Free Software Foundation, either version 3 of the License, or
%% any later version.
%%
%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%% GNU General Public License for more details.
%%
%% You should have received a copy of the GNU General Public License
%% along with this program. If not, see <http://www.gnu.org/licenses/>.
%% -*- texinfo -*-
%% @deftypefn {Function File} {@var{alpha} =} multinomExp (@var{m}, @var{n})
%% @deftypefnx {Function File} {@var{alpha} =} multinomExp (@var{m}, @var{n},@var{order})
%% Returns the exponents of the terms in the multinomial expansion
%% @tex
%% $$
%% (x1 + x2 + ... + xm).^n
%% $$
%% @end tex
%% @ifnottex
%%
%% @example
%% (x1 + x2 + ... + xm).^n
%% @end example
%%
%% @end ifnottex
%%
%% For example, for m=2, n=3 the expansion has the terms
%% @tex
%% $$
%% x1^3, x2^3, x1^2*x2, x1*x2^2
%% $$
%% @end tex
%% @ifnottex
%%
%% @example
%% x1^3, x2^3, x1^2*x2, x1*x2^2
%% @end example
%%
%% @end ifnottex
%%
%% then @code{alpha = [3 0; 2 1; 1 2; 0 3]};
%%
%% The optional argument @var{order} is passed to function @code{sort} to
%% sort the exponents by the maximum degree.
%% The example above calling @code{ multinom(m,n,"ascend")} produces
%%
%% @code{alpha = [2 1; 1 2; 3 0; 0 3]};
%%
%% calling @code{ multinom(m,n,"descend")} produces
%%
%% @code{alpha = [3 0; 0 3; 2 1; 1 2]};
%%
%% @seealso{multinom, multinom_coeff, sort}
%% @end deftypefn
function alpha = multinom_exp(m, n, sortmethod)
%% This is standard stars and bars.
numsymbols = m+n-1;
stars = nchoosek (1:numsymbols, n);
%% Star labels minus their consecutive position becomes their index
%% position!
idx = bsxfun (@minus, stars, [0:n-1]);
%% Manipulate indices into the proper shape for accumarray.
nr = size (idx, 1);
a = repmat ([1:nr], n,1);
b = idx';
idx = [a(:), b(:)];
alpha = accumarray (idx, 1);
if nargin > 2
[~, idx] = sort (max (alpha, [], 2), 1, sortmethod);
alpha = alpha(idx, :);
end
end
%!demo
%! m=2;
%! n=3;
%! alpha = multinomExp(m,n)
%! alpha_asc = multinomExp(m,n,'ascend')
%! alpha_dec = multinomExp(m,n,'descend')
------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
http://p.sf.net/sfu/rim-devcon-copy2
_______________________________________________
Octave-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/octave-dev