annotate scripts/specfun/nchoosek.m @ 19251:83b88e20e9c1

nchoosek.m: Overhaul function. * nchoosek.m: Update docstring. Use same variable names in function as in documentation for clarity. Improve input validation. Don't manually clear variables at end of function which will go out of scope anyways and the memory reclaimed. Update built-in self tests.
author Rik <rik@octave.org>
date Fri, 29 Aug 2014 16:30:11 -0700
parents 1514f5337781
children 4197fc428c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 14363
diff changeset
1 ## Copyright (C) 2001-2013 Rolf Fabian and Paul Kienzle
8391
343f0fbca6eb implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents: 8361
diff changeset
2 ## Copyright (C) 2008 Jaroslav Hajek
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
3 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
5 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
9 ## your option) any later version.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
10 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
15 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
19
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
21 ## @deftypefn {Function File} {@var{c} =} nchoosek (@var{n}, @var{k})
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
22 ## @deftypefnx {Function File} {@var{c} =} nchoosek (@var{set}, @var{k})
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
23 ##
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
24 ## Compute the binomial coefficient of @var{n} or list all possible
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
25 ## combinations of a @var{set} of items.
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
26 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
27 ## If @var{n} is a scalar then calculate the binomial coefficient
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
28 ## of @var{n} and @var{k} which is defined as
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
29 ## @tex
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
30 ## $$
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
31 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!}
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6575
diff changeset
32 ## = {n! \over k! (n-k)!}
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
33 ## $$
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
34 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
35 ## @ifnottex
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
36 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
37 ## @example
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
38 ## @group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
39 ## / \
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
40 ## | n | n (n-1) (n-2) @dots{} (n-k+1) n!
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6575
diff changeset
41 ## | | = ------------------------- = ---------
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6575
diff changeset
42 ## | k | k! k! (n-k)!
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
43 ## \ /
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
44 ## @end group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
45 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10800
diff changeset
46 ##
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
47 ## @end ifnottex
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
48 ## @noindent
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
49 ## This is the number of combinations of @var{n} items taken in groups of
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
50 ## size @var{k}.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
51 ##
14093
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14090
diff changeset
52 ## If the first argument is a vector, @var{set}, then generate all
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14090
diff changeset
53 ## combinations of the elements of @var{set}, taken @var{k} at a time, with
050bc580cb60 doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14090
diff changeset
54 ## one row per combination. The result @var{c} has @var{k} columns and
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
55 ## @w{@code{nchoosek (length (@var{set}), @var{k})}} rows.
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
56 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
57 ## For example:
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
58 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
59 ## How many ways can three items be grouped into pairs?
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
60 ##
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
61 ## @example
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
62 ## @group
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
63 ## nchoosek (3, 2)
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
64 ## @result{} 3
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
65 ## @end group
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
66 ## @end example
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
67 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
68 ## What are the possible pairs?
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
69 ##
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
70 ## @example
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
71 ## @group
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
72 ## nchoosek (1:3, 2)
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
73 ## @result{} 1 2
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
74 ## 1 3
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
75 ## 2 3
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
76 ## @end group
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
77 ## @end example
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
78 ##
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
79 ## Programming Note: When calculating the binomial coefficient @code{nchoosek}
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
80 ## works only for non-negative, integer arguments. Use @code{bincoeff} for
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
81 ## non-integer and negative scalar arguments, or for computing many binomial
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
82 ## coefficients at once with vector inputs for @var{n} or @var{k}.
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
83 ##
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
84 ## @seealso{bincoeff, perms}
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
85 ## @end deftypefn
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
86
6316
a3a2580435c2 [project @ 2007-02-16 07:23:49 by jwe]
jwe
parents: 5827
diff changeset
87 ## Author: Rolf Fabian <fabian@tu-cottbus.de>
a3a2580435c2 [project @ 2007-02-16 07:23:49 by jwe]
jwe
parents: 5827
diff changeset
88 ## Author: Paul Kienzle <pkienzle@users.sf.net>
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
89 ## Author: Jaroslav Hajek
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
90
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
91 function C = nchoosek (v, k)
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
92
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
93 if (nargin != 2
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
94 || ! (isreal (k) && isscalar (k))
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
95 || ! (isnumeric (v) && isvector (v)))
6316
a3a2580435c2 [project @ 2007-02-16 07:23:49 by jwe]
jwe
parents: 5827
diff changeset
96 print_usage ();
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
97 endif
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
98 if (k < 0 || k != fix (k))
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
99 error ("nchoosek: K must be an integer >= 0");
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
100 elseif (isscalar (v) && (iscomplex (v) || v < k || v < 0 || v != fix (v)))
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
101 error ("nchoosek: N must be a non-negative integer >= K");
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
102 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
103
6318
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
104 n = length (v);
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
105
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
106 if (n == 1)
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8404
diff changeset
107 ## Improve precision at next step.
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8404
diff changeset
108 k = min (k, v-k);
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
109 C = round (prod ((v-k+1:v)./(1:k)));
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
110 if (C*2*k*eps >= 0.5)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
111 warning ("nchoosek: possible loss of precision");
8391
343f0fbca6eb implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents: 8361
diff changeset
112 endif
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
113 elseif (k == 0)
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
114 C = zeros (1,0);
6318
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
115 elseif (k == 1)
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
116 C = v(:);
8404
868149aac690 nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents: 8391
diff changeset
117 elseif (k == n)
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
118 C = v(:).';
8391
343f0fbca6eb implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents: 8361
diff changeset
119 elseif (k > n)
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
120 C = zeros (0, k, class (v));
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
121 elseif (k == 2)
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
122 ## Can do it without transpose.
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
123 x = repelems (v(1:n-1), [1:n-1; n-1:-1:1]).';
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
124 y = cat (1, cellslices (v(:), 2:n, n*ones (1, n-1)){:});
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
125 C = [x, y];
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
126 elseif (k < n)
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
127 v = v(:).';
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
128 C = v(k:n);
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
129 l = 1:n-k+1;
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
130 for j = 2:k
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
131 c = columns (C);
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
132 cA = cellslices (C, l, c*ones (1, n-k+1), 2);
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
133 l = c-l+1;
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
134 b = repelems (v(k-j+1:n-j+1), [1:n-k+1; l]);
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
135 C = [b; cA{:}];
10800
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
136 l = cumsum (l);
23b3ae008f5e optimize nchoosek
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
137 l = [1, 1 + l(1:n-k)];
8391
343f0fbca6eb implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents: 8361
diff changeset
138 endfor
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
139 C = C.';
6318
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
140 endif
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
141
8361
cf620941af1a Set max_recursion_depth and use a subfunction in nchoosek
Francesco Potortì <pot@gnu.org>
parents: 7017
diff changeset
142 endfunction
6318
bae85c1e0e2a [project @ 2007-02-16 08:15:25 by jwe]
jwe
parents: 6316
diff changeset
143
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
144
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
145 %!assert (nchoosek (80,10), bincoeff (80,10))
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
146 %!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])
18983
1514f5337781 nchoosek.m: nchoosek(N,0) now returns [](1x0) when N is a vector (bug #41890).
Pedro Angelo <fonini@poli.ufrj.br>
parents: 17744
diff changeset
147 %!assert (size (nchoosek (1:5,0)), [1 0])
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
148
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
149 %% Test input validation
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
150 %!error nchoosek ()
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
151 %!error nchoosek (1)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
152 %!error nchoosek (1,2,3)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
153
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
154 %!error nchoosek (100, 2i)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
155 %!error nchoosek (100, [2 3])
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
156 %!error nchoosek ("100", 45)
19251
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
157 %!error nchoosek (100*ones (2, 2), 45)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
158 %!error <K must be an integer .= 0> nchoosek (100, -45)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
159 %!error <K must be an integer .= 0> nchoosek (100, 45.5)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
160 %!error <N must be a non-negative integer .= K> nchoosek (100i, 2)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
161 %!error <N must be a non-negative integer .= K> nchoosek (100, 145)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
162 %!error <N must be a non-negative integer .= K> nchoosek (-100, 45)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
163 %!error <N must be a non-negative integer .= K> nchoosek (100.5, 45)
83b88e20e9c1 nchoosek.m: Overhaul function.
Rik <rik@octave.org>
parents: 18983
diff changeset
164 %!warning <possible loss of precision> nchoosek (100, 45);
14090
281ecc6fb431 nchoosek.m: Update documentation, fix input validation, add more %!tests
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
165