1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: fac_ezgcd.cc,v 1.10 1998-03-10 14:48:24 schmidt Exp $ */ |
---|
3 | |
---|
4 | #include <config.h> |
---|
5 | |
---|
6 | #include "assert.h" |
---|
7 | #include "debug.h" |
---|
8 | |
---|
9 | #include "cf_defs.h" |
---|
10 | #include "canonicalform.h" |
---|
11 | #include "fac_util.h" |
---|
12 | #include "cf_algorithm.h" |
---|
13 | #include "cf_reval.h" |
---|
14 | #include "cf_random.h" |
---|
15 | #include "cf_primes.h" |
---|
16 | #include "fac_distrib.h" |
---|
17 | #include "ftmpl_functions.h" |
---|
18 | |
---|
19 | |
---|
20 | static void findeval( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & Fb, CanonicalForm & Gb, CanonicalForm & Db, REvaluation & b, int delta, int degF, int degG ); |
---|
21 | |
---|
22 | static CanonicalForm ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG, REvaluation & b, bool internal ); |
---|
23 | |
---|
24 | static CanonicalForm ezgcd_specialcase ( const CanonicalForm & F, const CanonicalForm & G, REvaluation & b, const modpk & bound ); |
---|
25 | |
---|
26 | static modpk findBound ( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & lcF, const CanonicalForm & lcG, int degF, int degG ); |
---|
27 | |
---|
28 | static modpk enlargeBound ( const CanonicalForm & F, const CanonicalForm & Lb, const CanonicalForm & Db, const modpk & pk ); |
---|
29 | |
---|
30 | CanonicalForm |
---|
31 | ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG ) |
---|
32 | { |
---|
33 | REvaluation b; |
---|
34 | return ezgcd( FF, GG, b, false ); |
---|
35 | } |
---|
36 | |
---|
37 | static CanonicalForm |
---|
38 | ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG, REvaluation & b, bool internal ) |
---|
39 | { |
---|
40 | CanonicalForm F, G, f, g, d, Fb, Gb, Db, Fbt, Gbt, Dbt, B0, B, D0, lcF, lcG, lcD; |
---|
41 | CFArray DD( 1, 2 ), lcDD( 1, 2 ); |
---|
42 | int degF, degG, delta, deltaold, t; |
---|
43 | REvaluation bt; |
---|
44 | bool gcdfound = false; |
---|
45 | Variable x = Variable(1); |
---|
46 | modpk bound; |
---|
47 | |
---|
48 | DEBINCLEVEL( cerr, "ezgcd" ); |
---|
49 | DEBOUTLN( cerr, "FF = " << FF ); |
---|
50 | DEBOUTLN( cerr, "GG = " << GG ); |
---|
51 | f = content( FF, x ); g = content( GG, x ); d = gcd( f, g ); |
---|
52 | DEBOUTLN( cerr, "f = " << f ); |
---|
53 | DEBOUTLN( cerr, "g = " << g ); |
---|
54 | F = FF / f; G = GG / g; |
---|
55 | if ( F.isUnivariate() && G.isUnivariate() ) |
---|
56 | return d * gcd( F, G ); |
---|
57 | else if ( gcd_test_one( F, G, false ) ) |
---|
58 | return d; |
---|
59 | lcF = LC( F, x ); lcG = LC( G, x ); |
---|
60 | lcD = gcd( lcF, lcG ); |
---|
61 | delta = 0; |
---|
62 | degF = degree( F, x ); degG = degree( G, x ); |
---|
63 | t = tmax( F.level(), G.level() ); |
---|
64 | bound = findBound( F, G, lcF, lcG, degF, degG ); |
---|
65 | if ( ! internal ) |
---|
66 | b = REvaluation( 2, t, IntRandom( 100 ) ); |
---|
67 | while ( ! gcdfound ) { |
---|
68 | /// ---> A2 |
---|
69 | DEBOUTLN( cerr, "search for evaluation, delta = " << delta ); |
---|
70 | DEBOUTLN( cerr, "F = " << F ); |
---|
71 | DEBOUTLN( cerr, "G = " << G ); |
---|
72 | findeval( F, G, Fb, Gb, Db, b, delta, degF, degG ); |
---|
73 | DEBOUTLN( cerr, "found evaluation b = " << b ); |
---|
74 | DEBOUTLN( cerr, "F(b) = " << Fb ); |
---|
75 | DEBOUTLN( cerr, "G(b) = " << Gb ); |
---|
76 | DEBOUTLN( cerr, "D(b) = " << Db ); |
---|
77 | delta = degree( Db ); |
---|
78 | /// ---> A3 |
---|
79 | if ( delta == 0 ) |
---|
80 | return d; |
---|
81 | /// ---> A4 |
---|
82 | deltaold = delta + 1; |
---|
83 | while ( deltaold != delta ) { |
---|
84 | bt = b; |
---|
85 | findeval( F, G, Fbt, Gbt, Dbt, bt, delta + 1, degF, degG ); |
---|
86 | if ( degree( Dbt ) == 0 ) |
---|
87 | return d; |
---|
88 | if ( degree( Dbt ) == delta ) |
---|
89 | deltaold = delta; |
---|
90 | else if ( degree( Dbt ) < delta ) { |
---|
91 | deltaold = delta; |
---|
92 | delta = degree( Dbt ); |
---|
93 | b = bt; |
---|
94 | Db = Dbt; Fb = Fbt; Gb = Gbt; |
---|
95 | } |
---|
96 | } |
---|
97 | DEBOUTLN( cerr, "now after A4, delta = " << delta ); |
---|
98 | /// ---> A5 |
---|
99 | if ( degF <= degG && delta == degF && divides( F, G ) ) |
---|
100 | return d*F; |
---|
101 | if ( degG < degF && delta == degG && divides( G, F ) ) |
---|
102 | return d*G; |
---|
103 | if ( delta != degF && delta != degG ) { |
---|
104 | /// ---> A6 |
---|
105 | CanonicalForm xxx; |
---|
106 | if ( gcd( (DD[1] = Fb / Db), Db ) == 1 ) { |
---|
107 | B = F; |
---|
108 | DD[2] = Db; |
---|
109 | lcDD[1] = lcF; |
---|
110 | lcDD[2] = lcF; |
---|
111 | B *= lcF; |
---|
112 | } |
---|
113 | else if ( gcd( (DD[1] = Gb / Db), Db ) == 1 ) { |
---|
114 | B = G; |
---|
115 | DD[2] = Db; |
---|
116 | lcDD[1] = lcG; |
---|
117 | lcDD[2] = lcG; |
---|
118 | B *= lcG; |
---|
119 | } |
---|
120 | else { |
---|
121 | #ifdef DEBUGOUTPUT |
---|
122 | CanonicalForm dummyres = d * ezgcd_specialcase( F, G, b, bound ); |
---|
123 | DEBDECLEVEL( cerr, "ezgcd" ); |
---|
124 | return dummyres; |
---|
125 | #else |
---|
126 | return d * ezgcd_specialcase( F, G, b, bound ); |
---|
127 | #endif |
---|
128 | } |
---|
129 | /// ---> A7 |
---|
130 | DD[2] = DD[2] * ( b( lcDD[2] ) / lc( DD[2] ) ); |
---|
131 | DD[1] = DD[1] * ( b( lcDD[1] ) / lc( DD[1] ) ); |
---|
132 | bound = enlargeBound( B, DD[2], DD[1], bound ); |
---|
133 | DEBOUTLN( cerr, "(hensel) B = " << B ); |
---|
134 | DEBOUTLN( cerr, "(hensel) lcB = " << LC( B, Variable(1) ) ); |
---|
135 | DEBOUTLN( cerr, "(hensel) b(B) = " << b(B) ); |
---|
136 | DEBOUTLN( cerr, "(hensel) DD = " << DD ); |
---|
137 | DEBOUTLN( cerr, "(hensel) lcDD = " << lcDD ); |
---|
138 | gcdfound = Hensel( B, DD, lcDD, b, bound, x ); |
---|
139 | DEBOUTLN( cerr, "(hensel finished) DD = " << DD ); |
---|
140 | /// ---> A8 (gcdfound) |
---|
141 | } |
---|
142 | } |
---|
143 | /// ---> A9 |
---|
144 | DEBDECLEVEL( cerr, "ezgcd" ); |
---|
145 | return d * DD[2] / content(DD[2],Variable(1)); |
---|
146 | } |
---|
147 | |
---|
148 | static CanonicalForm |
---|
149 | ezgcd_specialcase ( const CanonicalForm & F, const CanonicalForm & G, REvaluation & b, const modpk & bound ) |
---|
150 | { |
---|
151 | DEBOUTLN( cerr, "ezgcd: special case" ); |
---|
152 | CanonicalForm Ft, Gt, L, LL, Fb, Gb, Db, Lb, D, Ds, Dt, d; |
---|
153 | CFArray DD( 1, 2 ), lcDD( 1, 2 ); |
---|
154 | Variable x = Variable( 1 ); |
---|
155 | bool gcdfound; |
---|
156 | |
---|
157 | Dt = 1; |
---|
158 | /// ---> S1 |
---|
159 | DEBOUTLN( cerr, "ezgcdspec: (S1)" ); |
---|
160 | Ft = ezgcd( F, F.deriv( x ) ); |
---|
161 | DEBOUTLN( cerr, "ezgcdspec: (S1) done, Ft = " << Ft ); |
---|
162 | L = F / Ft; |
---|
163 | /// ---> S2 |
---|
164 | DEBOUTLN( cerr, "ezgcdspec: (S2)" ); |
---|
165 | L = ezgcd( L, G, b, true ); |
---|
166 | DEBOUTLN( cerr, "ezgcdspec: (S2) done, Ds = " << Ds ); |
---|
167 | Gt = G / L; |
---|
168 | Ds = L; Dt = L; |
---|
169 | Lb = b( L ); Gb = b( Gt ); Fb = b( Ft ); |
---|
170 | d = gcd( LC( L, x ), gcd( LC( Ft, x ), LC( Gt, x ) ) ); |
---|
171 | while ( true ) { |
---|
172 | /// ---> S3 |
---|
173 | DEBOUTLN( cerr, "ezgcdspec: (S3)" ); |
---|
174 | DEBOUTLN( cerr, "ezgcdspec: Fb = " << Fb ); |
---|
175 | DEBOUTLN( cerr, "ezgcdspec: Gb = " << Gb ); |
---|
176 | DD[1] = gcd( Lb, gcd( Fb, Gb ) ); |
---|
177 | /// ---> S4 |
---|
178 | DEBOUTLN( cerr, "ezgcdspec: (S4)" ); |
---|
179 | if ( degree( DD[1] ) == 0 ) |
---|
180 | return Ds; |
---|
181 | /// ---> S5 |
---|
182 | DEBOUTLN( cerr, "ezgcdspec: (S5)" ); |
---|
183 | DEBOUTLN( cerr, "ezgcdspec: Lb = " << Lb ); |
---|
184 | DEBOUTLN( cerr, "ezgcdspec: Db = " << DD[1] ); |
---|
185 | Db = DD[1]; |
---|
186 | if ( ! (DD[2] = Lb/DD[1]).isOne() ) { |
---|
187 | DEBOUTLN( cerr, "ezgcdspec: (S55)" ); |
---|
188 | lcDD[2] = LC( L, x ); |
---|
189 | lcDD[1] = d; |
---|
190 | DD[1] = ( DD[1] * b( d ) ) / lc( DD[1] ); |
---|
191 | DD[2] = ( DD[2] * b( lcDD[2] ) ) / lc( DD[2] ); |
---|
192 | LL = L * d; |
---|
193 | modpk newbound = enlargeBound( LL, DD[2], DD[1], bound ); |
---|
194 | DEBOUTLN( cerr, "ezgcdspec: begin with lift." ); |
---|
195 | DEBOUTLN( cerr, "ezgcdspec: B = " << LL ); |
---|
196 | DEBOUTLN( cerr, "ezgcdspec: DD = " << DD ); |
---|
197 | DEBOUTLN( cerr, "ezgcdspec: lcDD = " << lcDD ); |
---|
198 | DEBOUTLN( cerr, "ezgcdspec: b = " << b ); |
---|
199 | DEBOUTLN( cerr, "ezgcdspec: bound = " << bound.getpk() ); |
---|
200 | DEBOUTLN( cerr, "ezgcdspec: lc(B) = " << LC( LL, x ) ); |
---|
201 | DEBOUTLN( cerr, "ezgcdspec: test = " << b( LL ) - DD[1] * DD[2] ); |
---|
202 | gcdfound = Hensel( LL, DD, lcDD, b, newbound, x ); |
---|
203 | ASSERT( gcdfound, "fatal error in algorithm" ); |
---|
204 | Dt = pp( DD[1] ); |
---|
205 | } |
---|
206 | DEBOUTLN( cerr, "ezgcdspec: (S7)" ); |
---|
207 | Ds = Ds * Dt; |
---|
208 | Fb = Fb / Db; |
---|
209 | Gb = Gb / Db; |
---|
210 | } |
---|
211 | // this point is never reached, but to avoid compiler warnings let's return a value |
---|
212 | return 0; |
---|
213 | } |
---|
214 | |
---|
215 | static void |
---|
216 | findeval( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & Fb, CanonicalForm & Gb, CanonicalForm & Db, REvaluation & b, int delta, int degF, int degG ) |
---|
217 | { |
---|
218 | bool ok; |
---|
219 | if ( delta != 0 ) |
---|
220 | b.nextpoint(); |
---|
221 | do { |
---|
222 | DEBOUTLN( cerr, "ezgcd: (findeval) b = " << b ); |
---|
223 | Fb = b( F ); |
---|
224 | ok = degree( Fb ) == degF; |
---|
225 | if ( ok ) { |
---|
226 | Gb = b( G ); |
---|
227 | ok = degree( Gb ) == degG; |
---|
228 | } |
---|
229 | if ( ok ) { |
---|
230 | Db = gcd( Fb, Gb ); |
---|
231 | if ( delta > 0 ) |
---|
232 | ok = degree( Db ) < delta; |
---|
233 | } |
---|
234 | if ( ! ok ) |
---|
235 | b.nextpoint(); |
---|
236 | } while ( ! ok ); |
---|
237 | } |
---|
238 | |
---|
239 | static modpk |
---|
240 | enlargeBound ( const CanonicalForm & F, const CanonicalForm & Lb, const CanonicalForm & Db, const modpk & pk ) |
---|
241 | { |
---|
242 | DEBOUTLN( cerr, "ezgcd: (enlarge bound) F = " << F ); |
---|
243 | DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb = " << Lb ); |
---|
244 | DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db = " << Db ); |
---|
245 | DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb % p = " << mod( Lb, pk.getp() ) ); |
---|
246 | DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db % p = " << mod( Db, pk.getp() ) ); |
---|
247 | |
---|
248 | CanonicalForm limit = power( CanonicalForm(2), degree( Db ) ) * |
---|
249 | tmax( maxCoeff( Lb ), tmax( maxCoeff( Db ), maxCoeff( F ) ) ); |
---|
250 | int p = pk.getp(); |
---|
251 | int k = pk.getk(); |
---|
252 | CanonicalForm bound = pk.getpk(); |
---|
253 | while ( bound < limit ) { |
---|
254 | k++; |
---|
255 | bound *= p; |
---|
256 | } |
---|
257 | k *= 2; |
---|
258 | DEBOUTLN( cerr, "ezgcd: (enlarge bound) newbound = " << power( CanonicalForm( p ), k ) ); |
---|
259 | return modpk( p, k ); |
---|
260 | } |
---|
261 | |
---|
262 | static modpk |
---|
263 | findBound ( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & lcF, const CanonicalForm & lcG, int degF, int degG ) |
---|
264 | { |
---|
265 | CanonicalForm limit = power( CanonicalForm(2), tmin( degF, degG ) ) * |
---|
266 | gcd( icontent( lcF ), icontent( lcG ) ) * tmin( maxCoeff( F ), maxCoeff( G ) ); |
---|
267 | int p, i = 0; |
---|
268 | do { |
---|
269 | p = cf_getBigPrime( i ); |
---|
270 | i++; |
---|
271 | } while ( mod( lcF, p ).isZero() && mod( lcG, p ).isZero() ); |
---|
272 | CanonicalForm bound = p; |
---|
273 | i = 1; |
---|
274 | while ( bound < limit ) { |
---|
275 | i++; |
---|
276 | bound *= p; |
---|
277 | } |
---|
278 | return modpk( p, i ); |
---|
279 | } |
---|