# HG changeset patch # User jwe # Date 1176843930 0 # Node ID 25cad08843a07f7575d30ecb4d6ad015fc789041 # Parent c2609d0502bbaaa5c76544a95740e066054a4ff2 [project @ 2007-04-17 21:05:30 by jwe] diff -r c2609d0502bb -r 25cad08843a0 scripts/ChangeLog --- a/scripts/ChangeLog Tue Apr 17 18:17:40 2007 +0000 +++ b/scripts/ChangeLog Tue Apr 17 21:05:30 2007 +0000 @@ -1,3 +1,8 @@ +2007-04-17 Paul Kienzle + + * specfun/factorial.m: Use gamma function instead of cumprod. + Add tests. + 2007-04-16 John W. Eaton * gethelp.cc (looks_like_octave_copyright): Use same logic as in diff -r c2609d0502bb -r 25cad08843a0 scripts/specfun/factorial.m --- a/scripts/specfun/factorial.m Tue Apr 17 18:17:40 2007 +0000 +++ b/scripts/specfun/factorial.m Tue Apr 17 21:05:30 2007 +0000 @@ -27,15 +27,14 @@ function x = factorial (n) if (nargin != 1) print_usage (); - elseif (any (n(:) < 0)) - error ("factorial: n be be a scalar or array of positive integers"); + elseif (any (n(:) < 0 | n(:) != round (n(:)))) + error ("factorial: n must all be nonnegative integers"); endif - if (isscalar (n)) - x = prod (2:n); - else - n (n < 1) = 1; - m = max (n(:)); - c = cumprod (1:m); - x = c(floor (n)); - endif + x = round (gamma (n+1)); endfunction + +%!assert (factorial(5), prod(1:5)) +%!assert (factorial([1,2;3,4]), [1,2;6,24]) +%!assert (factorial(70), exp(sum(log(1:70))), -10*eps) +%!fail ('factorial(5.5)', "must all be nonnegative integers") +%!fail ('factorial(-3)', "must all be nonnegative integers")