annotate scripts/polynomial/polygcd.m @ 5642:2618a0750ae6

[project @ 2006-03-06 21:26:48 by jwe]
author jwe
date Mon, 06 Mar 2006 21:26:54 +0000
parents 7012c2492c12
children 34f96dd5441b
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 ## @seealso{poly, polyinteg, polyderiv, polyreduce, roots, conv, deconv,
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
37 ## residue, filter, polyval, and polyvalm}
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5382
diff changeset
38 ## @end deftypefn
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
39
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
40 function x = polygcd (b, a, tol)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
41
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
42 if (nargin == 2 || nargin == 3)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
43 if (nargin == 2)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
44 tol = sqrt (eps);
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
45 endif
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
46 if (length (a) == 1 || length (b) == 1)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
47 if (a == 0)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
48 x = b;
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
49 elseif (b == 0)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
50 x = a;
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
51 else
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
52 x = 1;
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
53 endif
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
54 else
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
55 a /= a(1);
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
56 while (1)
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
57 [d, r] = deconv (b, a);
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
58 nz = find (abs (r) > tol);
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
59 if (isempty (nz))
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
60 x = a;
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
61 break;
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
62 else
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
63 r = r(nz(1):length(r));
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
64 endif
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
65 b = a;
5382
7012c2492c12 [project @ 2005-06-05 13:30:48 by jwe]
jwe
parents: 5377
diff changeset
66 a = r / r(1);
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
67 endwhile
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
68 endif
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
69 else
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
70 usage ("x = polygcd (b, a [,tol])");
5216
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
71 endif
5217
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
72
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
73 endfunction