1 | /*****************************************************************************\ |
---|
2 | * Computer Algebra System SINGULAR |
---|
3 | \*****************************************************************************/ |
---|
4 | /** @file facFqFactorize.cc |
---|
5 | * |
---|
6 | * This file implements functions for factoring a multivariate polynomial over |
---|
7 | * a finite field. |
---|
8 | * |
---|
9 | * ABSTRACT: "Efficient Multivariate Factorization over Finite Fields" by |
---|
10 | * L. Bernardin & M. Monagon. Precomputation of leading coefficients is |
---|
11 | * described in "Sparse Hensel lifting" by E. Kaltofen |
---|
12 | * |
---|
13 | * @author Martin Lee |
---|
14 | * |
---|
15 | * @internal @version \$Id$ |
---|
16 | * |
---|
17 | **/ |
---|
18 | /*****************************************************************************/ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #include "cf_assert.h" |
---|
23 | #include "debug.h" |
---|
24 | #include "timing.h" |
---|
25 | |
---|
26 | #include "facFqFactorizeUtil.h" |
---|
27 | #include "facFqFactorize.h" |
---|
28 | #include "cf_random.h" |
---|
29 | #include "facHensel.h" |
---|
30 | #include "cf_gcd_smallp.h" |
---|
31 | #include "cf_map_ext.h" |
---|
32 | #include "algext.h" |
---|
33 | #include "facSparseHensel.h" |
---|
34 | #include "facMul.h" |
---|
35 | |
---|
36 | #ifdef HAVE_NTL |
---|
37 | #include <NTL/ZZ_pEX.h> |
---|
38 | #include "NTLconvert.h" |
---|
39 | |
---|
40 | TIMING_DEFINE_PRINT(fac_bi_factorizer) |
---|
41 | TIMING_DEFINE_PRINT(fac_hensel_lift) |
---|
42 | TIMING_DEFINE_PRINT(fac_factor_recombination) |
---|
43 | |
---|
44 | static inline |
---|
45 | CanonicalForm |
---|
46 | listGCD (const CFList& L); |
---|
47 | |
---|
48 | static inline |
---|
49 | CanonicalForm |
---|
50 | myContent (const CanonicalForm& F) |
---|
51 | { |
---|
52 | Variable x= Variable (1); |
---|
53 | CanonicalForm G= swapvar (F, F.mvar(), x); |
---|
54 | CFList L; |
---|
55 | for (CFIterator i= G; i.hasTerms(); i++) |
---|
56 | L.append (i.coeff()); |
---|
57 | if (L.length() == 2) |
---|
58 | return swapvar (gcd (L.getFirst(), L.getLast()), F.mvar(), x); |
---|
59 | if (L.length() == 1) |
---|
60 | return LC (F, x); |
---|
61 | return swapvar (listGCD (L), F.mvar(), x); |
---|
62 | } |
---|
63 | |
---|
64 | static inline |
---|
65 | CanonicalForm |
---|
66 | listGCD (const CFList& L) |
---|
67 | { |
---|
68 | if (L.length() == 1) |
---|
69 | return L.getFirst(); |
---|
70 | if (L.length() == 2) |
---|
71 | return gcd (L.getFirst(), L.getLast()); |
---|
72 | else |
---|
73 | { |
---|
74 | CFList lHi, lLo; |
---|
75 | CanonicalForm resultHi, resultLo; |
---|
76 | int length= L.length()/2; |
---|
77 | int j= 0; |
---|
78 | for (CFListIterator i= L; j < length; i++, j++) |
---|
79 | lHi.append (i.getItem()); |
---|
80 | lLo= Difference (L, lHi); |
---|
81 | resultHi= listGCD (lHi); |
---|
82 | resultLo= listGCD (lLo); |
---|
83 | if (resultHi.isOne() || resultLo.isOne()) |
---|
84 | return 1; |
---|
85 | return gcd (resultHi, resultLo); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | static inline |
---|
90 | CanonicalForm |
---|
91 | myContent (const CanonicalForm& F, const Variable& x) |
---|
92 | { |
---|
93 | if (degree (F, x) <= 0) |
---|
94 | return 1; |
---|
95 | CanonicalForm G= F; |
---|
96 | bool swap= false; |
---|
97 | if (x != F.mvar()) |
---|
98 | { |
---|
99 | swap= true; |
---|
100 | G= swapvar (F, x, F.mvar()); |
---|
101 | } |
---|
102 | CFList L; |
---|
103 | Variable alpha; |
---|
104 | for (CFIterator i= G; i.hasTerms(); i++) |
---|
105 | L.append (i.coeff()); |
---|
106 | if (L.length() == 2) |
---|
107 | { |
---|
108 | if (swap) |
---|
109 | return swapvar (gcd (L.getFirst(), L.getLast()), F.mvar(), x); |
---|
110 | else |
---|
111 | return gcd (L.getFirst(), L.getLast()); |
---|
112 | } |
---|
113 | if (L.length() == 1) |
---|
114 | { |
---|
115 | return LC (F, x); |
---|
116 | } |
---|
117 | if (swap) |
---|
118 | return swapvar (listGCD (L), F.mvar(), x); |
---|
119 | else |
---|
120 | return listGCD (L); |
---|
121 | } |
---|
122 | |
---|
123 | CanonicalForm myCompress (const CanonicalForm& F, CFMap& N) |
---|
124 | { |
---|
125 | int n= F.level(); |
---|
126 | int * degsf= new int [n + 1]; |
---|
127 | int ** swap; |
---|
128 | swap= new int* [n + 1]; |
---|
129 | for (int i= 0; i <= n; i++) |
---|
130 | { |
---|
131 | degsf[i]= 0; |
---|
132 | swap [i]= new int [2]; |
---|
133 | swap [i] [0]= 0; |
---|
134 | swap [i] [1]= 0; |
---|
135 | } |
---|
136 | int i= 1; |
---|
137 | n= 1; |
---|
138 | degsf= degrees (F, degsf); |
---|
139 | |
---|
140 | CanonicalForm result= F; |
---|
141 | while ( i <= F.level() ) |
---|
142 | { |
---|
143 | while( degsf[i] == 0 ) i++; |
---|
144 | swap[n][0]= i; |
---|
145 | swap[n][1]= degsf[i]; |
---|
146 | if (i != n) |
---|
147 | result= swapvar (result, Variable (n), Variable(i)); |
---|
148 | n++; i++; |
---|
149 | } |
---|
150 | |
---|
151 | int buf1, buf2; |
---|
152 | n--; |
---|
153 | |
---|
154 | for (i= 1; i < n; i++) |
---|
155 | { |
---|
156 | for (int j= 1; j < n - i + 1; j++) |
---|
157 | { |
---|
158 | if (swap[j][1] < swap[j + 1][1]) |
---|
159 | { |
---|
160 | buf1= swap [j + 1] [0]; |
---|
161 | buf2= swap [j + 1] [1]; |
---|
162 | swap[j + 1] [0]= swap[j] [0]; |
---|
163 | swap[j + 1] [1]= swap[j] [1]; |
---|
164 | swap[j][0]= buf1; |
---|
165 | swap[j][1]= buf2; |
---|
166 | result= swapvar (result, Variable (j + 1), Variable (j)); |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | for (i= n; i > 0; i--) |
---|
172 | { |
---|
173 | if (i != swap[i] [0]) |
---|
174 | N.newpair (Variable (i), Variable (swap[i] [0])); |
---|
175 | } |
---|
176 | |
---|
177 | for (i= 0; i <= n; i++) |
---|
178 | delete [] swap[i]; |
---|
179 | delete [] swap; |
---|
180 | |
---|
181 | delete [] degsf; |
---|
182 | |
---|
183 | return result; |
---|
184 | } |
---|
185 | |
---|
186 | CFList |
---|
187 | extFactorRecombination (const CFList& factors, const CanonicalForm& F, |
---|
188 | const CFList& M, const ExtensionInfo& info, |
---|
189 | const CFList& evaluation) |
---|
190 | { |
---|
191 | Variable alpha= info.getAlpha(); |
---|
192 | Variable beta= info.getBeta(); |
---|
193 | CanonicalForm gamma= info.getGamma(); |
---|
194 | CanonicalForm delta= info.getDelta(); |
---|
195 | int k= info.getGFDegree(); |
---|
196 | CFList source, dest; |
---|
197 | if (factors.length() == 1) |
---|
198 | { |
---|
199 | CanonicalForm buf= reverseShift (F, evaluation); |
---|
200 | return CFList (mapDown (buf, info, source, dest)); |
---|
201 | } |
---|
202 | if (factors.length() < 1) |
---|
203 | return CFList(); |
---|
204 | |
---|
205 | int degMipoBeta= 1; |
---|
206 | if (!k && beta.level() != 1) |
---|
207 | degMipoBeta= degree (getMipo (beta)); |
---|
208 | |
---|
209 | CFList T, S; |
---|
210 | T= factors; |
---|
211 | |
---|
212 | int s= 1; |
---|
213 | CFList result; |
---|
214 | CanonicalForm buf; |
---|
215 | |
---|
216 | buf= F; |
---|
217 | |
---|
218 | Variable x= Variable (1); |
---|
219 | CanonicalForm g, LCBuf= LC (buf, x); |
---|
220 | CanonicalForm buf2, quot; |
---|
221 | int * v= new int [T.length()]; |
---|
222 | for (int i= 0; i < T.length(); i++) |
---|
223 | v[i]= 0; |
---|
224 | bool noSubset= false; |
---|
225 | CFArray TT; |
---|
226 | TT= copy (factors); |
---|
227 | bool recombination= false; |
---|
228 | bool trueFactor= false; |
---|
229 | while (T.length() >= 2*s) |
---|
230 | { |
---|
231 | while (noSubset == false) |
---|
232 | { |
---|
233 | if (T.length() == s) |
---|
234 | { |
---|
235 | delete [] v; |
---|
236 | if (recombination) |
---|
237 | { |
---|
238 | T.insert (LCBuf); |
---|
239 | g= prodMod (T, M); |
---|
240 | T.removeFirst(); |
---|
241 | result.append (g/myContent (g)); |
---|
242 | g= reverseShift (g, evaluation); |
---|
243 | g /= Lc (g); |
---|
244 | appendTestMapDown (result, g, info, source, dest); |
---|
245 | return result; |
---|
246 | } |
---|
247 | else |
---|
248 | { |
---|
249 | buf= reverseShift (buf, evaluation); |
---|
250 | return CFList (buf); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | S= subset (v, s, TT, noSubset); |
---|
255 | if (noSubset) break; |
---|
256 | |
---|
257 | S.insert (LCBuf); |
---|
258 | g= prodMod (S, M); |
---|
259 | S.removeFirst(); |
---|
260 | g /= myContent (g); |
---|
261 | if (fdivides (g, buf, quot)) |
---|
262 | { |
---|
263 | buf2= reverseShift (g, evaluation); |
---|
264 | buf2 /= Lc (buf2); |
---|
265 | if (!k && beta == x) |
---|
266 | { |
---|
267 | if (degree (buf2, alpha) < degMipoBeta) |
---|
268 | { |
---|
269 | appendTestMapDown (result, buf2, info, source, dest); |
---|
270 | buf= quot; |
---|
271 | LCBuf= LC (buf, x); |
---|
272 | recombination= true; |
---|
273 | trueFactor= true; |
---|
274 | } |
---|
275 | } |
---|
276 | else |
---|
277 | { |
---|
278 | if (!isInExtension (buf2, gamma, k, delta, source, dest)) |
---|
279 | { |
---|
280 | appendTestMapDown (result, buf2, info, source, dest); |
---|
281 | buf /= g; |
---|
282 | LCBuf= LC (buf, x); |
---|
283 | recombination= true; |
---|
284 | trueFactor= true; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | if (trueFactor) |
---|
289 | { |
---|
290 | T= Difference (T, S); |
---|
291 | |
---|
292 | if (T.length() < 2*s || T.length() == s) |
---|
293 | { |
---|
294 | buf= reverseShift (buf, evaluation); |
---|
295 | buf /= Lc (buf); |
---|
296 | appendTestMapDown (result, buf, info, source, dest); |
---|
297 | delete [] v; |
---|
298 | return result; |
---|
299 | } |
---|
300 | trueFactor= false; |
---|
301 | TT= copy (T); |
---|
302 | indexUpdate (v, s, T.length(), noSubset); |
---|
303 | if (noSubset) break; |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | s++; |
---|
308 | if (T.length() < 2*s || T.length() == s) |
---|
309 | { |
---|
310 | buf= reverseShift (buf, evaluation); |
---|
311 | appendTestMapDown (result, buf, info, source, dest); |
---|
312 | delete [] v; |
---|
313 | return result; |
---|
314 | } |
---|
315 | for (int i= 0; i < T.length(); i++) |
---|
316 | v[i]= 0; |
---|
317 | noSubset= false; |
---|
318 | } |
---|
319 | if (T.length() < 2*s) |
---|
320 | { |
---|
321 | buf= reverseShift (F, evaluation); |
---|
322 | appendMapDown (result, buf, info, source, dest); |
---|
323 | } |
---|
324 | |
---|
325 | delete [] v; |
---|
326 | return result; |
---|
327 | } |
---|
328 | |
---|
329 | CFList |
---|
330 | factorRecombination (const CanonicalForm& F, const CFList& factors, |
---|
331 | const CFList& M) |
---|
332 | { |
---|
333 | if (factors.length() == 1) |
---|
334 | return CFList(F); |
---|
335 | if (factors.length() < 1) |
---|
336 | return CFList(); |
---|
337 | |
---|
338 | CFList T, S; |
---|
339 | |
---|
340 | T= factors; |
---|
341 | |
---|
342 | int s= 1; |
---|
343 | CFList result; |
---|
344 | CanonicalForm LCBuf= LC (F, Variable (1)); |
---|
345 | CanonicalForm g, buf= F; |
---|
346 | int * v= new int [T.length()]; |
---|
347 | for (int i= 0; i < T.length(); i++) |
---|
348 | v[i]= 0; |
---|
349 | bool noSubset= false; |
---|
350 | CFArray TT; |
---|
351 | TT= copy (factors); |
---|
352 | Variable y= F.level() - 1; |
---|
353 | bool recombination= false; |
---|
354 | CanonicalForm h, quot; |
---|
355 | while (T.length() >= 2*s) |
---|
356 | { |
---|
357 | while (noSubset == false) |
---|
358 | { |
---|
359 | if (T.length() == s) |
---|
360 | { |
---|
361 | delete [] v; |
---|
362 | if (recombination) |
---|
363 | { |
---|
364 | T.insert (LC (buf)); |
---|
365 | g= prodMod (T, M); |
---|
366 | result.append (g/myContent (g)); |
---|
367 | return result; |
---|
368 | } |
---|
369 | else |
---|
370 | return CFList (F); |
---|
371 | } |
---|
372 | S= subset (v, s, TT, noSubset); |
---|
373 | if (noSubset) break; |
---|
374 | S.insert (LCBuf); |
---|
375 | g= prodMod (S, M); |
---|
376 | S.removeFirst(); |
---|
377 | g /= myContent (g); |
---|
378 | if (fdivides (g, buf, quot)) |
---|
379 | { |
---|
380 | recombination= true; |
---|
381 | result.append (g); |
---|
382 | buf= quot; |
---|
383 | LCBuf= LC (buf, Variable(1)); |
---|
384 | T= Difference (T, S); |
---|
385 | if (T.length() < 2*s || T.length() == s) |
---|
386 | { |
---|
387 | result.append (buf); |
---|
388 | delete [] v; |
---|
389 | return result; |
---|
390 | } |
---|
391 | TT= copy (T); |
---|
392 | indexUpdate (v, s, T.length(), noSubset); |
---|
393 | if (noSubset) break; |
---|
394 | } |
---|
395 | } |
---|
396 | s++; |
---|
397 | if (T.length() < 2*s || T.length() == s) |
---|
398 | { |
---|
399 | result.append (buf); |
---|
400 | delete [] v; |
---|
401 | return result; |
---|
402 | } |
---|
403 | for (int i= 0; i < T.length(); i++) |
---|
404 | v[i]= 0; |
---|
405 | noSubset= false; |
---|
406 | } |
---|
407 | if (T.length() < 2*s) |
---|
408 | result.append (F); |
---|
409 | |
---|
410 | delete [] v; |
---|
411 | return result; |
---|
412 | } |
---|
413 | |
---|
414 | int |
---|
415 | liftBoundAdaption (const CanonicalForm& F, const CFList& factors, bool& |
---|
416 | success, const int deg, const CFList& MOD, const int bound) |
---|
417 | { |
---|
418 | int adaptedLiftBound= 0; |
---|
419 | CanonicalForm buf= F; |
---|
420 | Variable y= F.mvar(); |
---|
421 | Variable x= Variable (1); |
---|
422 | CanonicalForm LCBuf= LC (buf, x); |
---|
423 | CanonicalForm g, quot; |
---|
424 | CFList M= MOD; |
---|
425 | M.append (power (y, deg)); |
---|
426 | int d= bound; |
---|
427 | int e= 0; |
---|
428 | int nBuf; |
---|
429 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
430 | { |
---|
431 | g= mulMod (i.getItem(), LCBuf, M); |
---|
432 | g /= myContent (g); |
---|
433 | if (fdivides (g, buf, quot)) |
---|
434 | { |
---|
435 | nBuf= degree (g, y) + degree (LC (g, 1), y); |
---|
436 | d -= nBuf; |
---|
437 | e= tmax (e, nBuf); |
---|
438 | buf= quot; |
---|
439 | LCBuf= LC (buf, x); |
---|
440 | } |
---|
441 | } |
---|
442 | adaptedLiftBound= d; |
---|
443 | |
---|
444 | if (adaptedLiftBound < deg) |
---|
445 | { |
---|
446 | if (adaptedLiftBound < degree (F) + 1) |
---|
447 | { |
---|
448 | if (d == 1) |
---|
449 | { |
---|
450 | if (e + 1 > deg) |
---|
451 | { |
---|
452 | adaptedLiftBound= deg; |
---|
453 | success= false; |
---|
454 | } |
---|
455 | else |
---|
456 | { |
---|
457 | success= true; |
---|
458 | if (e + 1 < degree (F) + 1) |
---|
459 | adaptedLiftBound= deg; |
---|
460 | else |
---|
461 | adaptedLiftBound= e + 1; |
---|
462 | } |
---|
463 | } |
---|
464 | else |
---|
465 | { |
---|
466 | success= true; |
---|
467 | adaptedLiftBound= deg; |
---|
468 | } |
---|
469 | } |
---|
470 | else |
---|
471 | { |
---|
472 | success= true; |
---|
473 | } |
---|
474 | } |
---|
475 | return adaptedLiftBound; |
---|
476 | } |
---|
477 | |
---|
478 | int |
---|
479 | extLiftBoundAdaption (const CanonicalForm& F, const CFList& factors, bool& |
---|
480 | success, const ExtensionInfo& info, const CFList& eval, |
---|
481 | const int deg, const CFList& MOD, const int bound) |
---|
482 | { |
---|
483 | Variable alpha= info.getAlpha(); |
---|
484 | Variable beta= info.getBeta(); |
---|
485 | CanonicalForm gamma= info.getGamma(); |
---|
486 | CanonicalForm delta= info.getDelta(); |
---|
487 | int k= info.getGFDegree(); |
---|
488 | int adaptedLiftBound= 0; |
---|
489 | CanonicalForm buf= F; |
---|
490 | Variable y= F.mvar(); |
---|
491 | Variable x= Variable (1); |
---|
492 | CanonicalForm LCBuf= LC (buf, x); |
---|
493 | CanonicalForm g, gg, quot; |
---|
494 | CFList M= MOD; |
---|
495 | M.append (power (y, deg)); |
---|
496 | adaptedLiftBound= 0; |
---|
497 | int d= bound; |
---|
498 | int e= 0; |
---|
499 | int nBuf; |
---|
500 | int degMipoBeta= 1; |
---|
501 | if (!k && beta.level() != 1) |
---|
502 | degMipoBeta= degree (getMipo (beta)); |
---|
503 | |
---|
504 | CFList source, dest; |
---|
505 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
506 | { |
---|
507 | g= mulMod (i.getItem(), LCBuf, M); |
---|
508 | g /= myContent (g); |
---|
509 | if (fdivides (g, buf, quot)) |
---|
510 | { |
---|
511 | gg= reverseShift (g, eval); |
---|
512 | gg /= Lc (gg); |
---|
513 | if (!k && beta == x) |
---|
514 | { |
---|
515 | if (degree (gg, alpha) < degMipoBeta) |
---|
516 | { |
---|
517 | buf= quot; |
---|
518 | nBuf= degree (g, y) + degree (LC (g, x), y); |
---|
519 | d -= nBuf; |
---|
520 | e= tmax (e, nBuf); |
---|
521 | LCBuf= LC (buf, x); |
---|
522 | } |
---|
523 | } |
---|
524 | else |
---|
525 | { |
---|
526 | if (!isInExtension (gg, gamma, k, delta, source, dest)) |
---|
527 | { |
---|
528 | buf= quot; |
---|
529 | nBuf= degree (g, y) + degree (LC (g, x), y); |
---|
530 | d -= nBuf; |
---|
531 | e= tmax (e, nBuf); |
---|
532 | LCBuf= LC (buf, x); |
---|
533 | } |
---|
534 | } |
---|
535 | } |
---|
536 | } |
---|
537 | adaptedLiftBound= d; |
---|
538 | |
---|
539 | if (adaptedLiftBound < deg) |
---|
540 | { |
---|
541 | if (adaptedLiftBound < degree (F) + 1) |
---|
542 | { |
---|
543 | if (d == 1) |
---|
544 | { |
---|
545 | if (e + 1 > deg) |
---|
546 | { |
---|
547 | adaptedLiftBound= deg; |
---|
548 | success= false; |
---|
549 | } |
---|
550 | else |
---|
551 | { |
---|
552 | success= true; |
---|
553 | if (e + 1 < degree (F) + 1) |
---|
554 | adaptedLiftBound= deg; |
---|
555 | else |
---|
556 | adaptedLiftBound= e + 1; |
---|
557 | } |
---|
558 | } |
---|
559 | else |
---|
560 | { |
---|
561 | success= true; |
---|
562 | adaptedLiftBound= deg; |
---|
563 | } |
---|
564 | } |
---|
565 | else |
---|
566 | { |
---|
567 | success= true; |
---|
568 | } |
---|
569 | } |
---|
570 | |
---|
571 | return adaptedLiftBound; |
---|
572 | } |
---|
573 | |
---|
574 | CFList |
---|
575 | earlyFactorDetect (CanonicalForm& F, CFList& factors, int& adaptedLiftBound, |
---|
576 | bool& success, const int deg, const CFList& MOD, |
---|
577 | const int bound) |
---|
578 | { |
---|
579 | CFList result; |
---|
580 | CFList T= factors; |
---|
581 | CanonicalForm buf= F; |
---|
582 | Variable y= F.mvar(); |
---|
583 | Variable x= Variable (1); |
---|
584 | CanonicalForm LCBuf= LC (buf, x); |
---|
585 | CanonicalForm g, quot; |
---|
586 | CFList M= MOD; |
---|
587 | M.append (power (y, deg)); |
---|
588 | adaptedLiftBound= 0; |
---|
589 | int d= bound; |
---|
590 | int e= 0; |
---|
591 | int nBuf; |
---|
592 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
593 | { |
---|
594 | g= mulMod (i.getItem(), LCBuf, M); |
---|
595 | g /= myContent (g); |
---|
596 | if (fdivides (g, buf, quot)) |
---|
597 | { |
---|
598 | result.append (g); |
---|
599 | nBuf= degree (g, y) + degree (LC (g, x), y); |
---|
600 | d -= nBuf; |
---|
601 | e= tmax (e, nBuf); |
---|
602 | buf= quot; |
---|
603 | LCBuf= LC (buf, x); |
---|
604 | T= Difference (T, CFList (i.getItem())); |
---|
605 | } |
---|
606 | } |
---|
607 | adaptedLiftBound= d; |
---|
608 | |
---|
609 | if (adaptedLiftBound < deg) |
---|
610 | { |
---|
611 | if (adaptedLiftBound < degree (F) + 1) |
---|
612 | { |
---|
613 | if (d == 1) |
---|
614 | adaptedLiftBound= tmin (e + 1, deg); |
---|
615 | else |
---|
616 | adaptedLiftBound= deg; |
---|
617 | } |
---|
618 | factors= T; |
---|
619 | F= buf; |
---|
620 | success= true; |
---|
621 | } |
---|
622 | return result; |
---|
623 | } |
---|
624 | |
---|
625 | CFList |
---|
626 | extEarlyFactorDetect (CanonicalForm& F, CFList& factors, int& adaptedLiftBound, |
---|
627 | bool& success, const ExtensionInfo& info, const CFList& |
---|
628 | eval, const int deg, const CFList& MOD, const int bound) |
---|
629 | { |
---|
630 | Variable alpha= info.getAlpha(); |
---|
631 | Variable beta= info.getBeta(); |
---|
632 | CanonicalForm gamma= info.getGamma(); |
---|
633 | CanonicalForm delta= info.getDelta(); |
---|
634 | int k= info.getGFDegree(); |
---|
635 | CFList result; |
---|
636 | CFList T= factors; |
---|
637 | CanonicalForm buf= F; |
---|
638 | Variable y= F.mvar(); |
---|
639 | Variable x= Variable (1); |
---|
640 | CanonicalForm LCBuf= LC (buf, x); |
---|
641 | CanonicalForm g, gg, quot; |
---|
642 | CFList M= MOD; |
---|
643 | M.append (power (y, deg)); |
---|
644 | adaptedLiftBound= 0; |
---|
645 | int d= bound; |
---|
646 | int e= 0; |
---|
647 | int nBuf; |
---|
648 | CFList source, dest; |
---|
649 | |
---|
650 | int degMipoBeta= 1; |
---|
651 | if (!k && beta.level() != 1) |
---|
652 | degMipoBeta= degree (getMipo (beta)); |
---|
653 | |
---|
654 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
655 | { |
---|
656 | g= mulMod (i.getItem(), LCBuf, M); |
---|
657 | g /= myContent (g); |
---|
658 | if (fdivides (g, buf, quot)) |
---|
659 | { |
---|
660 | gg= reverseShift (g, eval); |
---|
661 | gg /= Lc (gg); |
---|
662 | if (!k && beta == x) |
---|
663 | { |
---|
664 | if (degree (gg, alpha) < degMipoBeta) |
---|
665 | { |
---|
666 | appendTestMapDown (result, gg, info, source, dest); |
---|
667 | buf= quot; |
---|
668 | nBuf= degree (g, y) + degree (LC (g, x), y); |
---|
669 | d -= nBuf; |
---|
670 | e= tmax (e, nBuf); |
---|
671 | LCBuf= LC (buf, x); |
---|
672 | T= Difference (T, CFList (i.getItem())); |
---|
673 | } |
---|
674 | } |
---|
675 | else |
---|
676 | { |
---|
677 | if (!isInExtension (gg, gamma, k, delta, source, dest)) |
---|
678 | { |
---|
679 | appendTestMapDown (result, gg, info, source, dest); |
---|
680 | buf= quot; |
---|
681 | nBuf= degree (g, y) + degree (LC (g, x), y); |
---|
682 | d -= nBuf; |
---|
683 | e= tmax (e, nBuf); |
---|
684 | LCBuf= LC (buf, x); |
---|
685 | T= Difference (T, CFList (i.getItem())); |
---|
686 | } |
---|
687 | } |
---|
688 | } |
---|
689 | } |
---|
690 | adaptedLiftBound= d; |
---|
691 | |
---|
692 | if (adaptedLiftBound < deg) |
---|
693 | { |
---|
694 | if (adaptedLiftBound < degree (F) + 1) |
---|
695 | { |
---|
696 | if (d == 1) |
---|
697 | adaptedLiftBound= tmin (e + 1, deg); |
---|
698 | else |
---|
699 | adaptedLiftBound= deg; |
---|
700 | } |
---|
701 | success= true; |
---|
702 | factors= T; |
---|
703 | F= buf; |
---|
704 | } |
---|
705 | return result; |
---|
706 | } |
---|
707 | |
---|
708 | CFList |
---|
709 | evalPoints (const CanonicalForm& F, CFList & eval, const Variable& alpha, |
---|
710 | CFList& list, const bool& GF, bool& fail) |
---|
711 | { |
---|
712 | int k= F.level() - 1; |
---|
713 | Variable x= Variable (1); |
---|
714 | CFList result; |
---|
715 | FFRandom genFF; |
---|
716 | GFRandom genGF; |
---|
717 | int p= getCharacteristic (); |
---|
718 | double bound; |
---|
719 | if (alpha != x) |
---|
720 | { |
---|
721 | bound= pow ((double) p, (double) degree (getMipo(alpha))); |
---|
722 | bound= pow ((double) bound, (double) k); |
---|
723 | } |
---|
724 | else if (GF) |
---|
725 | { |
---|
726 | bound= pow ((double) p, (double) getGFDegree()); |
---|
727 | bound= pow ((double) bound, (double) k); |
---|
728 | } |
---|
729 | else |
---|
730 | bound= pow ((double) p, (double) k); |
---|
731 | |
---|
732 | CanonicalForm random; |
---|
733 | CanonicalForm deriv_x, gcd_deriv; |
---|
734 | do |
---|
735 | { |
---|
736 | random= 0; |
---|
737 | // possible overflow if list.length() does not fit into a int |
---|
738 | if (list.length() >= bound) |
---|
739 | { |
---|
740 | fail= true; |
---|
741 | break; |
---|
742 | } |
---|
743 | for (int i= 0; i < k; i++) |
---|
744 | { |
---|
745 | if (list.isEmpty()) |
---|
746 | result.append (0); |
---|
747 | else if (GF) |
---|
748 | { |
---|
749 | result.append (genGF.generate()); |
---|
750 | random += result.getLast()*power (x, i); |
---|
751 | } |
---|
752 | else if (alpha.level() != 1) |
---|
753 | { |
---|
754 | AlgExtRandomF genAlgExt (alpha); |
---|
755 | result.append (genAlgExt.generate()); |
---|
756 | random += result.getLast()*power (x, i); |
---|
757 | } |
---|
758 | else |
---|
759 | { |
---|
760 | result.append (genFF.generate()); |
---|
761 | random += result.getLast()*power (x, i); |
---|
762 | } |
---|
763 | } |
---|
764 | if (find (list, random)) |
---|
765 | { |
---|
766 | result= CFList(); |
---|
767 | continue; |
---|
768 | } |
---|
769 | int l= F.level(); |
---|
770 | eval.insert (F); |
---|
771 | bool bad= false; |
---|
772 | for (CFListIterator i= result; i.hasItem(); i++, l--) |
---|
773 | { |
---|
774 | eval.insert (eval.getFirst()(i.getItem(), l)); |
---|
775 | if (degree (eval.getFirst(), l - 1) != degree (F, l - 1)) |
---|
776 | { |
---|
777 | if (!find (list, random)) |
---|
778 | list.append (random); |
---|
779 | result= CFList(); |
---|
780 | eval= CFList(); |
---|
781 | bad= true; |
---|
782 | break; |
---|
783 | } |
---|
784 | } |
---|
785 | |
---|
786 | if (bad) |
---|
787 | continue; |
---|
788 | |
---|
789 | if (degree (eval.getFirst()) != degree (F, 1)) |
---|
790 | { |
---|
791 | if (!find (list, random)) |
---|
792 | list.append (random); |
---|
793 | result= CFList(); |
---|
794 | eval= CFList(); |
---|
795 | continue; |
---|
796 | } |
---|
797 | |
---|
798 | deriv_x= deriv (eval.getFirst(), x); |
---|
799 | gcd_deriv= gcd (eval.getFirst(), deriv_x); |
---|
800 | if (degree (gcd_deriv) > 0) |
---|
801 | { |
---|
802 | if (!find (list, random)) |
---|
803 | list.append (random); |
---|
804 | result= CFList(); |
---|
805 | eval= CFList(); |
---|
806 | continue; |
---|
807 | } |
---|
808 | CFListIterator i= eval; |
---|
809 | i++; |
---|
810 | CanonicalForm contentx= content (i.getItem(), x); |
---|
811 | if (degree (contentx) > 0) |
---|
812 | { |
---|
813 | if (!find (list, random)) |
---|
814 | list.append (random); |
---|
815 | result= CFList(); |
---|
816 | eval= CFList(); |
---|
817 | continue; |
---|
818 | } |
---|
819 | |
---|
820 | if (list.length() >= bound) |
---|
821 | { |
---|
822 | fail= true; |
---|
823 | break; |
---|
824 | } |
---|
825 | } while (find (list, random)); |
---|
826 | |
---|
827 | if (!eval.isEmpty()) |
---|
828 | eval.removeFirst(); |
---|
829 | |
---|
830 | return result; |
---|
831 | } |
---|
832 | |
---|
833 | static inline |
---|
834 | int newMainVariableSearch (CanonicalForm& A, CFList& Aeval, CFList& |
---|
835 | evaluation, const Variable& alpha, const int lev, |
---|
836 | CanonicalForm& g |
---|
837 | ) |
---|
838 | { |
---|
839 | Variable x= Variable (1); |
---|
840 | CanonicalForm derivI, buf; |
---|
841 | bool GF= (CFFactory::gettype() == GaloisFieldDomain); |
---|
842 | int swapLevel= 0; |
---|
843 | CFList list; |
---|
844 | bool fail= false; |
---|
845 | buf= A; |
---|
846 | Aeval= CFList(); |
---|
847 | evaluation= CFList(); |
---|
848 | for (int i= lev; i <= A.level(); i++) |
---|
849 | { |
---|
850 | derivI= deriv (buf, Variable (i)); |
---|
851 | if (!derivI.isZero()) |
---|
852 | { |
---|
853 | g= gcd (buf, derivI); |
---|
854 | if (degree (g) > 0) |
---|
855 | return -1; |
---|
856 | |
---|
857 | buf= swapvar (buf, x, Variable (i)); |
---|
858 | Aeval= CFList(); |
---|
859 | evaluation= CFList(); |
---|
860 | fail= false; |
---|
861 | evaluation= evalPoints (buf, Aeval, alpha, list, GF, fail); |
---|
862 | if (!fail) |
---|
863 | { |
---|
864 | A= buf; |
---|
865 | swapLevel= i; |
---|
866 | break; |
---|
867 | } |
---|
868 | else |
---|
869 | buf= A; |
---|
870 | } |
---|
871 | } |
---|
872 | return swapLevel; |
---|
873 | } |
---|
874 | |
---|
875 | CanonicalForm lcmContent (const CanonicalForm& A, CFList& contentAi) |
---|
876 | { |
---|
877 | int i= A.level(); |
---|
878 | contentAi.append (myContent (A, i)); |
---|
879 | contentAi.append (myContent (A, i - 1)); |
---|
880 | CanonicalForm result= lcm (contentAi.getFirst(), contentAi.getLast()); |
---|
881 | for (i= i - 2; i > 0; i--) |
---|
882 | { |
---|
883 | contentAi.append (content (A, i)); |
---|
884 | result= lcm (result, contentAi.getLast()); |
---|
885 | } |
---|
886 | return result; |
---|
887 | } |
---|
888 | |
---|
889 | CFList |
---|
890 | henselLiftAndEarly (CanonicalForm& A, CFList& MOD, int*& liftBounds, bool& |
---|
891 | earlySuccess, CFList& earlyFactors, const CFList& Aeval, |
---|
892 | const CFList& biFactors, const CFList& evaluation, |
---|
893 | const ExtensionInfo& info) |
---|
894 | { |
---|
895 | bool extension= info.isInExtension(); |
---|
896 | CFList bufFactors= biFactors; |
---|
897 | bufFactors.insert (LC (Aeval.getFirst(), 1)); |
---|
898 | |
---|
899 | sortList (bufFactors, Variable (1)); |
---|
900 | |
---|
901 | CFList diophant; |
---|
902 | CFArray Pi; |
---|
903 | int smallFactorDeg= 11; //tunable parameter |
---|
904 | CFList result; |
---|
905 | int adaptedLiftBound= 0; |
---|
906 | int liftBound= liftBounds[1]; |
---|
907 | |
---|
908 | earlySuccess= false; |
---|
909 | CFList earlyReconstFactors; |
---|
910 | CFListIterator j= Aeval; |
---|
911 | j++; |
---|
912 | CanonicalForm buf= j.getItem(); |
---|
913 | CFMatrix Mat= CFMatrix (liftBound, bufFactors.length() - 1); |
---|
914 | MOD= CFList (power (Variable (2), liftBounds[0])); |
---|
915 | if (smallFactorDeg >= liftBound) |
---|
916 | { |
---|
917 | result= henselLift23 (Aeval, bufFactors, liftBounds, diophant, Pi, Mat); |
---|
918 | } |
---|
919 | else if (smallFactorDeg >= degree (buf) + 1) |
---|
920 | { |
---|
921 | liftBounds[1]= degree (buf) + 1; |
---|
922 | result= henselLift23 (Aeval, bufFactors, liftBounds, diophant, Pi, Mat); |
---|
923 | if (Aeval.length() == 2) |
---|
924 | { |
---|
925 | if (!extension) |
---|
926 | earlyFactors= earlyFactorDetect |
---|
927 | (buf, result, adaptedLiftBound, earlySuccess, |
---|
928 | degree (buf) + 1, MOD, liftBound); |
---|
929 | else |
---|
930 | earlyFactors= extEarlyFactorDetect |
---|
931 | (buf, result, adaptedLiftBound, earlySuccess, |
---|
932 | info, evaluation, degree |
---|
933 | (buf) + 1, MOD, liftBound); |
---|
934 | } |
---|
935 | else |
---|
936 | { |
---|
937 | if (!extension) |
---|
938 | adaptedLiftBound= liftBoundAdaption (buf, result, earlySuccess, |
---|
939 | degree (buf) + 1, MOD, liftBound); |
---|
940 | else |
---|
941 | adaptedLiftBound= extLiftBoundAdaption (buf, result, earlySuccess, info, |
---|
942 | evaluation, degree (buf) + 1, |
---|
943 | MOD, liftBound); |
---|
944 | } |
---|
945 | if (!earlySuccess) |
---|
946 | { |
---|
947 | result.insert (LC (buf, 1)); |
---|
948 | liftBounds[1]= adaptedLiftBound; |
---|
949 | liftBound= adaptedLiftBound; |
---|
950 | henselLiftResume (buf, result, degree (buf) + 1, liftBound, |
---|
951 | Pi, diophant, Mat, MOD); |
---|
952 | } |
---|
953 | else |
---|
954 | liftBounds[1]= adaptedLiftBound; |
---|
955 | } |
---|
956 | else if (smallFactorDeg < degree (buf) + 1) |
---|
957 | { |
---|
958 | liftBounds[1]= smallFactorDeg; |
---|
959 | result= henselLift23 (Aeval, bufFactors, liftBounds, diophant, Pi, Mat); |
---|
960 | if (Aeval.length() == 2) |
---|
961 | { |
---|
962 | if (!extension) |
---|
963 | earlyFactors= earlyFactorDetect (buf, result, adaptedLiftBound, |
---|
964 | earlySuccess, smallFactorDeg, MOD, |
---|
965 | liftBound); |
---|
966 | else |
---|
967 | earlyFactors= extEarlyFactorDetect (buf, result, adaptedLiftBound, |
---|
968 | earlySuccess, info, evaluation, |
---|
969 | smallFactorDeg, MOD, liftBound); |
---|
970 | } |
---|
971 | else |
---|
972 | { |
---|
973 | if (!extension) |
---|
974 | adaptedLiftBound= liftBoundAdaption (buf, result, earlySuccess, |
---|
975 | smallFactorDeg, MOD, liftBound); |
---|
976 | else |
---|
977 | adaptedLiftBound= extLiftBoundAdaption (buf, result, earlySuccess, info, |
---|
978 | evaluation, smallFactorDeg, MOD, |
---|
979 | liftBound); |
---|
980 | } |
---|
981 | |
---|
982 | if (!earlySuccess) |
---|
983 | { |
---|
984 | result.insert (LC (buf, 1)); |
---|
985 | henselLiftResume (buf, result, smallFactorDeg, degree (buf) + 1, |
---|
986 | Pi, diophant, Mat, MOD); |
---|
987 | if (Aeval.length() == 2) |
---|
988 | { |
---|
989 | if (!extension) |
---|
990 | earlyFactors= earlyFactorDetect (buf, result, adaptedLiftBound, |
---|
991 | earlySuccess, degree (buf) + 1, |
---|
992 | MOD, liftBound); |
---|
993 | else |
---|
994 | earlyFactors= extEarlyFactorDetect (buf, result, adaptedLiftBound, |
---|
995 | earlySuccess, info, evaluation, |
---|
996 | degree (buf) + 1, MOD, |
---|
997 | liftBound); |
---|
998 | } |
---|
999 | else |
---|
1000 | { |
---|
1001 | if (!extension) |
---|
1002 | adaptedLiftBound= liftBoundAdaption (buf, result, earlySuccess, |
---|
1003 | degree (buf) + 1, MOD,liftBound); |
---|
1004 | else |
---|
1005 | adaptedLiftBound= extLiftBoundAdaption (buf, result, earlySuccess, |
---|
1006 | info, evaluation, |
---|
1007 | degree (buf) + 1, MOD, |
---|
1008 | liftBound); |
---|
1009 | } |
---|
1010 | if (!earlySuccess) |
---|
1011 | { |
---|
1012 | result.insert (LC (buf, 1)); |
---|
1013 | liftBounds[1]= adaptedLiftBound; |
---|
1014 | liftBound= adaptedLiftBound; |
---|
1015 | henselLiftResume (buf, result, degree (buf) + 1, liftBound, |
---|
1016 | Pi, diophant, Mat, MOD); |
---|
1017 | } |
---|
1018 | else |
---|
1019 | liftBounds[1]= adaptedLiftBound; |
---|
1020 | } |
---|
1021 | else |
---|
1022 | liftBounds[1]= adaptedLiftBound; |
---|
1023 | } |
---|
1024 | |
---|
1025 | MOD.append (power (Variable (3), liftBounds[1])); |
---|
1026 | |
---|
1027 | if (Aeval.length() > 2) |
---|
1028 | { |
---|
1029 | CFListIterator j= Aeval; |
---|
1030 | j++; |
---|
1031 | CFList bufEval; |
---|
1032 | bufEval.append (j.getItem()); |
---|
1033 | j++; |
---|
1034 | int liftBoundsLength= Aeval.getLast().level() - 1; |
---|
1035 | for (int i= 2; i <= liftBoundsLength && j.hasItem(); i++, j++) |
---|
1036 | { |
---|
1037 | earlySuccess= false; |
---|
1038 | result.insert (LC (bufEval.getFirst(), 1)); |
---|
1039 | bufEval.append (j.getItem()); |
---|
1040 | liftBound= liftBounds[i]; |
---|
1041 | Mat= CFMatrix (liftBounds[i], result.length() - 1); |
---|
1042 | |
---|
1043 | buf= j.getItem(); |
---|
1044 | if (smallFactorDeg >= liftBound) |
---|
1045 | result= henselLift (bufEval, result, MOD, diophant, Pi, Mat, |
---|
1046 | liftBounds[i - 1], liftBounds[i]); |
---|
1047 | else if (smallFactorDeg >= degree (buf) + 1) |
---|
1048 | { |
---|
1049 | result= henselLift (bufEval, result, MOD, diophant, Pi, Mat, |
---|
1050 | liftBounds[i - 1], degree (buf) + 1); |
---|
1051 | |
---|
1052 | if (Aeval.length() == i + 1) |
---|
1053 | { |
---|
1054 | if (!extension) |
---|
1055 | earlyFactors= earlyFactorDetect |
---|
1056 | (buf, result, adaptedLiftBound, earlySuccess, |
---|
1057 | degree (buf) + 1, MOD, liftBound); |
---|
1058 | else |
---|
1059 | earlyFactors= extEarlyFactorDetect |
---|
1060 | (buf, result, adaptedLiftBound, earlySuccess, |
---|
1061 | info, evaluation, degree (buf) + 1, MOD, liftBound); |
---|
1062 | } |
---|
1063 | else |
---|
1064 | { |
---|
1065 | if (!extension) |
---|
1066 | adaptedLiftBound= liftBoundAdaption |
---|
1067 | (buf, result, earlySuccess, degree (buf) |
---|
1068 | + 1, MOD, liftBound); |
---|
1069 | else |
---|
1070 | adaptedLiftBound= extLiftBoundAdaption |
---|
1071 | (buf, result, earlySuccess, info, evaluation, |
---|
1072 | degree (buf) + 1, MOD, liftBound); |
---|
1073 | } |
---|
1074 | |
---|
1075 | if (!earlySuccess) |
---|
1076 | { |
---|
1077 | result.insert (LC (buf, 1)); |
---|
1078 | liftBounds[i]= adaptedLiftBound; |
---|
1079 | liftBound= adaptedLiftBound; |
---|
1080 | henselLiftResume (buf, result, degree (buf) + 1, liftBound, |
---|
1081 | Pi, diophant, Mat, MOD); |
---|
1082 | } |
---|
1083 | else |
---|
1084 | { |
---|
1085 | liftBounds[i]= adaptedLiftBound; |
---|
1086 | } |
---|
1087 | } |
---|
1088 | else if (smallFactorDeg < degree (buf) + 1) |
---|
1089 | { |
---|
1090 | result= henselLift (bufEval, result, MOD, diophant, Pi, Mat, |
---|
1091 | liftBounds[i - 1], smallFactorDeg); |
---|
1092 | |
---|
1093 | if (Aeval.length() == i + 1) |
---|
1094 | { |
---|
1095 | if (!extension) |
---|
1096 | earlyFactors= earlyFactorDetect |
---|
1097 | (buf, result, adaptedLiftBound, earlySuccess, |
---|
1098 | smallFactorDeg, MOD, liftBound); |
---|
1099 | else |
---|
1100 | earlyFactors= extEarlyFactorDetect |
---|
1101 | (buf, result, adaptedLiftBound, earlySuccess, |
---|
1102 | info, evaluation, smallFactorDeg, MOD, liftBound); |
---|
1103 | } |
---|
1104 | else |
---|
1105 | { |
---|
1106 | if (!extension) |
---|
1107 | adaptedLiftBound= liftBoundAdaption |
---|
1108 | (buf, result, earlySuccess, |
---|
1109 | smallFactorDeg, MOD, liftBound); |
---|
1110 | else |
---|
1111 | adaptedLiftBound= extLiftBoundAdaption |
---|
1112 | (buf, result, earlySuccess, info, evaluation, |
---|
1113 | smallFactorDeg, MOD, liftBound); |
---|
1114 | } |
---|
1115 | |
---|
1116 | if (!earlySuccess) |
---|
1117 | { |
---|
1118 | result.insert (LC (buf, 1)); |
---|
1119 | henselLiftResume (buf, result, smallFactorDeg, |
---|
1120 | degree (buf) + 1, Pi, diophant, Mat, MOD); |
---|
1121 | if (Aeval.length() == i + 1) |
---|
1122 | { |
---|
1123 | if (!extension) |
---|
1124 | earlyFactors= earlyFactorDetect |
---|
1125 | (buf, result, adaptedLiftBound, earlySuccess, |
---|
1126 | degree (buf) + 1, MOD, liftBound); |
---|
1127 | else |
---|
1128 | earlyFactors= extEarlyFactorDetect |
---|
1129 | (buf, result, adaptedLiftBound, earlySuccess, |
---|
1130 | info, evaluation, degree (buf) + 1, MOD, |
---|
1131 | liftBound); |
---|
1132 | } |
---|
1133 | else |
---|
1134 | { |
---|
1135 | if (!extension) |
---|
1136 | adaptedLiftBound= liftBoundAdaption |
---|
1137 | (buf, result, earlySuccess, degree |
---|
1138 | (buf) + 1, MOD, liftBound); |
---|
1139 | else |
---|
1140 | adaptedLiftBound= extLiftBoundAdaption |
---|
1141 | (buf, result, earlySuccess, info, evaluation, |
---|
1142 | degree (buf) + 1, MOD, liftBound); |
---|
1143 | } |
---|
1144 | |
---|
1145 | if (!earlySuccess) |
---|
1146 | { |
---|
1147 | result.insert (LC (buf, 1)); |
---|
1148 | liftBounds[i]= adaptedLiftBound; |
---|
1149 | liftBound= adaptedLiftBound; |
---|
1150 | henselLiftResume (buf, result, degree (buf) + 1, liftBound, |
---|
1151 | Pi, diophant, Mat, MOD); |
---|
1152 | } |
---|
1153 | else |
---|
1154 | liftBounds[i]= adaptedLiftBound; |
---|
1155 | } |
---|
1156 | else |
---|
1157 | liftBounds[i]= adaptedLiftBound; |
---|
1158 | } |
---|
1159 | MOD.append (power (Variable (i + 2), liftBounds[i])); |
---|
1160 | bufEval.removeFirst(); |
---|
1161 | } |
---|
1162 | bufFactors= result; |
---|
1163 | } |
---|
1164 | else |
---|
1165 | bufFactors= result; |
---|
1166 | |
---|
1167 | if (earlySuccess) |
---|
1168 | A= buf; |
---|
1169 | return result; |
---|
1170 | } |
---|
1171 | |
---|
1172 | void |
---|
1173 | gcdFreeBasis (CFFList& factors1, CFFList& factors2) |
---|
1174 | { |
---|
1175 | CanonicalForm g; |
---|
1176 | int k= factors1.length(); |
---|
1177 | int l= factors2.length(); |
---|
1178 | int n= 1; |
---|
1179 | int m; |
---|
1180 | CFFListIterator j; |
---|
1181 | for (CFFListIterator i= factors1; (n < k && i.hasItem()); i++, n++) |
---|
1182 | { |
---|
1183 | m= 1; |
---|
1184 | for (j= factors2; (m < l && j.hasItem()); j++, m++) |
---|
1185 | { |
---|
1186 | g= gcd (i.getItem().factor(), j.getItem().factor()); |
---|
1187 | if (degree (g) > 0) |
---|
1188 | { |
---|
1189 | j.getItem()= CFFactor (j.getItem().factor()/g, j.getItem().exp()); |
---|
1190 | i.getItem()= CFFactor (i.getItem().factor()/g, i.getItem().exp()); |
---|
1191 | factors1.append (CFFactor (g, i.getItem().exp())); |
---|
1192 | factors2.append (CFFactor (g, j.getItem().exp())); |
---|
1193 | } |
---|
1194 | } |
---|
1195 | } |
---|
1196 | } |
---|
1197 | |
---|
1198 | CFList |
---|
1199 | distributeContent (const CFList& L, const CFList* differentSecondVarFactors, |
---|
1200 | int length |
---|
1201 | ) |
---|
1202 | { |
---|
1203 | CFList l= L; |
---|
1204 | CanonicalForm content= l.getFirst(); |
---|
1205 | |
---|
1206 | if (content.inCoeffDomain()) |
---|
1207 | return l; |
---|
1208 | |
---|
1209 | if (l.length() == 1) |
---|
1210 | { |
---|
1211 | CFList result; |
---|
1212 | for (int i= 0; i < length; i++) |
---|
1213 | { |
---|
1214 | if (differentSecondVarFactors[i].isEmpty()) |
---|
1215 | continue; |
---|
1216 | if (result.isEmpty()) |
---|
1217 | { |
---|
1218 | result= differentSecondVarFactors[i]; |
---|
1219 | for (CFListIterator iter= result; iter.hasItem(); iter++) |
---|
1220 | content /= iter.getItem(); |
---|
1221 | } |
---|
1222 | else |
---|
1223 | { |
---|
1224 | CFListIterator iter1= result; |
---|
1225 | for (CFListIterator iter2= differentSecondVarFactors[i]; iter2.hasItem(); |
---|
1226 | iter2++, iter1++) |
---|
1227 | { |
---|
1228 | iter1.getItem() *= iter2.getItem(); |
---|
1229 | content /= iter2.getItem(); |
---|
1230 | } |
---|
1231 | } |
---|
1232 | } |
---|
1233 | result.insert (content); |
---|
1234 | return result; |
---|
1235 | } |
---|
1236 | |
---|
1237 | Variable v; |
---|
1238 | CFListIterator iter1; |
---|
1239 | CanonicalForm tmp, g; |
---|
1240 | for (int i= 0; i < length; i++) |
---|
1241 | { |
---|
1242 | if (differentSecondVarFactors[i].isEmpty()) |
---|
1243 | continue; |
---|
1244 | iter1= l; |
---|
1245 | iter1++; |
---|
1246 | |
---|
1247 | v= Variable (i + 3); |
---|
1248 | for (CFListIterator iter2= differentSecondVarFactors[i]; iter2.hasItem(); |
---|
1249 | iter2++, iter1++) |
---|
1250 | { |
---|
1251 | if (degree (iter2.getItem(),v) == degree (iter1.getItem(),v)) |
---|
1252 | continue; |
---|
1253 | tmp= iter1.getItem(); |
---|
1254 | for (int j= tmp.level(); j > 1; j--) |
---|
1255 | { |
---|
1256 | if (j == i + 3) |
---|
1257 | continue; |
---|
1258 | tmp= tmp (0, j); |
---|
1259 | } |
---|
1260 | g= gcd (iter2.getItem(), content); |
---|
1261 | if (degree (g) > 0) |
---|
1262 | { |
---|
1263 | iter2.getItem() /= tmp; |
---|
1264 | content /= g; |
---|
1265 | iter1.getItem() *= g; |
---|
1266 | } |
---|
1267 | } |
---|
1268 | } |
---|
1269 | |
---|
1270 | l.removeFirst(); |
---|
1271 | l.insert (content); |
---|
1272 | return l; |
---|
1273 | } |
---|
1274 | |
---|
1275 | CFList evaluateAtZero (const CanonicalForm& F) |
---|
1276 | { |
---|
1277 | CFList result; |
---|
1278 | CanonicalForm buf= F; |
---|
1279 | result.insert (buf); |
---|
1280 | for (int i= F.level(); i > 2; i--) |
---|
1281 | { |
---|
1282 | buf= buf (0, i); |
---|
1283 | result.insert (buf); |
---|
1284 | } |
---|
1285 | return result; |
---|
1286 | } |
---|
1287 | |
---|
1288 | CFList evaluateAtEval (const CanonicalForm& F, const CFArray& eval) |
---|
1289 | { |
---|
1290 | CFList result; |
---|
1291 | CanonicalForm buf= F; |
---|
1292 | result.insert (buf); |
---|
1293 | int k= eval.size(); |
---|
1294 | for (int i= 1; i < k; i++) |
---|
1295 | { |
---|
1296 | buf= buf (eval[i], i + 2); |
---|
1297 | result.insert (buf); |
---|
1298 | } |
---|
1299 | return result; |
---|
1300 | } |
---|
1301 | |
---|
1302 | CFList evaluateAtEval (const CanonicalForm& F, const CFList& evaluation, int l) |
---|
1303 | { |
---|
1304 | CFList result; |
---|
1305 | CanonicalForm buf= F; |
---|
1306 | result.insert (buf); |
---|
1307 | int k= evaluation.length() + l - 1; |
---|
1308 | CFListIterator j= evaluation; |
---|
1309 | for (int i= k; j.hasItem() && i > l; i--, j++) |
---|
1310 | { |
---|
1311 | if (F.level() < i) |
---|
1312 | continue; |
---|
1313 | buf= buf (j.getItem(), i); |
---|
1314 | result.insert (buf); |
---|
1315 | } |
---|
1316 | return result; |
---|
1317 | } |
---|
1318 | |
---|
1319 | int |
---|
1320 | testFactors (const CanonicalForm& G, const CFList& uniFactors, |
---|
1321 | const Variable& alpha, CanonicalForm& sqrfPartF, CFList& factors, |
---|
1322 | CFFList*& bufSqrfFactors, CFList& evalSqrfPartF, |
---|
1323 | const CFArray& evalPoint) |
---|
1324 | { |
---|
1325 | CanonicalForm tmp; |
---|
1326 | CFListIterator j; |
---|
1327 | for (CFListIterator i= uniFactors; i.hasItem(); i++) |
---|
1328 | { |
---|
1329 | tmp= i.getItem(); |
---|
1330 | if (i.hasItem()) |
---|
1331 | i++; |
---|
1332 | else |
---|
1333 | break; |
---|
1334 | for (j= i; j.hasItem(); j++) |
---|
1335 | { |
---|
1336 | if (tmp == j.getItem()) |
---|
1337 | return 0; |
---|
1338 | } |
---|
1339 | } |
---|
1340 | |
---|
1341 | CanonicalForm F= G; |
---|
1342 | CFFList sqrfFactorization= squarefreeFactorization (F, alpha); |
---|
1343 | |
---|
1344 | sqrfPartF= 1; |
---|
1345 | for (CFFListIterator i= sqrfFactorization; i.hasItem(); i++) |
---|
1346 | sqrfPartF *= i.getItem().factor(); |
---|
1347 | |
---|
1348 | evalSqrfPartF= evaluateAtEval (sqrfPartF, evalPoint); |
---|
1349 | |
---|
1350 | CanonicalForm test= evalSqrfPartF.getFirst() (evalPoint[0], 2); |
---|
1351 | |
---|
1352 | if (degree (test) != degree (sqrfPartF, 1)) |
---|
1353 | return 0; |
---|
1354 | |
---|
1355 | CFFList sqrfFactors; |
---|
1356 | CFList tmp2; |
---|
1357 | int k= 0; |
---|
1358 | factors= uniFactors; |
---|
1359 | CFFListIterator iter; |
---|
1360 | for (CFListIterator i= factors; i.hasItem(); i++, k++) |
---|
1361 | { |
---|
1362 | tmp= 1; |
---|
1363 | sqrfFactors= squarefreeFactorization (i.getItem(), alpha); |
---|
1364 | |
---|
1365 | for (iter= sqrfFactors; iter.hasItem(); iter++) |
---|
1366 | { |
---|
1367 | tmp2.append (iter.getItem().factor()); |
---|
1368 | tmp *= iter.getItem().factor(); |
---|
1369 | } |
---|
1370 | i.getItem()= tmp/Lc(tmp); |
---|
1371 | bufSqrfFactors [k]= sqrfFactors; |
---|
1372 | } |
---|
1373 | |
---|
1374 | for (int i= 0; i < factors.length() - 1; i++) |
---|
1375 | { |
---|
1376 | for (k= i + 1; k < factors.length(); k++) |
---|
1377 | { |
---|
1378 | gcdFreeBasis (bufSqrfFactors [i], bufSqrfFactors[k]); |
---|
1379 | } |
---|
1380 | } |
---|
1381 | |
---|
1382 | factors= CFList(); |
---|
1383 | for (int i= 0; i < uniFactors.length(); i++) |
---|
1384 | { |
---|
1385 | if (i == 0) |
---|
1386 | { |
---|
1387 | for (iter= bufSqrfFactors [i]; iter.hasItem(); iter++) |
---|
1388 | { |
---|
1389 | if (iter.getItem().factor().inCoeffDomain()) |
---|
1390 | continue; |
---|
1391 | iter.getItem()= CFFactor (iter.getItem().factor()/ |
---|
1392 | Lc (iter.getItem().factor()), |
---|
1393 | iter.getItem().exp()); |
---|
1394 | factors.append (iter.getItem().factor()); |
---|
1395 | } |
---|
1396 | } |
---|
1397 | else |
---|
1398 | { |
---|
1399 | for (iter= bufSqrfFactors [i]; iter.hasItem(); iter++) |
---|
1400 | { |
---|
1401 | if (iter.getItem().factor().inCoeffDomain()) |
---|
1402 | continue; |
---|
1403 | iter.getItem()= CFFactor (iter.getItem().factor()/ |
---|
1404 | Lc (iter.getItem().factor()), |
---|
1405 | iter.getItem().exp()); |
---|
1406 | if (!find (factors, iter.getItem().factor())) |
---|
1407 | factors.append (iter.getItem().factor()); |
---|
1408 | } |
---|
1409 | } |
---|
1410 | } |
---|
1411 | |
---|
1412 | test= prod (factors); |
---|
1413 | tmp= evalSqrfPartF.getFirst() (evalPoint[0],2); |
---|
1414 | if (test/Lc (test) != tmp/Lc (tmp)) |
---|
1415 | return 0; |
---|
1416 | else |
---|
1417 | return 1; |
---|
1418 | } |
---|
1419 | |
---|
1420 | CFList |
---|
1421 | precomputeLeadingCoeff (const CanonicalForm& LCF, const CFList& LCFFactors, |
---|
1422 | const Variable& alpha, const CFList& evaluation, |
---|
1423 | CFList* & differentSecondVarLCs, int lSecondVarLCs, |
---|
1424 | Variable& y |
---|
1425 | ) |
---|
1426 | { |
---|
1427 | y= Variable (1); |
---|
1428 | if (LCF.inCoeffDomain()) |
---|
1429 | { |
---|
1430 | CFList result; |
---|
1431 | for (int i= 1; i <= LCFFactors.length() + 1; i++) |
---|
1432 | result.append (1); |
---|
1433 | return result; |
---|
1434 | } |
---|
1435 | |
---|
1436 | CFMap N; |
---|
1437 | CanonicalForm F= compress (LCF, N); |
---|
1438 | if (LCF.isUnivariate()) |
---|
1439 | { |
---|
1440 | CFList result; |
---|
1441 | int LCFLevel= LCF.level(); |
---|
1442 | bool found= false; |
---|
1443 | if (LCFLevel == 2) |
---|
1444 | { |
---|
1445 | //bivariate leading coefficients are already the true leading coefficients |
---|
1446 | result= LCFFactors; |
---|
1447 | found= true; |
---|
1448 | } |
---|
1449 | else |
---|
1450 | { |
---|
1451 | CFListIterator j; |
---|
1452 | for (int i= 0; i < lSecondVarLCs; i++) |
---|
1453 | { |
---|
1454 | for (j= differentSecondVarLCs[i]; j.hasItem(); j++) |
---|
1455 | { |
---|
1456 | if (j.getItem().level() == LCFLevel) |
---|
1457 | { |
---|
1458 | found= true; |
---|
1459 | break; |
---|
1460 | } |
---|
1461 | } |
---|
1462 | if (found) |
---|
1463 | { |
---|
1464 | result= differentSecondVarLCs [i]; |
---|
1465 | break; |
---|
1466 | } |
---|
1467 | } |
---|
1468 | if (!found) |
---|
1469 | result= LCFFactors; |
---|
1470 | } |
---|
1471 | if (found) |
---|
1472 | result.insert (Lc (LCF)); |
---|
1473 | else |
---|
1474 | result.append (LCF); |
---|
1475 | return result; |
---|
1476 | } |
---|
1477 | |
---|
1478 | CFList factors= LCFFactors; |
---|
1479 | |
---|
1480 | CFMap dummy; |
---|
1481 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
1482 | i.getItem()= compress (i.getItem(), dummy); |
---|
1483 | |
---|
1484 | CanonicalForm sqrfPartF; |
---|
1485 | CFFList * bufSqrfFactors= new CFFList [factors.length()]; |
---|
1486 | CFList evalSqrfPartF, bufFactors; |
---|
1487 | CFArray evalPoint= CFArray (evaluation.length() - 1); |
---|
1488 | CFListIterator iter= evaluation; |
---|
1489 | for (int i= evaluation.length() - 2; i > -1; i--, iter++) |
---|
1490 | evalPoint[i]= iter.getItem(); |
---|
1491 | |
---|
1492 | int pass= testFactors (F, factors, alpha, sqrfPartF, |
---|
1493 | bufFactors, bufSqrfFactors, evalSqrfPartF, evalPoint); |
---|
1494 | |
---|
1495 | bool foundDifferent= false; |
---|
1496 | Variable z, x= y; |
---|
1497 | int j= 0; |
---|
1498 | if (!pass) |
---|
1499 | { |
---|
1500 | int lev; |
---|
1501 | // LCF is non-constant here |
---|
1502 | for (int i= 1; i <= LCF.level(); i++) |
---|
1503 | { |
---|
1504 | if(degree (LCF, i) > 0) |
---|
1505 | { |
---|
1506 | lev= i - 1; |
---|
1507 | break; |
---|
1508 | } |
---|
1509 | } |
---|
1510 | CFList bufBufFactors; |
---|
1511 | CanonicalForm bufF, swap; |
---|
1512 | CFArray buf; |
---|
1513 | for (int i= 0; i < lSecondVarLCs; i++) |
---|
1514 | { |
---|
1515 | if (!differentSecondVarLCs [i].isEmpty()) |
---|
1516 | { |
---|
1517 | bool allConstant= true; |
---|
1518 | for (iter= differentSecondVarLCs[i]; iter.hasItem(); iter++) |
---|
1519 | { |
---|
1520 | if (!iter.getItem().inCoeffDomain()) |
---|
1521 | { |
---|
1522 | allConstant= false; |
---|
1523 | y= Variable (iter.getItem().level()); |
---|
1524 | } |
---|
1525 | } |
---|
1526 | if (allConstant) |
---|
1527 | continue; |
---|
1528 | |
---|
1529 | bufFactors= differentSecondVarLCs [i]; |
---|
1530 | for (iter= bufFactors; iter.hasItem(); iter++) |
---|
1531 | iter.getItem()= swapvar (iter.getItem(), x, y); |
---|
1532 | bufF= F; |
---|
1533 | z= Variable (y.level() - lev); |
---|
1534 | bufF= swapvar (bufF, x, z); |
---|
1535 | bufBufFactors= bufFactors; |
---|
1536 | evalPoint= CFArray (evaluation.length() - 1); |
---|
1537 | buf= CFArray (evaluation.length()); |
---|
1538 | iter= evaluation; |
---|
1539 | int k= evaluation.length() - 1; |
---|
1540 | for (; iter.hasItem(); iter++, k--) |
---|
1541 | buf[k]= iter.getItem(); |
---|
1542 | swap= buf[z.level() - 1]; |
---|
1543 | buf[z.level() - 1]= buf[0]; |
---|
1544 | buf[0]= 0; |
---|
1545 | int l= 0; |
---|
1546 | for (k= 0; k < evaluation.length(); k++) |
---|
1547 | { |
---|
1548 | if (buf[k].isZero()) |
---|
1549 | continue; |
---|
1550 | evalPoint[l]= buf[k]; |
---|
1551 | l++; |
---|
1552 | } |
---|
1553 | pass= testFactors (bufF, bufBufFactors, alpha, sqrfPartF, bufFactors, |
---|
1554 | bufSqrfFactors, evalSqrfPartF, evalPoint); |
---|
1555 | if (pass) |
---|
1556 | { |
---|
1557 | foundDifferent= true; |
---|
1558 | F= bufF; |
---|
1559 | CFList l= factors; |
---|
1560 | for (iter= l; iter.hasItem(); iter++) |
---|
1561 | iter.getItem()= swapvar (iter.getItem(), x, y); |
---|
1562 | differentSecondVarLCs [i]= l; |
---|
1563 | j= i; |
---|
1564 | break; |
---|
1565 | } |
---|
1566 | if (!pass && i == lSecondVarLCs - 1) |
---|
1567 | { |
---|
1568 | CFList result; |
---|
1569 | result.append (LCF); |
---|
1570 | for (int k= 1; k <= factors.length(); k++) |
---|
1571 | result.append (LCF); |
---|
1572 | y= Variable (1); |
---|
1573 | delete [] bufSqrfFactors; |
---|
1574 | return result; |
---|
1575 | } |
---|
1576 | } |
---|
1577 | } |
---|
1578 | } |
---|
1579 | if (!pass) |
---|
1580 | { |
---|
1581 | CFList result; |
---|
1582 | result.append (LCF); |
---|
1583 | for (int k= 1; k <= factors.length(); k++) |
---|
1584 | result.append (LCF); |
---|
1585 | y= Variable (1); |
---|
1586 | delete [] bufSqrfFactors; |
---|
1587 | return result; |
---|
1588 | } |
---|
1589 | else |
---|
1590 | factors= bufFactors; |
---|
1591 | |
---|
1592 | bufFactors= factors; |
---|
1593 | CFList evaluation2; |
---|
1594 | if (y == x) |
---|
1595 | evaluation2= evaluation; |
---|
1596 | else |
---|
1597 | { |
---|
1598 | CanonicalForm tmp; |
---|
1599 | evaluation2= evaluation; |
---|
1600 | int i= evaluation.length() + 1; |
---|
1601 | for (CFListIterator iter= evaluation2; iter.hasItem(); iter++, i--) |
---|
1602 | { |
---|
1603 | if (i == y.level()) |
---|
1604 | { |
---|
1605 | tmp= iter.getItem(); |
---|
1606 | iter.getItem()= evaluation2.getLast(); |
---|
1607 | evaluation2.removeLast(); |
---|
1608 | evaluation2.append (tmp); |
---|
1609 | break; |
---|
1610 | } |
---|
1611 | } |
---|
1612 | } |
---|
1613 | |
---|
1614 | CFList interMedResult; |
---|
1615 | CanonicalForm oldSqrfPartF= sqrfPartF; |
---|
1616 | sqrfPartF= shift2Zero (sqrfPartF, evalSqrfPartF, evaluation2, 1); |
---|
1617 | if (factors.length() > 1) |
---|
1618 | { |
---|
1619 | CanonicalForm LC1= LC (oldSqrfPartF, 1); |
---|
1620 | CFList leadingCoeffs; |
---|
1621 | for (int i= 0; i < factors.length(); i++) |
---|
1622 | leadingCoeffs.append (LC1); |
---|
1623 | |
---|
1624 | CFList LC1eval= evaluateAtEval (LC1, evaluation2, 1); |
---|
1625 | CFList oldFactors= factors; |
---|
1626 | for (CFListIterator i= oldFactors; i.hasItem(); i++) |
---|
1627 | i.getItem() *= LC1eval.getFirst()/Lc (i.getItem()); |
---|
1628 | |
---|
1629 | bool success= false; |
---|
1630 | CanonicalForm oldSqrfPartFPowLC= oldSqrfPartF*power(LC1,factors.length()-1); |
---|
1631 | if (size (oldSqrfPartFPowLC)/getNumVars (oldSqrfPartFPowLC) < 500 && |
---|
1632 | LucksWangSparseHeuristic (oldSqrfPartFPowLC, |
---|
1633 | oldFactors, 1, leadingCoeffs, factors)) |
---|
1634 | { |
---|
1635 | interMedResult= recoverFactors (oldSqrfPartF, factors); |
---|
1636 | if (oldFactors.length() == interMedResult.length()) |
---|
1637 | success= true; |
---|
1638 | } |
---|
1639 | if (!success) |
---|
1640 | { |
---|
1641 | LC1= LC (evalSqrfPartF.getFirst(), 1); |
---|
1642 | |
---|
1643 | CFArray leadingCoeffs= CFArray (factors.length()); |
---|
1644 | for (int i= 0; i < factors.length(); i++) |
---|
1645 | leadingCoeffs[i]= LC1; |
---|
1646 | |
---|
1647 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
1648 | { |
---|
1649 | i.getItem()= i.getItem() (x + evaluation2.getLast(), x); |
---|
1650 | i.getItem() *= LC1 (0,2)/Lc (i.getItem()); |
---|
1651 | } |
---|
1652 | factors.insert (1); |
---|
1653 | |
---|
1654 | CanonicalForm |
---|
1655 | newSqrfPartF= evalSqrfPartF.getFirst()*power (LC1, factors.length() - 2); |
---|
1656 | |
---|
1657 | int liftBound= degree (newSqrfPartF,2) + 1; |
---|
1658 | |
---|
1659 | CFMatrix M= CFMatrix (liftBound, factors.length() - 1); |
---|
1660 | CFArray Pi; |
---|
1661 | CFList diophant; |
---|
1662 | nonMonicHenselLift12 (newSqrfPartF, factors, liftBound, Pi, diophant, M, |
---|
1663 | leadingCoeffs, false); |
---|
1664 | |
---|
1665 | if (sqrfPartF.level() > 2) |
---|
1666 | { |
---|
1667 | int* liftBounds= new int [sqrfPartF.level() - 1]; |
---|
1668 | liftBounds [0]= liftBound; |
---|
1669 | bool noOneToOne= false; |
---|
1670 | CFList *leadingCoeffs2= new CFList [sqrfPartF.level()-2]; |
---|
1671 | LC1= LC (evalSqrfPartF.getLast(), 1); |
---|
1672 | CFList LCs; |
---|
1673 | for (int i= 0; i < factors.length(); i++) |
---|
1674 | LCs.append (LC1); |
---|
1675 | leadingCoeffs2 [sqrfPartF.level() - 3]= LCs; |
---|
1676 | for (int i= sqrfPartF.level() - 1; i > 2; i--) |
---|
1677 | { |
---|
1678 | for (CFListIterator j= LCs; j.hasItem(); j++) |
---|
1679 | j.getItem()= j.getItem() (0, i + 1); |
---|
1680 | leadingCoeffs2 [i - 3]= LCs; |
---|
1681 | } |
---|
1682 | sqrfPartF *= power (LC1, factors.length()-1); |
---|
1683 | |
---|
1684 | int liftBoundsLength= sqrfPartF.level() - 1; |
---|
1685 | for (int i= 1; i < liftBoundsLength; i++) |
---|
1686 | liftBounds [i]= degree (sqrfPartF, i + 2) + 1; |
---|
1687 | evalSqrfPartF= evaluateAtZero (sqrfPartF); |
---|
1688 | evalSqrfPartF.removeFirst(); |
---|
1689 | factors= nonMonicHenselLift (evalSqrfPartF, factors, leadingCoeffs2, |
---|
1690 | diophant, Pi, liftBounds, sqrfPartF.level() - 1, noOneToOne); |
---|
1691 | delete [] leadingCoeffs2; |
---|
1692 | delete [] liftBounds; |
---|
1693 | } |
---|
1694 | for (CFListIterator iter= factors; iter.hasItem(); iter++) |
---|
1695 | iter.getItem()= reverseShift (iter.getItem(), evaluation2, 1); |
---|
1696 | |
---|
1697 | interMedResult= |
---|
1698 | recoverFactors (reverseShift(evalSqrfPartF.getLast(),evaluation2,1), |
---|
1699 | factors); |
---|
1700 | } |
---|
1701 | } |
---|
1702 | else |
---|
1703 | { |
---|
1704 | factors= CFList (oldSqrfPartF); |
---|
1705 | interMedResult= recoverFactors (oldSqrfPartF, factors); |
---|
1706 | } |
---|
1707 | |
---|
1708 | CFList result; |
---|
1709 | CFFListIterator k; |
---|
1710 | for (int i= 0; i < LCFFactors.length(); i++) |
---|
1711 | { |
---|
1712 | CanonicalForm tmp= 1; |
---|
1713 | for (k= bufSqrfFactors[i]; k.hasItem(); k++) |
---|
1714 | { |
---|
1715 | int pos= findItem (bufFactors, k.getItem().factor()); |
---|
1716 | if (pos) |
---|
1717 | tmp *= power (getItem (interMedResult, pos), k.getItem().exp()); |
---|
1718 | } |
---|
1719 | result.append (tmp); |
---|
1720 | } |
---|
1721 | |
---|
1722 | for (CFListIterator i= result; i.hasItem(); i++) |
---|
1723 | { |
---|
1724 | F /= i.getItem(); |
---|
1725 | if (foundDifferent) |
---|
1726 | i.getItem()= swapvar (i.getItem(), x, z); |
---|
1727 | i.getItem()= N (i.getItem()); |
---|
1728 | } |
---|
1729 | |
---|
1730 | if (foundDifferent) |
---|
1731 | { |
---|
1732 | CFList l= differentSecondVarLCs [j]; |
---|
1733 | for (CFListIterator i= l; i.hasItem(); i++) |
---|
1734 | i.getItem()= swapvar (i.getItem(), y, z); |
---|
1735 | differentSecondVarLCs [j]= l; |
---|
1736 | F= swapvar (F, x, z); |
---|
1737 | } |
---|
1738 | |
---|
1739 | result.insert (N (F)); |
---|
1740 | |
---|
1741 | result= distributeContent (result, differentSecondVarLCs, lSecondVarLCs); |
---|
1742 | |
---|
1743 | if (!result.getFirst().inCoeffDomain()) |
---|
1744 | { |
---|
1745 | CFListIterator i= result; |
---|
1746 | CanonicalForm tmp; |
---|
1747 | if (foundDifferent) |
---|
1748 | i.getItem()= swapvar (i.getItem(), Variable (2), y); |
---|
1749 | |
---|
1750 | tmp= i.getItem(); |
---|
1751 | |
---|
1752 | i++; |
---|
1753 | for (; i.hasItem(); i++) |
---|
1754 | { |
---|
1755 | if (foundDifferent) |
---|
1756 | i.getItem()= swapvar (i.getItem(), Variable (2), y)*tmp; |
---|
1757 | else |
---|
1758 | i.getItem() *= tmp; |
---|
1759 | } |
---|
1760 | } |
---|
1761 | else |
---|
1762 | y= Variable (1); |
---|
1763 | |
---|
1764 | delete [] bufSqrfFactors; |
---|
1765 | |
---|
1766 | return result; |
---|
1767 | } |
---|
1768 | |
---|
1769 | void |
---|
1770 | evaluationWRTDifferentSecondVars (CFList*& Aeval, const CFList& evaluation, |
---|
1771 | const CanonicalForm& A) |
---|
1772 | { |
---|
1773 | CanonicalForm tmp; |
---|
1774 | CFList tmp2; |
---|
1775 | CFListIterator iter; |
---|
1776 | for (int i= A.level(); i > 2; i--) |
---|
1777 | { |
---|
1778 | tmp= A; |
---|
1779 | tmp2= CFList(); |
---|
1780 | iter= evaluation; |
---|
1781 | bool preserveDegree= true; |
---|
1782 | for (int j= A.level(); j > 1; j--, iter++) |
---|
1783 | { |
---|
1784 | if (j == i) |
---|
1785 | continue; |
---|
1786 | else |
---|
1787 | { |
---|
1788 | tmp= tmp (iter.getItem(), j); |
---|
1789 | tmp2.insert (tmp); |
---|
1790 | if ((degree (tmp, i) != degree (A, i)) || |
---|
1791 | (degree (tmp, 1) != degree (A, 1))) |
---|
1792 | { |
---|
1793 | preserveDegree= false; |
---|
1794 | break; |
---|
1795 | } |
---|
1796 | if (!content(tmp).inCoeffDomain() || !content(tmp,1).inCoeffDomain()) |
---|
1797 | { |
---|
1798 | preserveDegree= false; |
---|
1799 | break; |
---|
1800 | } |
---|
1801 | } |
---|
1802 | } |
---|
1803 | if (preserveDegree) |
---|
1804 | Aeval [i - 3]= tmp2; |
---|
1805 | else |
---|
1806 | Aeval [i - 3]= CFList(); |
---|
1807 | } |
---|
1808 | } |
---|
1809 | |
---|
1810 | #endif |
---|
1811 | |
---|
1812 | static inline |
---|
1813 | CanonicalForm prodEval (const CFList& l, const CanonicalForm& evalPoint, |
---|
1814 | const Variable& v) |
---|
1815 | { |
---|
1816 | CanonicalForm result= 1; |
---|
1817 | for (CFListIterator i= l; i.hasItem(); i++) |
---|
1818 | result *= i.getItem() (evalPoint, v); |
---|
1819 | return result; |
---|
1820 | } |
---|
1821 | |
---|
1822 | //recombine bivariate factors in case one bivariate factorization yields less |
---|
1823 | // factors than the other |
---|
1824 | CFList |
---|
1825 | recombination (const CFList& factors1, const CFList& factors2, int s, int thres, |
---|
1826 | const CanonicalForm& evalPoint, const Variable& x) |
---|
1827 | { |
---|
1828 | CFList T, S; |
---|
1829 | |
---|
1830 | T= factors1; |
---|
1831 | CFList result; |
---|
1832 | CanonicalForm buf; |
---|
1833 | int * v= new int [T.length()]; |
---|
1834 | for (int i= 0; i < T.length(); i++) |
---|
1835 | v[i]= 0; |
---|
1836 | bool nosubset= false; |
---|
1837 | CFArray TT; |
---|
1838 | TT= copy (factors1); |
---|
1839 | while (T.length() >= 2*s && s <= thres) |
---|
1840 | { |
---|
1841 | while (nosubset == false) |
---|
1842 | { |
---|
1843 | if (T.length() == s) |
---|
1844 | { |
---|
1845 | delete [] v; |
---|
1846 | result.append (prod (T)); |
---|
1847 | return result; |
---|
1848 | } |
---|
1849 | S= subset (v, s, TT, nosubset); |
---|
1850 | if (nosubset) break; |
---|
1851 | buf= prodEval (S, evalPoint, x); |
---|
1852 | buf /= Lc (buf); |
---|
1853 | if (find (factors2, buf)) |
---|
1854 | { |
---|
1855 | T= Difference (T, S); |
---|
1856 | result.append (prod (S)); |
---|
1857 | TT= copy (T); |
---|
1858 | indexUpdate (v, s, T.length(), nosubset); |
---|
1859 | if (nosubset) break; |
---|
1860 | } |
---|
1861 | } |
---|
1862 | s++; |
---|
1863 | if (T.length() < 2*s || T.length() == s) |
---|
1864 | { |
---|
1865 | delete [] v; |
---|
1866 | result.append (prod (T)); |
---|
1867 | return result; |
---|
1868 | } |
---|
1869 | for (int i= 0; i < T.length(); i++) |
---|
1870 | v[i]= 0; |
---|
1871 | nosubset= false; |
---|
1872 | } |
---|
1873 | |
---|
1874 | delete [] v; |
---|
1875 | if (T.length() < 2*s) |
---|
1876 | { |
---|
1877 | result.append (prod (T)); |
---|
1878 | return result; |
---|
1879 | } |
---|
1880 | |
---|
1881 | return result; |
---|
1882 | } |
---|
1883 | |
---|
1884 | #ifdef HAVE_NTL |
---|
1885 | void |
---|
1886 | factorizationWRTDifferentSecondVars (const CanonicalForm& A, CFList*& Aeval, |
---|
1887 | const ExtensionInfo& info, |
---|
1888 | int& minFactorsLength, bool& irred) |
---|
1889 | { |
---|
1890 | Variable x= Variable (1); |
---|
1891 | minFactorsLength= 0; |
---|
1892 | irred= false; |
---|
1893 | CFList factors; |
---|
1894 | Variable v; |
---|
1895 | for (int j= 0; j < A.level() - 2; j++) |
---|
1896 | { |
---|
1897 | if (!Aeval[j].isEmpty()) |
---|
1898 | { |
---|
1899 | v= Variable (Aeval[j].getFirst().level()); |
---|
1900 | if (CFFactory::gettype() == GaloisFieldDomain) |
---|
1901 | factors= GFBiSqrfFactorize (Aeval[j].getFirst()); |
---|
1902 | else if (info.getAlpha().level() == 1) |
---|
1903 | factors= FpBiSqrfFactorize (Aeval[j].getFirst()); |
---|
1904 | else |
---|
1905 | factors= FqBiSqrfFactorize (Aeval[j].getFirst(), info.getAlpha()); |
---|
1906 | |
---|
1907 | factors.removeFirst(); |
---|
1908 | if (minFactorsLength == 0) |
---|
1909 | minFactorsLength= factors.length(); |
---|
1910 | else |
---|
1911 | minFactorsLength= tmin (minFactorsLength, factors.length()); |
---|
1912 | |
---|
1913 | if (factors.length() == 1) |
---|
1914 | { |
---|
1915 | irred= true; |
---|
1916 | return; |
---|
1917 | } |
---|
1918 | sortList (factors, x); |
---|
1919 | Aeval [j]= factors; |
---|
1920 | } |
---|
1921 | } |
---|
1922 | } |
---|
1923 | |
---|
1924 | void getLeadingCoeffs (const CanonicalForm& A, CFList*& Aeval, |
---|
1925 | const CFList& uniFactors, const CFList& evaluation |
---|
1926 | ) |
---|
1927 | { |
---|
1928 | CanonicalForm evalPoint; |
---|
1929 | int i; |
---|
1930 | CFListIterator iter, iter2; |
---|
1931 | Variable v; |
---|
1932 | CFList l, LCs, buf; |
---|
1933 | int pos; |
---|
1934 | for (int j= 0; j < A.level() - 2; j++) |
---|
1935 | { |
---|
1936 | if (!Aeval[j].isEmpty()) |
---|
1937 | { |
---|
1938 | i= A.level(); |
---|
1939 | for (iter= evaluation; iter.hasItem(); iter++, i--) |
---|
1940 | { |
---|
1941 | if (i == Aeval[j].getFirst().level()) |
---|
1942 | { |
---|
1943 | evalPoint= iter.getItem(); |
---|
1944 | break; |
---|
1945 | } |
---|
1946 | } |
---|
1947 | |
---|
1948 | v= Variable (i); |
---|
1949 | if (Aeval[j].length() > uniFactors.length()) |
---|
1950 | Aeval[j]= recombination (Aeval[j], uniFactors, 1, |
---|
1951 | Aeval[j].length() - uniFactors.length() + 1, |
---|
1952 | evalPoint, v); |
---|
1953 | |
---|
1954 | l= CFList(); |
---|
1955 | buf= buildUniFactors (Aeval[j], evalPoint, v); |
---|
1956 | for (iter= uniFactors; iter.hasItem(); iter++) |
---|
1957 | { |
---|
1958 | pos= findItem (buf, iter.getItem()); |
---|
1959 | if (pos) |
---|
1960 | l.append (getItem (Aeval[j], pos)); |
---|
1961 | } |
---|
1962 | Aeval [j]= l; |
---|
1963 | |
---|
1964 | LCs= CFList(); |
---|
1965 | for (iter= Aeval[j]; iter.hasItem(); iter++) |
---|
1966 | LCs.append (LC (iter.getItem(), 1)); |
---|
1967 | normalize (LCs); |
---|
1968 | Aeval[j]= LCs; |
---|
1969 | } |
---|
1970 | } |
---|
1971 | } |
---|
1972 | |
---|
1973 | CFList |
---|
1974 | buildUniFactors (const CFList& biFactors, const CanonicalForm& evalPoint, |
---|
1975 | const Variable& y) |
---|
1976 | { |
---|
1977 | CFList result; |
---|
1978 | CanonicalForm tmp; |
---|
1979 | for (CFListIterator i= biFactors; i.hasItem(); i++) |
---|
1980 | { |
---|
1981 | tmp= mod (i.getItem(), y - evalPoint); |
---|
1982 | tmp /= Lc (tmp); |
---|
1983 | result.append (tmp); |
---|
1984 | } |
---|
1985 | return result; |
---|
1986 | } |
---|
1987 | |
---|
1988 | void refineBiFactors (const CanonicalForm& A, CFList& biFactors, |
---|
1989 | CFList* const& Aeval, const CFList& evaluation, |
---|
1990 | int minFactorsLength) |
---|
1991 | { |
---|
1992 | CFListIterator iter; |
---|
1993 | CanonicalForm evalPoint; |
---|
1994 | int i; |
---|
1995 | Variable v; |
---|
1996 | Variable y= Variable (2); |
---|
1997 | CFList list; |
---|
1998 | for (int j= 0; j < A.level() - 2; j++) |
---|
1999 | { |
---|
2000 | if (Aeval[j].length() == minFactorsLength) |
---|
2001 | { |
---|
2002 | i= A.level(); |
---|
2003 | |
---|
2004 | for (iter= evaluation; iter.hasItem(); iter++, i--) |
---|
2005 | { |
---|
2006 | if (i == Aeval[j].getFirst().level()) |
---|
2007 | { |
---|
2008 | evalPoint= iter.getItem(); |
---|
2009 | break; |
---|
2010 | } |
---|
2011 | } |
---|
2012 | |
---|
2013 | v= Variable (i); |
---|
2014 | list= buildUniFactors (Aeval[j], evalPoint, v); |
---|
2015 | |
---|
2016 | biFactors= recombination (biFactors, list, 1, |
---|
2017 | biFactors.length() - list.length() + 1, |
---|
2018 | evaluation.getLast(), y); |
---|
2019 | return; |
---|
2020 | } |
---|
2021 | } |
---|
2022 | } |
---|
2023 | |
---|
2024 | void prepareLeadingCoeffs (CFList*& LCs, int n, const CFList& leadingCoeffs, |
---|
2025 | const CFList& biFactors, const CFList& evaluation) |
---|
2026 | { |
---|
2027 | CFList l= leadingCoeffs; |
---|
2028 | LCs [n-3]= l; |
---|
2029 | CFListIterator j; |
---|
2030 | CFListIterator iter= evaluation; |
---|
2031 | for (int i= n - 1; i > 2; i--, iter++) |
---|
2032 | { |
---|
2033 | for (j= l; j.hasItem(); j++) |
---|
2034 | j.getItem()= j.getItem() (iter.getItem(), i + 1); |
---|
2035 | LCs [i - 3]= l; |
---|
2036 | } |
---|
2037 | l= LCs [0]; |
---|
2038 | for (CFListIterator i= l; i.hasItem(); i++) |
---|
2039 | i.getItem()= i.getItem() (iter.getItem(), 3); |
---|
2040 | CFListIterator ii= biFactors; |
---|
2041 | CFList normalizeFactor; |
---|
2042 | for (CFListIterator i= l; i.hasItem(); i++, ii++) |
---|
2043 | normalizeFactor.append (Lc (LC (ii.getItem(), 1))/Lc (i.getItem())); |
---|
2044 | for (int i= 0; i < n-2; i++) |
---|
2045 | { |
---|
2046 | ii= normalizeFactor; |
---|
2047 | for (j= LCs [i]; j.hasItem(); j++, ii++) |
---|
2048 | j.getItem() *= ii.getItem(); |
---|
2049 | } |
---|
2050 | } |
---|
2051 | |
---|
2052 | CFList recoverFactors (const CanonicalForm& F, const CFList& factors) |
---|
2053 | { |
---|
2054 | CFList result; |
---|
2055 | CanonicalForm tmp, tmp2; |
---|
2056 | CanonicalForm G= F; |
---|
2057 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
2058 | { |
---|
2059 | tmp= i.getItem()/content (i.getItem(), 1); |
---|
2060 | if (fdivides (tmp, G, tmp2)) |
---|
2061 | { |
---|
2062 | G= tmp2; |
---|
2063 | result.append (tmp); |
---|
2064 | } |
---|
2065 | } |
---|
2066 | return result; |
---|
2067 | } |
---|
2068 | |
---|
2069 | CFList recoverFactors (const CanonicalForm& F, const CFList& factors, |
---|
2070 | const CFList& evaluation) |
---|
2071 | { |
---|
2072 | CFList result; |
---|
2073 | CanonicalForm tmp, tmp2; |
---|
2074 | CanonicalForm G= F; |
---|
2075 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
2076 | { |
---|
2077 | tmp= reverseShift (i.getItem(), evaluation); |
---|
2078 | tmp /= content (tmp, 1); |
---|
2079 | if (fdivides (tmp, G, tmp2)) |
---|
2080 | { |
---|
2081 | G= tmp2; |
---|
2082 | result.append (tmp); |
---|
2083 | } |
---|
2084 | } |
---|
2085 | return result; |
---|
2086 | } |
---|
2087 | |
---|
2088 | CFList |
---|
2089 | extNonMonicFactorRecombination (const CFList& factors, const CanonicalForm& F, |
---|
2090 | const ExtensionInfo& info) |
---|
2091 | { |
---|
2092 | Variable alpha= info.getAlpha(); |
---|
2093 | Variable beta= info.getBeta(); |
---|
2094 | CanonicalForm gamma= info.getGamma(); |
---|
2095 | CanonicalForm delta= info.getDelta(); |
---|
2096 | int k= info.getGFDegree(); |
---|
2097 | CFList source, dest; |
---|
2098 | |
---|
2099 | int degMipoBeta= 1; |
---|
2100 | if (!k && beta != Variable(1)) |
---|
2101 | degMipoBeta= degree (getMipo (beta)); |
---|
2102 | |
---|
2103 | CFList T, S; |
---|
2104 | T= factors; |
---|
2105 | int s= 1; |
---|
2106 | CFList result; |
---|
2107 | CanonicalForm quot, buf= F; |
---|
2108 | |
---|
2109 | CanonicalForm g; |
---|
2110 | CanonicalForm buf2; |
---|
2111 | int * v= new int [T.length()]; |
---|
2112 | for (int i= 0; i < T.length(); i++) |
---|
2113 | v[i]= 0; |
---|
2114 | bool noSubset= false; |
---|
2115 | CFArray TT; |
---|
2116 | TT= copy (factors); |
---|
2117 | bool recombination= false; |
---|
2118 | bool trueFactor= false; |
---|
2119 | while (T.length() >= 2*s) |
---|
2120 | { |
---|
2121 | while (noSubset == false) |
---|
2122 | { |
---|
2123 | if (T.length() == s) |
---|
2124 | { |
---|
2125 | delete [] v; |
---|
2126 | if (recombination) |
---|
2127 | { |
---|
2128 | g= prod (T); |
---|
2129 | T.removeFirst(); |
---|
2130 | result.append (g/myContent (g)); |
---|
2131 | g /= Lc (g); |
---|
2132 | appendTestMapDown (result, g, info, source, dest); |
---|
2133 | return result; |
---|
2134 | } |
---|
2135 | else |
---|
2136 | return CFList (buf/myContent(buf)); |
---|
2137 | } |
---|
2138 | |
---|
2139 | S= subset (v, s, TT, noSubset); |
---|
2140 | if (noSubset) break; |
---|
2141 | |
---|
2142 | g= prod (S); |
---|
2143 | g /= myContent (g); |
---|
2144 | if (fdivides (g, buf, quot)) |
---|
2145 | { |
---|
2146 | buf2= g; |
---|
2147 | buf2 /= Lc (buf2); |
---|
2148 | if (!k && beta.level() == 1) |
---|
2149 | { |
---|
2150 | if (degree (buf2, alpha) < degMipoBeta) |
---|
2151 | { |
---|
2152 | appendTestMapDown (result, buf2, info, source, dest); |
---|
2153 | buf= quot; |
---|
2154 | recombination= true; |
---|
2155 | trueFactor= true; |
---|
2156 | } |
---|
2157 | } |
---|
2158 | else |
---|
2159 | { |
---|
2160 | if (!isInExtension (buf2, gamma, k, delta, source, dest)) |
---|
2161 | { |
---|
2162 | appendTestMapDown (result, buf2, info, source, dest); |
---|
2163 | buf= quot; |
---|
2164 | recombination= true; |
---|
2165 | trueFactor= true; |
---|
2166 | } |
---|
2167 | } |
---|
2168 | if (trueFactor) |
---|
2169 | { |
---|
2170 | T= Difference (T, S); |
---|
2171 | |
---|
2172 | if (T.length() < 2*s || T.length() == s) |
---|
2173 | { |
---|
2174 | delete [] v; |
---|
2175 | buf /= myContent (buf); |
---|
2176 | buf /= Lc (buf); |
---|
2177 | appendTestMapDown (result, buf, info, source, dest); |
---|
2178 | return result; |
---|
2179 | } |
---|
2180 | trueFactor= false; |
---|
2181 | TT= copy (T); |
---|
2182 | indexUpdate (v, s, T.length(), noSubset); |
---|
2183 | if (noSubset) break; |
---|
2184 | } |
---|
2185 | } |
---|
2186 | } |
---|
2187 | s++; |
---|
2188 | if (T.length() < 2*s || T.length() == s) |
---|
2189 | { |
---|
2190 | delete [] v; |
---|
2191 | appendTestMapDown (result, buf/myContent(buf), info, source, dest); |
---|
2192 | return result; |
---|
2193 | } |
---|
2194 | for (int i= 0; i < T.length(); i++) |
---|
2195 | v[i]= 0; |
---|
2196 | noSubset= false; |
---|
2197 | } |
---|
2198 | if (T.length() < 2*s) |
---|
2199 | appendMapDown (result, F/myContent(F), info, source, dest); |
---|
2200 | |
---|
2201 | delete [] v; |
---|
2202 | return result; |
---|
2203 | } |
---|
2204 | |
---|
2205 | CFList |
---|
2206 | extFactorize (const CanonicalForm& F, const ExtensionInfo& info); |
---|
2207 | |
---|
2208 | CFList |
---|
2209 | multiFactorize (const CanonicalForm& F, const ExtensionInfo& info) |
---|
2210 | { |
---|
2211 | |
---|
2212 | if (F.inCoeffDomain()) |
---|
2213 | return CFList (F); |
---|
2214 | |
---|
2215 | // compress and find main Variable |
---|
2216 | CFMap N; |
---|
2217 | CanonicalForm A= myCompress (F, N); |
---|
2218 | |
---|
2219 | A /= Lc (A); // make monic |
---|
2220 | |
---|
2221 | Variable alpha= info.getAlpha(); |
---|
2222 | Variable beta= info.getBeta(); |
---|
2223 | CanonicalForm gamma= info.getGamma(); |
---|
2224 | CanonicalForm delta= info.getDelta(); |
---|
2225 | bool extension= info.isInExtension(); |
---|
2226 | bool GF= (CFFactory::gettype() == GaloisFieldDomain); |
---|
2227 | //univariate case |
---|
2228 | if (F.isUnivariate()) |
---|
2229 | { |
---|
2230 | if (extension == false) |
---|
2231 | return uniFactorizer (F, alpha, GF); |
---|
2232 | else |
---|
2233 | { |
---|
2234 | CFList source, dest; |
---|
2235 | A= mapDown (F, info, source, dest); |
---|
2236 | return uniFactorizer (A, beta, GF); |
---|
2237 | } |
---|
2238 | } |
---|
2239 | |
---|
2240 | //bivariate case |
---|
2241 | if (A.level() == 2) |
---|
2242 | { |
---|
2243 | CFList buf= biFactorize (F, info); |
---|
2244 | return buf; |
---|
2245 | } |
---|
2246 | |
---|
2247 | Variable x= Variable (1); |
---|
2248 | Variable y= Variable (2); |
---|
2249 | |
---|
2250 | // remove content |
---|
2251 | CFList contentAi; |
---|
2252 | CanonicalForm lcmCont= lcmContent (A, contentAi); |
---|
2253 | A /= lcmCont; |
---|
2254 | |
---|
2255 | // trivial after content removal |
---|
2256 | CFList contentAFactors; |
---|
2257 | if (A.inCoeffDomain()) |
---|
2258 | { |
---|
2259 | for (CFListIterator i= contentAi; i.hasItem(); i++) |
---|
2260 | { |
---|
2261 | if (i.getItem().inCoeffDomain()) |
---|
2262 | continue; |
---|
2263 | else |
---|
2264 | { |
---|
2265 | lcmCont /= i.getItem(); |
---|
2266 | contentAFactors= |
---|
2267 | Union (multiFactorize (lcmCont, info), |
---|
2268 | multiFactorize (i.getItem(), info)); |
---|
2269 | break; |
---|
2270 | } |
---|
2271 | } |
---|
2272 | decompress (contentAFactors, N); |
---|
2273 | normalize (contentAFactors); |
---|
2274 | return contentAFactors; |
---|
2275 | } |
---|
2276 | |
---|
2277 | // factorize content |
---|
2278 | contentAFactors= multiFactorize (lcmCont, info); |
---|
2279 | |
---|
2280 | // univariate after content removal |
---|
2281 | CFList factors; |
---|
2282 | if (A.isUnivariate ()) |
---|
2283 | { |
---|
2284 | factors= uniFactorizer (A, alpha, GF); |
---|
2285 | append (factors, contentAFactors); |
---|
2286 | decompress (factors, N); |
---|
2287 | return factors; |
---|
2288 | } |
---|
2289 | |
---|
2290 | // check main variable |
---|
2291 | int swapLevel= 0; |
---|
2292 | CanonicalForm derivZ; |
---|
2293 | CanonicalForm gcdDerivZ; |
---|
2294 | CanonicalForm bufA= A; |
---|
2295 | Variable z; |
---|
2296 | for (int i= 1; i <= A.level(); i++) |
---|
2297 | { |
---|
2298 | z= Variable (i); |
---|
2299 | derivZ= deriv (bufA, z); |
---|
2300 | if (derivZ.isZero()) |
---|
2301 | { |
---|
2302 | if (i == 1) |
---|
2303 | swapLevel= 1; |
---|
2304 | else |
---|
2305 | continue; |
---|
2306 | } |
---|
2307 | else |
---|
2308 | { |
---|
2309 | if (swapLevel == 1) |
---|
2310 | { |
---|
2311 | swapLevel= i; |
---|
2312 | bufA= swapvar (A, x, z); |
---|
2313 | } |
---|
2314 | gcdDerivZ= gcd (bufA, derivZ); |
---|
2315 | if (degree (gcdDerivZ) > 0 && !derivZ.isZero()) |
---|
2316 | { |
---|
2317 | CanonicalForm g= bufA/gcdDerivZ; |
---|
2318 | CFList factorsG= |
---|
2319 | Union (multiFactorize (g, info), |
---|
2320 | multiFactorize (gcdDerivZ, info)); |
---|
2321 | appendSwapDecompress (factorsG, contentAFactors, N, swapLevel, x); |
---|
2322 | normalize (factorsG); |
---|
2323 | return factorsG; |
---|
2324 | } |
---|
2325 | else |
---|
2326 | { |
---|
2327 | A= bufA; |
---|
2328 | break; |
---|
2329 | } |
---|
2330 | } |
---|
2331 | } |
---|
2332 | |
---|
2333 | |
---|
2334 | CFList Aeval, list, evaluation, bufEvaluation, bufAeval; |
---|
2335 | bool fail= false; |
---|
2336 | int swapLevel2= 0; |
---|
2337 | int level; |
---|
2338 | int factorNums= 3; |
---|
2339 | CanonicalForm bivarEval; |
---|
2340 | CFList biFactors, bufBiFactors; |
---|
2341 | CanonicalForm evalPoly; |
---|
2342 | int lift, bufLift; |
---|
2343 | double logarithm= (double) ilog2 (totaldegree (A)); |
---|
2344 | logarithm /= log2exp; |
---|
2345 | logarithm= ceil (logarithm); |
---|
2346 | if (factorNums < (int) logarithm) |
---|
2347 | factorNums= (int) logarithm; |
---|
2348 | CFList* bufAeval2= new CFList [A.level() - 2]; |
---|
2349 | CFList* Aeval2= new CFList [A.level() - 2]; |
---|
2350 | int counter; |
---|
2351 | int differentSecondVar= 0; |
---|
2352 | // several bivariate factorizations |
---|
2353 | for (int i= 0; i < factorNums; i++) |
---|
2354 | { |
---|
2355 | counter= 0; |
---|
2356 | bufA= A; |
---|
2357 | bufAeval= CFList(); |
---|
2358 | bufEvaluation= evalPoints (bufA, bufAeval, alpha, list, GF, fail); |
---|
2359 | evalPoly= 0; |
---|
2360 | |
---|
2361 | if (fail && (i == 0)) |
---|
2362 | { |
---|
2363 | if (!swapLevel) |
---|
2364 | level= 2; |
---|
2365 | else |
---|
2366 | level= swapLevel + 1; |
---|
2367 | |
---|
2368 | CanonicalForm g; |
---|
2369 | swapLevel2= newMainVariableSearch (A, Aeval, evaluation, alpha, level, g); |
---|
2370 | |
---|
2371 | if (!swapLevel2) // need to pass to an extension |
---|
2372 | { |
---|
2373 | factors= extFactorize (A, info); |
---|
2374 | appendSwapDecompress (factors, contentAFactors, N, swapLevel, x); |
---|
2375 | normalize (factors); |
---|
2376 | delete [] bufAeval2; |
---|
2377 | delete [] Aeval2; |
---|
2378 | return factors; |
---|
2379 | } |
---|
2380 | else |
---|
2381 | { |
---|
2382 | if (swapLevel2 == -1) |
---|
2383 | { |
---|
2384 | CFList factorsG= |
---|
2385 | Union (multiFactorize (g, info), |
---|
2386 | multiFactorize (A/g, info)); |
---|
2387 | appendSwapDecompress (factorsG, contentAFactors, N, swapLevel, x); |
---|
2388 | normalize (factorsG); |
---|
2389 | delete [] bufAeval2; |
---|
2390 | delete [] Aeval2; |
---|
2391 | return factorsG; |
---|
2392 | } |
---|
2393 | fail= false; |
---|
2394 | bufAeval= Aeval; |
---|
2395 | bufA= A; |
---|
2396 | bufEvaluation= evaluation; |
---|
2397 | } |
---|
2398 | } |
---|
2399 | else if (fail && (i > 0)) |
---|
2400 | break; |
---|
2401 | |
---|
2402 | bivarEval= bufEvaluation.getLast(); |
---|
2403 | |
---|
2404 | evaluationWRTDifferentSecondVars (bufAeval2, bufEvaluation, A); |
---|
2405 | |
---|
2406 | for (int j= 0; j < A.level() - 2; j++) |
---|
2407 | { |
---|
2408 | if (!bufAeval2[j].isEmpty()) |
---|
2409 | counter++; |
---|
2410 | } |
---|
2411 | |
---|
2412 | bufLift= degree (A, y) + 1 + degree (LC(A, x), y); |
---|
2413 | |
---|
2414 | TIMING_START (fac_bi_factorizer); |
---|
2415 | if (!GF && alpha.level() == 1) |
---|
2416 | bufBiFactors= FpBiSqrfFactorize (bufAeval.getFirst()); |
---|
2417 | else if (GF) |
---|
2418 | bufBiFactors= GFBiSqrfFactorize (bufAeval.getFirst()); |
---|
2419 | else |
---|
2420 | bufBiFactors= FqBiSqrfFactorize (bufAeval.getFirst(), alpha); |
---|
2421 | TIMING_END_AND_PRINT (fac_bi_factorizer, |
---|
2422 | "time for bivariate factorization: "); |
---|
2423 | bufBiFactors.removeFirst(); |
---|
2424 | |
---|
2425 | if (bufBiFactors.length() == 1) |
---|
2426 | { |
---|
2427 | if (extension) |
---|
2428 | { |
---|
2429 | CFList source, dest; |
---|
2430 | A= mapDown (A, info, source, dest); |
---|
2431 | } |
---|
2432 | factors.append (A); |
---|
2433 | appendSwapDecompress (factors, contentAFactors, N, swapLevel, |
---|
2434 | swapLevel2, x); |
---|
2435 | normalize (factors); |
---|
2436 | delete [] bufAeval2; |
---|
2437 | delete [] Aeval2; |
---|
2438 | return factors; |
---|
2439 | } |
---|
2440 | |
---|
2441 | if (i == 0) |
---|
2442 | { |
---|
2443 | Aeval= bufAeval; |
---|
2444 | evaluation= bufEvaluation; |
---|
2445 | biFactors= bufBiFactors; |
---|
2446 | lift= bufLift; |
---|
2447 | for (int j= 0; j < A.level() - 2; j++) |
---|
2448 | Aeval2 [j]= bufAeval2 [j]; |
---|
2449 | differentSecondVar= counter; |
---|
2450 | } |
---|
2451 | else |
---|
2452 | { |
---|
2453 | if (bufBiFactors.length() < biFactors.length() || |
---|
2454 | ((bufLift < lift) && (bufBiFactors.length() == biFactors.length())) || |
---|
2455 | counter > differentSecondVar) |
---|
2456 | { |
---|
2457 | Aeval= bufAeval; |
---|
2458 | evaluation= bufEvaluation; |
---|
2459 | biFactors= bufBiFactors; |
---|
2460 | lift= bufLift; |
---|
2461 | for (int j= 0; j < A.level() - 2; j++) |
---|
2462 | Aeval2 [j]= bufAeval2 [j]; |
---|
2463 | differentSecondVar= counter; |
---|
2464 | } |
---|
2465 | } |
---|
2466 | int k= 0; |
---|
2467 | for (CFListIterator j= bufEvaluation; j.hasItem(); j++, k++) |
---|
2468 | evalPoly += j.getItem()*power (x, k); |
---|
2469 | list.append (evalPoly); |
---|
2470 | } |
---|
2471 | |
---|
2472 | delete [] bufAeval2; |
---|
2473 | |
---|
2474 | sortList (biFactors, x); |
---|
2475 | |
---|
2476 | int minFactorsLength; |
---|
2477 | bool irred= false; |
---|
2478 | factorizationWRTDifferentSecondVars (A, Aeval2, info, minFactorsLength, irred); |
---|
2479 | |
---|
2480 | if (irred) |
---|
2481 | { |
---|
2482 | if (extension) |
---|
2483 | { |
---|
2484 | CFList source, dest; |
---|
2485 | A= mapDown (A, info, source, dest); |
---|
2486 | } |
---|
2487 | factors.append (A); |
---|
2488 | appendSwapDecompress (factors, contentAFactors, N, swapLevel, |
---|
2489 | swapLevel2, x); |
---|
2490 | normalize (factors); |
---|
2491 | delete [] Aeval2; |
---|
2492 | return factors; |
---|
2493 | } |
---|
2494 | |
---|
2495 | if (minFactorsLength == 0) |
---|
2496 | minFactorsLength= biFactors.length(); |
---|
2497 | else if (biFactors.length() > minFactorsLength) |
---|
2498 | refineBiFactors (A, biFactors, Aeval2, evaluation, minFactorsLength); |
---|
2499 | minFactorsLength= tmin (minFactorsLength, biFactors.length()); |
---|
2500 | |
---|
2501 | if (differentSecondVar == A.level() - 2) |
---|
2502 | { |
---|
2503 | bool zeroOccured= false; |
---|
2504 | for (CFListIterator iter= evaluation; iter.hasItem(); iter++) |
---|
2505 | { |
---|
2506 | if (iter.getItem().isZero()) |
---|
2507 | { |
---|
2508 | zeroOccured= true; |
---|
2509 | break; |
---|
2510 | } |
---|
2511 | } |
---|
2512 | if (!zeroOccured) |
---|
2513 | { |
---|
2514 | factors= sparseHeuristic (A, biFactors, Aeval2, evaluation, minFactorsLength); |
---|
2515 | if (factors.length() == biFactors.length()) |
---|
2516 | { |
---|
2517 | if (extension) |
---|
2518 | factors= extNonMonicFactorRecombination (factors, A, info); |
---|
2519 | |
---|
2520 | appendSwapDecompress (factors, contentAFactors, N, swapLevel, |
---|
2521 | swapLevel2, x); |
---|
2522 | normalize (factors); |
---|
2523 | delete [] Aeval2; |
---|
2524 | return factors; |
---|
2525 | } |
---|
2526 | else |
---|
2527 | factors= CFList(); |
---|
2528 | //TODO case where factors.length() > 0 |
---|
2529 | } |
---|
2530 | } |
---|
2531 | |
---|
2532 | CFList uniFactors= buildUniFactors (biFactors, evaluation.getLast(), y); |
---|
2533 | |
---|
2534 | CFList * oldAeval= new CFList [A.level() - 2]; //TODO use bufAeval2 for this |
---|
2535 | for (int i= 0; i < A.level() - 2; i++) |
---|
2536 | oldAeval[i]= Aeval2[i]; |
---|
2537 | |
---|
2538 | getLeadingCoeffs (A, Aeval2, uniFactors, evaluation); |
---|
2539 | |
---|
2540 | CFList biFactorsLCs; |
---|
2541 | for (CFListIterator i= biFactors; i.hasItem(); i++) |
---|
2542 | biFactorsLCs.append (LC (i.getItem(), 1)); |
---|
2543 | |
---|
2544 | Variable v; |
---|
2545 | CFList leadingCoeffs= precomputeLeadingCoeff (LC (A, 1), biFactorsLCs, alpha, |
---|
2546 | evaluation, Aeval2, A.level() - 2, v); |
---|
2547 | |
---|
2548 | if (v.level() != 1) |
---|
2549 | { |
---|
2550 | A= swapvar (A, y, v); |
---|
2551 | for (int i= 0; i < A.level() - 2; i++) |
---|
2552 | { |
---|
2553 | if (oldAeval[i].isEmpty()) |
---|
2554 | continue; |
---|
2555 | if (oldAeval[i].getFirst().level() == v.level()) |
---|
2556 | { |
---|
2557 | biFactors= CFList(); |
---|
2558 | for (CFListIterator iter= oldAeval [i]; iter.hasItem(); iter++) |
---|
2559 | biFactors.append (swapvar (iter.getItem(), v, y)); |
---|
2560 | } |
---|
2561 | } |
---|
2562 | int i= A.level(); |
---|
2563 | CanonicalForm evalPoint; |
---|
2564 | for (CFListIterator iter= evaluation; iter.hasItem(); iter++, i--) |
---|
2565 | { |
---|
2566 | if (i == v.level()) |
---|
2567 | { |
---|
2568 | evalPoint= iter.getItem(); |
---|
2569 | iter.getItem()= evaluation.getLast(); |
---|
2570 | evaluation.removeLast(); |
---|
2571 | evaluation.append (evalPoint); |
---|
2572 | break; |
---|
2573 | } |
---|
2574 | } |
---|
2575 | } |
---|
2576 | |
---|
2577 | CFListIterator iter; |
---|
2578 | CanonicalForm oldA= A; |
---|
2579 | CFList oldBiFactors= biFactors; |
---|
2580 | if (!leadingCoeffs.getFirst().inCoeffDomain()) |
---|
2581 | { |
---|
2582 | CanonicalForm tmp= power (leadingCoeffs.getFirst(), biFactors.length() - 1); |
---|
2583 | A *= tmp; |
---|
2584 | tmp= leadingCoeffs.getFirst(); |
---|
2585 | iter= evaluation; |
---|
2586 | for (int i= A.level(); i > 2; i--, iter++) |
---|
2587 | tmp= tmp (iter.getItem(), i); |
---|
2588 | if (!tmp.inCoeffDomain()) |
---|
2589 | { |
---|
2590 | for (CFListIterator i= biFactors; i.hasItem(); i++) |
---|
2591 | { |
---|
2592 | i.getItem() *= tmp/LC (i.getItem(), 1); |
---|
2593 | i.getItem() /= Lc (i.getItem()); |
---|
2594 | } |
---|
2595 | } |
---|
2596 | } |
---|
2597 | |
---|
2598 | leadingCoeffs.removeFirst(); |
---|
2599 | |
---|
2600 | //prepare leading coefficients |
---|
2601 | CFList* leadingCoeffs2= new CFList [A.level() - 2]; |
---|
2602 | prepareLeadingCoeffs (leadingCoeffs2, A.level(), leadingCoeffs, biFactors, |
---|
2603 | evaluation); |
---|
2604 | |
---|
2605 | Aeval= evaluateAtEval (A, evaluation, 2); |
---|
2606 | CanonicalForm hh= Lc (Aeval.getFirst()); |
---|
2607 | for (iter= Aeval; iter.hasItem(); iter++) |
---|
2608 | iter.getItem() /= hh; |
---|
2609 | |
---|
2610 | A /= hh; |
---|
2611 | |
---|
2612 | if (size (A)/getNumVars (A) < 500 && |
---|
2613 | LucksWangSparseHeuristic (A, biFactors, 2, leadingCoeffs2 [A.level() - 3], |
---|
2614 | factors)) |
---|
2615 | { |
---|
2616 | int check= factors.length(); |
---|
2617 | factors= recoverFactors (A, factors); |
---|
2618 | |
---|
2619 | if (check == factors.length()) |
---|
2620 | { |
---|
2621 | if (extension) |
---|
2622 | factors= extNonMonicFactorRecombination (factors, A, info); |
---|
2623 | |
---|
2624 | if (v.level() != 1) |
---|
2625 | { |
---|
2626 | for (CFListIterator iter= factors; iter.hasItem(); iter++) |
---|
2627 | iter.getItem()= swapvar (iter.getItem(), v, y); |
---|
2628 | } |
---|
2629 | appendSwapDecompress (factors, contentAFactors, N, swapLevel, |
---|
2630 | swapLevel2, x); |
---|
2631 | normalize (factors); |
---|
2632 | delete [] Aeval2; |
---|
2633 | return factors; |
---|
2634 | } |
---|
2635 | else |
---|
2636 | factors= CFList(); |
---|
2637 | //TODO handle this case |
---|
2638 | } |
---|
2639 | |
---|
2640 | A= shift2Zero (A, Aeval, evaluation); |
---|
2641 | |
---|
2642 | for (iter= biFactors; iter.hasItem(); iter++) |
---|
2643 | iter.getItem()= iter.getItem () (y + evaluation.getLast(), y); |
---|
2644 | |
---|
2645 | for (int i= 0; i < A.level() - 2; i++) |
---|
2646 | { |
---|
2647 | if (i != A.level() - 3) |
---|
2648 | leadingCoeffs2[i]= CFList(); |
---|
2649 | } |
---|
2650 | for (iter= leadingCoeffs2[A.level() - 3]; iter.hasItem(); iter++) |
---|
2651 | { |
---|
2652 | iter.getItem()= shift2Zero (iter.getItem(), list, evaluation); |
---|
2653 | for (int i= A.level() - 4; i > -1; i--) |
---|
2654 | { |
---|
2655 | if (i + 1 == A.level() - 3) |
---|
2656 | leadingCoeffs2[i].append (iter.getItem() (0, i + 4)); |
---|
2657 | else |
---|
2658 | leadingCoeffs2[i].append (leadingCoeffs2[i+1].getLast() (0, i + 4)); |
---|
2659 | } |
---|
2660 | } |
---|
2661 | |
---|
2662 | CFArray Pi; |
---|
2663 | CFList diophant; |
---|
2664 | int* liftBounds= new int [A.level() - 1]; |
---|
2665 | int liftBoundsLength= A.level() - 1; |
---|
2666 | for (int i= 0; i < liftBoundsLength; i++) |
---|
2667 | liftBounds [i]= degree (A, i + 2) + 1; |
---|
2668 | |
---|
2669 | Aeval.removeFirst(); |
---|
2670 | bool noOneToOne= false; |
---|
2671 | factors= nonMonicHenselLift (Aeval, biFactors, leadingCoeffs2, diophant, |
---|
2672 | Pi, liftBounds, liftBoundsLength, noOneToOne); |
---|
2673 | |
---|
2674 | if (!noOneToOne) |
---|
2675 | { |
---|
2676 | int check= factors.length(); |
---|
2677 | A= oldA; |
---|
2678 | factors= recoverFactors (A, factors, evaluation); |
---|
2679 | if (check != factors.length()) |
---|
2680 | noOneToOne= true; |
---|
2681 | |
---|
2682 | if (extension && !noOneToOne) |
---|
2683 | factors= extNonMonicFactorRecombination (factors, A, info); |
---|
2684 | } |
---|
2685 | if (noOneToOne) |
---|
2686 | { |
---|
2687 | A= shift2Zero (oldA, Aeval, evaluation); |
---|
2688 | biFactors= oldBiFactors; |
---|
2689 | for (iter= biFactors; iter.hasItem(); iter++) |
---|
2690 | iter.getItem()= iter.getItem () (y + evaluation.getLast(), y); |
---|
2691 | CanonicalForm LCA= LC (Aeval.getFirst(), 1); |
---|
2692 | CanonicalForm yToLift= power (y, lift); |
---|
2693 | CFListIterator i= biFactors; |
---|
2694 | lift= degree (i.getItem(), 2) + degree (LC (i.getItem(), 1)) + 1; |
---|
2695 | i++; |
---|
2696 | |
---|
2697 | for (; i.hasItem(); i++) |
---|
2698 | lift= tmax (lift, degree (i.getItem(), 2) + degree (LC (i.getItem(), 1)) + 1); |
---|
2699 | |
---|
2700 | lift= tmax (degree (Aeval.getFirst() , 2) + 1, lift); |
---|
2701 | |
---|
2702 | i= biFactors; |
---|
2703 | yToLift= power (y, lift); |
---|
2704 | CanonicalForm dummy; |
---|
2705 | for (; i.hasItem(); i++) |
---|
2706 | { |
---|
2707 | LCA= LC (i.getItem(), 1); |
---|
2708 | extgcd (LCA, yToLift, LCA, dummy); |
---|
2709 | i.getItem()= mod (i.getItem()*LCA, yToLift); |
---|
2710 | } |
---|
2711 | |
---|
2712 | liftBoundsLength= F.level() - 1; |
---|
2713 | liftBounds= liftingBounds (A, lift); |
---|
2714 | |
---|
2715 | CFList MOD; |
---|
2716 | bool earlySuccess; |
---|
2717 | CFList earlyFactors, liftedFactors; |
---|
2718 | TIMING_START (fac_hensel_lift); |
---|
2719 | liftedFactors= henselLiftAndEarly |
---|
2720 | (A, MOD, liftBounds, earlySuccess, earlyFactors, |
---|
2721 | Aeval, biFactors, evaluation, info); |
---|
2722 | TIMING_END_AND_PRINT (fac_hensel_lift, "time for hensel lifting: "); |
---|
2723 | |
---|
2724 | if (!extension) |
---|
2725 | { |
---|
2726 | TIMING_START (fac_factor_recombination); |
---|
2727 | factors= factorRecombination (A, liftedFactors, MOD); |
---|
2728 | TIMING_END_AND_PRINT (fac_factor_recombination, |
---|
2729 | "time for factor recombination: "); |
---|
2730 | } |
---|
2731 | else |
---|
2732 | { |
---|
2733 | TIMING_START (fac_factor_recombination); |
---|
2734 | factors= extFactorRecombination (liftedFactors, A, MOD, info, evaluation); |
---|
2735 | TIMING_END_AND_PRINT (fac_factor_recombination, |
---|
2736 | "time for factor recombination: "); |
---|
2737 | } |
---|
2738 | |
---|
2739 | if (earlySuccess) |
---|
2740 | factors= Union (factors, earlyFactors); |
---|
2741 | if (!extension) |
---|
2742 | { |
---|
2743 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
2744 | { |
---|
2745 | int kk= Aeval.getLast().level(); |
---|
2746 | for (CFListIterator j= evaluation; j.hasItem(); j++, kk--) |
---|
2747 | { |
---|
2748 | if (i.getItem().level() < kk) |
---|
2749 | continue; |
---|
2750 | i.getItem()= i.getItem() (Variable (kk) - j.getItem(), kk); |
---|
2751 | } |
---|
2752 | } |
---|
2753 | } |
---|
2754 | } |
---|
2755 | |
---|
2756 | if (v.level() != 1) |
---|
2757 | { |
---|
2758 | for (CFListIterator iter= factors; iter.hasItem(); iter++) |
---|
2759 | iter.getItem()= swapvar (iter.getItem(), v, y); |
---|
2760 | } |
---|
2761 | |
---|
2762 | swap (factors, swapLevel, swapLevel2, x); |
---|
2763 | append (factors, contentAFactors); |
---|
2764 | decompress (factors, N); |
---|
2765 | normalize (factors); |
---|
2766 | |
---|
2767 | delete[] liftBounds; |
---|
2768 | |
---|
2769 | return factors; |
---|
2770 | } |
---|
2771 | |
---|
2772 | /// multivariate factorization over an extension of the initial field |
---|
2773 | CFList |
---|
2774 | extFactorize (const CanonicalForm& F, const ExtensionInfo& info) |
---|
2775 | { |
---|
2776 | CanonicalForm A= F; |
---|
2777 | |
---|
2778 | Variable alpha= info.getAlpha(); |
---|
2779 | Variable beta= info.getBeta(); |
---|
2780 | int k= info.getGFDegree(); |
---|
2781 | char cGFName= info.getGFName(); |
---|
2782 | CanonicalForm delta= info.getDelta(); |
---|
2783 | bool GF= (CFFactory::gettype() == GaloisFieldDomain); |
---|
2784 | Variable w= Variable (1); |
---|
2785 | |
---|
2786 | CFList factors; |
---|
2787 | if (!GF && alpha == w) // we are in F_p |
---|
2788 | { |
---|
2789 | CFList factors; |
---|
2790 | bool extension= true; |
---|
2791 | int p= getCharacteristic(); |
---|
2792 | if (p*p < (1<<16)) // pass to GF if possible |
---|
2793 | { |
---|
2794 | setCharacteristic (getCharacteristic(), 2, 'Z'); |
---|
2795 | ExtensionInfo info= ExtensionInfo (extension); |
---|
2796 | A= A.mapinto(); |
---|
2797 | factors= multiFactorize (A, info); |
---|
2798 | |
---|
2799 | Variable vBuf= rootOf (gf_mipo); |
---|
2800 | setCharacteristic (getCharacteristic()); |
---|
2801 | for (CFListIterator j= factors; j.hasItem(); j++) |
---|
2802 | j.getItem()= GF2FalphaRep (j.getItem(), vBuf); |
---|
2803 | } |
---|
2804 | else // not able to pass to GF, pass to F_p(\alpha) |
---|
2805 | { |
---|
2806 | CanonicalForm mipo= randomIrredpoly (2, w); |
---|
2807 | Variable v= rootOf (mipo); |
---|
2808 | ExtensionInfo info= ExtensionInfo (v); |
---|
2809 | factors= multiFactorize (A, info); |
---|
2810 | } |
---|
2811 | return factors; |
---|
2812 | } |
---|
2813 | else if (!GF && (alpha != w)) // we are in F_p(\alpha) |
---|
2814 | { |
---|
2815 | if (k == 1) // need factorization over F_p |
---|
2816 | { |
---|
2817 | int extDeg= degree (getMipo (alpha)); |
---|
2818 | extDeg++; |
---|
2819 | CanonicalForm mipo= randomIrredpoly (extDeg + 1, w); |
---|
2820 | Variable v= rootOf (mipo); |
---|
2821 | ExtensionInfo info= ExtensionInfo (v); |
---|
2822 | factors= multiFactorize (A, info); |
---|
2823 | } |
---|
2824 | else |
---|
2825 | { |
---|
2826 | if (beta == w) |
---|
2827 | { |
---|
2828 | Variable v= chooseExtension (alpha, beta, k); |
---|
2829 | CanonicalForm primElem, imPrimElem; |
---|
2830 | bool primFail= false; |
---|
2831 | Variable vBuf; |
---|
2832 | primElem= primitiveElement (alpha, vBuf, primFail); |
---|
2833 | ASSERT (!primFail, "failure in integer factorizer"); |
---|
2834 | if (primFail) |
---|
2835 | ; //ERROR |
---|
2836 | else |
---|
2837 | imPrimElem= mapPrimElem (primElem, vBuf, v); |
---|
2838 | |
---|
2839 | CFList source, dest; |
---|
2840 | CanonicalForm bufA= mapUp (A, alpha, v, primElem, imPrimElem, |
---|
2841 | source, dest); |
---|
2842 | ExtensionInfo info= ExtensionInfo (v, alpha, imPrimElem, primElem); |
---|
2843 | factors= multiFactorize (bufA, info); |
---|
2844 | } |
---|
2845 | else |
---|
2846 | { |
---|
2847 | Variable v= chooseExtension (alpha, beta, k); |
---|
2848 | CanonicalForm primElem, imPrimElem; |
---|
2849 | bool primFail= false; |
---|
2850 | Variable vBuf; |
---|
2851 | ASSERT (!primFail, "failure in integer factorizer"); |
---|
2852 | if (primFail) |
---|
2853 | ; //ERROR |
---|
2854 | else |
---|
2855 | imPrimElem= mapPrimElem (delta, beta, v); |
---|
2856 | |
---|
2857 | CFList source, dest; |
---|
2858 | CanonicalForm bufA= mapDown (A, info, source, dest); |
---|
2859 | source= CFList(); |
---|
2860 | dest= CFList(); |
---|
2861 | bufA= mapUp (bufA, beta, v, delta, imPrimElem, source, dest); |
---|
2862 | ExtensionInfo info= ExtensionInfo (v, beta, imPrimElem, delta); |
---|
2863 | factors= multiFactorize (bufA, info); |
---|
2864 | } |
---|
2865 | } |
---|
2866 | return factors; |
---|
2867 | } |
---|
2868 | else // we are in GF (p^k) |
---|
2869 | { |
---|
2870 | int p= getCharacteristic(); |
---|
2871 | int extensionDeg= getGFDegree(); |
---|
2872 | bool extension= true; |
---|
2873 | if (k == 1) // need factorization over F_p |
---|
2874 | { |
---|
2875 | extensionDeg++; |
---|
2876 | if (pow ((double) p, (double) extensionDeg) < (1<<16)) |
---|
2877 | // pass to GF(p^k+1) |
---|
2878 | { |
---|
2879 | setCharacteristic (p); |
---|
2880 | Variable vBuf= rootOf (gf_mipo); |
---|
2881 | A= GF2FalphaRep (A, vBuf); |
---|
2882 | setCharacteristic (p, extensionDeg, 'Z'); |
---|
2883 | ExtensionInfo info= ExtensionInfo (extension); |
---|
2884 | factors= multiFactorize (A.mapinto(), info); |
---|
2885 | } |
---|
2886 | else // not able to pass to another GF, pass to F_p(\alpha) |
---|
2887 | { |
---|
2888 | setCharacteristic (p); |
---|
2889 | Variable vBuf= rootOf (gf_mipo); |
---|
2890 | A= GF2FalphaRep (A, vBuf); |
---|
2891 | Variable v= chooseExtension (vBuf, beta, k); |
---|
2892 | ExtensionInfo info= ExtensionInfo (v, extension); |
---|
2893 | factors= multiFactorize (A, info); |
---|
2894 | } |
---|
2895 | } |
---|
2896 | else // need factorization over GF (p^k) |
---|
2897 | { |
---|
2898 | if (pow ((double) p, (double) 2*extensionDeg) < (1<<16)) |
---|
2899 | // pass to GF(p^2k) |
---|
2900 | { |
---|
2901 | setCharacteristic (p, 2*extensionDeg, 'Z'); |
---|
2902 | ExtensionInfo info= ExtensionInfo (k, cGFName, extension); |
---|
2903 | factors= multiFactorize (GFMapUp (A, extensionDeg), info); |
---|
2904 | setCharacteristic (p, extensionDeg, cGFName); |
---|
2905 | } |
---|
2906 | else // not able to pass to GF (p^2k), pass to F_p (\alpha) |
---|
2907 | { |
---|
2908 | setCharacteristic (p); |
---|
2909 | Variable v1= rootOf (gf_mipo); |
---|
2910 | A= GF2FalphaRep (A, v1); |
---|
2911 | Variable v2= chooseExtension (v1, v1, k); |
---|
2912 | CanonicalForm primElem, imPrimElem; |
---|
2913 | bool primFail= false; |
---|
2914 | Variable vBuf; |
---|
2915 | primElem= primitiveElement (v1, v1, primFail); |
---|
2916 | if (primFail) |
---|
2917 | ; //ERROR |
---|
2918 | else |
---|
2919 | imPrimElem= mapPrimElem (primElem, v1, v2); |
---|
2920 | CFList source, dest; |
---|
2921 | CanonicalForm bufA= mapUp (A, v1, v2, primElem, imPrimElem, |
---|
2922 | source, dest); |
---|
2923 | ExtensionInfo info= ExtensionInfo (v2, v1, imPrimElem, primElem); |
---|
2924 | factors= multiFactorize (bufA, info); |
---|
2925 | setCharacteristic (p, k, cGFName); |
---|
2926 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
2927 | i.getItem()= Falpha2GFRep (i.getItem()); |
---|
2928 | } |
---|
2929 | } |
---|
2930 | return factors; |
---|
2931 | } |
---|
2932 | } |
---|
2933 | |
---|
2934 | #endif |
---|
2935 | /* HAVE_NTL */ |
---|
2936 | |
---|