Mercurial > octave
annotate scripts/specfun/factor.m @ 26376:00f796120a6d stable
maint: Update copyright dates in all source files.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 02 Jan 2019 16:32:43 -0500 |
parents | 996d78102a71 |
children | b442ec6dda5c |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25436
diff
changeset
|
1 ## Copyright (C) 2000-2019 Paul Kienzle |
5827 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
5827 | 6 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
7 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## (at your option) any later version. |
5827 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
5827 | 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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
17 ## <https://www.gnu.org/licenses/>. |
5827 | 18 |
19 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20486
diff
changeset
|
20 ## @deftypefn {} {@var{pf} =} factor (@var{q}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20486
diff
changeset
|
21 ## @deftypefnx {} {[@var{pf}, @var{n}] =} factor (@var{q}) |
19097 | 22 ## Return the prime factorization of @var{q}. |
5827 | 23 ## |
19097 | 24 ## The prime factorization is defined as @code{prod (@var{pf}) == @var{q}} |
25 ## where every element of @var{pf} is a prime number. If @code{@var{q} == 1}, | |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
26 ## return 1. The output @var{pf} is of the same numeric class as the input. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
27 ## |
19097 | 28 ## With two output arguments, return the unique prime factors @var{pf} and |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
29 ## their multiplicities. That is, |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
30 ## @code{prod (@var{pf} .^ @var{n}) == @var{q}}. |
19596
0e1f5a750d00
maint: Periodic merge of gui-release to default.
John W. Eaton <jwe@octave.org>
diff
changeset
|
31 ## |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
32 ## Implementation Note: The input @var{q} must be less than @code{flintmax} |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
33 ## (9.0072e+15) in order to factor correctly. |
19097 | 34 ## @seealso{gcd, lcm, isprime, primes} |
5827 | 35 ## @end deftypefn |
36 | |
37 ## Author: Paul Kienzle | |
38 | |
19097 | 39 function [pf, n] = factor (q) |
5827 | 40 |
19097 | 41 if (nargin != 1) |
5827 | 42 print_usage (); |
43 endif | |
44 | |
25269
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
45 if (! isscalar (q) || ! isreal (q) || q < 0 || q != fix (q)) |
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
46 error ("factor: Q must be a real non-negative integer"); |
5827 | 47 endif |
48 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
49 ## Special case of no primes less than sqrt(q). |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
50 if (q < 4) |
19097 | 51 pf = q; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
52 n = 1; |
5827 | 53 return; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
54 endif |
5827 | 55 |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
56 cls = class (q); # store class |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
57 q = double (q); # internal algorithm relies on numbers being doubles. |
17716
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
58 qorig = q; |
19097 | 59 pf = []; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
60 ## There is at most one prime greater than sqrt(q), and if it exists, |
5827 | 61 ## it has multiplicity 1, so no need to consider any factors greater |
19097 | 62 ## than sqrt(q) directly. [If there were two factors p1, p2 > sqrt(q), |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
63 ## then q >= p1*p2 > sqrt(q)*sqrt(q) == q. Contradiction.] |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
64 p = primes (sqrt (q)); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
65 while (q > 1) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
66 ## Find prime factors in remaining q. |
17716
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
67 p = p(rem (q, p) == 0); |
5827 | 68 if (isempty (p)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
69 ## Can't be reduced further, so q must itself be a prime. |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
70 p = q; |
5827 | 71 endif |
19097 | 72 pf = [pf, p]; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
73 ## Reduce q. |
17716
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
74 q /= prod (p); |
5827 | 75 endwhile |
19097 | 76 pf = sort (pf); |
5827 | 77 |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
78 ## Verify algorithm was successful |
19097 | 79 q = prod (pf); |
17716
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
80 if (q != qorig) |
19097 | 81 error ("factor: Q too large to factor"); |
20486 | 82 elseif (q >= flintmax ()) |
19097 | 83 warning ("factor: Q too large. Answer is unreliable"); |
17716
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
84 endif |
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
85 |
25269
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
86 ## Determine multiplicity. |
5827 | 87 if (nargout > 1) |
19097 | 88 idx = find ([0, pf] != [pf, 0]); |
89 pf = pf(idx(1:length (idx)-1)); | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
90 n = diff (idx); |
5827 | 91 endif |
92 | |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
93 ## Restore class of input |
25436
996d78102a71
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25270
diff
changeset
|
94 pf = feval (cls, pf); |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
95 |
5827 | 96 endfunction |
97 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
99 %!assert (factor (1), 1) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
100 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 %! for i = 2:20 |
19097 | 102 %! pf = factor (i); |
103 %! assert (prod (pf), i); | |
104 %! assert (all (isprime (pf))); | |
105 %! [pf, n] = factor (i); | |
106 %! assert (prod (pf.^n), i); | |
107 %! assert (all ([0,pf] != [pf,0])); | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
108 %! endfor |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
109 |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
110 %!assert (factor (uint8 (8)), uint8 ([2 2 2])) |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
111 %!assert (factor (single (8)), single ([2 2 2])) |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
112 %!test |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
113 %! [pf, n] = factor (int16 (8)); |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
114 %! assert (pf, int16 (2)); |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
115 %! assert (n, double (3)); |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
116 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
117 ## Test input validation |
17716
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
118 %!error factor () |
19097 | 119 %!error factor (1,2) |
25269
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
120 %!error <Q must be a real non-negative integer> factor (6i) |
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
121 %!error <Q must be a real non-negative integer> factor ([1,2]) |
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
122 %!error <Q must be a real non-negative integer> factor (1.5) |
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
123 %!error <Q must be a real non-negative integer> factor (-20) |