1 | #include "config.h" |
---|
2 | |
---|
3 | #ifndef NOSTREAMIO |
---|
4 | #ifdef HAVE_CSTDIO |
---|
5 | #include <cstdio> |
---|
6 | #else |
---|
7 | #include <stdio.h> |
---|
8 | #endif |
---|
9 | #ifdef HAVE_IOSTREAM_H |
---|
10 | #include <iostream.h> |
---|
11 | #elif defined(HAVE_IOSTREAM) |
---|
12 | #include <iostream> |
---|
13 | #endif |
---|
14 | #endif |
---|
15 | |
---|
16 | #include "cf_assert.h" |
---|
17 | #include "timing.h" |
---|
18 | |
---|
19 | #include "templates/ftmpl_functions.h" |
---|
20 | #include "cf_defs.h" |
---|
21 | #include "canonicalform.h" |
---|
22 | #include "cf_iter.h" |
---|
23 | #include "cf_primes.h" |
---|
24 | #include "cf_algorithm.h" |
---|
25 | #include "algext.h" |
---|
26 | #include "cf_map.h" |
---|
27 | #include "cf_generator.h" |
---|
28 | #include "facMul.h" |
---|
29 | #include "facNTLzzpEXGCD.h" |
---|
30 | |
---|
31 | #ifdef HAVE_NTL |
---|
32 | #include "NTLconvert.h" |
---|
33 | #endif |
---|
34 | |
---|
35 | #ifdef HAVE_FLINT |
---|
36 | #include "FLINTconvert.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | TIMING_DEFINE_PRINT(alg_content_p) |
---|
40 | TIMING_DEFINE_PRINT(alg_content) |
---|
41 | TIMING_DEFINE_PRINT(alg_compress) |
---|
42 | TIMING_DEFINE_PRINT(alg_termination) |
---|
43 | TIMING_DEFINE_PRINT(alg_termination_p) |
---|
44 | TIMING_DEFINE_PRINT(alg_reconstruction) |
---|
45 | TIMING_DEFINE_PRINT(alg_newton_p) |
---|
46 | TIMING_DEFINE_PRINT(alg_recursion_p) |
---|
47 | TIMING_DEFINE_PRINT(alg_gcd_p) |
---|
48 | TIMING_DEFINE_PRINT(alg_euclid_p) |
---|
49 | |
---|
50 | /// compressing two polynomials F and G, M is used for compressing, |
---|
51 | /// N to reverse the compression |
---|
52 | static |
---|
53 | int myCompress (const CanonicalForm& F, const CanonicalForm& G, CFMap & M, |
---|
54 | CFMap & N, bool topLevel) |
---|
55 | { |
---|
56 | int n= tmax (F.level(), G.level()); |
---|
57 | int * degsf= new int [n + 1]; |
---|
58 | int * degsg= new int [n + 1]; |
---|
59 | |
---|
60 | for (int i = 0; i <= n; i++) |
---|
61 | degsf[i]= degsg[i]= 0; |
---|
62 | |
---|
63 | degsf= degrees (F, degsf); |
---|
64 | degsg= degrees (G, degsg); |
---|
65 | |
---|
66 | int both_non_zero= 0; |
---|
67 | int f_zero= 0; |
---|
68 | int g_zero= 0; |
---|
69 | int both_zero= 0; |
---|
70 | |
---|
71 | if (topLevel) |
---|
72 | { |
---|
73 | for (int i= 1; i <= n; i++) |
---|
74 | { |
---|
75 | if (degsf[i] != 0 && degsg[i] != 0) |
---|
76 | { |
---|
77 | both_non_zero++; |
---|
78 | continue; |
---|
79 | } |
---|
80 | if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level()) |
---|
81 | { |
---|
82 | f_zero++; |
---|
83 | continue; |
---|
84 | } |
---|
85 | if (degsg[i] == 0 && degsf[i] && i <= F.level()) |
---|
86 | { |
---|
87 | g_zero++; |
---|
88 | continue; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | if (both_non_zero == 0) |
---|
93 | { |
---|
94 | delete [] degsf; |
---|
95 | delete [] degsg; |
---|
96 | return 0; |
---|
97 | } |
---|
98 | |
---|
99 | // map Variables which do not occur in both polynomials to higher levels |
---|
100 | int k= 1; |
---|
101 | int l= 1; |
---|
102 | for (int i= 1; i <= n; i++) |
---|
103 | { |
---|
104 | if (degsf[i] != 0 && degsg[i] == 0 && i <= F.level()) |
---|
105 | { |
---|
106 | if (k + both_non_zero != i) |
---|
107 | { |
---|
108 | M.newpair (Variable (i), Variable (k + both_non_zero)); |
---|
109 | N.newpair (Variable (k + both_non_zero), Variable (i)); |
---|
110 | } |
---|
111 | k++; |
---|
112 | } |
---|
113 | if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level()) |
---|
114 | { |
---|
115 | if (l + g_zero + both_non_zero != i) |
---|
116 | { |
---|
117 | M.newpair (Variable (i), Variable (l + g_zero + both_non_zero)); |
---|
118 | N.newpair (Variable (l + g_zero + both_non_zero), Variable (i)); |
---|
119 | } |
---|
120 | l++; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | // sort Variables x_{i} in increasing order of |
---|
125 | // min(deg_{x_{i}}(f),deg_{x_{i}}(g)) |
---|
126 | int m= tmax (F.level(), G.level()); |
---|
127 | int min_max_deg; |
---|
128 | k= both_non_zero; |
---|
129 | l= 0; |
---|
130 | int i= 1; |
---|
131 | while (k > 0) |
---|
132 | { |
---|
133 | if (degsf [i] != 0 && degsg [i] != 0) |
---|
134 | min_max_deg= tmax (degsf[i], degsg[i]); |
---|
135 | else |
---|
136 | min_max_deg= 0; |
---|
137 | while (min_max_deg == 0) |
---|
138 | { |
---|
139 | i++; |
---|
140 | min_max_deg= tmax (degsf[i], degsg[i]); |
---|
141 | if (degsf [i] != 0 && degsg [i] != 0) |
---|
142 | min_max_deg= tmax (degsf[i], degsg[i]); |
---|
143 | else |
---|
144 | min_max_deg= 0; |
---|
145 | } |
---|
146 | for (int j= i + 1; j <= m; j++) |
---|
147 | { |
---|
148 | if (tmax (degsf[j],degsg[j]) <= min_max_deg && degsf[j] != 0 && degsg [j] != 0) |
---|
149 | { |
---|
150 | min_max_deg= tmax (degsf[j], degsg[j]); |
---|
151 | l= j; |
---|
152 | } |
---|
153 | } |
---|
154 | if (l != 0) |
---|
155 | { |
---|
156 | if (l != k) |
---|
157 | { |
---|
158 | M.newpair (Variable (l), Variable(k)); |
---|
159 | N.newpair (Variable (k), Variable(l)); |
---|
160 | degsf[l]= 0; |
---|
161 | degsg[l]= 0; |
---|
162 | l= 0; |
---|
163 | } |
---|
164 | else |
---|
165 | { |
---|
166 | degsf[l]= 0; |
---|
167 | degsg[l]= 0; |
---|
168 | l= 0; |
---|
169 | } |
---|
170 | } |
---|
171 | else if (l == 0) |
---|
172 | { |
---|
173 | if (i != k) |
---|
174 | { |
---|
175 | M.newpair (Variable (i), Variable (k)); |
---|
176 | N.newpair (Variable (k), Variable (i)); |
---|
177 | degsf[i]= 0; |
---|
178 | degsg[i]= 0; |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | degsf[i]= 0; |
---|
183 | degsg[i]= 0; |
---|
184 | } |
---|
185 | i++; |
---|
186 | } |
---|
187 | k--; |
---|
188 | } |
---|
189 | } |
---|
190 | else |
---|
191 | { |
---|
192 | //arrange Variables such that no gaps occur |
---|
193 | for (int i= 1; i <= n; i++) |
---|
194 | { |
---|
195 | if (degsf[i] == 0 && degsg[i] == 0) |
---|
196 | { |
---|
197 | both_zero++; |
---|
198 | continue; |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | if (both_zero != 0) |
---|
203 | { |
---|
204 | M.newpair (Variable (i), Variable (i - both_zero)); |
---|
205 | N.newpair (Variable (i - both_zero), Variable (i)); |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | delete [] degsf; |
---|
212 | delete [] degsg; |
---|
213 | |
---|
214 | return 1; |
---|
215 | } |
---|
216 | |
---|
217 | void tryInvert( const CanonicalForm & F, const CanonicalForm & M, CanonicalForm & inv, bool & fail ) |
---|
218 | { // F, M are required to be "univariate" polynomials in an algebraic variable |
---|
219 | // we try to invert F modulo M |
---|
220 | if(F.inBaseDomain()) |
---|
221 | { |
---|
222 | if(F.isZero()) |
---|
223 | { |
---|
224 | fail = true; |
---|
225 | return; |
---|
226 | } |
---|
227 | inv = 1/F; |
---|
228 | return; |
---|
229 | } |
---|
230 | CanonicalForm b; |
---|
231 | Variable a = M.mvar(); |
---|
232 | Variable x = Variable(1); |
---|
233 | if(!extgcd( replacevar( F, a, x ), replacevar( M, a, x ), inv, b ).isOne()) |
---|
234 | fail = true; |
---|
235 | else |
---|
236 | inv = replacevar( inv, x, a ); // change back to alg var |
---|
237 | } |
---|
238 | |
---|
239 | void tryDivrem (const CanonicalForm& F, const CanonicalForm& G, CanonicalForm& Q, |
---|
240 | CanonicalForm& R, CanonicalForm& inv, const CanonicalForm& mipo, |
---|
241 | bool& fail) |
---|
242 | { |
---|
243 | if (F.inCoeffDomain()) |
---|
244 | { |
---|
245 | Q= 0; |
---|
246 | R= F; |
---|
247 | return; |
---|
248 | } |
---|
249 | |
---|
250 | CanonicalForm A, B; |
---|
251 | Variable x= F.mvar(); |
---|
252 | A= F; |
---|
253 | B= G; |
---|
254 | int degA= degree (A, x); |
---|
255 | int degB= degree (B, x); |
---|
256 | |
---|
257 | if (degA < degB) |
---|
258 | { |
---|
259 | R= A; |
---|
260 | Q= 0; |
---|
261 | return; |
---|
262 | } |
---|
263 | |
---|
264 | tryInvert (Lc (B), mipo, inv, fail); |
---|
265 | if (fail) |
---|
266 | return; |
---|
267 | |
---|
268 | R= A; |
---|
269 | Q= 0; |
---|
270 | CanonicalForm Qi; |
---|
271 | for (int i= degA -degB; i >= 0; i--) |
---|
272 | { |
---|
273 | if (degree (R, x) == i + degB) |
---|
274 | { |
---|
275 | Qi= Lc (R)*inv*power (x, i); |
---|
276 | Qi= reduce (Qi, mipo); |
---|
277 | R -= Qi*B; |
---|
278 | R= reduce (R, mipo); |
---|
279 | Q += Qi; |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | void tryEuclid( const CanonicalForm & A, const CanonicalForm & B, const CanonicalForm & M, CanonicalForm & result, bool & fail ) |
---|
285 | { |
---|
286 | CanonicalForm P; |
---|
287 | if(A.inCoeffDomain()) |
---|
288 | { |
---|
289 | tryInvert( A, M, P, fail ); |
---|
290 | if(fail) |
---|
291 | return; |
---|
292 | result = 1; |
---|
293 | return; |
---|
294 | } |
---|
295 | if(B.inCoeffDomain()) |
---|
296 | { |
---|
297 | tryInvert( B, M, P, fail ); |
---|
298 | if(fail) |
---|
299 | return; |
---|
300 | result = 1; |
---|
301 | return; |
---|
302 | } |
---|
303 | // here: both not inCoeffDomain |
---|
304 | if( A.degree() > B.degree() ) |
---|
305 | { |
---|
306 | P = A; result = B; |
---|
307 | } |
---|
308 | else |
---|
309 | { |
---|
310 | P = B; result = A; |
---|
311 | } |
---|
312 | CanonicalForm inv; |
---|
313 | if( result.isZero() ) |
---|
314 | { |
---|
315 | tryInvert( Lc(P), M, inv, fail ); |
---|
316 | if(fail) |
---|
317 | return; |
---|
318 | result = inv*P; // monify result (not reduced, yet) |
---|
319 | result= reduce (result, M); |
---|
320 | return; |
---|
321 | } |
---|
322 | Variable x = P.mvar(); |
---|
323 | CanonicalForm rem, Q; |
---|
324 | // here: degree(P) >= degree(result) |
---|
325 | while(true) |
---|
326 | { |
---|
327 | tryDivrem (P, result, Q, rem, inv, M, fail); |
---|
328 | if (fail) |
---|
329 | return; |
---|
330 | if( rem.isZero() ) |
---|
331 | { |
---|
332 | result *= inv; |
---|
333 | result= reduce (result, M); |
---|
334 | return; |
---|
335 | } |
---|
336 | if(result.degree(x) >= rem.degree(x)) |
---|
337 | { |
---|
338 | P = result; |
---|
339 | result = rem; |
---|
340 | } |
---|
341 | else |
---|
342 | P = rem; |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | bool hasFirstAlgVar( const CanonicalForm & f, Variable & a ) |
---|
347 | { |
---|
348 | if( f.inBaseDomain() ) // f has NO alg. variable |
---|
349 | return false; |
---|
350 | if( f.level()<0 ) // f has only alg. vars, so take the first one |
---|
351 | { |
---|
352 | a = f.mvar(); |
---|
353 | return true; |
---|
354 | } |
---|
355 | for(CFIterator i=f; i.hasTerms(); i++) |
---|
356 | if( hasFirstAlgVar( i.coeff(), a )) |
---|
357 | return true; // 'a' is already set |
---|
358 | return false; |
---|
359 | } |
---|
360 | |
---|
361 | CanonicalForm QGCD( const CanonicalForm & F, const CanonicalForm & G ); |
---|
362 | int * leadDeg(const CanonicalForm & f, int *degs); |
---|
363 | bool isLess(int *a, int *b, int lower, int upper); |
---|
364 | bool isEqual(int *a, int *b, int lower, int upper); |
---|
365 | CanonicalForm firstLC(const CanonicalForm & f); |
---|
366 | static CanonicalForm trycontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail ); |
---|
367 | static CanonicalForm tryvcontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail ); |
---|
368 | static CanonicalForm trycf_content ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, bool & fail ); |
---|
369 | |
---|
370 | static inline CanonicalForm |
---|
371 | tryNewtonInterp (const CanonicalForm alpha, const CanonicalForm u, |
---|
372 | const CanonicalForm newtonPoly, const CanonicalForm oldInterPoly, |
---|
373 | const Variable & x, const CanonicalForm& M, bool& fail) |
---|
374 | { |
---|
375 | CanonicalForm interPoly; |
---|
376 | |
---|
377 | CanonicalForm inv; |
---|
378 | tryInvert (newtonPoly (alpha, x), M, inv, fail); |
---|
379 | if (fail) |
---|
380 | return 0; |
---|
381 | |
---|
382 | interPoly= oldInterPoly+reduce ((u - oldInterPoly (alpha, x))*inv*newtonPoly, M); |
---|
383 | return interPoly; |
---|
384 | } |
---|
385 | |
---|
386 | void tryBrownGCD( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, bool & fail, bool topLevel ) |
---|
387 | { // assume F,G are multivariate polys over Z/p(a) for big prime p, M "univariate" polynomial in an algebraic variable |
---|
388 | // M is assumed to be monic |
---|
389 | if(F.isZero()) |
---|
390 | { |
---|
391 | if(G.isZero()) |
---|
392 | { |
---|
393 | result = G; // G is zero |
---|
394 | return; |
---|
395 | } |
---|
396 | if(G.inCoeffDomain()) |
---|
397 | { |
---|
398 | tryInvert(G,M,result,fail); |
---|
399 | if(fail) |
---|
400 | return; |
---|
401 | result = 1; |
---|
402 | return; |
---|
403 | } |
---|
404 | // try to make G monic modulo M |
---|
405 | CanonicalForm inv; |
---|
406 | tryInvert(Lc(G),M,inv,fail); |
---|
407 | if(fail) |
---|
408 | return; |
---|
409 | result = inv*G; |
---|
410 | result= reduce (result, M); |
---|
411 | return; |
---|
412 | } |
---|
413 | if(G.isZero()) // F is non-zero |
---|
414 | { |
---|
415 | if(F.inCoeffDomain()) |
---|
416 | { |
---|
417 | tryInvert(F,M,result,fail); |
---|
418 | if(fail) |
---|
419 | return; |
---|
420 | result = 1; |
---|
421 | return; |
---|
422 | } |
---|
423 | // try to make F monic modulo M |
---|
424 | CanonicalForm inv; |
---|
425 | tryInvert(Lc(F),M,inv,fail); |
---|
426 | if(fail) |
---|
427 | return; |
---|
428 | result = inv*F; |
---|
429 | result= reduce (result, M); |
---|
430 | return; |
---|
431 | } |
---|
432 | // here: F,G both nonzero |
---|
433 | if(F.inCoeffDomain()) |
---|
434 | { |
---|
435 | tryInvert(F,M,result,fail); |
---|
436 | if(fail) |
---|
437 | return; |
---|
438 | result = 1; |
---|
439 | return; |
---|
440 | } |
---|
441 | if(G.inCoeffDomain()) |
---|
442 | { |
---|
443 | tryInvert(G,M,result,fail); |
---|
444 | if(fail) |
---|
445 | return; |
---|
446 | result = 1; |
---|
447 | return; |
---|
448 | } |
---|
449 | TIMING_START (alg_compress) |
---|
450 | CFMap MM,NN; |
---|
451 | int lev= myCompress (F, G, MM, NN, topLevel); |
---|
452 | if (lev == 0) |
---|
453 | { |
---|
454 | result= 1; |
---|
455 | return; |
---|
456 | } |
---|
457 | CanonicalForm f=MM(F); |
---|
458 | CanonicalForm g=MM(G); |
---|
459 | TIMING_END_AND_PRINT (alg_compress, "time to compress in alg gcd: ") |
---|
460 | // here: f,g are compressed |
---|
461 | // compute largest variable in f or g (least one is Variable(1)) |
---|
462 | int mv = f.level(); |
---|
463 | if(g.level() > mv) |
---|
464 | mv = g.level(); |
---|
465 | // here: mv is level of the largest variable in f, g |
---|
466 | Variable v1= Variable (1); |
---|
467 | #ifdef HAVE_NTL |
---|
468 | Variable v= M.mvar(); |
---|
469 | zz_p::init (getCharacteristic()); |
---|
470 | zz_pX NTLMipo= convertFacCF2NTLzzpX (M); |
---|
471 | zz_pE::init (NTLMipo); |
---|
472 | zz_pEX NTLResult; |
---|
473 | zz_pEX NTLF; |
---|
474 | zz_pEX NTLG; |
---|
475 | #endif |
---|
476 | if(mv == 1) // f,g univariate |
---|
477 | { |
---|
478 | TIMING_START (alg_euclid_p) |
---|
479 | #ifdef HAVE_NTL |
---|
480 | NTLF= convertFacCF2NTLzz_pEX (f, NTLMipo); |
---|
481 | NTLG= convertFacCF2NTLzz_pEX (g, NTLMipo); |
---|
482 | tryNTLGCD (NTLResult, NTLF, NTLG, fail); |
---|
483 | if (fail) |
---|
484 | return; |
---|
485 | result= convertNTLzz_pEX2CF (NTLResult, f.mvar(), v); |
---|
486 | #else |
---|
487 | tryEuclid(f,g,M,result,fail); |
---|
488 | if(fail) |
---|
489 | return; |
---|
490 | #endif |
---|
491 | TIMING_END_AND_PRINT (alg_euclid_p, "time for euclidean alg mod p: ") |
---|
492 | result= NN (reduce (result, M)); // do not forget to map back |
---|
493 | return; |
---|
494 | } |
---|
495 | TIMING_START (alg_content_p) |
---|
496 | // here: mv > 1 |
---|
497 | CanonicalForm cf = tryvcontent(f, Variable(2), M, fail); // cf is univariate poly in var(1) |
---|
498 | if(fail) |
---|
499 | return; |
---|
500 | CanonicalForm cg = tryvcontent(g, Variable(2), M, fail); |
---|
501 | if(fail) |
---|
502 | return; |
---|
503 | CanonicalForm c; |
---|
504 | #ifdef HAVE_NTL |
---|
505 | NTLF= convertFacCF2NTLzz_pEX (cf, NTLMipo); |
---|
506 | NTLG= convertFacCF2NTLzz_pEX (cg, NTLMipo); |
---|
507 | tryNTLGCD (NTLResult, NTLF, NTLG, fail); |
---|
508 | if (fail) |
---|
509 | return; |
---|
510 | c= convertNTLzz_pEX2CF (NTLResult, v1, v); |
---|
511 | #else |
---|
512 | tryEuclid(cf,cg,M,c,fail); |
---|
513 | if(fail) |
---|
514 | return; |
---|
515 | #endif |
---|
516 | // f /= cf |
---|
517 | f.tryDiv (cf, M, fail); |
---|
518 | if(fail) |
---|
519 | return; |
---|
520 | // g /= cg |
---|
521 | g.tryDiv (cg, M, fail); |
---|
522 | if(fail) |
---|
523 | return; |
---|
524 | TIMING_END_AND_PRINT (alg_content_p, "time for content in alg gcd mod p: ") |
---|
525 | if(f.inCoeffDomain()) |
---|
526 | { |
---|
527 | tryInvert(f,M,result,fail); |
---|
528 | if(fail) |
---|
529 | return; |
---|
530 | result = NN(c); |
---|
531 | return; |
---|
532 | } |
---|
533 | if(g.inCoeffDomain()) |
---|
534 | { |
---|
535 | tryInvert(g,M,result,fail); |
---|
536 | if(fail) |
---|
537 | return; |
---|
538 | result = NN(c); |
---|
539 | return; |
---|
540 | } |
---|
541 | int *L = new int[mv+1]; // L is addressed by i from 2 to mv |
---|
542 | int *N = new int[mv+1]; |
---|
543 | for(int i=2; i<=mv; i++) |
---|
544 | L[i] = N[i] = 0; |
---|
545 | L = leadDeg(f, L); |
---|
546 | N = leadDeg(g, N); |
---|
547 | CanonicalForm gamma; |
---|
548 | TIMING_START (alg_euclid_p) |
---|
549 | #ifdef HAVE_NTL |
---|
550 | NTLF= convertFacCF2NTLzz_pEX (firstLC (f), NTLMipo); |
---|
551 | NTLG= convertFacCF2NTLzz_pEX (firstLC (g), NTLMipo); |
---|
552 | tryNTLGCD (NTLResult, NTLF, NTLG, fail); |
---|
553 | if (fail) |
---|
554 | return; |
---|
555 | gamma= convertNTLzz_pEX2CF (NTLResult, v1, v); |
---|
556 | #else |
---|
557 | tryEuclid( firstLC(f), firstLC(g), M, gamma, fail ); |
---|
558 | if(fail) |
---|
559 | return; |
---|
560 | #endif |
---|
561 | TIMING_END_AND_PRINT (alg_euclid_p, "time for gcd of lcs in alg mod p: ") |
---|
562 | for(int i=2; i<=mv; i++) // entries at i=0,1 not visited |
---|
563 | if(N[i] < L[i]) |
---|
564 | L[i] = N[i]; |
---|
565 | // L is now upper bound for degrees of gcd |
---|
566 | int *dg_im = new int[mv+1]; // for the degree vector of the image we don't need any entry at i=1 |
---|
567 | for(int i=2; i<=mv; i++) |
---|
568 | dg_im[i] = 0; // initialize |
---|
569 | CanonicalForm gamma_image, m=1; |
---|
570 | CanonicalForm gm=0; |
---|
571 | CanonicalForm g_image, alpha, gnew; |
---|
572 | FFGenerator gen = FFGenerator(); |
---|
573 | Variable x= Variable (1); |
---|
574 | bool divides= true; |
---|
575 | for(FFGenerator gen = FFGenerator(); gen.hasItems(); gen.next()) |
---|
576 | { |
---|
577 | alpha = gen.item(); |
---|
578 | gamma_image = reduce(gamma(alpha, x),M); // plug in alpha for var(1) |
---|
579 | if(gamma_image.isZero()) // skip lc-bad points var(1)-alpha |
---|
580 | continue; |
---|
581 | TIMING_START (alg_recursion_p) |
---|
582 | tryBrownGCD( f(alpha, x), g(alpha, x), M, g_image, fail, false ); // recursive call with one var less |
---|
583 | TIMING_END_AND_PRINT (alg_recursion_p, |
---|
584 | "time for recursive calls in alg gcd mod p: ") |
---|
585 | if(fail) |
---|
586 | return; |
---|
587 | g_image = reduce(g_image, M); |
---|
588 | if(g_image.inCoeffDomain()) // early termination |
---|
589 | { |
---|
590 | tryInvert(g_image,M,result,fail); |
---|
591 | if(fail) |
---|
592 | return; |
---|
593 | result = NN(c); |
---|
594 | return; |
---|
595 | } |
---|
596 | for(int i=2; i<=mv; i++) |
---|
597 | dg_im[i] = 0; // reset (this is necessary, because some entries may not be updated by call to leadDeg) |
---|
598 | dg_im = leadDeg(g_image, dg_im); // dg_im cannot be NIL-pointer |
---|
599 | if(isEqual(dg_im, L, 2, mv)) |
---|
600 | { |
---|
601 | CanonicalForm inv; |
---|
602 | tryInvert (firstLC (g_image), M, inv, fail); |
---|
603 | if (fail) |
---|
604 | return; |
---|
605 | g_image *= inv; |
---|
606 | g_image *= gamma_image; // multiply by multiple of image lc(gcd) |
---|
607 | g_image= reduce (g_image, M); |
---|
608 | TIMING_START (alg_newton_p) |
---|
609 | gnew= tryNewtonInterp (alpha, g_image, m, gm, x, M, fail); |
---|
610 | TIMING_END_AND_PRINT (alg_newton_p, |
---|
611 | "time for Newton interpolation in alg gcd mod p: ") |
---|
612 | // gnew = gm mod m |
---|
613 | // gnew = g_image mod var(1)-alpha |
---|
614 | // mnew = m * (var(1)-alpha) |
---|
615 | if(fail) |
---|
616 | return; |
---|
617 | m *= (x - alpha); |
---|
618 | if((firstLC(gnew) == gamma) || (gnew == gm)) // gnew did not change |
---|
619 | { |
---|
620 | TIMING_START (alg_termination_p) |
---|
621 | cf = tryvcontent(gnew, Variable(2), M, fail); |
---|
622 | if(fail) |
---|
623 | return; |
---|
624 | divides = true; |
---|
625 | g_image= gnew; |
---|
626 | g_image.tryDiv (cf, M, fail); |
---|
627 | if(fail) |
---|
628 | return; |
---|
629 | divides= tryFdivides (g_image,f, M, fail); // trial division (f) |
---|
630 | if(fail) |
---|
631 | return; |
---|
632 | if(divides) |
---|
633 | { |
---|
634 | bool divides2= tryFdivides (g_image,g, M, fail); // trial division (g) |
---|
635 | if(fail) |
---|
636 | return; |
---|
637 | if(divides2) |
---|
638 | { |
---|
639 | result = NN(reduce (c*g_image, M)); |
---|
640 | TIMING_END_AND_PRINT (alg_termination_p, |
---|
641 | "time for successful termination test in alg gcd mod p: ") |
---|
642 | return; |
---|
643 | } |
---|
644 | } |
---|
645 | TIMING_END_AND_PRINT (alg_termination_p, |
---|
646 | "time for unsuccessful termination test in alg gcd mod p: ") |
---|
647 | } |
---|
648 | gm = gnew; |
---|
649 | continue; |
---|
650 | } |
---|
651 | |
---|
652 | if(isLess(L, dg_im, 2, mv)) // dg_im > L --> current point unlucky |
---|
653 | continue; |
---|
654 | |
---|
655 | // here: isLess(dg_im, L, 2, mv) --> all previous points were unlucky |
---|
656 | m = CanonicalForm(1); // reset |
---|
657 | gm = 0; // reset |
---|
658 | for(int i=2; i<=mv; i++) // tighten bound |
---|
659 | L[i] = dg_im[i]; |
---|
660 | } |
---|
661 | // we are out of evaluation points |
---|
662 | fail = true; |
---|
663 | } |
---|
664 | |
---|
665 | static CanonicalForm |
---|
666 | myicontent ( const CanonicalForm & f, const CanonicalForm & c ) |
---|
667 | { |
---|
668 | #ifdef HAVE_NTL |
---|
669 | if (f.isOne() || c.isOne()) |
---|
670 | return 1; |
---|
671 | if ( f.inBaseDomain() && c.inBaseDomain()) |
---|
672 | { |
---|
673 | if (c.isZero()) return abs(f); |
---|
674 | return bgcd( f, c ); |
---|
675 | } |
---|
676 | else if ( (f.inCoeffDomain() && c.inCoeffDomain()) || |
---|
677 | (f.inCoeffDomain() && c.inBaseDomain()) || |
---|
678 | (f.inBaseDomain() && c.inCoeffDomain())) |
---|
679 | { |
---|
680 | if (c.isZero()) return abs (f); |
---|
681 | #ifdef HAVE_FLINT |
---|
682 | fmpz_poly_t FLINTf, FLINTc; |
---|
683 | convertFacCF2Fmpz_poly_t (FLINTf, f); |
---|
684 | convertFacCF2Fmpz_poly_t (FLINTc, c); |
---|
685 | fmpz_poly_gcd (FLINTc, FLINTc, FLINTf); |
---|
686 | CanonicalForm result; |
---|
687 | if (f.inCoeffDomain()) |
---|
688 | result= convertFmpz_poly_t2FacCF (FLINTc, f.mvar()); |
---|
689 | else |
---|
690 | result= convertFmpz_poly_t2FacCF (FLINTc, c.mvar()); |
---|
691 | fmpz_poly_clear (FLINTc); |
---|
692 | fmpz_poly_clear (FLINTf); |
---|
693 | return result; |
---|
694 | #else |
---|
695 | ZZX NTLf= convertFacCF2NTLZZX (f); |
---|
696 | ZZX NTLc= convertFacCF2NTLZZX (c); |
---|
697 | NTLc= GCD (NTLc, NTLf); |
---|
698 | if (f.inCoeffDomain()) |
---|
699 | return convertNTLZZX2CF(NTLc,f.mvar()); |
---|
700 | else |
---|
701 | return convertNTLZZX2CF(NTLc,c.mvar()); |
---|
702 | #endif |
---|
703 | } |
---|
704 | else |
---|
705 | { |
---|
706 | CanonicalForm g = c; |
---|
707 | for ( CFIterator i = f; i.hasTerms() && ! g.isOne(); i++ ) |
---|
708 | g = myicontent( i.coeff(), g ); |
---|
709 | return g; |
---|
710 | } |
---|
711 | #else |
---|
712 | return 1; |
---|
713 | #endif |
---|
714 | } |
---|
715 | |
---|
716 | CanonicalForm |
---|
717 | myicontent ( const CanonicalForm & f ) |
---|
718 | { |
---|
719 | #ifdef HAVE_NTL |
---|
720 | return myicontent( f, 0 ); |
---|
721 | #else |
---|
722 | return 1; |
---|
723 | #endif |
---|
724 | } |
---|
725 | |
---|
726 | CanonicalForm QGCD( const CanonicalForm & F, const CanonicalForm & G ) |
---|
727 | { // f,g in Q(a)[x1,...,xn] |
---|
728 | if(F.isZero()) |
---|
729 | { |
---|
730 | if(G.isZero()) |
---|
731 | return G; // G is zero |
---|
732 | if(G.inCoeffDomain()) |
---|
733 | return CanonicalForm(1); |
---|
734 | CanonicalForm lcinv= 1/Lc (G); |
---|
735 | return G*lcinv; // return monic G |
---|
736 | } |
---|
737 | if(G.isZero()) // F is non-zero |
---|
738 | { |
---|
739 | if(F.inCoeffDomain()) |
---|
740 | return CanonicalForm(1); |
---|
741 | CanonicalForm lcinv= 1/Lc (F); |
---|
742 | return F*lcinv; // return monic F |
---|
743 | } |
---|
744 | if(F.inCoeffDomain() || G.inCoeffDomain()) |
---|
745 | return CanonicalForm(1); |
---|
746 | // here: both NOT inCoeffDomain |
---|
747 | CanonicalForm f, g, tmp, M, q, D, Dp, cl, newq, mipo; |
---|
748 | int p, i; |
---|
749 | int *bound, *other; // degree vectors |
---|
750 | bool fail; |
---|
751 | bool off_rational=!isOn(SW_RATIONAL); |
---|
752 | On( SW_RATIONAL ); // needed by bCommonDen |
---|
753 | f = F * bCommonDen(F); |
---|
754 | g = G * bCommonDen(G); |
---|
755 | TIMING_START (alg_content) |
---|
756 | CanonicalForm contf= myicontent (f); |
---|
757 | CanonicalForm contg= myicontent (g); |
---|
758 | f /= contf; |
---|
759 | g /= contg; |
---|
760 | CanonicalForm gcdcfcg= myicontent (contf, contg); |
---|
761 | TIMING_END_AND_PRINT (alg_content, "time for content in alg gcd: ") |
---|
762 | Variable a, b; |
---|
763 | if(hasFirstAlgVar(f,a)) |
---|
764 | { |
---|
765 | if(hasFirstAlgVar(g,b)) |
---|
766 | { |
---|
767 | if(b.level() > a.level()) |
---|
768 | a = b; |
---|
769 | } |
---|
770 | } |
---|
771 | else |
---|
772 | { |
---|
773 | if(!hasFirstAlgVar(g,a))// both not in extension |
---|
774 | { |
---|
775 | Off( SW_RATIONAL ); |
---|
776 | Off( SW_USE_QGCD ); |
---|
777 | tmp = gcdcfcg*gcd( f, g ); |
---|
778 | On( SW_USE_QGCD ); |
---|
779 | if (off_rational) Off(SW_RATIONAL); |
---|
780 | return tmp; |
---|
781 | } |
---|
782 | } |
---|
783 | // here: a is the biggest alg. var in f and g AND some of f,g is in extension |
---|
784 | setReduce(a,false); // do not reduce expressions modulo mipo |
---|
785 | tmp = getMipo(a); |
---|
786 | M = tmp * bCommonDen(tmp); |
---|
787 | // here: f, g in Z[a][x1,...,xn], M in Z[a] not necessarily monic |
---|
788 | Off( SW_RATIONAL ); // needed by mod |
---|
789 | // calculate upper bound for degree vector of gcd |
---|
790 | int mv = f.level(); i = g.level(); |
---|
791 | if(i > mv) |
---|
792 | mv = i; |
---|
793 | // here: mv is level of the largest variable in f, g |
---|
794 | bound = new int[mv+1]; // 'bound' could be indexed from 0 to mv, but we will only use from 1 to mv |
---|
795 | other = new int[mv+1]; |
---|
796 | for(int i=1; i<=mv; i++) // initialize 'bound', 'other' with zeros |
---|
797 | bound[i] = other[i] = 0; |
---|
798 | bound = leadDeg(f,bound); // 'bound' is set the leading degree vector of f |
---|
799 | other = leadDeg(g,other); |
---|
800 | for(int i=1; i<=mv; i++) // entry at i=0 not visited |
---|
801 | if(other[i] < bound[i]) |
---|
802 | bound[i] = other[i]; |
---|
803 | // now 'bound' is the smaller vector |
---|
804 | cl = lc(M) * lc(f) * lc(g); |
---|
805 | q = 1; |
---|
806 | D = 0; |
---|
807 | CanonicalForm test= 0; |
---|
808 | bool equal= false; |
---|
809 | for( i=cf_getNumBigPrimes()-1; i>-1; i-- ) |
---|
810 | { |
---|
811 | p = cf_getBigPrime(i); |
---|
812 | if( mod( cl, p ).isZero() ) // skip lc-bad primes |
---|
813 | continue; |
---|
814 | fail = false; |
---|
815 | setCharacteristic(p); |
---|
816 | mipo = mapinto(M); |
---|
817 | mipo /= mipo.lc(); |
---|
818 | // here: mipo is monic |
---|
819 | TIMING_START (alg_gcd_p) |
---|
820 | tryBrownGCD( mapinto(f), mapinto(g), mipo, Dp, fail ); |
---|
821 | TIMING_END_AND_PRINT (alg_gcd_p, "time for alg gcd mod p: ") |
---|
822 | if( fail ) // mipo splits in char p |
---|
823 | continue; |
---|
824 | if( Dp.inCoeffDomain() ) // early termination |
---|
825 | { |
---|
826 | tryInvert(Dp,mipo,tmp,fail); // check if zero divisor |
---|
827 | if(fail) |
---|
828 | continue; |
---|
829 | setReduce(a,true); |
---|
830 | if (off_rational) Off(SW_RATIONAL); else On(SW_RATIONAL); |
---|
831 | setCharacteristic(0); |
---|
832 | return gcdcfcg; |
---|
833 | } |
---|
834 | setCharacteristic(0); |
---|
835 | // here: Dp NOT inCoeffDomain |
---|
836 | for(int i=1; i<=mv; i++) |
---|
837 | other[i] = 0; // reset (this is necessary, because some entries may not be updated by call to leadDeg) |
---|
838 | other = leadDeg(Dp,other); |
---|
839 | |
---|
840 | if(isEqual(bound, other, 1, mv)) // equal |
---|
841 | { |
---|
842 | chineseRemainder( D, q, mapinto(Dp), p, tmp, newq ); |
---|
843 | // tmp = Dp mod p |
---|
844 | // tmp = D mod q |
---|
845 | // newq = p*q |
---|
846 | q = newq; |
---|
847 | if( D != tmp ) |
---|
848 | D = tmp; |
---|
849 | On( SW_RATIONAL ); |
---|
850 | TIMING_START (alg_reconstruction) |
---|
851 | tmp = Farey( D, q ); // Farey |
---|
852 | tmp *= bCommonDen (tmp); |
---|
853 | TIMING_END_AND_PRINT (alg_reconstruction, |
---|
854 | "time for rational reconstruction in alg gcd: ") |
---|
855 | setReduce(a,true); // reduce expressions modulo mipo |
---|
856 | On( SW_RATIONAL ); // needed by fdivides |
---|
857 | if (test != tmp) |
---|
858 | test= tmp; |
---|
859 | else |
---|
860 | equal= true; // modular image did not add any new information |
---|
861 | TIMING_START (alg_termination) |
---|
862 | #ifdef HAVE_FLINT |
---|
863 | if (equal && tmp.isUnivariate() && f.isUnivariate() && g.isUnivariate() |
---|
864 | && f.level() == tmp.level() && tmp.level() == g.level()) |
---|
865 | { |
---|
866 | CanonicalForm Q, R, sf, sg, stmp; |
---|
867 | Variable x= Variable (1); |
---|
868 | sf= swapvar (f, f.mvar(), x); |
---|
869 | sg= swapvar (g, f.mvar(), x); |
---|
870 | stmp= swapvar (tmp, f.mvar(), x); |
---|
871 | newtonDivrem (sf, stmp, Q, R); |
---|
872 | if (R.isZero()) |
---|
873 | { |
---|
874 | newtonDivrem (sg, stmp, Q, R); |
---|
875 | if (R.isZero()) |
---|
876 | { |
---|
877 | Off (SW_RATIONAL); |
---|
878 | setReduce (a,true); |
---|
879 | if (off_rational) Off(SW_RATIONAL); else On(SW_RATIONAL); |
---|
880 | TIMING_END_AND_PRINT (alg_termination, |
---|
881 | "time for successful termination test in alg gcd: ") |
---|
882 | return tmp*gcdcfcg; |
---|
883 | } |
---|
884 | } |
---|
885 | } |
---|
886 | else |
---|
887 | #endif |
---|
888 | if(equal && fdivides( tmp, f ) && fdivides( tmp, g )) // trial division |
---|
889 | { |
---|
890 | Off( SW_RATIONAL ); |
---|
891 | setReduce(a,true); |
---|
892 | if (off_rational) Off(SW_RATIONAL); else On(SW_RATIONAL); |
---|
893 | TIMING_END_AND_PRINT (alg_termination, |
---|
894 | "time for successful termination test in alg gcd: ") |
---|
895 | return tmp*gcdcfcg; |
---|
896 | } |
---|
897 | TIMING_END_AND_PRINT (alg_termination, |
---|
898 | "time for unsuccessful termination test in alg gcd: ") |
---|
899 | Off( SW_RATIONAL ); |
---|
900 | setReduce(a,false); // do not reduce expressions modulo mipo |
---|
901 | continue; |
---|
902 | } |
---|
903 | if( isLess(bound, other, 1, mv) ) // current prime unlucky |
---|
904 | continue; |
---|
905 | // here: isLess(other, bound, 1, mv) ) ==> all previous primes unlucky |
---|
906 | q = p; |
---|
907 | D = mapinto(Dp); // shortcut CRA // shortcut CRA |
---|
908 | for(int i=1; i<=mv; i++) // tighten bound |
---|
909 | bound[i] = other[i]; |
---|
910 | } |
---|
911 | // hopefully, we never reach this point |
---|
912 | setReduce(a,true); |
---|
913 | Off( SW_USE_QGCD ); |
---|
914 | D = gcdcfcg*gcd( f, g ); |
---|
915 | On( SW_USE_QGCD ); |
---|
916 | if (off_rational) Off(SW_RATIONAL); else On(SW_RATIONAL); |
---|
917 | return D; |
---|
918 | } |
---|
919 | |
---|
920 | |
---|
921 | int * leadDeg(const CanonicalForm & f, int *degs) |
---|
922 | { // leading degree vector w.r.t. lex. monomial order x(i+1) > x(i) |
---|
923 | // if f is in a coeff domain, the zero pointer is returned |
---|
924 | // 'a' should point to an array of sufficient size level(f)+1 |
---|
925 | if(f.inCoeffDomain()) |
---|
926 | return 0; |
---|
927 | CanonicalForm tmp = f; |
---|
928 | do |
---|
929 | { |
---|
930 | degs[tmp.level()] = tmp.degree(); |
---|
931 | tmp = LC(tmp); |
---|
932 | } |
---|
933 | while(!tmp.inCoeffDomain()); |
---|
934 | return degs; |
---|
935 | } |
---|
936 | |
---|
937 | |
---|
938 | bool isLess(int *a, int *b, int lower, int upper) |
---|
939 | { // compares the degree vectors a,b on the specified part. Note: x(i+1) > x(i) |
---|
940 | for(int i=upper; i>=lower; i--) |
---|
941 | if(a[i] == b[i]) |
---|
942 | continue; |
---|
943 | else |
---|
944 | return a[i] < b[i]; |
---|
945 | return true; |
---|
946 | } |
---|
947 | |
---|
948 | |
---|
949 | bool isEqual(int *a, int *b, int lower, int upper) |
---|
950 | { // compares the degree vectors a,b on the specified part. Note: x(i+1) > x(i) |
---|
951 | for(int i=lower; i<=upper; i++) |
---|
952 | if(a[i] != b[i]) |
---|
953 | return false; |
---|
954 | return true; |
---|
955 | } |
---|
956 | |
---|
957 | |
---|
958 | CanonicalForm firstLC(const CanonicalForm & f) |
---|
959 | { // returns the leading coefficient (LC) of level <= 1 |
---|
960 | CanonicalForm ret = f; |
---|
961 | while(ret.level() > 1) |
---|
962 | ret = LC(ret); |
---|
963 | return ret; |
---|
964 | } |
---|
965 | |
---|
966 | void tryExtgcd( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, CanonicalForm & s, CanonicalForm & t, bool & fail ) |
---|
967 | { // F, G are univariate polynomials (i.e. they have exactly one polynomial variable) |
---|
968 | // F and G must have the same level AND level > 0 |
---|
969 | // we try to calculate gcd(F,G) = s*F + t*G |
---|
970 | // if a zero divisor is encontered, 'fail' is set to one |
---|
971 | // M is assumed to be monic |
---|
972 | CanonicalForm P; |
---|
973 | if(F.inCoeffDomain()) |
---|
974 | { |
---|
975 | tryInvert( F, M, P, fail ); |
---|
976 | if(fail) |
---|
977 | return; |
---|
978 | result = 1; |
---|
979 | s = P; t = 0; |
---|
980 | return; |
---|
981 | } |
---|
982 | if(G.inCoeffDomain()) |
---|
983 | { |
---|
984 | tryInvert( G, M, P, fail ); |
---|
985 | if(fail) |
---|
986 | return; |
---|
987 | result = 1; |
---|
988 | s = 0; t = P; |
---|
989 | return; |
---|
990 | } |
---|
991 | // here: both not inCoeffDomain |
---|
992 | CanonicalForm inv, rem, tmp, u, v, q, sum=0; |
---|
993 | if( F.degree() > G.degree() ) |
---|
994 | { |
---|
995 | P = F; result = G; s=v=0; t=u=1; |
---|
996 | } |
---|
997 | else |
---|
998 | { |
---|
999 | P = G; result = F; s=v=1; t=u=0; |
---|
1000 | } |
---|
1001 | Variable x = P.mvar(); |
---|
1002 | // here: degree(P) >= degree(result) |
---|
1003 | while(true) |
---|
1004 | { |
---|
1005 | tryDivrem (P, result, q, rem, inv, M, fail); |
---|
1006 | if(fail) |
---|
1007 | return; |
---|
1008 | if( rem.isZero() ) |
---|
1009 | { |
---|
1010 | s*=inv; |
---|
1011 | s= reduce (s, M); |
---|
1012 | t*=inv; |
---|
1013 | t= reduce (t, M); |
---|
1014 | result *= inv; // monify result |
---|
1015 | result= reduce (result, M); |
---|
1016 | return; |
---|
1017 | } |
---|
1018 | sum += q; |
---|
1019 | if(result.degree(x) >= rem.degree(x)) |
---|
1020 | { |
---|
1021 | P=result; |
---|
1022 | result=rem; |
---|
1023 | tmp=u-sum*s; |
---|
1024 | u=s; |
---|
1025 | s=tmp; |
---|
1026 | tmp=v-sum*t; |
---|
1027 | v=t; |
---|
1028 | t=tmp; |
---|
1029 | sum = 0; // reset |
---|
1030 | } |
---|
1031 | else |
---|
1032 | P = rem; |
---|
1033 | } |
---|
1034 | } |
---|
1035 | |
---|
1036 | |
---|
1037 | static CanonicalForm trycontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail ) |
---|
1038 | { // as 'content', but takes care of zero divisors |
---|
1039 | ASSERT( x.level() > 0, "cannot calculate content with respect to algebraic variable" ); |
---|
1040 | Variable y = f.mvar(); |
---|
1041 | if ( y == x ) |
---|
1042 | return trycf_content( f, 0, M, fail ); |
---|
1043 | if ( y < x ) |
---|
1044 | return f; |
---|
1045 | return swapvar( trycontent( swapvar( f, y, x ), y, M, fail ), y, x ); |
---|
1046 | } |
---|
1047 | |
---|
1048 | |
---|
1049 | static CanonicalForm tryvcontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail ) |
---|
1050 | { // as vcontent, but takes care of zero divisors |
---|
1051 | ASSERT( x.level() > 0, "cannot calculate vcontent with respect to algebraic variable" ); |
---|
1052 | if ( f.mvar() <= x ) |
---|
1053 | return trycontent( f, x, M, fail ); |
---|
1054 | CFIterator i; |
---|
1055 | CanonicalForm d = 0, e, ret; |
---|
1056 | for ( i = f; i.hasTerms() && ! d.isOne() && ! fail; i++ ) |
---|
1057 | { |
---|
1058 | e = tryvcontent( i.coeff(), x, M, fail ); |
---|
1059 | if(fail) |
---|
1060 | break; |
---|
1061 | tryBrownGCD( d, e, M, ret, fail ); |
---|
1062 | d = ret; |
---|
1063 | } |
---|
1064 | return d; |
---|
1065 | } |
---|
1066 | |
---|
1067 | |
---|
1068 | static CanonicalForm trycf_content ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, bool & fail ) |
---|
1069 | { // as cf_content, but takes care of zero divisors |
---|
1070 | if ( f.inPolyDomain() || ( f.inExtension() && ! getReduce( f.mvar() ) ) ) |
---|
1071 | { |
---|
1072 | CFIterator i = f; |
---|
1073 | CanonicalForm tmp = g, result; |
---|
1074 | while ( i.hasTerms() && ! tmp.isOne() && ! fail ) |
---|
1075 | { |
---|
1076 | tryBrownGCD( i.coeff(), tmp, M, result, fail ); |
---|
1077 | tmp = result; |
---|
1078 | i++; |
---|
1079 | } |
---|
1080 | return result; |
---|
1081 | } |
---|
1082 | return abs( f ); |
---|
1083 | } |
---|
1084 | |
---|
1085 | void tryExtgcd( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & result, CanonicalForm & s, CanonicalForm & t, bool & fail ) |
---|
1086 | { |
---|
1087 | // F, G are univariate polynomials (i.e. they have exactly one polynomial variable) |
---|
1088 | // F and G must have the same level AND level > 0 |
---|
1089 | // we try to calculate gcd(f,g) = s*F + t*G |
---|
1090 | // if a zero divisor is encontered, 'fail' is set to one |
---|
1091 | Variable a, b; |
---|
1092 | if( !hasFirstAlgVar(F,a) && !hasFirstAlgVar(G,b) ) // note lazy evaluation |
---|
1093 | { |
---|
1094 | result = extgcd( F, G, s, t ); // no zero divisors possible |
---|
1095 | return; |
---|
1096 | } |
---|
1097 | if( b.level() > a.level() ) |
---|
1098 | a = b; |
---|
1099 | // here: a is the biggest alg. var in F and G |
---|
1100 | CanonicalForm M = getMipo(a); |
---|
1101 | CanonicalForm P; |
---|
1102 | if( degree(F) > degree(G) ) |
---|
1103 | { |
---|
1104 | P=F; result=G; s=0; t=1; |
---|
1105 | } |
---|
1106 | else |
---|
1107 | { |
---|
1108 | P=G; result=F; s=1; t=0; |
---|
1109 | } |
---|
1110 | CanonicalForm inv, rem, q, u, v; |
---|
1111 | // here: degree(P) >= degree(result) |
---|
1112 | while(true) |
---|
1113 | { |
---|
1114 | tryInvert( Lc(result), M, inv, fail ); |
---|
1115 | if(fail) |
---|
1116 | return; |
---|
1117 | // here: Lc(result) is invertible modulo M |
---|
1118 | q = Lc(P)*inv * power( P.mvar(), degree(P)-degree(result) ); |
---|
1119 | rem = P - q*result; |
---|
1120 | // here: s*F + t*G = result |
---|
1121 | if( rem.isZero() ) |
---|
1122 | { |
---|
1123 | s*=inv; |
---|
1124 | t*=inv; |
---|
1125 | result *= inv; // monify result |
---|
1126 | return; |
---|
1127 | } |
---|
1128 | P=result; |
---|
1129 | result=rem; |
---|
1130 | rem=u-q*s; |
---|
1131 | u=s; |
---|
1132 | s=rem; |
---|
1133 | rem=v-q*t; |
---|
1134 | v=t; |
---|
1135 | t=rem; |
---|
1136 | } |
---|
1137 | } |
---|
1138 | |
---|
1139 | void tryCRA( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, CanonicalForm & xnew, CanonicalForm & qnew, bool & fail ) |
---|
1140 | { // polys of level <= 1 are considered coefficients. q1,q2 are assumed to be coprime |
---|
1141 | // xnew = x1 mod q1 (coefficientwise in the above sense) |
---|
1142 | // xnew = x2 mod q2 |
---|
1143 | // qnew = q1*q2 |
---|
1144 | CanonicalForm tmp; |
---|
1145 | if(x1.level() <= 1 && x2.level() <= 1) // base case |
---|
1146 | { |
---|
1147 | tryExtgcd(q1,q2,tmp,xnew,qnew,fail); |
---|
1148 | if(fail) |
---|
1149 | return; |
---|
1150 | xnew = x1 + (x2-x1) * xnew * q1; |
---|
1151 | qnew = q1*q2; |
---|
1152 | xnew = mod(xnew,qnew); |
---|
1153 | return; |
---|
1154 | } |
---|
1155 | CanonicalForm tmp2; |
---|
1156 | xnew = 0; |
---|
1157 | qnew = q1 * q2; |
---|
1158 | // here: x1.level() > 1 || x2.level() > 1 |
---|
1159 | if(x1.level() > x2.level()) |
---|
1160 | { |
---|
1161 | for(CFIterator i=x1; i.hasTerms(); i++) |
---|
1162 | { |
---|
1163 | if(i.exp() == 0) // const. term |
---|
1164 | { |
---|
1165 | tryCRA(i.coeff(),q1,x2,q2,tmp,tmp2,fail); |
---|
1166 | if(fail) |
---|
1167 | return; |
---|
1168 | xnew += tmp; |
---|
1169 | } |
---|
1170 | else |
---|
1171 | { |
---|
1172 | tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail); |
---|
1173 | if(fail) |
---|
1174 | return; |
---|
1175 | xnew += tmp * power(x1.mvar(),i.exp()); |
---|
1176 | } |
---|
1177 | } |
---|
1178 | return; |
---|
1179 | } |
---|
1180 | // here: x1.level() <= x2.level() && ( x1.level() > 1 || x2.level() > 1 ) |
---|
1181 | if(x2.level() > x1.level()) |
---|
1182 | { |
---|
1183 | for(CFIterator j=x2; j.hasTerms(); j++) |
---|
1184 | { |
---|
1185 | if(j.exp() == 0) // const. term |
---|
1186 | { |
---|
1187 | tryCRA(x1,q1,j.coeff(),q2,tmp,tmp2,fail); |
---|
1188 | if(fail) |
---|
1189 | return; |
---|
1190 | xnew += tmp; |
---|
1191 | } |
---|
1192 | else |
---|
1193 | { |
---|
1194 | tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail); |
---|
1195 | if(fail) |
---|
1196 | return; |
---|
1197 | xnew += tmp * power(x2.mvar(),j.exp()); |
---|
1198 | } |
---|
1199 | } |
---|
1200 | return; |
---|
1201 | } |
---|
1202 | // here: x1.level() == x2.level() && x1.level() > 1 && x2.level() > 1 |
---|
1203 | CFIterator i = x1; |
---|
1204 | CFIterator j = x2; |
---|
1205 | while(i.hasTerms() || j.hasTerms()) |
---|
1206 | { |
---|
1207 | if(i.hasTerms()) |
---|
1208 | { |
---|
1209 | if(j.hasTerms()) |
---|
1210 | { |
---|
1211 | if(i.exp() == j.exp()) |
---|
1212 | { |
---|
1213 | tryCRA(i.coeff(),q1,j.coeff(),q2,tmp,tmp2,fail); |
---|
1214 | if(fail) |
---|
1215 | return; |
---|
1216 | xnew += tmp * power(x1.mvar(),i.exp()); |
---|
1217 | i++; j++; |
---|
1218 | } |
---|
1219 | else |
---|
1220 | { |
---|
1221 | if(i.exp() < j.exp()) |
---|
1222 | { |
---|
1223 | tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail); |
---|
1224 | if(fail) |
---|
1225 | return; |
---|
1226 | xnew += tmp * power(x1.mvar(),i.exp()); |
---|
1227 | i++; |
---|
1228 | } |
---|
1229 | else // i.exp() > j.exp() |
---|
1230 | { |
---|
1231 | tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail); |
---|
1232 | if(fail) |
---|
1233 | return; |
---|
1234 | xnew += tmp * power(x1.mvar(),j.exp()); |
---|
1235 | j++; |
---|
1236 | } |
---|
1237 | } |
---|
1238 | } |
---|
1239 | else // j is out of terms |
---|
1240 | { |
---|
1241 | tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail); |
---|
1242 | if(fail) |
---|
1243 | return; |
---|
1244 | xnew += tmp * power(x1.mvar(),i.exp()); |
---|
1245 | i++; |
---|
1246 | } |
---|
1247 | } |
---|
1248 | else // i is out of terms |
---|
1249 | { |
---|
1250 | tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail); |
---|
1251 | if(fail) |
---|
1252 | return; |
---|
1253 | xnew += tmp * power(x1.mvar(),j.exp()); |
---|
1254 | j++; |
---|
1255 | } |
---|
1256 | } |
---|
1257 | } |
---|
1258 | |
---|