annotate scripts/polynomial/polygcd.m @ 5216:5ed60b8b1ac4

[project @ 2005-03-16 19:51:39 by jwe]
author jwe
date Wed, 16 Mar 2005 19:51:46 +0000
parents
children e88886a6934d
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
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
40 function x = polygcd(b,a,tol)
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
41 if (nargin<2 || nargin>3)
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
42 usage("x=polygcd(b,a [,tol])");
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
43 endif
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
44 if (nargin<3), tol=sqrt(eps); endif
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
45 if (length(a)==1 || length(b)==1)
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
46 if a==0, x=b;
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
47 elseif b==0, x=a;
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
48 else x=1;
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
49 endif
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
50 return;
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
51 endif
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
52 a = a./a(1);
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
53 while (1)
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
54 [d, r] = deconv(b, a);
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
55 nz = find(abs(r)>tol);
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
56 if isempty(nz)
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
57 x = a;
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
58 return;
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
59 else
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
60 r = r(nz(1):length(r));
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
61 endif
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
62 b = a;
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
63 a = r./r(1);
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
64 endwhile
5ed60b8b1ac4 [project @ 2005-03-16 19:51:39 by jwe]
jwe
parents:
diff changeset
65 endfunction