1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: extgcd.m4,v 1.1 1998-07-07 16:20:18 schmidt Exp $ */ |
---|
3 | |
---|
4 | ftestSetNameOfGame( extgcd, `" |
---|
5 | Usage: extgcd [<options>] [<envSpec>] <f> <g> [<realResult>] |
---|
6 | calculates greatest common divisor d of canonical forms <f> and |
---|
7 | <g> and canonical forms a and b such that d = a*<f>+b*<g>. If |
---|
8 | the gcd of <f> and <g> is already known, the optional canonical |
---|
9 | form <realResult> may be used to check the result of the gcd |
---|
10 | computation. |
---|
11 | The extended gcd may be calculated in Euclidean domains only. |
---|
12 | The results are printed in the order a - b - d. |
---|
13 | "'`' ) |
---|
14 | |
---|
15 | //{{{ docu |
---|
16 | // |
---|
17 | // ftestAlgorithm.m4 - ftestAlgorithm test program. |
---|
18 | // |
---|
19 | // To create ftestAlgorithm.cc, run m4 using the ftest_util.m4 library in |
---|
20 | // the following way: |
---|
21 | // |
---|
22 | // m4 ftest_util.m4 ftestAlgorithm.m4 > ftestAlgorithm.cc |
---|
23 | // |
---|
24 | //}}} |
---|
25 | |
---|
26 | ftestPreprocInit(); |
---|
27 | |
---|
28 | ftestGlobalInit(); |
---|
29 | |
---|
30 | // |
---|
31 | // - functions. |
---|
32 | // |
---|
33 | |
---|
34 | //{{{ static CanonicalForm normalizeGcd ( const CanonicalForm & f ) |
---|
35 | //{{{ docu |
---|
36 | // |
---|
37 | // normalizeGcd() - normalize result of gcd computation for |
---|
38 | // testing. |
---|
39 | // |
---|
40 | // Unit normalization is done in case of a field, sign |
---|
41 | // normalization otherwise. |
---|
42 | // |
---|
43 | //}}} |
---|
44 | static CanonicalForm |
---|
45 | normalizeGcd ( const CanonicalForm & f ) |
---|
46 | { |
---|
47 | if ( getCharacteristic() > 0 || isOn( SW_RATIONAL ) ) |
---|
48 | return f/Lc( f ); |
---|
49 | else |
---|
50 | return abs( f ); |
---|
51 | } |
---|
52 | //}}} |
---|
53 | |
---|
54 | //{{{ static ftestStatusT extgcdCheck ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & a, const CanonicalForm & b, const CanonicalForm & result, const CanonicalForm & realResult ) |
---|
55 | //{{{ docu |
---|
56 | // |
---|
57 | // extgcdCheck() - check result of extgcd(). |
---|
58 | // |
---|
59 | //}}} |
---|
60 | static ftestStatusT |
---|
61 | extgcdCheck ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & a, const CanonicalForm & b, const CanonicalForm & result, const CanonicalForm & realResult ) |
---|
62 | { |
---|
63 | // check whether result == a*f+b*g |
---|
64 | if ( result != a*f+b*g ) { |
---|
65 | ftestError( CheckError, "gcd != a*f+b*g\n" ); |
---|
66 | return Failed; |
---|
67 | } |
---|
68 | |
---|
69 | // if realResult is given, use it to compare with result |
---|
70 | if ( ! realResult.isZero() ) |
---|
71 | if ( normalizeGcd( realResult ) == normalizeGcd( result ) ) |
---|
72 | return Passed; |
---|
73 | else { |
---|
74 | ftestError( CheckError, "result and real result differ\n" ); |
---|
75 | return Failed; |
---|
76 | } |
---|
77 | |
---|
78 | if ( result.isZero() ) |
---|
79 | if ( f.isZero() && g.isZero() ) |
---|
80 | return Passed; |
---|
81 | else { |
---|
82 | ftestError( CheckError, "result is zero but f and g are not\n" ); |
---|
83 | return Failed; |
---|
84 | } |
---|
85 | |
---|
86 | if ( ! divides( result, f ) || ! divides( result, g ) ) { |
---|
87 | ftestError( CheckError, "result is not a common divisor\n" ); |
---|
88 | return Failed; |
---|
89 | } else if ( ! gcd( f/result, g/result ).isOne() ) { |
---|
90 | ftestError( CheckError, "result is not greatest common divisor\n" ); |
---|
91 | return Failed; |
---|
92 | } else |
---|
93 | return Passed; |
---|
94 | } |
---|
95 | //}}} |
---|
96 | |
---|
97 | // |
---|
98 | // - main program. |
---|
99 | // |
---|
100 | int |
---|
101 | main ( int argc, char ** argv ) |
---|
102 | { |
---|
103 | // initialization |
---|
104 | ftestMainInit(); |
---|
105 | |
---|
106 | // declare input and output variables |
---|
107 | ftestOutVar( CanonicalForm, a ); |
---|
108 | ftestOutVar( CanonicalForm, b ); |
---|
109 | ftestOutVar( CanonicalForm, result ); |
---|
110 | ftestInVar( CanonicalForm, f ); |
---|
111 | ftestInVar( CanonicalForm, g ); |
---|
112 | ftestInVar( CanonicalForm, realResult ); |
---|
113 | |
---|
114 | // process argument list and set environment |
---|
115 | ftestGetOpts(); |
---|
116 | ftestGetEnv(); |
---|
117 | ftestGetInVar( f ); |
---|
118 | ftestGetInVar( g ); |
---|
119 | ftestGetInVar( realResult, 0 ); |
---|
120 | |
---|
121 | // do the test! |
---|
122 | ftestRun( result = extgcd( f, g, a, b ); ); |
---|
123 | |
---|
124 | // do the check |
---|
125 | ftestCheck( extgcdCheck( f, g, a, b, result, realResult ); ); |
---|
126 | |
---|
127 | // print results |
---|
128 | ftestOutput( "a", a, "b", b, "extgcd(f, g, a, b)", result ); |
---|
129 | |
---|
130 | // clean up |
---|
131 | ftestMainExit(); |
---|
132 | } |
---|