Mercurial > hg > octave-lyh
annotate scripts/polynomial/polygcd.m @ 13045:92cb87addf25
codesprint: Add tests to polygcd
* polygcd.m : Add tests.
author | Carlo de Falco <kingcrimson@tiscali.it> |
---|---|
date | Sat, 03 Sep 2011 17:44:20 +0200 |
parents | fd0a3ac60b0e |
children | e81ddf9cacd5 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 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 -*- | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {@var{q} =} polygcd (@var{b}, @var{a}) |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{q} =} polygcd (@var{b}, @var{a}, @var{tol}) |
5216 | 22 ## |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
23 ## Find the greatest common divisor of two polynomials. This is equivalent |
5216 | 24 ## to the polynomial found by multiplying together all the common roots. |
25 ## Together with deconv, you can reduce a ratio of two polynomials. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
26 ## Tolerance defaults to @code{sqrt(eps)}. |
5216 | 27 ## |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
28 ## Note that this is a numerically unstable algorithm, and should not be used |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
29 ## on large polynomials. |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
30 ## |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
31 ## Example: |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
32 ## |
5216 | 33 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
34 ## @group |
5382 | 35 ## polygcd (poly(1:8), poly(3:12)) - poly(3:8) |
6850 | 36 ## @result{} [ 0, 0, 0, 0, 0, 0, 0 ] |
7031 | 37 ## deconv (poly(1:8), polygcd (poly(1:8), poly(3:12))) ... |
38 ## - poly(1:2) | |
6850 | 39 ## @result{} [ 0, 0, 0 ] |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## @end group |
5216 | 41 ## @end example |
10224
f6e0404421f4
point to polyint in @seealso, not polyinteg
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
42 ## @seealso{poly, polyint, polyderiv, polyreduce, roots, conv, deconv, |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7795
diff
changeset
|
43 ## residue, filter, polyval, polyvalm} |
5642 | 44 ## @end deftypefn |
5216 | 45 |
5217 | 46 function x = polygcd (b, a, tol) |
47 | |
48 if (nargin == 2 || nargin == 3) | |
49 if (nargin == 2) | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
50 if (isa (a, "single") || isa (b, "single")) |
10549 | 51 tol = sqrt (eps ("single")); |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
52 else |
10549 | 53 tol = sqrt (eps); |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
54 endif |
5216 | 55 endif |
5217 | 56 if (length (a) == 1 || length (b) == 1) |
57 if (a == 0) | |
10549 | 58 x = b; |
5217 | 59 elseif (b == 0) |
10549 | 60 x = a; |
5217 | 61 else |
10549 | 62 x = 1; |
5217 | 63 endif |
64 else | |
65 a /= a(1); | |
66 while (1) | |
10549 | 67 [d, r] = deconv (b, a); |
68 nz = find (abs (r) > tol); | |
69 if (isempty (nz)) | |
70 x = a; | |
71 break; | |
72 else | |
73 r = r(nz(1):length(r)); | |
74 endif | |
75 b = a; | |
76 a = r / r(1); | |
5217 | 77 endwhile |
78 endif | |
79 else | |
6046 | 80 print_usage (); |
5216 | 81 endif |
5217 | 82 |
83 endfunction | |
13045
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
84 |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
85 |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
86 %!test |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
87 %! poly1 = [1 6 11 6]; % (x+1)(x+2)(x+3) |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
88 %! poly2 = [1 3 2]; % (x+1)(x+2) |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
89 %! poly3 = polygcd (poly1, poly2); |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
90 %! assert (poly3, poly2, sqrt (eps)) |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
91 |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
92 %!test |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
93 %! assert (polygcd (poly(1:8), poly(3:12)), poly(3:8), sqrt (eps)) |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
94 |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
95 %!test |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
96 %! assert (deconv (poly(1:8), polygcd (poly(1:8), poly(3:12))), poly(1:2), sqrt (eps)) |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
97 |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
98 %!test |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
99 %! for ii=1:10 |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
100 %! p = (unique (randn (10, 1)) * 10).'; |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
101 %! p1 = p(3:end); |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
102 %! p2 = p(1:end-2); |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
103 %! assert (polygcd (poly (-p1), poly (-p2)), poly (- intersect (p1, p2)), sqrt (eps)) |
92cb87addf25
codesprint: Add tests to polygcd
Carlo de Falco <kingcrimson@tiscali.it>
parents:
11523
diff
changeset
|
104 %! endfor |