annotate scripts/polynomial/polygcd.m @ 5217:e88886a6934d

[project @ 2005-03-16 20:03:01 by jwe]
author jwe
date Wed, 16 Mar 2005 20:03:01 +0000
parents 5ed60b8b1ac4
children 4c8a2e4e0717
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
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
16
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
17 ## -*- texinfo -*-
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
18 ## @deftypefn {Function File} {[@var{q}]} polygcd (@var{b}, @var{a}, @var{tol})
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
19 ##
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
20 ## Find greatest common divisor of two polynomials. This is equivalent
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
21 ## 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
22 ## 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
23 ## Tolerance defaults to
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
24 ## @example
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
25 ## sqrt(eps).
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
26 ## @end example
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
27 ## Note that this is an unstable
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
28 ## algorithm, so don't try it on large polynomials.
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
29 ##
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
30 ## Example
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 ## polygcd(poly(1:8),poly(3:12)) - poly(3:8)
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
33 ## deconv(poly(1:8),polygcd(poly(1:8),poly(3:12))) - poly(1:2)
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
34 ## @end example
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
35 ## @end deftypefn
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
36 ##
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
37 ## @seealso{poly, polyinteg, polyderiv, polyreduce, roots, conv, deconv,
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
38 ## residue, filter, polyval, and polyvalm}
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;
e88886a6934d [project @ 2005-03-16 20:03:01 by jwe]
jwe
parents: 5216
diff changeset
66 a /= r(1);
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