annotate scripts/specfun/factorial.m @ 14062:5b49cafe0599 stable

Use non-negative, non-positive with hyphens in error messages. * accumarray.m, polyfit.m, factorial.m, nchoosek.m, mcnemar_test.m, find.cc, graphics.cc, sparse.cc: Use non-negative, non-positive with hyphens in error messages for consistency with documentation.
author Rik <octave@nomad.inbox5.com>
date Thu, 15 Dec 2011 22:03:03 -0800
parents 984359717d71
children 72c96de7a403
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11472
diff changeset
1 ## Copyright (C) 2000-2011 Paul Kienzle
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
2 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
4 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6542
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6542
diff changeset
8 ## your option) any later version.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
9 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
14 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6542
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6542
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
18
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
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
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
27 ## @end deftypefn
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
28
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
29 function x = factorial (n)
6391
3f3e86e9fb57 [project @ 2007-03-07 18:14:44 by jwe]
jwe
parents: 5963
diff changeset
30 if (nargin != 1)
3f3e86e9fb57 [project @ 2007-03-07 18:14:44 by jwe]
jwe
parents: 5963
diff changeset
31 print_usage ();
13279
984359717d71 Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
32 elseif (any (n(:) < 0 | n(:) != fix (n(:))))
14062
5b49cafe0599 Use non-negative, non-positive with hyphens in error messages.
Rik <octave@nomad.inbox5.com>
parents: 13279
diff changeset
33 error ("factorial: N must all be non-negative integers");
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
34 endif
6532
25cad08843a0 [project @ 2007-04-17 21:05:30 by jwe]
jwe
parents: 6391
diff changeset
35 x = round (gamma (n+1));
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
36 endfunction
6532
25cad08843a0 [project @ 2007-04-17 21:05:30 by jwe]
jwe
parents: 6391
diff changeset
37
25cad08843a0 [project @ 2007-04-17 21:05:30 by jwe]
jwe
parents: 6391
diff changeset
38 %!assert (factorial(5), prod(1:5))
25cad08843a0 [project @ 2007-04-17 21:05:30 by jwe]
jwe
parents: 6391
diff changeset
39 %!assert (factorial([1,2;3,4]), [1,2;6,24])
6542
90bc7594a7f8 [project @ 2007-04-19 03:19:11 by jwe]
jwe
parents: 6532
diff changeset
40 %!assert (factorial(70), exp(sum(log(1:70))), -128*eps)
14062
5b49cafe0599 Use non-negative, non-positive with hyphens in error messages.
Rik <octave@nomad.inbox5.com>
parents: 13279
diff changeset
41 %!fail ('factorial(5.5)', "must all be non-negative integers")
5b49cafe0599 Use non-negative, non-positive with hyphens in error messages.
Rik <octave@nomad.inbox5.com>
parents: 13279
diff changeset
42 %!fail ('factorial(-3)', "must all be non-negative integers")