Mercurial > hg > octave-lyh
annotate scripts/specfun/factorial.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | 16f53d29049f |
children | 1740012184f9 |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2000, 2006, 2007, 2009 Paul Kienzle |
5820 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5820 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5820 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} factorial (@var{n}) | |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
21 ## Return the factorial of @var{n} where @var{n} is a positive integer. If |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
22 ## @var{n} is a scalar, this is equivalent to @code{prod (1:@var{n})}. For |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
23 ## vector or matrix arguments, return the factorial of each element in the |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
24 ## array. For non-integers see the generalized factorial function |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
25 ## @code{gamma}. |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
26 ## @seealso{prod, gamma} |
5820 | 27 ## @end deftypefn |
28 | |
29 function x = factorial (n) | |
6391 | 30 if (nargin != 1) |
31 print_usage (); | |
6532 | 32 elseif (any (n(:) < 0 | n(:) != round (n(:)))) |
33 error ("factorial: n must all be nonnegative integers"); | |
5820 | 34 endif |
6532 | 35 x = round (gamma (n+1)); |
5820 | 36 endfunction |
6532 | 37 |
38 %!assert (factorial(5), prod(1:5)) | |
39 %!assert (factorial([1,2;3,4]), [1,2;6,24]) | |
6542 | 40 %!assert (factorial(70), exp(sum(log(1:70))), -128*eps) |
6532 | 41 %!fail ('factorial(5.5)', "must all be nonnegative integers") |
42 %!fail ('factorial(-3)', "must all be nonnegative integers") |