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 |
2538
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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 ## @iftex |
|
24 ## @tex |
|
25 ## $$ |
|
26 ## {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} |
|
27 ## $$ |
|
28 ## @end tex |
|
29 ## @end iftex |
|
30 ## @ifinfo |
3426
|
31 ## |
3321
|
32 ## @example |
|
33 ## @group |
|
34 ## / \ |
|
35 ## | n | n (n-1) (n-2) ... (n-k+1) |
|
36 ## | | = ------------------------- |
|
37 ## | k | k! |
|
38 ## \ / |
|
39 ## @end group |
|
40 ## @end example |
|
41 ## @end ifinfo |
3426
|
42 ## |
3321
|
43 ## For example, |
3426
|
44 ## |
3321
|
45 ## @example |
|
46 ## @group |
|
47 ## bincoeff (5, 2) |
|
48 ## @result{} 10 |
|
49 ## @end group |
|
50 ## @end example |
|
51 ## @end deftypefn |
2538
|
52 |
5428
|
53 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2538
|
54 ## Created: 8 October 1994 |
|
55 ## Adapted-By: jwe |
|
56 |
|
57 function b = bincoeff (n, k) |
3426
|
58 |
2538
|
59 if (nargin != 2) |
6046
|
60 print_usage (); |
2538
|
61 endif |
3426
|
62 |
2538
|
63 [retval, n, k] = common_size (n, k); |
|
64 if (retval > 0) |
3458
|
65 error ("bincoeff: n and k must be of common size or scalars"); |
2538
|
66 endif |
3426
|
67 |
4854
|
68 sz = size (n); |
|
69 b = zeros (sz); |
3426
|
70 |
5961
|
71 ind = (! (k >= 0) | (k != real (round (k))) | isnan (n)); |
|
72 b(ind) = NaN; |
|
73 |
|
74 ind = (k == 0); |
|
75 b(ind) = 1; |
2538
|
76 |
5961
|
77 ind = ((k > 0) & ((n == real (round (n))) & (n < 0))); |
|
78 b(ind) = (-1) .^ k(ind) .* exp (gammaln (abs (n(ind)) + k(ind)) ... |
|
79 - gammaln (k(ind) + 1) - gammaln (abs (n(ind)))); |
3426
|
80 |
5961
|
81 ind = ((k > 0) & ((n != real (round (n))) | (n >= k))); |
|
82 b(ind) = exp (gammaln (n(ind) + 1) - gammaln (k(ind) + 1) ... |
|
83 - gammaln (n(ind) - k(ind) + 1)); |
|
84 |
2538
|
85 ## clean up rounding errors |
5961
|
86 ind = (n == round (n)); |
|
87 b(ind) = round (b(ind)); |
|
88 |
2538
|
89 endfunction |
|
90 |