Mercurial > hg > octave-lyh
annotate scripts/specfun/nchoosek.m @ 10549:95c3e38098bf
Untabify .m scripts
author | Rik <code@nomad.inbox5.com> |
---|---|
date | Fri, 23 Apr 2010 11:28:50 -0700 |
parents | a7a9eecc07b5 |
children | 23b3ae008f5e |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2001, 2006, 2007, 2009 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 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5827 | 10 ## |
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. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5827 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {@var{c} =} nchoosek (@var{n}, @var{k}) | |
22 ## | |
6575 | 23 ## Compute the binomial coefficient or all combinations of @var{n}. |
5827 | 24 ## If @var{n} is a scalar then, calculate the binomial coefficient |
25 ## of @var{n} and @var{k}, defined as | |
26 ## | |
27 ## @tex | |
28 ## $$ | |
29 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} | |
6754 | 30 ## = {n! \over k! (n-k)!} |
5827 | 31 ## $$ |
32 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8506
diff
changeset
|
33 ## @ifnottex |
5827 | 34 ## |
35 ## @example | |
36 ## @group | |
37 ## / \ | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
38 ## | n | n (n-1) (n-2) @dots{} (n-k+1) n! |
6754 | 39 ## | | = ------------------------- = --------- |
40 ## | k | k! k! (n-k)! | |
5827 | 41 ## \ / |
42 ## @end group | |
43 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8506
diff
changeset
|
44 ## @end ifnottex |
5827 | 45 ## |
46 ## If @var{n} is a vector generate all combinations of the elements | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
47 ## of @var{n}, taken @var{k} at a time, one row per combination. The |
5827 | 48 ## resulting @var{c} has size @code{[nchoosek (length (@var{n}), |
49 ## @var{k}), @var{k}]}. | |
50 ## | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
51 ## @code{nchoosek} works only for non-negative integer arguments; use |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
52 ## @code{bincoeff} for non-integer scalar arguments and for using vector |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
53 ## arguments to compute many coefficients at once. |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
54 ## |
6316 | 55 ## @seealso{bincoeff} |
5827 | 56 ## @end deftypefn |
57 | |
6316 | 58 ## Author: Rolf Fabian <fabian@tu-cottbus.de> |
59 ## 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
|
60 ## Author: Jaroslav Hajek |
5827 | 61 |
62 function A = nchoosek (v, k) | |
63 | |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
64 if (nargin != 2 |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
65 || !isnumeric(k) || !isnumeric(v) |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
66 || !isscalar(k) || (!isscalar(v) && !isvector(v))) |
6316 | 67 print_usage (); |
5827 | 68 endif |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
69 if ((isscalar(v) && v < k) || k < 0 |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
70 || k != round(k) || any (v < 0 || v != round(v))) |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
71 error ("nchoosek: args are nonnegative integers with V not less than K"); |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
72 endif |
5827 | 73 |
6318 | 74 n = length (v); |
75 | |
76 if (n == 1) | |
8506 | 77 ## Improve precision at next step. |
78 k = min (k, v-k); | |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
79 A = round (prod ((v-k+1:v)./(1:k))); |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
80 if (A*2*k*eps >= 0.5) |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
81 warning ("nchoosek", "nchoosek: possible loss of precision"); |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
82 endif |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
83 elseif (k == 0) |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
84 A = []; |
6318 | 85 elseif (k == 1) |
86 A = v(:); | |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
87 elseif (k == n) |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
88 A = v(:).'; |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
89 elseif (k > n) |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
90 A = zeros (0, k, class (v)); |
6318 | 91 else |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
92 p = cell (1, k); |
8506 | 93 ## Hack: do the op in the smallest integer class possible to avoid |
94 ## moving too much data. | |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
95 if (n < intmax ("uint8")) |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
96 cl = "uint8"; |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
97 elseif (n < intmax ("uint16")) |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
98 cl = "uint16"; |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
99 elseif (n < intmax ("uint32")) |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
100 cl = "uint32"; |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
101 else |
8506 | 102 ## This would exhaust memory anyway. |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
103 cl = "double"; |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
104 endif |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
105 |
8506 | 106 ## Use a generalized Pascal triangle. Traverse backwards to keep |
107 ## alphabetical order. | |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
108 for i = 1:k |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
109 p{i} = zeros (0, i, cl); |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
110 endfor |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
111 s = ones (1, 1, cl); |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
112 p{1} = n*s; |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
113 for j = n-1:-1:1 |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
114 for i = k:-1:2 |
10549 | 115 q = p{i-1}; |
116 p{i} = [[repmat(s*j, rows (p{i-1}), 1), p{i-1}]; p{i}]; | |
8391
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
117 endfor |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
118 p{1} = [j;p{1}]; |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
119 endfor |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
120 v = v(:); |
343f0fbca6eb
implement nchoosek without recursion
Jaroslav Hajek <highegg@gmail.com>
parents:
8361
diff
changeset
|
121 A = v(p{k}); |
6318 | 122 endif |
8361
cf620941af1a
Set max_recursion_depth and use a subfunction in nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7017
diff
changeset
|
123 endfunction |
6318 | 124 |
8404
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
125 %!warning (nchoosek(100,45)); |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
126 %!error (nchoosek(100,45.5)); |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
127 %!error (nchoosek(100,145)); |
868149aac690
nchoosek checks, warning, corner cases and tests
Francesco Potortì <pot@gnu.org>
parents:
8391
diff
changeset
|
128 %!assert (nchoosek(80,10), bincoeff(80,10)) |
8361
cf620941af1a
Set max_recursion_depth and use a subfunction in nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7017
diff
changeset
|
129 %!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]) |