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) |
|
34 ## deconv (poly(1:8), polygcd (poly(1:8), poly(3:12))) - poly(1:2) |
5216
|
35 ## @end example |
|
36 ## @end deftypefn |
|
37 ## |
|
38 ## @seealso{poly, polyinteg, polyderiv, polyreduce, roots, conv, deconv, |
|
39 ## residue, filter, polyval, and polyvalm} |
|
40 |
5217
|
41 function x = polygcd (b, a, tol) |
|
42 |
|
43 if (nargin == 2 || nargin == 3) |
|
44 if (nargin == 2) |
|
45 tol = sqrt (eps); |
5216
|
46 endif |
5217
|
47 if (length (a) == 1 || length (b) == 1) |
|
48 if (a == 0) |
|
49 x = b; |
|
50 elseif (b == 0) |
|
51 x = a; |
|
52 else |
|
53 x = 1; |
|
54 endif |
|
55 else |
|
56 a /= a(1); |
|
57 while (1) |
|
58 [d, r] = deconv (b, a); |
|
59 nz = find (abs (r) > tol); |
|
60 if (isempty (nz)) |
|
61 x = a; |
|
62 break; |
|
63 else |
|
64 r = r(nz(1):length(r)); |
|
65 endif |
|
66 b = a; |
5382
|
67 a = r / r(1); |
5217
|
68 endwhile |
|
69 endif |
|
70 else |
|
71 usage ("x = polygcd (b, a [,tol])"); |
5216
|
72 endif |
5217
|
73 |
|
74 endfunction |