Mercurial > octave-nkf
annotate scripts/miscellaneous/bincoeff.m @ 10549:95c3e38098bf
Untabify .m scripts
author | Rik <code@nomad.inbox5.com> |
---|---|
date | Fri, 23 Apr 2010 11:28:50 -0700 |
parents | f0c3d3fc4903 |
children | 693e22af08ae |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2004, 2005, 2006, 2007, |
2 ## 2008, 2009 Kurt Hornik | |
3426 | 3 ## |
3922 | 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. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
2538 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
2538 | 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/>. | |
2538 | 19 |
3321 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Mapping Function} {} bincoeff (@var{n}, @var{k}) | |
22 ## Return the binomial coefficient of @var{n} and @var{k}, defined as | |
23 ## @tex | |
24 ## $$ | |
25 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} | |
26 ## $$ | |
27 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8435
diff
changeset
|
28 ## @ifnottex |
3426 | 29 ## |
3321 | 30 ## @example |
31 ## @group | |
32 ## / \ | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
33 ## | n | n (n-1) (n-2) @dots{} (n-k+1) |
3321 | 34 ## | | = ------------------------- |
35 ## | k | k! | |
36 ## \ / | |
37 ## @end group | |
38 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8435
diff
changeset
|
39 ## @end ifnottex |
3426 | 40 ## |
3321 | 41 ## For example, |
3426 | 42 ## |
3321 | 43 ## @example |
44 ## @group | |
45 ## bincoeff (5, 2) | |
46 ## @result{} 10 | |
47 ## @end group | |
48 ## @end example | |
8435
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
49 ## |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
50 ## In most cases, the @code{nchoosek} function is faster for small |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
51 ## scalar integer arguments. It also warns about loss of precision for |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
52 ## big arguments. |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
53 ## |
69e27978114a
bincoeff.m: make reference to nchoosek
Francesco Potortì <pot@gnu.org>
parents:
7385
diff
changeset
|
54 ## @seealso{nchoosek} |
3321 | 55 ## @end deftypefn |
2538 | 56 |
5428 | 57 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2538 | 58 ## Created: 8 October 1994 |
59 ## Adapted-By: jwe | |
60 | |
61 function b = bincoeff (n, k) | |
3426 | 62 |
2538 | 63 if (nargin != 2) |
6046 | 64 print_usage (); |
2538 | 65 endif |
3426 | 66 |
2538 | 67 [retval, n, k] = common_size (n, k); |
68 if (retval > 0) | |
3458 | 69 error ("bincoeff: n and k must be of common size or scalars"); |
2538 | 70 endif |
3426 | 71 |
4854 | 72 sz = size (n); |
73 b = zeros (sz); | |
3426 | 74 |
5961 | 75 ind = (! (k >= 0) | (k != real (round (k))) | isnan (n)); |
76 b(ind) = NaN; | |
77 | |
78 ind = (k == 0); | |
79 b(ind) = 1; | |
2538 | 80 |
5961 | 81 ind = ((k > 0) & ((n == real (round (n))) & (n < 0))); |
6902 | 82 b(ind) = (-1) .^ k(ind) .* exp (gammaln (abs (n(ind)) + k(ind)) |
10549 | 83 - gammaln (k(ind) + 1) |
84 - gammaln (abs (n(ind)))); | |
3426 | 85 |
6902 | 86 ind = ((k > 0) & (n >= k)); |
87 b(ind) = exp (gammaln (n(ind) + 1) | |
10549 | 88 - gammaln (k(ind) + 1) |
89 - gammaln (n(ind) - k(ind) + 1)); | |
6902 | 90 |
91 ind = ((k > 0) & ((n != real (round (n))) & (n < k))); | |
92 b(ind) = (1/pi) * exp (gammaln (n(ind) + 1) | |
10549 | 93 - gammaln (k(ind) + 1) |
94 + gammaln (k(ind) - n(ind)) | |
95 + log (sin (pi * (n(ind) - k(ind) + 1)))); | |
6902 | 96 |
97 ## Clean up rounding errors. | |
5961 | 98 ind = (n == round (n)); |
99 b(ind) = round (b(ind)); | |
6902 | 100 |
101 ind = (n != round (n)); | |
102 b(ind) = real (b(ind)); | |
103 | |
2538 | 104 endfunction |
105 | |
6902 | 106 %!assert(bincoeff(4,2), 6) |
107 %!assert(bincoeff(2,4), 0) | |
108 %!assert(bincoeff(0.4,2), -.12, 8*eps) | |
7385 | 109 |
110 %!assert(bincoeff (5, 2) == 10 && bincoeff (50, 6) == 15890700); | |
111 | |
112 %!error bincoeff (); | |
113 | |
114 %!error bincoeff (1, 2, 3); |