1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: cf_factor.cc,v 1.15 2002-09-24 11:28:29 Singular Exp $ */ |
---|
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 | |
---|
30 | #include "int_int.h" |
---|
31 | #ifdef HAVE_NTL |
---|
32 | #include "NTLconvert.h" |
---|
33 | #endif |
---|
34 | |
---|
35 | int getExp(); /* cf_char.cc */ |
---|
36 | |
---|
37 | static bool isUnivariateBaseDomain( const CanonicalForm & f ) |
---|
38 | { |
---|
39 | CFIterator i = f; |
---|
40 | bool ok = i.coeff().inBaseDomain(); |
---|
41 | i++; |
---|
42 | while ( i.hasTerms() && ( ok = ok && i.coeff().inBaseDomain() ) ) i++; |
---|
43 | return ok; |
---|
44 | } |
---|
45 | |
---|
46 | void find_exp(const CanonicalForm & f, int * exp_f) |
---|
47 | { |
---|
48 | if ( ! f.inCoeffDomain() ) |
---|
49 | { |
---|
50 | int e=f.level(); |
---|
51 | CFIterator i = f; |
---|
52 | if (e>=0) |
---|
53 | { |
---|
54 | if (i.exp() > exp_f[e]) exp_f[e]=i.exp(); |
---|
55 | } |
---|
56 | for (; i.hasTerms(); i++ ) |
---|
57 | { |
---|
58 | find_exp(i.coeff(), exp_f); |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | int find_mvar(const CanonicalForm & f) |
---|
64 | { |
---|
65 | int mv=f.level(); |
---|
66 | int *exp_f=new int[mv+1]; |
---|
67 | int i; |
---|
68 | for(i=mv;i>0;i--) exp_f[i]=0; |
---|
69 | find_exp(f,exp_f); |
---|
70 | for(i=mv;i>0;i--) |
---|
71 | { |
---|
72 | if ((exp_f[i]>0) && (exp_f[i]<exp_f[mv])) |
---|
73 | { |
---|
74 | mv=i; |
---|
75 | } |
---|
76 | } |
---|
77 | delete[] exp_f; |
---|
78 | return mv; |
---|
79 | } |
---|
80 | |
---|
81 | #if 0 |
---|
82 | void out_cf(char *s1,const CanonicalForm &f,char *s2) |
---|
83 | { |
---|
84 | printf("%s",s1); |
---|
85 | if (f==0) printf("+0"); |
---|
86 | //else if (! f.inCoeffDomain() ) |
---|
87 | else if (! f.inBaseDomain() ) |
---|
88 | { |
---|
89 | int l = f.level(); |
---|
90 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
91 | { |
---|
92 | int e=i.exp(); |
---|
93 | if (i.coeff().isOne()) |
---|
94 | { |
---|
95 | printf("+"); |
---|
96 | if (e==0) printf("1"); |
---|
97 | else |
---|
98 | { |
---|
99 | printf("v(%d)",l); |
---|
100 | if (e!=1) printf("^%d",e); |
---|
101 | } |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | out_cf("+(",i.coeff(),")"); |
---|
106 | if (e!=0) |
---|
107 | { |
---|
108 | printf("*v(%d)",l); |
---|
109 | if (e!=1) printf("^%d",e); |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | if ( f.isImm() ) |
---|
117 | { |
---|
118 | printf("+%d",f.intval()); |
---|
119 | } |
---|
120 | else printf("+..."); |
---|
121 | //if (f.inZ()) printf("(Z)"); |
---|
122 | //else if (f.inQ()) printf("(Q)"); |
---|
123 | //else if (f.inFF()) printf("(FF)"); |
---|
124 | //else if (f.inPP()) printf("(PP)"); |
---|
125 | //else if (f.inGF()) printf("(PP)"); |
---|
126 | //else |
---|
127 | if (f.inExtension()) printf("E(%d)",f.level()); |
---|
128 | } |
---|
129 | printf("%s",s2); |
---|
130 | } |
---|
131 | void out_cff(CFFList &L) |
---|
132 | { |
---|
133 | int n = L.length(); |
---|
134 | CFFListIterator J=L; |
---|
135 | int j=0; |
---|
136 | for ( ; J.hasItem(); J++, j++ ) |
---|
137 | { |
---|
138 | printf("F%d",j);out_cf(":",J.getItem().factor()," ^ "); |
---|
139 | printf("%d\n", J.getItem().exp()); |
---|
140 | } |
---|
141 | } |
---|
142 | void test_cff(CFFList &L,const CanonicalForm & f) |
---|
143 | { |
---|
144 | int n = L.length(); |
---|
145 | CFFListIterator J=L; |
---|
146 | CanonicalForm t=1; |
---|
147 | int j=0; |
---|
148 | if (!(L.getFirst().factor().inCoeffDomain())) |
---|
149 | printf("first entry is not const\n"); |
---|
150 | for ( ; J.hasItem(); J++, j++ ) |
---|
151 | { |
---|
152 | CanonicalForm tt=J.getItem().factor(); |
---|
153 | if (tt.inCoeffDomain() && (j!=0)) |
---|
154 | printf("other entry is const\n"); |
---|
155 | j=J.getItem().exp(); |
---|
156 | while(j>0) { t*=tt; j--; } |
---|
157 | } |
---|
158 | if ((f-t)!=0) { printf("problem:\n");out_cf("factor:",f," has problems\n");} |
---|
159 | } |
---|
160 | #endif |
---|
161 | |
---|
162 | static bool isPurePoly(const CanonicalForm & f) |
---|
163 | { |
---|
164 | if (f.level()<=0) return false; |
---|
165 | for (CFIterator i=f;i.hasTerms();i++) |
---|
166 | { |
---|
167 | if (!(i.coeff().inBaseDomain())) return false; |
---|
168 | } |
---|
169 | return true; |
---|
170 | } |
---|
171 | |
---|
172 | CFFList factorize ( const CanonicalForm & f, bool issqrfree ) |
---|
173 | { |
---|
174 | if ( f.inCoeffDomain() ) |
---|
175 | return CFFList( f ); |
---|
176 | int mv=f.level(); |
---|
177 | if (! f.isUnivariate() ) |
---|
178 | { |
---|
179 | mv=find_mvar(f); |
---|
180 | if (mv!=f.level()) |
---|
181 | { |
---|
182 | swapvar(f,Variable(mv),f.mvar()); |
---|
183 | } |
---|
184 | if ( getCharacteristic() == 0 ) Off(SW_USE_NTL); |
---|
185 | } |
---|
186 | CFFList F; |
---|
187 | if ( getCharacteristic() > 0 ) |
---|
188 | { |
---|
189 | ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
190 | #ifdef HAVE_NTL |
---|
191 | if (isOn(SW_USE_NTL) && (isPurePoly(f))) |
---|
192 | { |
---|
193 | // USE NTL |
---|
194 | if (getCharacteristic()!=2) |
---|
195 | { |
---|
196 | // set remainder |
---|
197 | ZZ r; |
---|
198 | r=getCharacteristic(); |
---|
199 | ZZ_pContext ccc(r); |
---|
200 | ccc.restore(); |
---|
201 | ZZ_p::init(r); |
---|
202 | // convert to NTL |
---|
203 | ZZ_pX f1=convertFacCF2NTLZZpX(f); |
---|
204 | ZZ_p leadcoeff = LeadCoeff(f1); |
---|
205 | //make monic |
---|
206 | f1=f1 / LeadCoeff(f1); |
---|
207 | |
---|
208 | // factorize |
---|
209 | vec_pair_ZZ_pX_long factors; |
---|
210 | CanZass(factors,f1); |
---|
211 | |
---|
212 | // convert back to factory |
---|
213 | F=convertNTLvec_pair_ZZpX_long2FacCFFList(factors,leadcoeff,f.mvar()); |
---|
214 | //test_cff(F,f); |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | // Specialcase characteristic==2 |
---|
219 | ZZ r;r=2; |
---|
220 | ZZ_p::init(r); |
---|
221 | |
---|
222 | // remainder is 2 --> nothing to set |
---|
223 | |
---|
224 | // convert to NTL using the faster conversion routine for characteristic 2 |
---|
225 | GF2X f1=convertFacCF2NTLGF2X(f); |
---|
226 | // no make monic necessary in GF2 |
---|
227 | //factorize |
---|
228 | vec_pair_GF2X_long factors; |
---|
229 | CanZass(factors,f1); |
---|
230 | |
---|
231 | // convert back to factory again using the faster conversion routine for vectors over GF2X |
---|
232 | F=convertNTLvec_pair_GF2X_long2FacCFFList(factors,LeadCoeff(f1),f.mvar()); |
---|
233 | } |
---|
234 | } |
---|
235 | else |
---|
236 | #endif |
---|
237 | { // Use Factory without NTL |
---|
238 | if ( isOn( SW_BERLEKAMP ) ) |
---|
239 | F=FpFactorizeUnivariateB( f, issqrfree ); |
---|
240 | else |
---|
241 | F=FpFactorizeUnivariateCZ( f, issqrfree, 0, Variable(), Variable() ); |
---|
242 | } |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | CanonicalForm cd = bCommonDen( f ); |
---|
247 | CanonicalForm fz = f * cd; |
---|
248 | bool on_rational = isOn(SW_RATIONAL); |
---|
249 | Off(SW_RATIONAL); |
---|
250 | if ( f.isUnivariate() ) |
---|
251 | { |
---|
252 | #ifdef HAVE_NTL |
---|
253 | if ((isOn(SW_USE_NTL)) && (isPurePoly(f))) |
---|
254 | { |
---|
255 | //USE NTL |
---|
256 | CanonicalForm ic=icontent(fz); |
---|
257 | fz/=ic; |
---|
258 | ZZ c; |
---|
259 | vec_pair_ZZX_long factors; |
---|
260 | //factorize the converted polynomial |
---|
261 | factor(c,factors,convertFacCF2NTLZZX(fz)); |
---|
262 | |
---|
263 | //convert the result back to Factory |
---|
264 | F=convertNTLvec_pair_ZZX_long2FacCFFList(factors,c,fz.mvar()); |
---|
265 | if ( ! ic.isOne() ) |
---|
266 | { |
---|
267 | if ( F.getFirst().factor().inCoeffDomain() ) |
---|
268 | { |
---|
269 | CFFactor new_first( F.getFirst().factor() * ic ); |
---|
270 | F.removeFirst(); |
---|
271 | F.insert( new_first ); |
---|
272 | } |
---|
273 | else |
---|
274 | F.insert( CFFactor( ic ) ); |
---|
275 | } |
---|
276 | if ( F.getFirst().factor().isOne() ) |
---|
277 | { |
---|
278 | F.removeFirst(); |
---|
279 | } |
---|
280 | } |
---|
281 | else |
---|
282 | #endif |
---|
283 | { |
---|
284 | //Use Factory without NTL |
---|
285 | F = ZFactorizeUnivariate( fz, issqrfree ); |
---|
286 | } |
---|
287 | } |
---|
288 | else |
---|
289 | F = ZFactorizeMultivariate( fz, issqrfree ); |
---|
290 | |
---|
291 | if ( on_rational ) |
---|
292 | On(SW_RATIONAL); |
---|
293 | if ( ! cd.isOne() ) |
---|
294 | { |
---|
295 | if ( F.getFirst().factor().inCoeffDomain() ) |
---|
296 | { |
---|
297 | CFFactor new_first( F.getFirst().factor() / cd ); |
---|
298 | F.removeFirst(); |
---|
299 | F.insert( new_first ); |
---|
300 | } |
---|
301 | else |
---|
302 | { |
---|
303 | F.insert( CFFactor( 1/cd ) ); |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | if ((mv!=f.level()) && (! f.isUnivariate() )) |
---|
309 | { |
---|
310 | CFFListIterator J=F; |
---|
311 | for ( ; J.hasItem(); J++) |
---|
312 | { |
---|
313 | swapvar(J.getItem().factor(),Variable(mv),f.mvar()); |
---|
314 | } |
---|
315 | swapvar(f,Variable(mv),f.mvar()); |
---|
316 | } |
---|
317 | return F; |
---|
318 | } |
---|
319 | #ifdef HAVE_NTL |
---|
320 | CanonicalForm fntl ( const CanonicalForm & f, int j ) |
---|
321 | { |
---|
322 | ZZX f1=convertFacCF2NTLZZX(f); |
---|
323 | return convertZZ2CF(coeff(f1,j)); |
---|
324 | } |
---|
325 | #endif |
---|
326 | |
---|
327 | CFFList factorize ( const CanonicalForm & f, const Variable & alpha ) |
---|
328 | { |
---|
329 | CFFList F; |
---|
330 | ASSERT( alpha.level() < 0, "not an algebraic extension" ); |
---|
331 | ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
332 | ASSERT( getCharacteristic() > 0, "char 0 factorization not implemented" ); |
---|
333 | #ifdef HAVE_NTL |
---|
334 | if (isOn(SW_USE_NTL)) |
---|
335 | { |
---|
336 | //USE NTL |
---|
337 | if (1 ) //getCharacteristic()!=2) |
---|
338 | { |
---|
339 | // First all cases with characteristic !=2 |
---|
340 | // set remainder |
---|
341 | ZZ r; |
---|
342 | r=getCharacteristic(); |
---|
343 | ZZ_pContext ccc(r); |
---|
344 | ccc.restore(); |
---|
345 | |
---|
346 | // set minimal polynomial in NTL |
---|
347 | ZZ_pX minPo=convertFacCF2NTLZZpX(getMipo(alpha)); |
---|
348 | ZZ_pEContext c(minPo); |
---|
349 | |
---|
350 | c.restore(); |
---|
351 | |
---|
352 | // convert to NTL |
---|
353 | ZZ_pEX f1=convertFacCF2NTLZZ_pEX(f,minPo); |
---|
354 | |
---|
355 | //make monic |
---|
356 | ZZ_pE leadcoeff= LeadCoeff(f1); |
---|
357 | f1=f1 / leadcoeff; |
---|
358 | |
---|
359 | // factorize using NTL |
---|
360 | vec_pair_ZZ_pEX_long factors; |
---|
361 | CanZass(factors,f1); |
---|
362 | |
---|
363 | // return converted result |
---|
364 | F=convertNTLvec_pair_ZZpEX_long2FacCFFList(factors,leadcoeff,f.mvar(),alpha); |
---|
365 | } |
---|
366 | else |
---|
367 | { |
---|
368 | // special case : GF2 |
---|
369 | |
---|
370 | // remainder is two ==> nothing to do |
---|
371 | |
---|
372 | // set minimal polynomial in NTL using the optimized conversion routines for characteristic 2 |
---|
373 | GF2X minPo=convertFacCF2NTLGF2X(getMipo(alpha,f.mvar())); |
---|
374 | GF2EContext c(minPo); |
---|
375 | c.restore(); |
---|
376 | |
---|
377 | // convert to NTL again using the faster conversion routines |
---|
378 | GF2X f_tmp=convertFacCF2NTLGF2X(f); |
---|
379 | GF2EX f1=to_GF2EX(f_tmp); |
---|
380 | |
---|
381 | // no make monic necessary in GF2 |
---|
382 | |
---|
383 | // factorize using NTL |
---|
384 | vec_pair_GF2EX_long factors; |
---|
385 | CanZass(factors,f1); |
---|
386 | |
---|
387 | // return converted result |
---|
388 | F=convertNTLvec_pair_GF2EX_long2FacCFFList(factors,LeadCoeff(f1),f.mvar(),alpha); |
---|
389 | } |
---|
390 | |
---|
391 | } |
---|
392 | else |
---|
393 | #endif |
---|
394 | { |
---|
395 | printf("factorize without NTL: alg. ext.\n"); |
---|
396 | F=FpFactorizeUnivariateCZ( f, false, 1, alpha, Variable() ); |
---|
397 | } |
---|
398 | return F; |
---|
399 | } |
---|
400 | |
---|
401 | CFFList sqrFree ( const CanonicalForm & f, bool sort ) |
---|
402 | { |
---|
403 | // ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
404 | CFFList result; |
---|
405 | |
---|
406 | if ( getCharacteristic() == 0 ) |
---|
407 | result = sqrFreeZ( f ); |
---|
408 | else |
---|
409 | result = sqrFreeFp( f ); |
---|
410 | |
---|
411 | return ( sort ? sortCFFList( result ) : result ); |
---|
412 | } |
---|
413 | |
---|
414 | bool isSqrFree ( const CanonicalForm & f ) |
---|
415 | { |
---|
416 | // ASSERT( f.isUnivariate(), "multivariate factorization not implemented" ); |
---|
417 | if ( getCharacteristic() == 0 ) |
---|
418 | return isSqrFreeZ( f ); |
---|
419 | else |
---|
420 | return isSqrFreeFp( f ); |
---|
421 | } |
---|
422 | |
---|