2538
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
2538
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
2538
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2538
|
18 |
3321
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Mapping Function} {} bincoeff (@var{n}, @var{k}) |
|
21 ## Return the binomial coefficient of @var{n} and @var{k}, defined as |
|
22 ## @iftex |
|
23 ## @tex |
|
24 ## $$ |
|
25 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} |
|
26 ## $$ |
|
27 ## @end tex |
|
28 ## @end iftex |
|
29 ## @ifinfo |
3426
|
30 ## |
3321
|
31 ## @example |
|
32 ## @group |
|
33 ## / \ |
|
34 ## | n | n (n-1) (n-2) ... (n-k+1) |
|
35 ## | | = ------------------------- |
|
36 ## | k | k! |
|
37 ## \ / |
|
38 ## @end group |
|
39 ## @end example |
|
40 ## @end ifinfo |
3426
|
41 ## |
3321
|
42 ## For example, |
3426
|
43 ## |
3321
|
44 ## @example |
|
45 ## @group |
|
46 ## bincoeff (5, 2) |
|
47 ## @result{} 10 |
|
48 ## @end group |
|
49 ## @end example |
|
50 ## @end deftypefn |
2538
|
51 |
5428
|
52 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2538
|
53 ## Created: 8 October 1994 |
|
54 ## Adapted-By: jwe |
|
55 |
|
56 function b = bincoeff (n, k) |
3426
|
57 |
2538
|
58 if (nargin != 2) |
6046
|
59 print_usage (); |
2538
|
60 endif |
3426
|
61 |
2538
|
62 [retval, n, k] = common_size (n, k); |
|
63 if (retval > 0) |
3458
|
64 error ("bincoeff: n and k must be of common size or scalars"); |
2538
|
65 endif |
3426
|
66 |
4854
|
67 sz = size (n); |
|
68 b = zeros (sz); |
3426
|
69 |
5961
|
70 ind = (! (k >= 0) | (k != real (round (k))) | isnan (n)); |
|
71 b(ind) = NaN; |
|
72 |
|
73 ind = (k == 0); |
|
74 b(ind) = 1; |
2538
|
75 |
5961
|
76 ind = ((k > 0) & ((n == real (round (n))) & (n < 0))); |
6902
|
77 b(ind) = (-1) .^ k(ind) .* exp (gammaln (abs (n(ind)) + k(ind)) |
|
78 - gammaln (k(ind) + 1) |
|
79 - gammaln (abs (n(ind)))); |
3426
|
80 |
6902
|
81 ind = ((k > 0) & (n >= k)); |
|
82 b(ind) = exp (gammaln (n(ind) + 1) |
|
83 - gammaln (k(ind) + 1) |
|
84 - gammaln (n(ind) - k(ind) + 1)); |
|
85 |
|
86 ind = ((k > 0) & ((n != real (round (n))) & (n < k))); |
|
87 b(ind) = (1/pi) * exp (gammaln (n(ind) + 1) |
|
88 - gammaln (k(ind) + 1) |
|
89 + gammaln (k(ind) - n(ind)) |
|
90 + log (sin (pi * (n(ind) - k(ind) + 1)))); |
|
91 |
|
92 ## Clean up rounding errors. |
5961
|
93 ind = (n == round (n)); |
|
94 b(ind) = round (b(ind)); |
6902
|
95 |
|
96 ind = (n != round (n)); |
|
97 b(ind) = real (b(ind)); |
|
98 |
2538
|
99 endfunction |
|
100 |
6902
|
101 %!assert(bincoeff(4,2), 6) |
|
102 %!assert(bincoeff(2,4), 0) |
|
103 %!assert(bincoeff(0.4,2), -.12, 8*eps) |