Mercurial > hg > octave-lyh
annotate scripts/polynomial/polygcd.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | 16f53d29049f |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2005, 2006, 2007, 2008 Paul Kienzle |
5216 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5216 | 4 ## |
7016 | 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 | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
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. | |
5216 | 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/>. | |
5216 | 18 |
19 ## -*- texinfo -*- | |
5382 | 20 ## @deftypefn {Function File} {@var{q} =} polygcd (@var{b}, @var{a}, @var{tol}) |
5216 | 21 ## |
22 ## Find greatest common divisor of two polynomials. This is equivalent | |
23 ## to the polynomial found by multiplying together all the common roots. | |
24 ## Together with deconv, you can reduce a ratio of two polynomials. | |
25 ## Tolerance defaults to | |
26 ## @example | |
27 ## sqrt(eps). | |
28 ## @end example | |
29 ## Note that this is an unstable | |
30 ## algorithm, so don't try it on large polynomials. | |
31 ## | |
32 ## Example | |
33 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
34 ## @group |
5382 | 35 ## polygcd (poly(1:8), poly(3:12)) - poly(3:8) |
6850 | 36 ## @result{} [ 0, 0, 0, 0, 0, 0, 0 ] |
7031 | 37 ## deconv (poly(1:8), polygcd (poly(1:8), poly(3:12))) ... |
38 ## - poly(1:2) | |
6850 | 39 ## @result{} [ 0, 0, 0 ] |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## @end group |
5216 | 41 ## @end example |
42 ## @seealso{poly, polyinteg, polyderiv, polyreduce, roots, conv, deconv, | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7795
diff
changeset
|
43 ## residue, filter, polyval, polyvalm} |
5642 | 44 ## @end deftypefn |
5216 | 45 |
5217 | 46 function x = polygcd (b, a, tol) |
47 | |
48 if (nargin == 2 || nargin == 3) | |
49 if (nargin == 2) | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
50 if (isa (a, "single") || isa (b, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
51 tol = sqrt (eps ("single")); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
52 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
53 tol = sqrt (eps); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
54 endif |
5216 | 55 endif |
5217 | 56 if (length (a) == 1 || length (b) == 1) |
57 if (a == 0) | |
58 x = b; | |
59 elseif (b == 0) | |
60 x = a; | |
61 else | |
62 x = 1; | |
63 endif | |
64 else | |
65 a /= a(1); | |
66 while (1) | |
67 [d, r] = deconv (b, a); | |
68 nz = find (abs (r) > tol); | |
69 if (isempty (nz)) | |
70 x = a; | |
71 break; | |
72 else | |
73 r = r(nz(1):length(r)); | |
74 endif | |
75 b = a; | |
5382 | 76 a = r / r(1); |
5217 | 77 endwhile |
78 endif | |
79 else | |
6046 | 80 print_usage (); |
5216 | 81 endif |
5217 | 82 |
83 endfunction |