On 15 September 2011 10:08, Juan Pablo Carbajal <[email protected]> wrote:
> Dear all,
>
> I have added to the features tracker a small set of functions that I
> think they could go into the specfun package.
>
> https://sourceforge.net/tracker/?func=detail&aid=3409993&group_id=2888&atid=352888
>

Below is a much better implementation of multinom_exp, which uses the
standard stars and bars method for counting monomials, or in this
case, enumerating them:

    function alpha = multinom_exp(m, n)

      ## 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 = rows(idx);
      idx = [repmat([1:nr], n,1)(:), idx'(:)];
      alpha = accumarray (idx, ones(nr*n, 1));

    endfunction

HTH,
- Jordi G. H.

------------------------------------------------------------------------------
BlackBerry&reg; 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

Reply via email to