annotate scripts/polynomial/polygcd.m @ 5382:7012c2492c12

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