Mercurial > octave
annotate scripts/specfun/factor.m @ 28891:de5f2f9a64ff
maint: Use same coding style when checking for a minimum of 1 input.
* disp.m, __get_properties__.m, disp.m, isplaying.m, pause.m, resume.m,
stop.m, __get_properties__.m, disp.m, isrecording.m, pause.m, resume.m, stop.m,
mu2lin.m, acosd.m, acot.m, acotd.m, acoth.m, acsc.m, acscd.m, acsch.m, asec.m,
asecd.m, asech.m, asind.m, atand.m, cosd.m, cot.m, cotd.m, coth.m, csc.m,
cscd.m, csch.m, sec.m, secd.m, sech.m, sind.m, tand.m, cell2mat.m, deg2rad.m,
fliplr.m, flipud.m, int2str.m, nextpow2.m, num2str.m, rad2deg.m, subsindex.m,
rotx.m, roty.m, rotz.m, voronoin.m, __unimplemented__.m, frame2im.m, hsv2rgb.m,
iscolormap.m, __imfinfo__.m, rgb2gray.m, rgb2hsv.m, fileread.m,
is_valid_file_id.m, usejava.m, argnames.m, char.m, formula.m, vectorize.m,
isdir.m, vectorize.m, condeig.m, duplication_matrix.m, expm.m, isdiag.m,
istril.m, istriu.m, normest.m, planerot.m, trace.m, vech.m, fieldnames.m,
fileparts.m, grabcode.m, isfile.m, isfolder.m, mustBeFinite.m, mustBeInteger.m,
mustBeNegative.m, mustBeNonNan.m, mustBeNonempty.m, mustBeNonnegative.m,
mustBeNonpositive.m, mustBeNonsparse.m, mustBeNonzero.m, mustBeNumeric.m,
mustBeNumericOrLogical.m, mustBePositive.m, mustBeReal.m, namedargs2cell.m,
open.m, run.m, swapbytes.m, pareto.m, allchild.m, colstyle.m, hdl2struct.m,
isaxes.m, isfigure.m, ishandle.m, compan.m, poly.m, polyreduce.m, ppjumps.m,
roots.m, unmkpp.m, profexplore.m, profile.m, bartlett.m, fftshift.m, hurst.m,
ifftshift.m, sinc.m, spencer.m, yulewalker.m, colperm.m, nonzeros.m,
spconvert.m, spones.m, sprandsym.m, cosint.m, expint.m, factor.m, factorial.m,
isprime.m, perms.m, primes.m, reallog.m, realsqrt.m, sinint.m, gallery.m,
hadamard.m, hilb.m, invhilb.m, magic.m, wilkinson.m, center.m, iqr.m, meansq.m,
median.m, runlength.m, bin2dec.m, blanks.m, deblank.m, hex2dec.m, isletter.m,
isstring.m, str2num.m, strtrim.m, asctime.m, ctime.m:
Replace "if (nargin != 1)" formulations with "if (nargin < 1)" to match
the rest of Octave.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 10 Oct 2020 16:32:20 -0700 |
parents | a4268efb7334 |
children | 90fea9cc9caa |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 ## Copyright (C) 2000-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5827 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5827 | 11 ## 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
|
12 ## 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
|
13 ## (at your option) any later version. |
5827 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
5827 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## 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
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5827 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20486
diff
changeset
|
27 ## @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
|
28 ## @deftypefnx {} {[@var{pf}, @var{n}] =} factor (@var{q}) |
19097 | 29 ## Return the prime factorization of @var{q}. |
5827 | 30 ## |
19097 | 31 ## The prime factorization is defined as @code{prod (@var{pf}) == @var{q}} |
32 ## 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
|
33 ## 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
|
34 ## |
19097 | 35 ## 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
|
36 ## their multiplicities. That is, |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
37 ## @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
|
38 ## |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
39 ## 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
|
40 ## (9.0072e+15) in order to factor correctly. |
19097 | 41 ## @seealso{gcd, lcm, isprime, primes} |
5827 | 42 ## @end deftypefn |
43 | |
19097 | 44 function [pf, n] = factor (q) |
5827 | 45 |
28891
de5f2f9a64ff
maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
46 if (nargin < 1) |
5827 | 47 print_usage (); |
48 endif | |
49 | |
25269
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
50 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
|
51 error ("factor: Q must be a real non-negative integer"); |
5827 | 52 endif |
53 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
54 ## 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
|
55 if (q < 4) |
19097 | 56 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
|
57 n = 1; |
5827 | 58 return; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
59 endif |
5827 | 60 |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
61 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
|
62 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
|
63 qorig = q; |
19097 | 64 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
|
65 ## There is at most one prime greater than sqrt(q), and if it exists, |
5827 | 66 ## it has multiplicity 1, so no need to consider any factors greater |
19097 | 67 ## 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
|
68 ## 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
|
69 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
|
70 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
|
71 ## 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
|
72 p = p(rem (q, p) == 0); |
5827 | 73 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
|
74 ## 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
|
75 p = q; |
5827 | 76 endif |
19097 | 77 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
|
78 ## 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
|
79 q /= prod (p); |
5827 | 80 endwhile |
19097 | 81 pf = sort (pf); |
5827 | 82 |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
83 ## Verify algorithm was successful |
19097 | 84 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
|
85 if (q != qorig) |
19097 | 86 error ("factor: Q too large to factor"); |
20486 | 87 elseif (q >= flintmax ()) |
19097 | 88 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
|
89 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
|
90 |
25269
cac96fd5310d
factor.m: Emit an error if input is negative (bug #53425).
Dildar Sk <dildarsk101010@gmail.com>
parents:
25054
diff
changeset
|
91 ## Determine multiplicity. |
5827 | 92 if (nargout > 1) |
19097 | 93 idx = find ([0, pf] != [pf, 0]); |
94 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
|
95 n = diff (idx); |
5827 | 96 endif |
97 | |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
98 ## Restore class of input |
25436
996d78102a71
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25270
diff
changeset
|
99 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
|
100 |
5827 | 101 endfunction |
102 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
104 %!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
|
105 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
106 %! for i = 2:20 |
19097 | 107 %! pf = factor (i); |
108 %! assert (prod (pf), i); | |
109 %! assert (all (isprime (pf))); | |
110 %! [pf, n] = factor (i); | |
111 %! assert (prod (pf.^n), i); | |
112 %! 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
|
113 %! 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
|
114 |
25270
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
115 %!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
|
116 %!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
|
117 %!test |
617fe022e965
factor.m: Return output factors of same class as input for Matlab compatibility.
Rik <rik@octave.org>
parents:
25269
diff
changeset
|
118 %! [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
|
119 %! 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
|
120 %! 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
|
121 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
122 ## 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
|
123 %!error factor () |
19097 | 124 %!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
|
125 %!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
|
126 %!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
|
127 %!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
|
128 %!error <Q must be a real non-negative integer> factor (-20) |