]> Creatis software - CreaPhase.git/blob - octave_packages/m/specfun/beta.m
update packages
[CreaPhase.git] / octave_packages / m / specfun / beta.m
1 ## Copyright (C) 1994-2012 John W. Eaton
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Mapping Function} {} beta (@var{a}, @var{b})
21 ## For real inputs, return the Beta function,
22 ## @tex
23 ## $$
24 ##  B (a, b) = {\Gamma (a) \Gamma (b) \over \Gamma (a + b)}.
25 ## $$
26 ## @end tex
27 ## @ifnottex
28 ##
29 ## @example
30 ## beta (a, b) = gamma (a) * gamma (b) / gamma (a + b).
31 ## @end example
32 ##
33 ## @end ifnottex
34 ## @end deftypefn
35
36 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
37 ## Created: 13 June 1993
38 ## Adapted-By: jwe
39
40 function retval = beta (a, b)
41
42   if (nargin != 2)
43     print_usage ();
44   endif
45
46   if (any (size (a) != size (b)) && numel (a) != 1 && numel (b) != 1)
47     error ("beta: inputs A and B have inconsistent sizes");
48   endif
49
50   if (! isreal (a) || ! isreal (b))
51     error ("beta: inputs A and B must be real");
52   endif
53
54   retval = real (exp (gammaln (a) + gammaln (b) - gammaln (a+b)));
55
56 endfunction
57
58 %!test
59 %! a=[1, 1.5, 2, 3];
60 %! b=[4, 3, 2, 1];
61 %! v1=beta(a,b);
62 %! v2=beta(b,a);
63 %! v3=gamma(a).*gamma(b)./gamma(a+b);
64 %! assert(all(abs(v1-v2)<sqrt(eps)) && all(abs(v2-v3)<sqrt(eps)));
65
66 %!error beta();
67
68 %!error beta(1);
69
70 %!assert (1, beta (1, 1))
71
72 %!test
73 %! a = 2:10;
74 %! tol = 10 * max (a) * eps;
75 %! assert (-a, beta (-1./a, 1), tol)
76 %! assert (-a, beta (1, -1./a), tol)
77
78 %!test
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)