1########################################################################
2##
3## Copyright (C) 1998-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn {} {} betaln (@var{a}, @var{b})
28## Compute the natural logarithm of the Beta function for real inputs @var{a}
29## and @var{b}.
30##
31## @code{betaln} is defined as
32## @tex
33## $$
34##  {\rm betaln} (a, b) = \ln (B (a,b)) \equiv \ln ({\Gamma (a) \Gamma (b) \over \Gamma (a + b)}).
35## $$
36## @end tex
37## @ifnottex
38##
39## @example
40## betaln (a, b) = log (beta (a, b))
41## @end example
42##
43## @end ifnottex
44## and is calculated in a way to reduce the occurrence of underflow.
45##
46## The Beta function can grow quite large and it is often more useful to work
47## with the logarithm of the output rather than the function directly.
48## @seealso{beta, betainc, betaincinv, gammaln}
49## @end deftypefn
50
51function retval = betaln (a, b)
52
53  if (nargin != 2)
54    print_usage ();
55  endif
56
57  if (! isreal (a) || ! isreal (b))
58    error ("betaln: A and B must be real");
59  elseif (! size_equal (a, b) && numel (a) != 1 && numel (b) != 1)
60    error ("betaln: A and B must have consistent sizes");
61  endif
62
63  retval = gammaln (a) + gammaln (b) - gammaln (a + b);
64
65endfunction
66
67
68%!assert (betaln (3,4), log (beta (3,4)), eps)
69
70## Test input validation
71%!error betaln ()
72%!error betaln (1)
73%!error betaln (1,2,3)
74%!error <A and B must be real> betaln (1i, 2)
75%!error <A and B must be real> betaln (2, 1i)
76%!error <A and B must have consistent sizes> betaln ([1 2], [1 2 3])
77%!error <A and B must have consistent sizes> betaln ([1 2 3], [1 2])
78%!error <A and B must have consistent sizes> betaln ([1 2 3], [1 2 3]')
79