1 | /* Emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id$ */ |
---|
3 | |
---|
4 | //{{{ docu |
---|
5 | // |
---|
6 | // cf_factor.cc - factorization and square free algorithms. |
---|
7 | // |
---|
8 | // Used by: fac_multivar.cc, fac_univar.cc, cf_irred.cc |
---|
9 | // |
---|
10 | // Header file: cf_algorithm.h |
---|
11 | // |
---|
12 | //}}} |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | |
---|
16 | #include "cf_assert.h" |
---|
17 | |
---|
18 | #include "cf_defs.h" |
---|
19 | #include "canonicalform.h" |
---|
20 | #include "cf_iter.h" |
---|
21 | #include "fac_berlekamp.h" |
---|
22 | #include "fac_cantzass.h" |
---|
23 | #include "fac_univar.h" |
---|
24 | #include "fac_multivar.h" |
---|
25 | #include "fac_sqrfree.h" |
---|
26 | #include "cf_algorithm.h" |
---|
27 | #include "facFqFactorize.h" |
---|
28 | #include "facFqSquarefree.h" |
---|
29 | #include "cf_map.h" |
---|
30 | #include "algext.h" |
---|
31 | #include "facAlgExt.h" |
---|
32 | #include "facFactorize.h" |
---|
33 | #include "singext.h" |
---|
34 | #include "cf_util.h" |
---|
35 | |
---|
36 | #include "int_int.h" |
---|
37 | #ifdef HAVE_NTL |
---|
38 | #include "NTLconvert.h" |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <factory/cf_gmp.h> |
---|
42 | #ifdef HAVE_FLINT |
---|
43 | #include "FLINTconvert.h" |
---|
44 | #endif |
---|
45 | |
---|
46 | int getExp(); /* cf_char.cc */ |
---|
47 | |
---|
48 | //static bool isUnivariateBaseDomain( const CanonicalForm & f ) |
---|
49 | //{ |
---|
50 | // CFIterator i = f; |
---|
51 | // bool ok = i.coeff().inBaseDomain(); |
---|
52 | // i++; |
---|
53 | // while ( i.hasTerms() && ( ok = ok && i.coeff().inBaseDomain() ) ) i++; |
---|
54 | // return ok; |
---|
55 | //} |
---|
56 | |
---|
57 | void find_exp(const CanonicalForm & f, int * exp_f) |
---|
58 | { |
---|
59 | if ( ! f.inCoeffDomain() ) |
---|
60 | { |
---|
61 | int e=f.level(); |
---|
62 | CFIterator i = f; |
---|
63 | if (e>=0) |
---|
64 | { |
---|
65 | if (i.exp() > exp_f[e]) exp_f[e]=i.exp(); |
---|
66 | } |
---|
67 | for (; i.hasTerms(); i++ ) |
---|
68 | { |
---|
69 | find_exp(i.coeff(), exp_f); |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | int find_mvar(const CanonicalForm & f) |
---|
75 | { |
---|
76 | int mv=f.level(); |
---|
77 | int *exp_f=new int[mv+1]; |
---|
78 | int i; |
---|
79 | for(i=mv;i>0;i--) exp_f[i]=0; |
---|
80 | find_exp(f,exp_f); |
---|
81 | for(i=mv;i>0;i--) |
---|
82 | { |
---|
83 | if ((exp_f[i]>0) && (exp_f[i]<exp_f[mv])) |
---|
84 | { |
---|
85 | mv=i; |
---|
86 | } |
---|
87 | } |
---|
88 | delete[] exp_f; |
---|
89 | return mv; |
---|
90 | } |
---|
91 | |
---|
92 | #if 0 |
---|
93 | //#ifndef NOSTREAMIO |
---|
94 | void out_cf(const char *s1,const CanonicalForm &f,const char *s2) |
---|
95 | { |
---|
96 | printf("%s",s1); |
---|
97 | if (f.isZero()) printf("+0"); |
---|
98 | //else if (! f.inCoeffDomain() ) |
---|
99 | else if (! f.inBaseDomain() ) |
---|
100 | { |
---|
101 | int l = f.level(); |
---|
102 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
103 | { |
---|
104 | int e=i.exp(); |
---|
105 | if (i.coeff().isOne()) |
---|
106 | { |
---|
107 | printf("+"); |
---|
108 | if (e==0) printf("1"); |
---|
109 | else |
---|
110 | { |
---|
111 | printf("v(%d)",l); |
---|
112 | if (e!=1) printf("^%d",e); |
---|
113 | } |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | out_cf("+(",i.coeff(),")"); |
---|
118 | if (e!=0) |
---|
119 | { |
---|
120 | printf("*v(%d)",l); |
---|
121 | if (e!=1) printf("^%d",e); |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | if ( f.isImm() ) |
---|
129 | { |
---|
130 | printf("+%d",f.intval()); |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | #ifdef NOSTREAMIO |
---|
135 | if (f.inZ()) |
---|
136 | { |
---|
137 | mpz_t m; |
---|
138 | gmp_numerator(f,m); |
---|
139 | char * str = new char[mpz_sizeinbase( m, 10 ) + 2]; |
---|
140 | str = mpz_get_str( str, 10, m ); |
---|
141 | printf("%s",str); |
---|
142 | delete[] str; |
---|
143 | mpz_clear(m); |
---|
144 | } |
---|
145 | else if (f.inQ()) |
---|
146 | { |
---|
147 | mpz_t m; |
---|
148 | gmp_numerator(f,m); |
---|
149 | char * str = new char[mpz_sizeinbase( m, 10 ) + 2]; |
---|
150 | str = mpz_get_str( str, 10, m ); |
---|
151 | printf("%s/",str); |
---|
152 | delete[] str; |
---|
153 | mpz_clear(m); |
---|
154 | gmp_denominator(f,m); |
---|
155 | str = new char[mpz_sizeinbase( m, 10 ) + 2]; |
---|
156 | str = mpz_get_str( str, 10, m ); |
---|
157 | printf("%s",str); |
---|
158 | delete[] str; |
---|
159 | mpz_clear(m); |
---|
160 | } |
---|
161 | #else |
---|
162 | std::cout << f; |
---|
163 | #endif |
---|
164 | } |
---|
165 | //if (f.inZ()) printf("(Z)"); |
---|
166 | //else if (f.inQ()) printf("(Q)"); |
---|
167 | //else if (f.inFF()) printf("(FF)"); |
---|
168 | //else if (f.inPP()) printf("(PP)"); |
---|
169 | //else if (f.inGF()) printf("(PP)"); |
---|
170 | //else |
---|
171 | if (f.inExtension()) printf("E(%d)",f.level()); |
---|
172 | } |
---|
173 | printf("%s",s2); |
---|
174 | } |
---|
175 | void out_cff(CFFList &L) |
---|
176 | { |
---|
177 | //int n = L.length(); |
---|
178 | CFFListIterator J=L; |
---|
179 | int j=0; |
---|
180 | for ( ; J.hasItem(); J++, j++ ) |
---|
181 | { |
---|
182 | printf("F%d",j);out_cf(":",J.getItem().factor()," ^ "); |
---|
183 | printf("%d\n", J.getItem().exp()); |
---|
184 | } |
---|
185 | } |
---|
186 | void test_cff(CFFList &L,const CanonicalForm & f) |
---|
187 | { |
---|
188 | //int n = L.length(); |
---|
189 | CFFListIterator J=L; |
---|
190 | CanonicalForm t=1; |
---|
191 | int j=0; |
---|
192 | if (!(L.getFirst().factor().inCoeffDomain())) |
---|
193 | printf("first entry is not const\n"); |
---|
194 | for ( ; J.hasItem(); J++, j++ ) |
---|
195 | { |
---|
196 | CanonicalForm tt=J.getItem().factor(); |
---|
197 | if (tt.inCoeffDomain() && (j!=0)) |
---|
198 | printf("other entry is const\n"); |
---|
199 | j=J.getItem().exp(); |
---|
200 | while(j>0) { t*=tt; j--; } |
---|
201 | } |
---|
202 | if (!(f-t).isZero()) { printf("problem:\n");out_cf("factor:",f," has problems\n");} |
---|
203 | } |
---|
204 | //#endif |
---|
205 | #endif |
---|
206 | |
---|
207 | bool isPurePoly_m(const CanonicalForm & f) |
---|
208 | { |
---|
209 | if (f.inBaseDomain()) return true; |
---|
210 | if (f.level()<0) return false; |
---|
211 | for (CFIterator i=f;i.hasTerms();i++) |
---|
212 | { |
---|
213 | if (!isPurePoly_m(i.coeff())) return false; |
---|
214 | } |
---|
215 | return true; |
---|
216 | } |
---|
217 | bool isPurePoly(const CanonicalForm & f) |
---|
218 | { |
---|
219 | if (f.level()<=0) return false; |
---|
220 | for (CFIterator i=f;i.hasTerms();i++) |
---|
221 | { |
---|
222 | if (!(i.coeff().inBaseDomain())) return false; |
---|
223 | } |
---|
224 | return true; |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | /////////////////////////////////////////////////////////////// |
---|
229 | // get_max_degree_Variable returns Variable with // |
---|
230 | // highest degree. We assume f is *not* a constant! // |
---|
231 | /////////////////////////////////////////////////////////////// |
---|
232 | Variable |
---|
233 | get_max_degree_Variable(const CanonicalForm & f) |
---|
234 | { |
---|
235 | ASSERT( ( ! f.inCoeffDomain() ), "no constants" ); |
---|
236 | int max=0, maxlevel=0, n=level(f); |
---|
237 | for ( int i=1; i<=n; i++ ) |
---|
238 | { |
---|
239 | if (degree(f,Variable(i)) >= max) |
---|
240 | { |
---|
241 | max= degree(f,Variable(i)); maxlevel= i; |
---|
242 | } |
---|
243 | } |
---|
244 | return Variable(maxlevel); |
---|
245 | } |
---|
246 | |
---|
247 | /////////////////////////////////////////////////////////////// |
---|
248 | // get_Terms: Split the polynomial in the containing terms. // |
---|
249 | // getTerms: the real work is done here. // |
---|
250 | /////////////////////////////////////////////////////////////// |
---|
251 | void |
---|
252 | getTerms( const CanonicalForm & f, const CanonicalForm & t, CFList & result ) |
---|
253 | { |
---|
254 | if ( getNumVars(f) == 0 ) result.append(f*t); |
---|
255 | else{ |
---|
256 | Variable x(level(f)); |
---|
257 | for ( CFIterator i=f; i.hasTerms(); i++ ) |
---|
258 | getTerms( i.coeff(), t*power(x,i.exp()), result); |
---|
259 | } |
---|
260 | } |
---|
261 | CFList |
---|
262 | get_Terms( const CanonicalForm & f ){ |
---|
263 | CFList result,dummy,dummy2; |
---|
264 | CFIterator i; |
---|
265 | CFListIterator j; |
---|
266 | |
---|
267 | if ( getNumVars(f) == 0 ) result.append(f); |
---|
268 | else{ |
---|
269 | Variable _x(level(f)); |
---|
270 | for ( i=f; i.hasTerms(); i++ ){ |
---|
271 | getTerms(i.coeff(), 1, dummy); |
---|
272 | for ( j=dummy; j.hasItem(); j++ ) |
---|
273 | result.append(j.getItem() * power(_x, i.exp())); |
---|
274 | |
---|
275 | dummy= dummy2; // have to initalize new |
---|
276 | } |
---|
277 | } |
---|
278 | return result; |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | /////////////////////////////////////////////////////////////// |
---|
283 | // homogenize homogenizes f with Variable x // |
---|
284 | /////////////////////////////////////////////////////////////// |
---|
285 | |
---|
286 | CanonicalForm |
---|
287 | homogenize( const CanonicalForm & f, const Variable & x) |
---|
288 | { |
---|
289 | #if 0 |
---|
290 | int maxdeg=totaldegree(f), deg; |
---|
291 | CFIterator i; |
---|
292 | CanonicalForm elem, result(0); |
---|
293 | |
---|
294 | for (i=f; i.hasTerms(); i++) |
---|
295 | { |
---|
296 | elem= i.coeff()*power(f.mvar(),i.exp()); |
---|
297 | deg = totaldegree(elem); |
---|
298 | if ( deg < maxdeg ) |
---|
299 | result += elem * power(x,maxdeg-deg); |
---|
300 | else |
---|
301 | result+=elem; |
---|
302 | } |
---|
303 | return result; |
---|
304 | #else |
---|
305 | CFList Newlist, Termlist= get_Terms(f); |
---|
306 | int maxdeg=totaldegree(f), deg; |
---|
307 | CFListIterator i; |
---|
308 | CanonicalForm elem, result(0); |
---|
309 | |
---|
310 | for (i=Termlist; i.hasItem(); i++) |
---|
311 | { |
---|
312 | elem= i.getItem(); |
---|
313 | deg = totaldegree(elem); |
---|
314 | if ( deg < maxdeg ) |
---|
315 | Newlist.append(elem * power(x,maxdeg-deg)); |
---|
316 | else |
---|
317 | Newlist.append(elem); |
---|
318 | } |
---|
319 | for (i=Newlist; i.hasItem(); i++) // rebuild |
---|
320 | result += i.getItem(); |
---|
321 | |
---|
322 | return result; |
---|
323 | #endif |
---|
324 | } |
---|
325 | |
---|
326 | CanonicalForm |
---|
327 | homogenize( const CanonicalForm & f, const Variable & x, const Variable & v1, const Variable & v2) |
---|
328 | { |
---|
329 | #if 0 |
---|
330 | int maxdeg=totaldegree(f), deg; |
---|
331 | CFIterator i; |
---|
332 | CanonicalForm elem, result(0); |
---|
333 | |
---|
334 | for (i=f; i.hasTerms(); i++) |
---|
335 | { |
---|
336 | elem= i.coeff()*power(f.mvar(),i.exp()); |
---|
337 | deg = totaldegree(elem); |
---|
338 | if ( deg < maxdeg ) |
---|
339 | result += elem * power(x,maxdeg-deg); |
---|
340 | else |
---|
341 | result+=elem; |
---|
342 | } |
---|
343 | return result; |
---|
344 | #else |
---|
345 | CFList Newlist, Termlist= get_Terms(f); |
---|
346 | int maxdeg=totaldegree(f), deg; |
---|
347 | CFListIterator i; |
---|
348 | CanonicalForm elem, result(0); |
---|
349 | |
---|
350 | for (i=Termlist; i.hasItem(); i++) |
---|
351 | { |
---|
352 | elem= i.getItem(); |
---|
353 | deg = totaldegree(elem,v1,v2); |
---|
354 | if ( deg < maxdeg ) |
---|
355 | Newlist.append(elem * power(x,maxdeg-deg)); |
---|
356 | else |
---|
357 | Newlist.append(elem); |
---|
358 | } |
---|
359 | for (i=Newlist; i.hasItem(); i++) // rebuild |
---|
360 | result += i.getItem(); |
---|
361 | |
---|
362 | return result; |
---|
363 | #endif |
---|
364 | } |
---|
365 | |
---|
366 | int singular_homog_flag=1; |
---|
367 | |
---|
368 | int cmpCF( const CFFactor & f, const CFFactor & g ) |
---|
369 | { |
---|
370 | if (f.exp() > g.exp()) return 1; |
---|
371 | if (f.exp() < g.exp()) return 0; |
---|
372 | if (f.factor() > g.factor()) return 1; |
---|
373 | return 0; |
---|
374 | } |
---|
375 | |
---|
376 | CFFList factorize ( const CanonicalForm & f, bool issqrfree ) |
---|
377 | { |
---|
378 | if ( f.inCoeffDomain() ) |
---|
379 | return CFFList( f ); |
---|
380 | int mv=f.level(); |
---|
381 | int org_v=mv; |
---|
382 | //out_cf("factorize:",f,"==================================\n"); |
---|
383 | if (! f.isUnivariate() ) |
---|
384 | { |
---|
385 | if ( singular_homog_flag && f.isHomogeneous()) |
---|
386 | { |
---|
387 | Variable xn = get_max_degree_Variable(f); |
---|
388 | int d_xn = degree(f,xn); |
---|
389 | CFMap n; |
---|
390 | CanonicalForm F = compress(f(1,xn),n); |
---|
391 | CFFList Intermediatelist; |
---|
392 | Intermediatelist = factorize(F); |
---|
393 | CFFList Homoglist; |
---|
394 | CFFListIterator j; |
---|
395 | for ( j=Intermediatelist; j.hasItem(); j++ ) |
---|
396 | { |
---|
397 | Homoglist.append( |
---|
398 | CFFactor( n(j.getItem().factor()), j.getItem().exp()) ); |
---|
399 | } |
---|
400 | CFFList Unhomoglist; |
---|
401 | CanonicalForm unhomogelem; |
---|
402 | for ( j=Homoglist; j.hasItem(); j++ ) |
---|
403 | { |
---|
404 | unhomogelem= homogenize(j.getItem().factor(),xn); |
---|
405 | Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp())); |
---|
406 | d_xn -= (degree(unhomogelem,xn)*j.getItem().exp()); |
---|
407 | } |
---|
408 | if ( d_xn != 0 ) // have to append xn^(d_xn) |
---|
409 | Unhomoglist.append(CFFactor(CanonicalForm(xn),d_xn)); |
---|
410 | if(isOn(SW_USE_NTL_SORT)) Unhomoglist.sort(cmpCF); |
---|
411 | return Unhomoglist; |
---|
412 | } |
---|
413 | mv=find_mvar(f); |
---|
414 | if ( getCharacteristic() == 0 ) |
---|
415 | { |
---|
416 | if (mv!=f.level()) |
---|
417 | { |
---|
418 | swapvar(f,Variable(mv),f.mvar()); |
---|
419 | } |
---|
420 | } |
---|
421 | else |
---|
422 | { |
---|
423 | if (mv!=1) |
---|
424 | { |
---|
425 | swapvar(f,Variable(mv),Variable(1)); |
---|
426 | org_v=1; |
---|
427 | } |
---|
428 | } |
---|
429 | } |
---|
430 | CFFList F; |
---|
431 | if ( getCharacteristic() > 0 ) |
---|
432 | { |
---|
433 | if (f.isUnivariate()) |
---|
434 | { |
---|
435 | #ifdef HAVE_FLINT |
---|
436 | nmod_poly_t f1; |
---|
437 | convertFacCF2nmod_poly_t (f1, f); |
---|
438 | nmod_poly_factor_t result; |
---|
439 | nmod_poly_factor_init (result); |
---|
440 | mp_limb_t leadingCoeff= nmod_poly_factor (result, f1); |
---|
441 | F= convertFLINTnmod_poly_factor2FacCFFList (result, leadingCoeff, f.mvar()); |
---|
442 | nmod_poly_factor_clear (result); |
---|
443 | #else |
---|
444 | #ifdef HAVE_NTL |
---|
445 | if (isOn(SW_USE_NTL) && (isPurePoly(f))) |
---|
446 | { |
---|
447 | // USE NTL |
---|
448 | if (getCharacteristic()!=2) |
---|
449 | { |
---|
450 | if (fac_NTL_char != getCharacteristic()) |
---|
451 | { |
---|
452 | fac_NTL_char = getCharacteristic(); |
---|
453 | #ifndef NTL_ZZ |
---|
454 | if (fac_NTL_char > NTL_SP_BOUND) |
---|
455 | { |
---|
456 | ZZ r; |
---|
457 | r=getCharacteristic(); |
---|
458 | ZZ_pContext ccc(r); |
---|
459 | ccc.restore(); |
---|
460 | ZZ_p::init(r); |
---|
461 | } |
---|
462 | else |
---|
463 | #endif |
---|
464 | { |
---|
465 | #ifdef NTL_ZZ |
---|
466 | ZZ r; |
---|
467 | r=getCharacteristic(); |
---|
468 | ZZ_pContext ccc(r); |
---|
469 | #else |
---|
470 | zz_pContext ccc(getCharacteristic()); |
---|
471 | #endif |
---|
472 | ccc.restore(); |
---|
473 | #ifdef NTL_ZZ |
---|
474 | ZZ_p::init(r); |
---|
475 | #else |
---|
476 | zz_p::init(getCharacteristic()); |
---|
477 | #endif |
---|
478 | } |
---|
479 | } |
---|
480 | #ifndef NTL_ZZ |
---|
481 | if (fac_NTL_char > NTL_SP_BOUND) |
---|
482 | { |
---|
483 | // convert to NTL |
---|
484 | ZZ_pX f1=convertFacCF2NTLZZpX(f); |
---|
485 | ZZ_p leadcoeff = LeadCoeff(f1); |
---|
486 | //make monic |
---|
487 | f1=f1 / LeadCoeff(f1); |
---|
488 | // factorize |
---|
489 | vec_pair_ZZ_pX_long factors; |
---|
490 | CanZass(factors,f1); |
---|
491 | // convert back to factory |
---|
492 | F=convertNTLvec_pair_ZZpX_long2FacCFFList(factors,leadcoeff,f.mvar()); |
---|
493 | } |
---|
494 | else |
---|
495 | #endif |
---|
496 | { |
---|
497 | // convert to NTL |
---|
498 | #ifdef NTL_ZZ |
---|
499 | ZZ_pX f1=convertFacCF2NTLZZpX(f); |
---|
500 | ZZ_p leadcoeff = LeadCoeff(f1); |
---|
501 | #else |
---|
502 | zz_pX f1=convertFacCF2NTLzzpX(f); |
---|
503 | zz_p leadcoeff = LeadCoeff(f1); |
---|
504 | #endif |
---|
505 | //make monic |
---|
506 | f1=f1 / LeadCoeff(f1); |
---|
507 | // factorize |
---|
508 | #ifdef NTL_ZZ |
---|
509 | vec_pair_ZZ_pX_long factors; |
---|
510 | #else |
---|
511 | vec_pair_zz_pX_long factors; |
---|
512 | #endif |
---|
513 | CanZass(factors,f1); |
---|
514 | // convert back to factory |
---|
515 | #ifdef NTL_ZZ |
---|
516 | F=convertNTLvec_pair_ZZpX_long2FacCFFList(factors,leadcoeff,f.mvar()); |
---|
517 | #else |
---|
518 | F=convertNTLvec_pair_zzpX_long2FacCFFList(factors,leadcoeff,f.mvar()); |
---|
519 | #endif |
---|
520 | } |
---|
521 | //test_cff(F,f); |
---|
522 | } |
---|
523 | else /*getCharacteristic()==2*/ |
---|
524 | { |
---|
525 | // Specialcase characteristic==2 |
---|
526 | if (fac_NTL_char != 2) |
---|
527 | { |
---|
528 | fac_NTL_char = 2; |
---|
529 | zz_p::init(2); |
---|
530 | } |
---|
531 | // convert to NTL using the faster conversion routine for characteristic 2 |
---|
532 | GF2X f1=convertFacCF2NTLGF2X(f); |
---|
533 | // no make monic necessary in GF2 |
---|
534 | //factorize |
---|
535 | vec_pair_GF2X_long factors; |
---|
536 | CanZass(factors,f1); |
---|
537 | |
---|
538 | // convert back to factory again using the faster conversion routine for vectors over GF2X |
---|
539 | F=convertNTLvec_pair_GF2X_long2FacCFFList(factors,LeadCoeff(f1),f.mvar()); |
---|
540 | } |
---|
541 | } |
---|
542 | else |
---|
543 | #endif //HAVE_NTL |
---|
544 | #endif //HAVE_FLINT |
---|
545 | { // Use Factory without NTL |
---|
546 | if ( isOn( SW_BERLEKAMP ) ) |
---|
547 | F=FpFactorizeUnivariateB( f, issqrfree ); |
---|
548 | else |
---|
549 | F=FpFactorizeUnivariateCZ( f, issqrfree, 0, Variable(), Variable() ); |
---|
550 | } |
---|
551 | } |
---|
552 | else |
---|
553 | { |
---|
554 | #ifdef HAVE_NTL |
---|
555 | if (issqrfree) |
---|
556 | { |
---|
557 | CFList factors; |
---|
558 | Variable alpha; |
---|
559 | if (CFFactory::gettype() == GaloisFieldDomain) |
---|
560 | factors= GFSqrfFactorize (f); |
---|
561 | else if (hasFirstAlgVar (f, alpha)) |
---|
562 | factors= FqSqrfFactorize (f, alpha); |
---|
563 | else |
---|
564 | factors= FpSqrfFactorize (f); |
---|
565 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
566 | F.append (CFFactor (i.getItem(), 1)); |
---|
567 | } |
---|
568 | else |
---|
569 | { |
---|
570 | Variable alpha; |
---|
571 | if (CFFactory::gettype() == GaloisFieldDomain) |
---|
572 | F= GFFactorize (f); |
---|
573 | else if (hasFirstAlgVar (f, alpha)) |
---|
574 | F= FqFactorize (f, alpha); |
---|
575 | else |
---|
576 | F= FpFactorize (f); |
---|
577 | } |
---|
578 | #else |
---|
579 | ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
580 | factoryError ("multivariate factorization not implemented"); |
---|
581 | return CFFList (CFFactor (f, 1)); |
---|
582 | #endif |
---|
583 | } |
---|
584 | } |
---|
585 | else |
---|
586 | { |
---|
587 | bool on_rational = isOn(SW_RATIONAL); |
---|
588 | On(SW_RATIONAL); |
---|
589 | CanonicalForm cd = bCommonDen( f ); |
---|
590 | CanonicalForm fz = f * cd; |
---|
591 | Off(SW_RATIONAL); |
---|
592 | if ( f.isUnivariate() ) |
---|
593 | { |
---|
594 | #ifdef HAVE_NTL |
---|
595 | if ((isOn(SW_USE_NTL)) && (isPurePoly(f))) |
---|
596 | { |
---|
597 | //USE NTL |
---|
598 | CanonicalForm ic=icontent(fz); |
---|
599 | fz/=ic; |
---|
600 | ZZ c; |
---|
601 | vec_pair_ZZX_long factors; |
---|
602 | //factorize the converted polynomial |
---|
603 | factor(c,factors,convertFacCF2NTLZZX(fz)); |
---|
604 | |
---|
605 | //convert the result back to Factory |
---|
606 | F=convertNTLvec_pair_ZZX_long2FacCFFList(factors,c,fz.mvar()); |
---|
607 | if ( ! ic.isOne() ) |
---|
608 | { |
---|
609 | if ( F.getFirst().factor().inCoeffDomain() ) |
---|
610 | { |
---|
611 | CFFactor new_first( F.getFirst().factor() * ic ); |
---|
612 | F.removeFirst(); |
---|
613 | F.insert( new_first ); |
---|
614 | } |
---|
615 | else |
---|
616 | F.insert( CFFactor( ic ) ); |
---|
617 | } |
---|
618 | else |
---|
619 | { |
---|
620 | if ( !F.getFirst().factor().inCoeffDomain() ) |
---|
621 | { |
---|
622 | CFFactor new_first( 1 ); |
---|
623 | F.insert( new_first ); |
---|
624 | } |
---|
625 | } |
---|
626 | //if ( F.getFirst().factor().isOne() ) |
---|
627 | //{ |
---|
628 | // F.removeFirst(); |
---|
629 | //} |
---|
630 | //printf("NTL:\n");out_cff(F); |
---|
631 | //F=ZFactorizeUnivariate( fz, issqrfree ); |
---|
632 | //printf("fac.:\n");out_cff(F); |
---|
633 | } |
---|
634 | #else |
---|
635 | { |
---|
636 | //Use Factory without NTL |
---|
637 | //F = ZFactorizeUnivariate( fz, issqrfree ); |
---|
638 | factoryError ("univariate factorization over Z not implemented"); |
---|
639 | return CFFList (CFFactor (f, 1)); |
---|
640 | } |
---|
641 | #endif |
---|
642 | } |
---|
643 | else |
---|
644 | { |
---|
645 | #ifdef HAVE_NTL |
---|
646 | On (SW_RATIONAL); |
---|
647 | if (issqrfree) |
---|
648 | { |
---|
649 | CFList factors; |
---|
650 | factors= ratSqrfFactorize (fz); |
---|
651 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
652 | F.append (CFFactor (i.getItem(), 1)); |
---|
653 | } |
---|
654 | else |
---|
655 | F = ratFactorize (fz); |
---|
656 | Off (SW_RATIONAL); |
---|
657 | #else |
---|
658 | factoryError ("multivariate factorization not implemented"); |
---|
659 | return CFFList (CFFactor (f, 1)); |
---|
660 | #endif |
---|
661 | } |
---|
662 | |
---|
663 | if ( on_rational ) |
---|
664 | On(SW_RATIONAL); |
---|
665 | if ( ! cd.isOne() ) |
---|
666 | { |
---|
667 | if ( F.getFirst().factor().inCoeffDomain() ) |
---|
668 | { |
---|
669 | CFFactor new_first( F.getFirst().factor() / cd ); |
---|
670 | F.removeFirst(); |
---|
671 | F.insert( new_first ); |
---|
672 | } |
---|
673 | else |
---|
674 | { |
---|
675 | F.insert( CFFactor( 1/cd ) ); |
---|
676 | } |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | if ((mv!=org_v) && (! f.isUnivariate() )) |
---|
681 | { |
---|
682 | CFFListIterator J=F; |
---|
683 | for ( ; J.hasItem(); J++) |
---|
684 | { |
---|
685 | swapvar(J.getItem().factor(),Variable(mv),Variable(org_v)); |
---|
686 | } |
---|
687 | swapvar(f,Variable(mv),Variable(org_v)); |
---|
688 | } |
---|
689 | //out_cff(F); |
---|
690 | if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF); |
---|
691 | return F; |
---|
692 | } |
---|
693 | |
---|
694 | #ifdef HAVE_NTL |
---|
695 | CanonicalForm fntl ( const CanonicalForm & f, int j ) |
---|
696 | { |
---|
697 | ZZX f1=convertFacCF2NTLZZX(f); |
---|
698 | return convertZZ2CF(coeff(f1,j)); |
---|
699 | } |
---|
700 | #endif |
---|
701 | |
---|
702 | CFFList factorize ( const CanonicalForm & f, const Variable & alpha ) |
---|
703 | { |
---|
704 | if ( f.inCoeffDomain() ) |
---|
705 | return CFFList( f ); |
---|
706 | //out_cf("factorize:",f,"==================================\n"); |
---|
707 | //out_cf("mipo:",getMipo(alpha),"\n"); |
---|
708 | CFFList F; |
---|
709 | ASSERT( alpha.level() < 0, "not an algebraic extension" ); |
---|
710 | int ch=getCharacteristic(); |
---|
711 | if (f.isUnivariate()&& (ch>0)) |
---|
712 | { |
---|
713 | #ifdef HAVE_NTL |
---|
714 | if (isOn(SW_USE_NTL)) |
---|
715 | { |
---|
716 | //USE NTL |
---|
717 | if (ch>2) |
---|
718 | { |
---|
719 | // First all cases with characteristic !=2 |
---|
720 | // set remainder |
---|
721 | if (fac_NTL_char != getCharacteristic()) |
---|
722 | { |
---|
723 | fac_NTL_char = getCharacteristic(); |
---|
724 | #ifdef NTL_ZZ |
---|
725 | ZZ r; |
---|
726 | r=getCharacteristic(); |
---|
727 | ZZ_pContext ccc(r); |
---|
728 | #else |
---|
729 | zz_pContext ccc(getCharacteristic()); |
---|
730 | #endif |
---|
731 | ccc.restore(); |
---|
732 | #ifdef NTL_ZZ |
---|
733 | ZZ_p::init(r); |
---|
734 | #else |
---|
735 | zz_p::init(getCharacteristic()); |
---|
736 | #endif |
---|
737 | } |
---|
738 | |
---|
739 | // set minimal polynomial in NTL |
---|
740 | #ifdef NTL_ZZ |
---|
741 | ZZ_pX minPo=convertFacCF2NTLZZpX(getMipo(alpha)); |
---|
742 | ZZ_pEContext c(minPo); |
---|
743 | #else |
---|
744 | zz_pX minPo=convertFacCF2NTLzzpX(getMipo(alpha)); |
---|
745 | zz_pEContext c(minPo); |
---|
746 | #endif |
---|
747 | |
---|
748 | c.restore(); |
---|
749 | |
---|
750 | // convert to NTL |
---|
751 | #ifdef NTL_ZZ |
---|
752 | ZZ_pEX f1=convertFacCF2NTLZZ_pEX(f,minPo); |
---|
753 | ZZ_pE leadcoeff= LeadCoeff(f1); |
---|
754 | #else |
---|
755 | zz_pEX f1=convertFacCF2NTLzz_pEX(f,minPo); |
---|
756 | zz_pE leadcoeff= LeadCoeff(f1); |
---|
757 | #endif |
---|
758 | |
---|
759 | //make monic |
---|
760 | f1=f1 / leadcoeff; |
---|
761 | |
---|
762 | // factorize using NTL |
---|
763 | #ifdef NTL_ZZ |
---|
764 | vec_pair_ZZ_pEX_long factors; |
---|
765 | #else |
---|
766 | vec_pair_zz_pEX_long factors; |
---|
767 | #endif |
---|
768 | CanZass(factors,f1); |
---|
769 | |
---|
770 | // return converted result |
---|
771 | #ifdef NTL_ZZ |
---|
772 | F=convertNTLvec_pair_ZZpEX_long2FacCFFList(factors,leadcoeff,f.mvar(),alpha); |
---|
773 | #else |
---|
774 | F=convertNTLvec_pair_zzpEX_long2FacCFFList(factors,leadcoeff,f.mvar(),alpha); |
---|
775 | #endif |
---|
776 | } |
---|
777 | else if (/*getCharacteristic()*/ch==2) |
---|
778 | { |
---|
779 | // special case : GF2 |
---|
780 | |
---|
781 | // remainder is two ==> nothing to do |
---|
782 | // set remainder |
---|
783 | ZZ r; |
---|
784 | r=getCharacteristic(); |
---|
785 | ZZ_pContext ccc(r); |
---|
786 | ccc.restore(); |
---|
787 | |
---|
788 | // set minimal polynomial in NTL using the optimized conversion routines for characteristic 2 |
---|
789 | GF2X minPo=convertFacCF2NTLGF2X(getMipo(alpha,f.mvar())); |
---|
790 | GF2EContext c(minPo); |
---|
791 | c.restore(); |
---|
792 | |
---|
793 | // convert to NTL again using the faster conversion routines |
---|
794 | GF2EX f1; |
---|
795 | if (isPurePoly(f)) |
---|
796 | { |
---|
797 | GF2X f_tmp=convertFacCF2NTLGF2X(f); |
---|
798 | f1=to_GF2EX(f_tmp); |
---|
799 | } |
---|
800 | else |
---|
801 | { |
---|
802 | f1=convertFacCF2NTLGF2EX(f,minPo); |
---|
803 | } |
---|
804 | |
---|
805 | // make monic (in Z/2(a)) |
---|
806 | GF2E f1_coef=LeadCoeff(f1); |
---|
807 | MakeMonic(f1); |
---|
808 | |
---|
809 | // factorize using NTL |
---|
810 | vec_pair_GF2EX_long factors; |
---|
811 | CanZass(factors,f1); |
---|
812 | |
---|
813 | // return converted result |
---|
814 | F=convertNTLvec_pair_GF2EX_long2FacCFFList(factors,f1_coef,f.mvar(),alpha); |
---|
815 | } |
---|
816 | else |
---|
817 | { |
---|
818 | } |
---|
819 | } |
---|
820 | else |
---|
821 | #endif |
---|
822 | { |
---|
823 | F=FpFactorizeUnivariateCZ( f, false, 1, alpha, Variable() ); |
---|
824 | } |
---|
825 | } |
---|
826 | else if (ch>0) |
---|
827 | { |
---|
828 | #ifdef HAVE_NTL |
---|
829 | F= FqFactorize (f, alpha); |
---|
830 | #else |
---|
831 | ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
832 | factoryError ("multivariate factorization not implemented"); |
---|
833 | return CFFList (CFFactor (f, 1)); |
---|
834 | #endif |
---|
835 | |
---|
836 | } |
---|
837 | else if (f.isUnivariate() && (ch == 0)) // Q(a)[x] |
---|
838 | { |
---|
839 | F= AlgExtFactorize (f, alpha); |
---|
840 | } |
---|
841 | else //Q(a)[x1,...,xn] |
---|
842 | { |
---|
843 | #ifdef HAVE_NTL |
---|
844 | F= ratFactorize (f, alpha); |
---|
845 | #else |
---|
846 | ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
847 | factoryError ("multivariate factorization not implemented"); |
---|
848 | return CFFList (CFFactor (f, 1)); |
---|
849 | #endif |
---|
850 | } |
---|
851 | if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF); |
---|
852 | return F; |
---|
853 | } |
---|
854 | |
---|
855 | CFFList sqrFree ( const CanonicalForm & f, bool sort ) |
---|
856 | { |
---|
857 | // ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
858 | CFFList result; |
---|
859 | |
---|
860 | if ( getCharacteristic() == 0 ) |
---|
861 | result = sqrFreeZ( f ); |
---|
862 | else |
---|
863 | { |
---|
864 | Variable alpha; |
---|
865 | if (hasFirstAlgVar (f, alpha)) |
---|
866 | result = FqSqrf( f, alpha ); |
---|
867 | else |
---|
868 | result= FpSqrf (f); |
---|
869 | } |
---|
870 | if (sort) |
---|
871 | { |
---|
872 | CFFactor buf= result.getFirst(); |
---|
873 | result.removeFirst(); |
---|
874 | result= sortCFFList (result); |
---|
875 | result.insert (buf); |
---|
876 | } |
---|
877 | return result; |
---|
878 | } |
---|
879 | |
---|
880 | bool isSqrFree ( const CanonicalForm & f ) |
---|
881 | { |
---|
882 | // ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
883 | if ( getCharacteristic() == 0 ) |
---|
884 | return isSqrFreeZ( f ); |
---|
885 | else |
---|
886 | return isSqrFreeFp( f ); |
---|
887 | } |
---|
888 | |
---|