Mercurial > octave
annotate scripts/specfun/factorial.m @ 21546:f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
* isrecording.m, soundsc.m, delaunay3.m, cell2mat.m, cumtrapz.m, del2.m,
inputParser.m, interp1.m, interp3.m, narginchk.m, profile.m,
validateattributes.m, delaunayn.m, tsearchn.m, voronoin.m, brighten.m,
cmunique.m, colorcube.m, imfinfo.m, imshow.m, edit.m, orderfields.m, run.m,
warning_ids.m, ode23.m, ode45.m, odeget.m, integrate_adaptive.m, kahan.m,
ode_struct_value_check.m, runge_kutta_23.m, fminunc.m, fsolve.m, fzero.m,
pkg.m, build.m, specular.m, view.m, bar.m, barh.m, contour3.m, isosurface.m,
line.m, pie.m, pie3.m, quiver3.m, scatter.m, scatter3.m, stem3.m, stemleaf.m,
surfl.m, tetramesh.m, isfigure.m, mkpp.m, pchip.m, residue.m, splinefit.m,
rmpref.m, unique.m, eigs.m, ilu.m, factor.m, factorial.m, gallery.m, hankel.m,
histc.m, ols.m, finv.m, fpdf.m, kruskal_wallis_test.m, weekday.m:
Wrap m-file docstrings to 79 characters + newline (80 total).
author | Rik <rik@octave.org> |
---|---|
date | Sun, 27 Mar 2016 15:50:01 -0700 |
parents | 516bb87ea72e |
children | bac0d6f07a3e |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19101
diff
changeset
|
1 ## Copyright (C) 2000-2015 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 -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
20 ## @deftypefn {} {} factorial (@var{n}) |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
21 ## Return the factorial of @var{n} where @var{n} is a real non-negative |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
22 ## integer. |
19101 | 23 ## |
24 ## If @var{n} is a scalar, this is equivalent to @code{prod (1:@var{n})}. For | |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
25 ## vector or matrix arguments, return the factorial of each element in the |
19101 | 26 ## array. |
27 ## | |
28 ## For non-integers see the generalized factorial function @code{gamma}. | |
29 ## Note that the factorial function grows large quite quickly, and even | |
30 ## with double precision values overflow will occur if @var{n} > 171. For | |
31 ## such cases consider @code{gammaln}. | |
32 ## @seealso{prod, gamma, gammaln} | |
5820 | 33 ## @end deftypefn |
34 | |
35 function x = factorial (n) | |
19101 | 36 |
6391 | 37 if (nargin != 1) |
38 print_usage (); | |
19101 | 39 elseif (! isreal (n) || any (n(:) < 0 | n(:) != fix (n(:)))) |
40 error ("factorial: all N must be real non-negative integers"); | |
5820 | 41 endif |
19101 | 42 |
6532 | 43 x = round (gamma (n+1)); |
19101 | 44 |
45 ## FIXME: Matlab returns an output of the same type as the input. | |
46 ## This doesn't seem particularly worth copying--for example uint8 would | |
47 ## saturate for n > 5. If desired, however, the following code could be | |
48 ## uncommented. | |
49 # if (! isfloat (x)) | |
50 # x = cast (x, class (n)); | |
51 # endif | |
52 | |
5820 | 53 endfunction |
6532 | 54 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
55 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
56 %!assert (factorial (5), prod (1:5)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
57 %!assert (factorial ([1,2;3,4]), [1,2;6,24]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
58 %!assert (factorial (70), exp (sum (log (1:70))), -128*eps) |
19101 | 59 %!assert (factorial (0), 1) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
60 |
19101 | 61 %!error factorial () |
62 %!error factorial (1,2) | |
63 %!error <must be real non-negative integers> factorial (2i) | |
64 %!error <must be real non-negative integers> factorial (-3) | |
65 %!error <must be real non-negative integers> factorial (5.5) | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
66 |