5216
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {[@var{q}]} polygcd (@var{b}, @var{a}, @var{tol}) |
|
19 ## |
|
20 ## Find greatest common divisor of two polynomials. This is equivalent |
|
21 ## to the polynomial found by multiplying together all the common roots. |
|
22 ## Together with deconv, you can reduce a ratio of two polynomials. |
|
23 ## Tolerance defaults to |
|
24 ## @example |
|
25 ## sqrt(eps). |
|
26 ## @end example |
|
27 ## Note that this is an unstable |
|
28 ## algorithm, so don't try it on large polynomials. |
|
29 ## |
|
30 ## Example |
|
31 ## @example |
|
32 ## polygcd(poly(1:8),poly(3:12)) - poly(3:8) |
|
33 ## deconv(poly(1:8),polygcd(poly(1:8),poly(3:12))) - poly(1:2) |
|
34 ## @end example |
|
35 ## @end deftypefn |
|
36 ## |
|
37 ## @seealso{poly, polyinteg, polyderiv, polyreduce, roots, conv, deconv, |
|
38 ## residue, filter, polyval, and polyvalm} |
|
39 |
5217
|
40 function x = polygcd (b, a, tol) |
|
41 |
|
42 if (nargin == 2 || nargin == 3) |
|
43 if (nargin == 2) |
|
44 tol = sqrt (eps); |
5216
|
45 endif |
5217
|
46 if (length (a) == 1 || length (b) == 1) |
|
47 if (a == 0) |
|
48 x = b; |
|
49 elseif (b == 0) |
|
50 x = a; |
|
51 else |
|
52 x = 1; |
|
53 endif |
|
54 else |
|
55 a /= a(1); |
|
56 while (1) |
|
57 [d, r] = deconv (b, a); |
|
58 nz = find (abs (r) > tol); |
|
59 if (isempty (nz)) |
|
60 x = a; |
|
61 break; |
|
62 else |
|
63 r = r(nz(1):length(r)); |
|
64 endif |
|
65 b = a; |
|
66 a /= r(1); |
|
67 endwhile |
|
68 endif |
|
69 else |
|
70 usage ("x = polygcd (b, a [,tol])"); |
5216
|
71 endif |
5217
|
72 |
|
73 endfunction |