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