octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

nchoosek: a better solution


From: John W. Eaton
Subject: nchoosek: a better solution
Date: Tue, 25 Nov 2008 14:58:27 -0500

On 25-Nov-2008, Francesco Potortì wrote:

| I think I have found a better solution for nchoosek.  Here I use a
| subfunction.  I also added two tests.
| 
| I can make a new changeset, if people think this solution is better as I
| do.  How do I convince Mercurial to make a changeset against the
| previous version?
| 
| 
| ===File /home/pot/gnu/octave/scripts/specfun/nchoosek.m=====
| ## Copyright (C) 2001, 2006, 2007 Rolf Fabian and Paul Kienzle
| ##
| ## This file is part of Octave.
| ##
| ## Octave 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 (at
| ## your option) any later version.
| ##
| ## Octave 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 Octave; see the file COPYING.  If not, see
| ## <http://www.gnu.org/licenses/>.
| 
| ## -*- texinfo -*-
| ## @deftypefn {Function File} address@hidden =} nchoosek (@var{n}, @var{k})
| ##
| ## Compute the binomial coefficient or all combinations of @var{n}.
| ## If @var{n} is a scalar then, calculate the binomial coefficient
| ## of @var{n} and @var{k}, defined as
| ##
| ## @iftex
| ## @tex
| ## $$
| ##  {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!}
| ##                = {n! \over k! (n-k)!}
| ## $$
| ## @end tex
| ## @end iftex
| ## @ifinfo
| ##
| ## @example
| ## @group
| ##  /   \
| ##  | n |    n (n-1) (n-2) ... (n-k+1)       n!
| ##  |   |  = ------------------------- =  ---------
| ##  | k |               k!                k! (n-k)!
| ##  \   /
| ## @end group
| ## @end example
| ## @end ifinfo
| ##
| ## If @var{n} is a vector generate all combinations of the elements
| ## of @var{n}, taken @var{k} at a time, one row per combination. The
| ## resulting @var{c} has size @code{[nchoosek (length (@var{n}),
| ## @var{k}), @var{k}]}.
| ##
| ## @seealso{bincoeff}
| ## @end deftypefn
| 
| ## Author: Rolf Fabian  <address@hidden>
| ## Author: Paul Kienzle <address@hidden>
| 
| ## FIXME -- This function is identical to bincoeff for scalar
| ## values, and so should probably be combined with bincoeff.
| 
| function A = nchoosek (v, k)
| 
|   if (nargin != 2)
|     print_usage ();
|   endif
| 
|   n = length (v);
| 
|   if (n == 1)
|      A = round (exp (sum (log (k+1:v)) - sum (log (2:v-k))));
|   elseif (k == 0)
|     A = [];
|   elseif (k == 1)
|     A = v(:);
|   elseif (k == n)
|     A = v(:).';
|   else
|     oldmax = max_recursion_depth (2*n);
|     A = nck (v, k);
|     max_recursion_depth (oldmax);

You should still use unwdind_protect any time you want to make a
temporary change to a global setting like this.

jwe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]