Mercurial > hg > octave-lyh
changeset 6532:25cad08843a0
[project @ 2007-04-17 21:05:30 by jwe]
author | jwe |
---|---|
date | Tue, 17 Apr 2007 21:05:30 +0000 |
parents | c2609d0502bb |
children | c64687e6f1c3 |
files | scripts/ChangeLog scripts/specfun/factorial.m |
diffstat | 2 files changed, 14 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2007-04-17 Paul Kienzle <pkienzle@users.sf.net> + + * specfun/factorial.m: Use gamma function instead of cumprod. + Add tests. + 2007-04-16 John W. Eaton <jwe@octave.org> * gethelp.cc (looks_like_octave_copyright): Use same logic as in
--- a/scripts/specfun/factorial.m +++ b/scripts/specfun/factorial.m @@ -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")