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