Mercurial > hg > octave-lyh
comparison scripts/specfun/nchoosek.m @ 14090:281ecc6fb431 stable
nchoosek.m: Update documentation, fix input validation, add more %!tests
* nchoosek.m: Update documentation, fix input validation, add more %!tests
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 21 Dec 2011 16:53:43 -0800 |
parents | 5b49cafe0599 |
children | 050bc580cb60 |
comparison
equal
deleted
inserted
replaced
14089:9c36b3b7c818 | 14090:281ecc6fb431 |
---|---|
16 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
17 ## along with Octave; see the file COPYING. If not, see | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {@var{c} =} nchoosek (@var{n}, @var{k}) | 21 ## @deftypefn {Function File} {@var{c} =} nchoosek (@var{n}, @var{k}) |
22 ## @deftypefnx {Function File} {@var{c} =} nchoosek (@var{set}, @var{k}) | |
22 ## | 23 ## |
23 ## Compute the binomial coefficient or all combinations of @var{n}. | 24 ## Compute the binomial coefficient or all combinations of a set of items. |
24 ## If @var{n} is a scalar then, calculate the binomial coefficient | 25 ## |
25 ## of @var{n} and @var{k}, defined as | 26 ## If @var{n} is a scalar then calculate the binomial coefficient |
27 ## of @var{n} and @var{k} which is defined as | |
26 ## @tex | 28 ## @tex |
27 ## $$ | 29 ## $$ |
28 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} | 30 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} |
29 ## = {n! \over k! (n-k)!} | 31 ## = {n! \over k! (n-k)!} |
30 ## $$ | 32 ## $$ |
40 ## \ / | 42 ## \ / |
41 ## @end group | 43 ## @end group |
42 ## @end example | 44 ## @end example |
43 ## | 45 ## |
44 ## @end ifnottex | 46 ## @end ifnottex |
47 ## @noindent | |
48 ## This is the number of combinations of @var{n} items taken in groups of | |
49 ## size @var{k}. | |
45 ## | 50 ## |
46 ## If @var{n} is a vector generate all combinations of the elements | 51 ## If the first argument is a vector, @var{set}, then generate all combinations |
47 ## of @var{n}, taken @var{k} at a time, one row per combination. The | 52 ## of the elements of @var{set}, taken @var{k} at a time, with one row per |
48 ## resulting @var{c} has size @code{[nchoosek (length (@var{n}), | 53 ## combination. The result @var{c} has @var{k} columns and |
49 ## @var{k}), @var{k}]}. | 54 ## @w{@code{nchoosek (length (@var{set}), @var{k})}} rows. |
50 ## | 55 ## |
51 ## @code{nchoosek} works only for non-negative integer arguments; use | 56 ## For example: |
52 ## @code{bincoeff} for non-integer scalar arguments and for using vector | |
53 ## arguments to compute many coefficients at once. | |
54 ## | 57 ## |
55 ## @seealso{bincoeff} | 58 ## How many ways can three items be grouped into pairs? |
59 ## | |
60 ## @example | |
61 ## @group | |
62 ## nchoosek (3, 2) | |
63 ## @result{} 3 | |
64 ## @end group | |
65 ## @end example | |
66 ## | |
67 ## What are the possible pairs? | |
68 ## | |
69 ## @example | |
70 ## @group | |
71 ## nchoosek (1:3, 2) | |
72 ## @result{} 1 2 | |
73 ## 1 3 | |
74 ## 2 3 | |
75 ## @end group | |
76 ## @end example | |
77 ## | |
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 for @var{n} | |
81 ## or @var{k}. | |
82 ## | |
83 ## @seealso{bincoeff, perms} | |
56 ## @end deftypefn | 84 ## @end deftypefn |
57 | 85 |
58 ## Author: Rolf Fabian <fabian@tu-cottbus.de> | 86 ## Author: Rolf Fabian <fabian@tu-cottbus.de> |
59 ## Author: Paul Kienzle <pkienzle@users.sf.net> | 87 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
60 ## Author: Jaroslav Hajek | 88 ## Author: Jaroslav Hajek |
61 | 89 |
62 function A = nchoosek (v, k) | 90 function A = nchoosek (v, k) |
63 | 91 |
64 if (nargin != 2 | 92 if (nargin != 2 |
65 || !isnumeric(k) || !isnumeric(v) | 93 || !isnumeric (k) || !isnumeric (v) |
66 || !isscalar(k) || (!isscalar(v) && !isvector(v))) | 94 || !isscalar (k) || ! (isscalar (v) || isvector (v))) |
67 print_usage (); | 95 print_usage (); |
68 endif | 96 endif |
69 if ((isscalar(v) && v < k) || k < 0 | 97 if (k < 0 || k != fix (k) |
70 || k != round(k) || any (v < 0 || v != round(v))) | 98 || (isscalar (v) && (v < k || v < 0 || v != fix (v)))) |
71 error ("nchoosek: args are non-negative integers with V not less than K"); | 99 error ("nchoosek: args are non-negative integers with V not less than K"); |
72 endif | 100 endif |
73 | 101 |
74 n = length (v); | 102 n = length (v); |
75 | 103 |
109 clear cA b; | 137 clear cA b; |
110 A = A.'; | 138 A = A.'; |
111 endif | 139 endif |
112 endfunction | 140 endfunction |
113 | 141 |
114 %!warning (nchoosek(100,45)); | 142 |
115 %!error (nchoosek(100,45.5)); | 143 %!assert (nchoosek (80,10), bincoeff (80,10)) |
116 %!error (nchoosek(100,145)); | 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]) |
117 %!assert (nchoosek(80,10), bincoeff(80,10)) | 145 |
118 %!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) | |
157 |