changeset 19132:4591a1238ee0

betaln.m: Add input validation. * betaln.m: Improve docstring. Add input validation. Add %!error tests for input validation.
author Rik <rik@octave.org>
date Fri, 19 Sep 2014 14:25:21 -0700
parents e7bffcea619f
children 55b613e5183d
files scripts/specfun/betaln.m
diffstat 1 files changed, 23 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/specfun/betaln.m	Fri Sep 19 14:21:46 2014 -0700
+++ b/scripts/specfun/betaln.m	Fri Sep 19 14:25:21 2014 -0700
@@ -18,7 +18,10 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {Mapping Function} {} betaln (@var{a}, @var{b})
-## Return the natural logarithm of the Beta function,
+## Compute the natural logarithm of the Beta function for real inputs @var{a}
+## and @var{b}.
+##
+## @code{betaln} is defined as
 ## @tex
 ## $$
 ##  {\rm betaln} (a, b) = \ln (B (a,b)) \equiv \ln ({\Gamma (a) \Gamma (b) \over \Gamma (a + b)}).
@@ -31,8 +34,11 @@
 ## @end example
 ##
 ## @end ifnottex
-## calculated in a way to reduce the occurrence of underflow.
-## @seealso{beta, betainc, gammaln}
+## and is calculated in a way to reduce the occurrence of underflow.
+##
+## The Beta function can grow quite large and it is often more useful to work
+## with the logarithm of the output rather than the function directly.
+## @seealso{beta, betainc, betaincinv, gammaln}
 ## @end deftypefn
 
 ## Author:   Nicol N. Schraudolph <nic@idsia.ch>
@@ -45,6 +51,12 @@
     print_usage ();
   endif
 
+  if (! isreal (a) || ! isreal (b))
+    error ("betaln: A and B must be real");
+  elseif (! size_equal (a, b) && numel (a) != 1 && numel (b) != 1)
+    error ("betaln: A and B must have consistent sizes");
+  endif
+
   retval = gammaln (a) + gammaln (b) - gammaln (a + b);
 
 endfunction
@@ -53,6 +65,12 @@
 %!assert (betaln (3,4), log (beta (3,4)), eps)
 
 %% Test input validation
-%!error (betaln (1))
-%!error (betaln (1,2,3))
+%!error betaln ()
+%!error betaln (1)
+%!error betaln (1,2,3)
+%!error <A and B must be real> betaln (1i, 2)
+%!error <A and B must be real> betaln (2, 1i)
+%!error <A and B must have consistent sizes> betaln ([1 2], [1 2 3])
+%!error <A and B must have consistent sizes> betaln ([1 2 3], [1 2])
+%!error <A and B must have consistent sizes> betaln ([1 2 3], [1 2 3]')