1 ## Copyright (C) 2001-2012 Rolf Fabian and Paul Kienzle
2 ## Copyright (C) 2008 Jaroslav Hajek
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {@var{c} =} nchoosek (@var{n}, @var{k})
22 ## @deftypefnx {Function File} {@var{c} =} nchoosek (@var{set}, @var{k})
24 ## Compute the binomial coefficient or all combinations of a set of items.
26 ## If @var{n} is a scalar then calculate the binomial coefficient
27 ## of @var{n} and @var{k} which is defined as
30 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!}
31 ## = {n! \over k! (n-k)!}
39 ## | n | n (n-1) (n-2) @dots{} (n-k+1) n!
40 ## | | = ------------------------- = ---------
48 ## This is the number of combinations of @var{n} items taken in groups of
51 ## If the first argument is a vector, @var{set}, then generate all
52 ## combinations of the elements of @var{set}, taken @var{k} at a time, with
53 ## one row per combination. The result @var{c} has @var{k} columns and
54 ## @w{@code{nchoosek (length (@var{set}), @var{k})}} rows.
58 ## How many ways can three items be grouped into pairs?
67 ## What are the possible pairs?
78 ## @code{nchoosek} works only for non-negative, integer arguments. Use
79 ## @code{bincoeff} for non-integer and negative scalar arguments, or for
80 ## computing many binomial coefficients at once with vector inputs
81 ## for @var{n} or @var{k}.
83 ## @seealso{bincoeff, perms}
86 ## Author: Rolf Fabian <fabian@tu-cottbus.de>
87 ## Author: Paul Kienzle <pkienzle@users.sf.net>
88 ## Author: Jaroslav Hajek
90 function A = nchoosek (v, k)
93 || !isnumeric (k) || !isnumeric (v)
94 || !isscalar (k) || ! (isscalar (v) || isvector (v)))
97 if (k < 0 || k != fix (k)
98 || (isscalar (v) && (v < k || v < 0 || v != fix (v))))
99 error ("nchoosek: args are non-negative integers with V not less than K");
105 ## Improve precision at next step.
107 A = round (prod ((v-k+1:v)./(1:k)));
108 if (A*2*k*eps >= 0.5)
109 warning ("nchoosek", "nchoosek: possible loss of precision");
118 A = zeros (0, k, class (v));
120 ## Can do it without transpose.
121 x = repelems (v(1:n-1), [1:n-1; n-1:-1:1]).';
122 y = cat (1, cellslices (v(:), 2:n, n*ones (1, n-1)){:});
130 cA = cellslices (A, l, c*ones (1, n-k+1), 2);
132 b = repelems (v(k-j+1:n-j+1), [1:n-k+1; l]);
135 l = [1, 1 + l(1:n-k)];
143 %!assert (nchoosek (80,10), bincoeff (80,10))
144 %!assert (nchoosek(1:5,3), [1:3;1,2,4;1,2,5;1,3,4;1,3,5;1,4,5;2:4;2,3,5;2,4,5;3:5])
146 %% Test input validation
147 %!warning nchoosek (100,45);
148 %!error nchoosek ("100", 45)
149 %!error nchoosek (100, "45")
150 %!error nchoosek (100, ones (2,2))
151 %!error nchoosek (repmat (100, [2 2]), 45)
152 %!error nchoosek (100, -45)
153 %!error nchoosek (100, 45.5)
154 %!error nchoosek (100, 145)
155 %!error nchoosek (-100, 45)
156 %!error nchoosek (100.5, 45)