1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id$ */ |
---|
3 | |
---|
4 | ftestSetNameOfGame( gcd.ntl, `" |
---|
5 | Usage: gcd.ntl [<options>] [<envSpec>] <f> <g> [<realResult>] |
---|
6 | calculates greatest common divisor of canonical forms <f> and |
---|
7 | <g>. If the gcd of <f> and <g> is already known, the optional |
---|
8 | canonical form <realResult> may be used to check the result of |
---|
9 | the gcd computation. |
---|
10 | The gcd is calculated using the NTL library. Thus, <f> and <g> |
---|
11 | have to be univariate polynomials over some finite field or |
---|
12 | over the integers. |
---|
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 | #include "ntl_util.h" |
---|
27 | ftestPreprocInit(); |
---|
28 | |
---|
29 | ftestGlobalInit(); |
---|
30 | |
---|
31 | // |
---|
32 | // - functions. |
---|
33 | // |
---|
34 | |
---|
35 | //{{{ static CanonicalForm normalizeGcd ( const CanonicalForm & f ) |
---|
36 | //{{{ docu |
---|
37 | // |
---|
38 | // normalizeGcd() - normalize result of gcd computation for |
---|
39 | // testing. |
---|
40 | // |
---|
41 | // Unit normalization is done in case of a field, sign |
---|
42 | // normalization otherwise. |
---|
43 | // |
---|
44 | //}}} |
---|
45 | static CanonicalForm |
---|
46 | normalizeGcd ( const CanonicalForm & f ) |
---|
47 | { |
---|
48 | if ( getCharacteristic() > 0 || isOn( SW_RATIONAL ) ) |
---|
49 | return f/Lc( f ); |
---|
50 | else |
---|
51 | return abs( f ); |
---|
52 | } |
---|
53 | //}}} |
---|
54 | |
---|
55 | //{{{ ftestStatusT gcdCheck ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & result, const CanonicalForm & realResult ) |
---|
56 | //{{{ docu |
---|
57 | // |
---|
58 | // gcdCheck() - check result of gcd(). |
---|
59 | // |
---|
60 | //}}} |
---|
61 | ftestStatusT |
---|
62 | gcdCheck ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & result, const CanonicalForm & realResult ) |
---|
63 | { |
---|
64 | // if realResult is given, use it to compare with result |
---|
65 | if ( ! realResult.isZero() ) |
---|
66 | if ( normalizeGcd( realResult ) == normalizeGcd( result ) ) |
---|
67 | return Passed; |
---|
68 | else { |
---|
69 | ftestError( CheckError, "result and real result differ\n" ); |
---|
70 | return Failed; |
---|
71 | } |
---|
72 | |
---|
73 | if ( result.isZero() ) |
---|
74 | if ( f.isZero() && g.isZero() ) |
---|
75 | return Passed; |
---|
76 | else { |
---|
77 | ftestError( CheckError, "result is zero but f and g are not\n" ); |
---|
78 | return Failed; |
---|
79 | } |
---|
80 | |
---|
81 | // compare with Factory's gcd |
---|
82 | CanonicalForm factoryResult = gcd( f, g ); |
---|
83 | if ( normalizeGcd( factoryResult ) == normalizeGcd( result ) ) |
---|
84 | return Passed; |
---|
85 | else { |
---|
86 | ftestError( CheckError, "result and Factory's result differ\n" ); |
---|
87 | return Failed; |
---|
88 | } |
---|
89 | |
---|
90 | if ( ! divides( result, f ) || ! divides( result, g ) ) { |
---|
91 | ftestError( CheckError, "result is not a common divisor\n" ); |
---|
92 | return Failed; |
---|
93 | } else if ( ! gcd( f/result, g/result ).isOne() ) { |
---|
94 | ftestError( CheckError, "result is not greatest common divisor\n" ); |
---|
95 | return Failed; |
---|
96 | } else |
---|
97 | return Passed; |
---|
98 | } |
---|
99 | //}}} |
---|
100 | |
---|
101 | // |
---|
102 | // - main program. |
---|
103 | // |
---|
104 | int |
---|
105 | main ( int argc, char ** argv ) |
---|
106 | { |
---|
107 | // initialization |
---|
108 | ftestMainInit(); |
---|
109 | |
---|
110 | // declare input and output variables |
---|
111 | ftestOutVar( CanonicalForm, result ); |
---|
112 | ftestInVar( CanonicalForm, f ); |
---|
113 | ftestInVar( CanonicalForm, g ); |
---|
114 | ftestInVar( CanonicalForm, realResult ); |
---|
115 | |
---|
116 | // process argument list and set environment |
---|
117 | ftestGetOpts(); |
---|
118 | ftestGetEnv(); |
---|
119 | ftestGetInVar( f ); |
---|
120 | ftestGetInVar( g ); |
---|
121 | ftestGetInVar( realResult, 0 ); |
---|
122 | |
---|
123 | // prepare NTL |
---|
124 | setCharacteristicNTL( getCharacteristic() ); |
---|
125 | |
---|
126 | // run gcd from NTL |
---|
127 | if ( getCharacteristic() > 0 ) { |
---|
128 | zz_pX fNTL; |
---|
129 | zz_pX gNTL; |
---|
130 | zz_pX resultNTL; |
---|
131 | fNTL << f; |
---|
132 | gNTL << g; |
---|
133 | |
---|
134 | // do the test! |
---|
135 | ftestRun( GCD( resultNTL, fNTL, gNTL ); ); |
---|
136 | |
---|
137 | result << resultNTL; |
---|
138 | } else { |
---|
139 | ZZX fNTL; |
---|
140 | ZZX gNTL; |
---|
141 | ZZX resultNTL; |
---|
142 | fNTL << f; |
---|
143 | gNTL << g; |
---|
144 | |
---|
145 | // do the test! |
---|
146 | ftestRun( GCD( resultNTL, fNTL, gNTL ); ); |
---|
147 | |
---|
148 | result << resultNTL; |
---|
149 | } |
---|
150 | |
---|
151 | // do the check |
---|
152 | ftestCheck( gcdCheck( f, g, result, realResult ) ); |
---|
153 | |
---|
154 | // print results |
---|
155 | ftestOutput( "gcd(f, g)", result ); |
---|
156 | |
---|
157 | // clean up |
---|
158 | ftestMainExit(); |
---|
159 | } |
---|