1 ## Copyright (C) 1994-2012 John W. Eaton
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Mapping Function} {} beta (@var{a}, @var{b})
21 ## For real inputs, return the Beta function,
24 ## B (a, b) = {\Gamma (a) \Gamma (b) \over \Gamma (a + b)}.
30 ## beta (a, b) = gamma (a) * gamma (b) / gamma (a + b).
36 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
37 ## Created: 13 June 1993
40 function retval = beta (a, b)
46 if (any (size (a) != size (b)) && numel (a) != 1 && numel (b) != 1)
47 error ("beta: inputs A and B have inconsistent sizes");
50 if (! isreal (a) || ! isreal (b))
51 error ("beta: inputs A and B must be real");
54 retval = real (exp (gammaln (a) + gammaln (b) - gammaln (a+b)));
63 %! v3=gamma(a).*gamma(b)./gamma(a+b);
64 %! assert(all(abs(v1-v2)<sqrt(eps)) && all(abs(v2-v3)<sqrt(eps)));
70 %!assert (1, beta (1, 1))
74 %! tol = 10 * max (a) * eps;
75 %! assert (-a, beta (-1./a, 1), tol)
76 %! assert (-a, beta (1, -1./a), tol)
79 %! a = 0.25 + (0:5) * 0.5;
80 %! tol = 10 * max (a) * eps;
81 %! assert (zeros (size (a)), beta (a, -a), tol)
82 %! assert (zeros (size (a)), beta (-a, a), tol)