Mercurial > hg > octave-nkf
annotate scripts/specfun/factor.m @ 19344:0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
* doc/interpreter/contrib.txi, doc/interpreter/expr.txi,
doc/interpreter/func.txi, doc/interpreter/linalg.txi, doc/interpreter/plot.txi,
libinterp/corefcn/data.cc, libinterp/corefcn/debug.cc,
libinterp/corefcn/graphics.cc, libinterp/corefcn/help.cc,
libinterp/corefcn/load-save.cc, libinterp/corefcn/profiler.cc,
libinterp/corefcn/syscalls.cc, libinterp/corefcn/utils.cc,
libinterp/corefcn/variables.cc, libinterp/dldfcn/__init_fltk__.cc,
scripts/general/deal.m, scripts/help/__gripe_missing_component__.m,
scripts/miscellaneous/desktop.m, scripts/pkg/private/default_prefix.m,
scripts/plot/appearance/__getlegenddata__.m, scripts/plot/util/pan.m,
scripts/plot/util/rotate.m, scripts/plot/util/rotate3d.m,
scripts/plot/util/zoom.m, scripts/signal/periodogram.m,
scripts/specfun/factor.m, scripts/specfun/primes.m,
scripts/statistics/base/lscov.m, scripts/testfun/private/compare_plot_demos.m,
scripts/testfun/private/dump_demos.m,
scripts/testfun/private/html_compare_plot_demos.m, scripts/testfun/test.m:
Periodic grammarcheck of documentation.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 22 Sep 2014 20:46:54 -0700 |
parents | e7bffcea619f |
children | 0e1f5a750d00 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17716
diff
changeset
|
1 ## Copyright (C) 2000-2013 Paul Kienzle |
5827 | 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. | |
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 | |
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/>. | |
5827 | 18 |
19 ## -*- texinfo -*- | |
19289 | 20 ## @deftypefn {Function File} {@var{pf} =} factor (@var{q}) |
21 ## @deftypefnx {Function File} {[@var{pf}, @var{n}] =} factor (@var{q}) | |
22 ## Return the prime factorization of @var{q}. | |
5827 | 23 ## |
19289 | 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}, | |
26 ## return 1. | |
27 ## | |
28 ## With two output arguments, return the unique prime factors @var{pf} and | |
29 ## their multiplicities. That is, @code{prod (@var{pf} .^ @var{n}) == @var{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
|
30 ## |
19344
0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19289
diff
changeset
|
31 ## Implementation Note: The input @var{q} must be less than |
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
|
32 ## @code{bitmax} (9.0072e+15) in order to factor correctly. |
19289 | 33 ## @seealso{gcd, lcm, isprime, primes} |
5827 | 34 ## @end deftypefn |
35 | |
36 ## Author: Paul Kienzle | |
37 | |
38 ## 2002-01-28 Paul Kienzle | |
39 ## * remove recursion; only check existing primes for multiplicity > 1 | |
40 ## * return multiplicity as suggested by Dirk Laurie | |
41 ## * add error handling | |
42 | |
19289 | 43 function [pf, n] = factor (q) |
5827 | 44 |
19289 | 45 if (nargin != 1) |
5827 | 46 print_usage (); |
47 endif | |
48 | |
19289 | 49 if (! isreal (q) || ! isscalar (q) || q != fix (q)) |
50 error ("factor: Q must be a real integer"); | |
5827 | 51 endif |
52 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
53 ## 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
|
54 if (q < 4) |
19289 | 55 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
|
56 n = 1; |
5827 | 57 return; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
58 endif |
5827 | 59 |
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
|
60 q = double (q); # For the time being, calcs rely on double precision var. |
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
61 qorig = q; |
19289 | 62 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
|
63 ## There is at most one prime greater than sqrt(q), and if it exists, |
5827 | 64 ## it has multiplicity 1, so no need to consider any factors greater |
19289 | 65 ## than sqrt(q) directly. [If there were two factors p1, p2 > sqrt(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
|
66 ## then q >= p1*p2 > sqrt(q)*sqrt(q) == q. Contradiction.] |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10476
diff
changeset
|
67 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
|
68 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
|
69 ## 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
|
70 p = p(rem (q, p) == 0); |
5827 | 71 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
|
72 ## 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
|
73 p = q; |
5827 | 74 endif |
19289 | 75 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
|
76 ## 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
|
77 q /= prod (p); |
5827 | 78 endwhile |
19289 | 79 pf = sort (pf); |
5827 | 80 |
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
|
81 ## Verify algorithm was succesful |
19289 | 82 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
|
83 if (q != qorig) |
19289 | 84 error ("factor: Q too large to factor"); |
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 elseif (q > bitmax) |
19289 | 86 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
|
87 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
|
88 |
8506 | 89 ## Determine muliplicity. |
5827 | 90 if (nargout > 1) |
19289 | 91 idx = find ([0, pf] != [pf, 0]); |
92 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
|
93 n = diff (idx); |
5827 | 94 endif |
95 | |
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 |
19289 | 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 |
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
|
110 %% Test input validation |
e48f5a52e838
factor.m: Warn when the input is too large to calculate reliable answer.
Doug Stewart <doug.dastew@gmail.com>
parents:
14868
diff
changeset
|
111 %!error factor () |
19289 | 112 %!error factor (1,2) |
113 %!error <Q must be a real integer> factor (6i) | |
114 %!error <Q must be a real integer> factor ([1,2]) | |
115 %!error <Q must be a real integer> factor (1.5) | |
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
|
116 |