Mercurial > hg > octave-nkf
annotate scripts/specfun/factorial.m @ 20818:9d2023d1a63c
binoinv.m: Implement binary search algorithm for 28X performance increase (bug #34363).
* binoinv.m: Call new functions scalar_binoinv or vector_binoinv to calculate
binoinv. If there are still uncalculated values then call bin_search_binoinv
to perform binary search for remaining values. Add more BIST tests.
* binoinv.m (scalar_binoinv): New subfunction to calculate binoinv for scalar x.
Stops when x > 1000.
* binoinv.m (vector_binoinv): New subfunction to calculate binoinv for scalar x.
Stops when x > 1000.
author | Lachlan Andrew <lachlanbis@gmail.com> |
---|---|
date | Sun, 11 Oct 2015 19:49:40 -0700 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19293
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 -*- | |
20 ## @deftypefn {Function File} {} factorial (@var{n}) | |
19293 | 21 ## Return the factorial of @var{n} where @var{n} is a real non-negative integer. |
22 ## | |
23 ## 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
|
24 ## vector or matrix arguments, return the factorial of each element in the |
19293 | 25 ## array. |
26 ## | |
27 ## For non-integers see the generalized factorial function @code{gamma}. | |
28 ## Note that the factorial function grows large quite quickly, and even | |
29 ## with double precision values overflow will occur if @var{n} > 171. For | |
30 ## such cases consider @code{gammaln}. | |
31 ## @seealso{prod, gamma, gammaln} | |
5820 | 32 ## @end deftypefn |
33 | |
34 function x = factorial (n) | |
19293 | 35 |
6391 | 36 if (nargin != 1) |
37 print_usage (); | |
19293 | 38 elseif (! isreal (n) || any (n(:) < 0 | n(:) != fix (n(:)))) |
39 error ("factorial: all N must be real non-negative integers"); | |
5820 | 40 endif |
19293 | 41 |
6532 | 42 x = round (gamma (n+1)); |
19293 | 43 |
44 ## FIXME: Matlab returns an output of the same type as the input. | |
45 ## This doesn't seem particularly worth copying--for example uint8 would | |
46 ## saturate for n > 5. If desired, however, the following code could be | |
47 ## uncommented. | |
48 # if (! isfloat (x)) | |
49 # x = cast (x, class (n)); | |
50 # endif | |
51 | |
5820 | 52 endfunction |
6532 | 53 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
54 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
55 %!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
|
56 %!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
|
57 %!assert (factorial (70), exp (sum (log (1:70))), -128*eps) |
19293 | 58 %!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
|
59 |
19293 | 60 %!error factorial () |
61 %!error factorial (1,2) | |
62 %!error <must be real non-negative integers> factorial (2i) | |
63 %!error <must be real non-negative integers> factorial (-3) | |
64 %!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
|
65 |