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 |
5307
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
5216
|
17 |
|
18 ## -*- texinfo -*- |
5382
|
19 ## @deftypefn {Function File} {@var{q} =} polygcd (@var{b}, @var{a}, @var{tol}) |
5216
|
20 ## |
|
21 ## Find greatest common divisor of two polynomials. This is equivalent |
|
22 ## to the polynomial found by multiplying together all the common roots. |
|
23 ## Together with deconv, you can reduce a ratio of two polynomials. |
|
24 ## Tolerance defaults to |
|
25 ## @example |
|
26 ## sqrt(eps). |
|
27 ## @end example |
|
28 ## Note that this is an unstable |
|
29 ## algorithm, so don't try it on large polynomials. |
|
30 ## |
|
31 ## Example |
|
32 ## @example |
5382
|
33 ## polygcd (poly(1:8), poly(3:12)) - poly(3:8) |
6850
|
34 ## @result{} [ 0, 0, 0, 0, 0, 0, 0 ] |
|
35 ## deconv (poly(1:8), polygcd (poly(1:8), poly(3:12))) - poly(1:2) |
|
36 ## @result{} [ 0, 0, 0 ] |
5216
|
37 ## @end example |
|
38 ## @seealso{poly, polyinteg, polyderiv, polyreduce, roots, conv, deconv, |
|
39 ## residue, filter, polyval, and polyvalm} |
5642
|
40 ## @end deftypefn |
5216
|
41 |
5217
|
42 function x = polygcd (b, a, tol) |
|
43 |
|
44 if (nargin == 2 || nargin == 3) |
|
45 if (nargin == 2) |
|
46 tol = sqrt (eps); |
5216
|
47 endif |
5217
|
48 if (length (a) == 1 || length (b) == 1) |
|
49 if (a == 0) |
|
50 x = b; |
|
51 elseif (b == 0) |
|
52 x = a; |
|
53 else |
|
54 x = 1; |
|
55 endif |
|
56 else |
|
57 a /= a(1); |
|
58 while (1) |
|
59 [d, r] = deconv (b, a); |
|
60 nz = find (abs (r) > tol); |
|
61 if (isempty (nz)) |
|
62 x = a; |
|
63 break; |
|
64 else |
|
65 r = r(nz(1):length(r)); |
|
66 endif |
|
67 b = a; |
5382
|
68 a = r / r(1); |
5217
|
69 endwhile |
|
70 endif |
|
71 else |
6046
|
72 print_usage (); |
5216
|
73 endif |
5217
|
74 |
|
75 endfunction |