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