1 | /* Copyright 1996 Michael Messollen. All rights reserved. */ |
---|
2 | //////////////////////////////////////////////////////////// |
---|
3 | // emacs edit mode for this file is -*- C++ -*- |
---|
4 | static char * rcsid = "$Id: csutil.cc,v 1.15 2007-10-16 15:49:49 Singular Exp $"; |
---|
5 | ///////////////////////////////////////////////////////////// |
---|
6 | // FACTORY - Includes |
---|
7 | #include <factory.h> |
---|
8 | // Factor - Includes |
---|
9 | #include <tmpl_inst.h> |
---|
10 | #include <Factor.h> |
---|
11 | #include <SqrFree.h> |
---|
12 | #include <helpstuff.h> |
---|
13 | #include <homogfactor.h> |
---|
14 | // Charset - Includes |
---|
15 | #include "csutil.h" |
---|
16 | extern void out_cf(char *s1,const CanonicalForm &f,char *s2); |
---|
17 | extern CanonicalForm alg_lc(const CanonicalForm &f); |
---|
18 | extern int hasAlgVar(const CanonicalForm &f); |
---|
19 | |
---|
20 | static bool |
---|
21 | lowerRank ( const CanonicalForm & f, const CanonicalForm & g, int & ind ) |
---|
22 | { |
---|
23 | int df, dg; |
---|
24 | Variable vf = f.mvar(), vg = g.mvar(); |
---|
25 | |
---|
26 | if ( f.inCoeffDomain() ) |
---|
27 | { |
---|
28 | if ( g.inCoeffDomain() ) ind= 1; |
---|
29 | return true;//( vg > vf ); |
---|
30 | } |
---|
31 | else if ( g.inCoeffDomain() ) return false; |
---|
32 | else if ( vf < vg ) return true; |
---|
33 | else if ( vf == vg ) |
---|
34 | { |
---|
35 | df = degree( f ); dg = degree( g ); |
---|
36 | if ( df < dg ) return true; |
---|
37 | else if ( df == dg ) return lowerRank( LC( f ), LC( g ) , ind); |
---|
38 | else return false; |
---|
39 | } |
---|
40 | return false; |
---|
41 | } |
---|
42 | |
---|
43 | CanonicalForm |
---|
44 | lowestRank( const CFList & F ) |
---|
45 | { |
---|
46 | CFListIterator i = F; |
---|
47 | CanonicalForm f; |
---|
48 | int ind=0; |
---|
49 | if ( ! i.hasItem() ) return f; |
---|
50 | f = i.getItem(); ++i; |
---|
51 | while ( i.hasItem() ) |
---|
52 | { |
---|
53 | //CERR << "comparing " << f << " and " << i.getItem() |
---|
54 | // << " == " << lowerRank( i.getItem(), f, ind ) << "\n"; |
---|
55 | if ( lowerRank( i.getItem(), f, ind ) ) |
---|
56 | { |
---|
57 | if ( ind ) |
---|
58 | { |
---|
59 | CFList Itemlist= get_Terms(i.getItem()); |
---|
60 | CFList Flist= get_Terms(f); |
---|
61 | |
---|
62 | // Have to further compare number of terms! |
---|
63 | //CERR << "compare terms! f= " << Flist.length() << " item= " |
---|
64 | // << Itemlist.length() <<"\n"; |
---|
65 | if ( Itemlist.length() < Flist.length()) f = i.getItem(); |
---|
66 | ind=0; |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | f = i.getItem(); |
---|
71 | } |
---|
72 | } |
---|
73 | ++i; |
---|
74 | } |
---|
75 | return f; |
---|
76 | } |
---|
77 | |
---|
78 | // old version |
---|
79 | // CanonicalForm |
---|
80 | // prem ( const CanonicalForm &f, const CanonicalForm &g ) |
---|
81 | // { |
---|
82 | // CanonicalForm ff, gg, cg; |
---|
83 | // int df, dg; |
---|
84 | // bool reord; |
---|
85 | // Variable vf, vg, v; |
---|
86 | // |
---|
87 | // if ( (vf = f.mvar()) < (vg = g.mvar()) ) return f; |
---|
88 | // else { |
---|
89 | // if ( vf == vg ) { |
---|
90 | // ff = f; gg = g; |
---|
91 | // reord = false; |
---|
92 | // v = vg; |
---|
93 | // } |
---|
94 | // else { |
---|
95 | // v = Variable(level(f.mvar()) + 1); |
---|
96 | // ff = swapvar(f,vg,v); |
---|
97 | // gg = swapvar(g,vg,v); |
---|
98 | // reord=true; |
---|
99 | // } |
---|
100 | // cg = ini( gg, v ); |
---|
101 | // dg = degree( gg, v ); |
---|
102 | // while ( ( df = degree( ff, v ) ) >= dg ) |
---|
103 | // ff = cg * ff - power( v, df-dg ) * gg * LC( ff, v ); |
---|
104 | // if ( reord ) { |
---|
105 | // return swapvar( ff, vg, v ); |
---|
106 | // } |
---|
107 | // else |
---|
108 | // return ff; |
---|
109 | // } |
---|
110 | // } |
---|
111 | |
---|
112 | CanonicalForm |
---|
113 | Prem ( const CanonicalForm &f, const CanonicalForm &g ) |
---|
114 | { |
---|
115 | CanonicalForm ff, gg, l, test, lu, lv, t, retvalue; |
---|
116 | int df, dg; |
---|
117 | bool reord; |
---|
118 | Variable vf, vg, v; |
---|
119 | |
---|
120 | if ( (vf = f.mvar()) < (vg = g.mvar()) ) return f; |
---|
121 | else |
---|
122 | { |
---|
123 | if ( vf == vg ) |
---|
124 | { |
---|
125 | ff = f; gg = g; |
---|
126 | reord = false; |
---|
127 | v = vg; |
---|
128 | } |
---|
129 | else |
---|
130 | { |
---|
131 | v = Variable(level(f.mvar()) + 1); |
---|
132 | ff = swapvar(f,vg,v); |
---|
133 | gg = swapvar(g,vg,v); |
---|
134 | reord=true; |
---|
135 | } |
---|
136 | dg = degree( gg, v ); |
---|
137 | df = degree( ff, v ); |
---|
138 | if (dg <= df) {l=LC(gg); gg = gg -LC(gg)*power(v,dg);} |
---|
139 | else { l = 1; } |
---|
140 | while ( ( dg <= df ) && ( ff != ff.genZero()) ) |
---|
141 | { |
---|
142 | // CERR << "Start gcd..." << "\n"; |
---|
143 | test = gcd(l,LC(ff)); |
---|
144 | //CERR << "gcd(" << l << "," << LC(ff) << ")= " << test << "\n"; |
---|
145 | lu = l/test; lv = LC(ff)/test; |
---|
146 | t = power(v,df-dg) * gg * lv; |
---|
147 | if ( df == 0 ){ ff = ff.genZero(); } |
---|
148 | else { ff = ff - LC(ff)*power(v,df); } |
---|
149 | ff = lu*ff - t; |
---|
150 | df = degree( ff, v ); |
---|
151 | } |
---|
152 | if ( reord ) |
---|
153 | { |
---|
154 | retvalue= swapvar( ff, vg, v ); |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | retvalue= ff; |
---|
159 | } |
---|
160 | return retvalue; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | static CanonicalForm |
---|
165 | Sprem ( const CanonicalForm &f, const CanonicalForm &g, CanonicalForm & m, CanonicalForm & q ) |
---|
166 | { |
---|
167 | CanonicalForm ff, gg, l, test, retvalue; |
---|
168 | int df, dg,n; |
---|
169 | bool reord; |
---|
170 | Variable vf, vg, v; |
---|
171 | |
---|
172 | if ( (vf = f.mvar()) < (vg = g.mvar()) ) |
---|
173 | { |
---|
174 | m=CanonicalForm(0); q=CanonicalForm(0); |
---|
175 | return f; |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | if ( vf == vg ) |
---|
180 | { |
---|
181 | ff = f; gg = g; |
---|
182 | reord = false; |
---|
183 | v = vg; // == x |
---|
184 | } |
---|
185 | else |
---|
186 | { |
---|
187 | v = Variable(level(f.mvar()) + 1); |
---|
188 | ff = swapvar(f,vg,v); // == r |
---|
189 | gg = swapvar(g,vg,v); // == v |
---|
190 | reord=true; |
---|
191 | } |
---|
192 | dg = degree( gg, v ); // == dv |
---|
193 | df = degree( ff, v ); // == dr |
---|
194 | if (dg <= df) {l=LC(gg); gg = gg -LC(gg)*power(v,dg);} |
---|
195 | else { l = 1; } |
---|
196 | n= 0; |
---|
197 | while ( ( dg <= df ) && ( ff != ff.genZero()) ) |
---|
198 | { |
---|
199 | test= power(v,df-dg) * gg * LC(ff); |
---|
200 | if ( df == 0 ){ff= ff.genZero();} |
---|
201 | else {ff= ff - LC(ff)*power(v,df);} |
---|
202 | ff = l*ff-test; |
---|
203 | df= degree(ff,v); |
---|
204 | n++; |
---|
205 | } |
---|
206 | if ( reord ) |
---|
207 | { |
---|
208 | retvalue= swapvar( ff, vg, v ); |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | retvalue= ff; |
---|
213 | } |
---|
214 | m= power(l,n); |
---|
215 | if ( fdivides(g,m*f-retvalue) ) |
---|
216 | q= (m*f-retvalue)/g; |
---|
217 | else |
---|
218 | q= CanonicalForm(0); |
---|
219 | return retvalue; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | CanonicalForm |
---|
224 | divide( const CanonicalForm & ff, const CanonicalForm & f, const CFList & as) |
---|
225 | { |
---|
226 | CanonicalForm r,m,q; |
---|
227 | |
---|
228 | //out_cf("divide f=",ff,"\n"); |
---|
229 | //out_cf("divide g=",f,"\n"); |
---|
230 | if (f.inCoeffDomain()) |
---|
231 | { |
---|
232 | bool b=!isOn(SW_RATIONAL); |
---|
233 | On(SW_RATIONAL); |
---|
234 | q=ff/f; |
---|
235 | if (b) Off(SW_RATIONAL); |
---|
236 | } |
---|
237 | else |
---|
238 | r= Sprem(ff,f,m,q); //result in q, ignore r,m |
---|
239 | //CERR << "r= " << r << " , m= " << m << " , q= " << q << "\n"; |
---|
240 | r= Prem(q,as); |
---|
241 | //CERR << "r= " << r << "\n"; |
---|
242 | //out_cf(" ->",r,"\n"); |
---|
243 | return r; |
---|
244 | } |
---|
245 | |
---|
246 | // This function allows as to be empty; in that case, it is equivalent |
---|
247 | // to the previous version (treating no variables as algebraic). |
---|
248 | static CanonicalForm |
---|
249 | myfitting( const CanonicalForm &f, const CFList &as ) |
---|
250 | { |
---|
251 | CanonicalForm rem=f; |
---|
252 | |
---|
253 | if ( !(rem.isZero()) ) |
---|
254 | { |
---|
255 | if ( getCharacteristic() > 0 ) |
---|
256 | return num((rem/lc(rem))); |
---|
257 | else |
---|
258 | { |
---|
259 | On(SW_RATIONAL); |
---|
260 | CanonicalForm temp= mapinto(rem); |
---|
261 | // CERR << "temp= " << temp << "\n"; |
---|
262 | // CERR << "lc(temp)= " << lc(temp) << "\n"; |
---|
263 | // CERR << "temp/lc(temp)= " << temp/lc(temp) << "\n"; |
---|
264 | // CERR << "num(rem/lc(rem))= " << num(rem/lc(rem)) << "\n"; |
---|
265 | |
---|
266 | // If as is of length 1, and its polynomial is level 1, then |
---|
267 | // we treat the first variable as algebraic and invert the leading |
---|
268 | // coefficient where this variable is part of the coefficient domain. |
---|
269 | |
---|
270 | if (as.length() == 1 && level(as.getFirst()) == 1) |
---|
271 | { |
---|
272 | CanonicalForm lcoeff = temp; |
---|
273 | while (level(lcoeff) > 1) |
---|
274 | { |
---|
275 | lcoeff = LC(lcoeff); |
---|
276 | } |
---|
277 | // out_cf("myfitting: lcoeff = ", lcoeff, "\n"); |
---|
278 | |
---|
279 | CanonicalForm p = as.getFirst(); |
---|
280 | // out_cf("myfitting: p = ", p, "\n"); |
---|
281 | |
---|
282 | CanonicalForm unused, inverse; |
---|
283 | |
---|
284 | extgcd(lcoeff, p, inverse, unused); |
---|
285 | // out_cf("myfitting: inverse = ", inverse, "\n"); |
---|
286 | |
---|
287 | // This may leave temp with non-integral coefficients, |
---|
288 | // which will be cleaned up below. |
---|
289 | temp = temp * inverse; |
---|
290 | // out_cf("myfitting: temp = ", temp, "\n"); |
---|
291 | } |
---|
292 | |
---|
293 | temp= bCommonDen(temp/lc(temp))*(temp/lc(temp)); |
---|
294 | Off(SW_RATIONAL); |
---|
295 | rem= mapinto(temp); |
---|
296 | return rem; |
---|
297 | } |
---|
298 | } |
---|
299 | else |
---|
300 | return rem; |
---|
301 | } |
---|
302 | |
---|
303 | CanonicalForm |
---|
304 | Prem( const CanonicalForm &f, const CFList &L ) |
---|
305 | { |
---|
306 | CanonicalForm rem = f; |
---|
307 | CFListIterator i = L; |
---|
308 | for ( i.lastItem(); i.hasItem(); i-- ) |
---|
309 | { |
---|
310 | //CERR << " PREM: Prem(" << rem << "," ; |
---|
311 | rem = Prem( rem, i.getItem() ); |
---|
312 | //CERR << " PREM: Prem(" << rem << "," << i.getItem() << ") = " << rem << "\n"; |
---|
313 | } |
---|
314 | return myfitting(rem, CFList()); |
---|
315 | } |
---|
316 | |
---|
317 | CanonicalForm |
---|
318 | Prem( const CanonicalForm &f, const CFList &L, const CFList &as ) |
---|
319 | { |
---|
320 | CanonicalForm rem = f; |
---|
321 | CFListIterator i = L; |
---|
322 | for ( i.lastItem(); i.hasItem(); i-- ) |
---|
323 | { |
---|
324 | //CERR << " PREM: Prem(" << rem << "," ; |
---|
325 | rem = Prem( rem, i.getItem() ); |
---|
326 | //CERR << " PREM: Prem(" << rem << "," << i.getItem() << ") = " << rem << "\n"; |
---|
327 | } |
---|
328 | return myfitting(rem, as); |
---|
329 | } |
---|
330 | |
---|
331 | CFList |
---|
332 | Prem( const CFList &AS, const CFList &L ) |
---|
333 | { |
---|
334 | CFList Output; |
---|
335 | |
---|
336 | for ( CFListIterator i=AS; i.hasItem(); i++ ) |
---|
337 | Output = Union(CFList(Prem(i.getItem(),L)), Output); |
---|
338 | |
---|
339 | return Output; |
---|
340 | } |
---|
341 | |
---|
342 | static CanonicalForm |
---|
343 | premasb( const CanonicalForm & f, const CFList & as) |
---|
344 | { |
---|
345 | CanonicalForm remd=f; |
---|
346 | CFList AS=as; |
---|
347 | |
---|
348 | if ( as.length() > 1 ) |
---|
349 | { |
---|
350 | AS.removeFirst(); // get rid of first elem |
---|
351 | CanonicalForm elem; |
---|
352 | while ( ! AS.isEmpty() ) |
---|
353 | { // thats true for at least the first iteration |
---|
354 | elem= AS.getLast(); |
---|
355 | remd= Prem(remd,elem); |
---|
356 | AS.removeLast(); |
---|
357 | } |
---|
358 | } |
---|
359 | CanonicalForm a,b; |
---|
360 | if ( mydivremt(remd, as.getFirst(), a,b )){ remd= remd.genZero();} |
---|
361 | else { remd= Prem(remd, as.getFirst()); } |
---|
362 | |
---|
363 | return remd; |
---|
364 | } |
---|
365 | |
---|
366 | CFList |
---|
367 | remsetb( const CFList & ps, const CFList & as) |
---|
368 | { |
---|
369 | CFList output; |
---|
370 | CanonicalForm elem; |
---|
371 | for (CFListIterator i=ps; i.hasItem(); i++) |
---|
372 | { |
---|
373 | elem= premasb(i.getItem(),as); |
---|
374 | if ( elem != elem.genZero() ) output.append(elem); |
---|
375 | } |
---|
376 | return output; |
---|
377 | } |
---|
378 | |
---|
379 | // for characteristic sets |
---|
380 | ////////////////////////////////// |
---|
381 | // replace the power of factors of polys in as by 1 if any |
---|
382 | static CFList |
---|
383 | nopower( const CanonicalForm & init ) |
---|
384 | { |
---|
385 | CFFList sqrfreelist;// = Factorize(init);//SqrFree(init); |
---|
386 | CFList output; |
---|
387 | CanonicalForm elem; |
---|
388 | int count=0; |
---|
389 | |
---|
390 | for ( CFIterator j=init; j.hasTerms(); j++ ) |
---|
391 | if (!(j.coeff().isOne()) ) count++; |
---|
392 | // if ( init != 1 ){ |
---|
393 | // CERR << "nopower: f is " << init << "\n"; |
---|
394 | // CERR << "nopower: count is " << count << "\n";} |
---|
395 | if ( count > 1 ) sqrfreelist = CFFList( CFFactor(init,1)); |
---|
396 | else |
---|
397 | { |
---|
398 | sqrfreelist = Factorize(init); |
---|
399 | //sqrfreelist.removeFirst(); |
---|
400 | } |
---|
401 | for ( CFFListIterator i=sqrfreelist; i.hasItem(); i++ ) |
---|
402 | { |
---|
403 | elem=i.getItem().factor(); |
---|
404 | if ( cls(elem) > 0 ) output.append(elem); |
---|
405 | } |
---|
406 | return output; |
---|
407 | } |
---|
408 | |
---|
409 | // remove the content of polys in PS; add the removed content to |
---|
410 | // Remembern.FS2 ( the set of removed factors ) |
---|
411 | CFList |
---|
412 | removecontent ( const CFList & PS, PremForm & Remembern ) |
---|
413 | { |
---|
414 | CFListIterator i=PS; |
---|
415 | if ((!i.hasItem()) || ( cls(PS.getFirst()) == 0 )) return PS; |
---|
416 | |
---|
417 | CFList output; |
---|
418 | CanonicalForm cc,elem; |
---|
419 | |
---|
420 | for (; i.hasItem(); i++) |
---|
421 | { |
---|
422 | elem = i.getItem(); |
---|
423 | cc = content(elem, elem.mvar()); |
---|
424 | if ( cls(cc) > 0 ) |
---|
425 | { |
---|
426 | output.append(elem/cc); |
---|
427 | Remembern.FS2 = Union(CFList(cc), Remembern.FS2); |
---|
428 | } |
---|
429 | else{ output.append(elem); } |
---|
430 | } |
---|
431 | return output; |
---|
432 | } |
---|
433 | |
---|
434 | // remove possible factors in Remember.FS1 from poly r |
---|
435 | // Remember.FS2 contains all factors removed before |
---|
436 | void |
---|
437 | removefactor( CanonicalForm & r , PremForm & Remembern) |
---|
438 | { |
---|
439 | int test; |
---|
440 | CanonicalForm a,b,testelem; |
---|
441 | CFList testlist; |
---|
442 | int n=level(r); |
---|
443 | CFListIterator j ; |
---|
444 | |
---|
445 | for ( int J=1; J<= n ; J++ ) |
---|
446 | { |
---|
447 | testlist.append(CanonicalForm(Variable(J))); |
---|
448 | } |
---|
449 | |
---|
450 | // testlist = Union(Remembern.FS1, testlist); // add candidates |
---|
451 | |
---|
452 | // remove already removed factors |
---|
453 | for ( j = Remembern.FS2 ; j.hasItem(); j++ ) |
---|
454 | { |
---|
455 | testelem = j.getItem(); |
---|
456 | while ( 1 ) |
---|
457 | { |
---|
458 | test = mydivremt(r,testelem,a,b); |
---|
459 | if ( test && b == r.genZero() ) r = a; |
---|
460 | else break; |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | // Let's look if we have other canditates to remove |
---|
465 | for ( j = testlist ; j.hasItem(); j++ ) |
---|
466 | { |
---|
467 | testelem = j.getItem(); |
---|
468 | // if ( testelem != r && testelem != r.mvar() ){ |
---|
469 | if ( testelem != r ) |
---|
470 | { |
---|
471 | while ( 1 ) |
---|
472 | { |
---|
473 | test = divremt(r,testelem,a,b); |
---|
474 | if ( test && b == r.genZero() ) |
---|
475 | { |
---|
476 | Remembern.FS2= Union(Remembern.FS2, CFList(testelem)); |
---|
477 | r = a; |
---|
478 | if ( r == 1 ) break; |
---|
479 | } |
---|
480 | else break; |
---|
481 | } |
---|
482 | } |
---|
483 | } |
---|
484 | // CERR << "Remembern.FS1 = " << Remembern.FS1 << "\n"; |
---|
485 | // CERR << "Remembern.FS2 = " << Remembern.FS2 << "\n"; |
---|
486 | // Remembern.FS1 = Difference(Remembern.FS1, Remembern.FS2); |
---|
487 | // CERR << " New Remembern.FS1 = " << Remembern.FS1 << "\n"; |
---|
488 | } |
---|
489 | |
---|
490 | |
---|
491 | // all irreducible nonconstant factors of a set of polynomials |
---|
492 | CFList |
---|
493 | factorps( const CFList &ps ) |
---|
494 | { |
---|
495 | CFList qs; |
---|
496 | CFFList q; |
---|
497 | CanonicalForm elem; |
---|
498 | |
---|
499 | for ( CFListIterator i=ps; i. hasItem(); i++ ) |
---|
500 | { |
---|
501 | q=Factorize(i.getItem()); |
---|
502 | q.removeFirst(); |
---|
503 | // Next can be simplified ( first (already removed) elem in q is the only constant |
---|
504 | for ( CFFListIterator j=q; j.hasItem(); j++ ) |
---|
505 | { |
---|
506 | elem = j.getItem().factor(); |
---|
507 | if ( getNumVars(elem) > 0 ) |
---|
508 | qs= Union(qs, CFList(myfitting(elem, CFList()))); |
---|
509 | } |
---|
510 | } |
---|
511 | return qs; |
---|
512 | } |
---|
513 | |
---|
514 | // the initial of poly f wrt to the order of the variables |
---|
515 | static CanonicalForm |
---|
516 | inital( const CanonicalForm &f ) |
---|
517 | { |
---|
518 | CanonicalForm leadcoeff; |
---|
519 | |
---|
520 | if ( cls(f) == 0 ) {return f.genOne(); } |
---|
521 | else |
---|
522 | { |
---|
523 | leadcoeff = LC(f,lvar(f)); |
---|
524 | // if ( leadcoeff != 0 ) |
---|
525 | return myfitting(leadcoeff, CFList()); //num(leadcoeff/lc(leadcoeff)); |
---|
526 | // else return leadcoeff; |
---|
527 | } |
---|
528 | } |
---|
529 | |
---|
530 | // the set of all nonconstant factors of initals of polys in as |
---|
531 | // CFList |
---|
532 | // initalset(const CFList &as){ |
---|
533 | // CanonicalForm elem; |
---|
534 | // CFList is, iss,iniset; |
---|
535 | |
---|
536 | // for ( CFListIterator i=as ; i.hasItem(); i++ ){ |
---|
537 | // elem = inital(i.getItem()); |
---|
538 | // if ( cls(elem) > 0 ) is.append(elem); |
---|
539 | // } |
---|
540 | // iss = factorps(is); |
---|
541 | // for ( CFListIterator j=iss; j.hasItem();j++ ){ |
---|
542 | // elem = j.getItem(); |
---|
543 | // if ( cls(elem) > 0 ) iniset.append(num(elem/lc(elem))); |
---|
544 | // } |
---|
545 | // return iniset; |
---|
546 | // } |
---|
547 | |
---|
548 | // the set of nonconstant initials of Cset |
---|
549 | // with certain repeated factors cancelled |
---|
550 | CFList |
---|
551 | initalset1(const CFList & Cset) |
---|
552 | { |
---|
553 | CFList temp; |
---|
554 | CFList initals; |
---|
555 | CanonicalForm init; |
---|
556 | |
---|
557 | for ( CFListIterator i = Cset ; i.hasItem(); i++ ) |
---|
558 | { |
---|
559 | initals= nopower( inital(i.getItem()) ); |
---|
560 | // init= inital(i.getItem()); |
---|
561 | for ( CFListIterator j = initals; j.hasItem(); j++) |
---|
562 | { |
---|
563 | init = j.getItem(); |
---|
564 | if ( cls(init) > 0 ) |
---|
565 | temp= Union(temp, CFList(init)); |
---|
566 | } |
---|
567 | } |
---|
568 | return temp; |
---|
569 | } |
---|
570 | |
---|
571 | // the set of nonconstant initials of Cset of those polys |
---|
572 | // not having their cls higher than reducible |
---|
573 | // with certain repeated factors cancelled |
---|
574 | CFList |
---|
575 | initalset2(const CFList & Cset, const CanonicalForm & reducible) |
---|
576 | { |
---|
577 | CFList temp; |
---|
578 | CFList initals; |
---|
579 | CanonicalForm init; |
---|
580 | int clsred = cls(reducible); |
---|
581 | |
---|
582 | for ( CFListIterator i = Cset ; i.hasItem(); i++ ) |
---|
583 | { |
---|
584 | init = i.getItem(); |
---|
585 | if ( cls(init) < clsred ) |
---|
586 | { |
---|
587 | initals= nopower( inital(init) ); |
---|
588 | // init= inital(i.getItem()); |
---|
589 | for ( CFListIterator j = initals; j.hasItem(); j++) |
---|
590 | { |
---|
591 | init = j.getItem(); |
---|
592 | if ( cls(init) > 0 ) |
---|
593 | temp= Union(temp, CFList(init)); |
---|
594 | } |
---|
595 | } |
---|
596 | } |
---|
597 | return temp; |
---|
598 | } |
---|
599 | |
---|
600 | //replace the power of factors of poly in CF init by 1 if any |
---|
601 | // and return the sqrfree poly |
---|
602 | // static CanonicalForm |
---|
603 | // nopower1( const CanonicalForm & init ){ |
---|
604 | // CFFList returnlist=Factorize(init); |
---|
605 | // CanonicalForm elem, test=init.genOne(); |
---|
606 | // for ( CFFListIterator i= returnlist; i.hasItem(); i++){ |
---|
607 | // elem = i.getItem().factor(); |
---|
608 | // if ( cls(elem)>0 ) test *= elem; |
---|
609 | // } |
---|
610 | // return test; |
---|
611 | // } |
---|
612 | |
---|
613 | // the sequence of distinct factors of f |
---|
614 | //CF pfactor( ..... ) |
---|
615 | |
---|
616 | // ////////////////////////////////////////// |
---|
617 | // // for IrrCharSeries |
---|
618 | |
---|
619 | #ifdef IRRCHARSERIESDEBUG |
---|
620 | # define DEBUGOUTPUT |
---|
621 | #else |
---|
622 | # undef DEBUGOUTPUT |
---|
623 | #endif |
---|
624 | #include "debug.h" |
---|
625 | // examine the irreducibility of as for IrrCharSeries |
---|
626 | int |
---|
627 | irreducible( const CFList & AS) |
---|
628 | { |
---|
629 | // AS is given by AS = { A1, A2, .. Ar }, d_i = degree(Ai) |
---|
630 | |
---|
631 | DEBOUTMSG(CERR, rcsid); |
---|
632 | // 1) we test: if d_i > 1, d_j =1 for all j<>i, then AS is irreducible. |
---|
633 | bool deg1=true; |
---|
634 | for ( CFListIterator i = AS ; i.hasItem(); i++ ) |
---|
635 | { |
---|
636 | if ( degree(i.getItem()) > 1 ) |
---|
637 | { |
---|
638 | if ( deg1 ) deg1=0; |
---|
639 | else return 0; // found 2nd poly with deg > 1 |
---|
640 | } |
---|
641 | } |
---|
642 | return 1; |
---|
643 | } |
---|
644 | |
---|
645 | |
---|
646 | // select an item from PS for irras |
---|
647 | CFList |
---|
648 | select( const ListCFList & PS) |
---|
649 | { |
---|
650 | return PS.getFirst(); |
---|
651 | } |
---|
652 | |
---|
653 | // divide list ppi in elems having length <= and > length |
---|
654 | void |
---|
655 | select( const ListCFList & ppi, int length, ListCFList & ppi1, ListCFList & ppi2) |
---|
656 | { |
---|
657 | CFList elem; |
---|
658 | for ( ListCFListIterator i=ppi ; i.hasItem(); i++ ) |
---|
659 | { |
---|
660 | elem = i.getItem(); |
---|
661 | if ( ! elem.isEmpty() ) |
---|
662 | if ( length <= elem.length() ){ ppi2.append(elem); } |
---|
663 | else { ppi1.append(elem); } |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | |
---|
668 | ////////////////////////////////////////////////////////////// |
---|
669 | // help-functions for sets |
---|
670 | |
---|
671 | // is f in F ? |
---|
672 | static bool |
---|
673 | member( const CanonicalForm &f, const CFList &F) |
---|
674 | { |
---|
675 | for ( CFListIterator i=F; i.hasItem(); i++ ) |
---|
676 | if ( i.getItem() == f ) return 1; |
---|
677 | return 0; |
---|
678 | } |
---|
679 | |
---|
680 | // are list A and B the same? |
---|
681 | bool |
---|
682 | same( const CFList &A, const CFList &B ) |
---|
683 | { |
---|
684 | CFListIterator i; |
---|
685 | |
---|
686 | for (i = A; i.hasItem(); i++) |
---|
687 | if (! member(i.getItem(), B) ) return 0; |
---|
688 | for (i = B; i.hasItem(); i++) |
---|
689 | if (! member(i.getItem(), A) ) return 0; |
---|
690 | return 1; |
---|
691 | } |
---|
692 | |
---|
693 | |
---|
694 | // is List cs contained in List of lists pi? |
---|
695 | bool |
---|
696 | member( const CFList & cs, const ListCFList & pi ) |
---|
697 | { |
---|
698 | ListCFListIterator i; |
---|
699 | CFList elem; |
---|
700 | |
---|
701 | for ( i=pi; i.hasItem(); i++) |
---|
702 | { |
---|
703 | elem = i.getItem(); |
---|
704 | if ( same(cs,elem) ) return 1; |
---|
705 | } |
---|
706 | return 0; |
---|
707 | } |
---|
708 | |
---|
709 | // is PS a subset of Cset ? |
---|
710 | bool |
---|
711 | subset( const CFList &PS, const CFList &Cset ) |
---|
712 | { |
---|
713 | // CERR << "subset: called with: " << PS << " " << Cset << "\n"; |
---|
714 | for ( CFListIterator i=PS; i.hasItem(); i++ ) |
---|
715 | if ( ! member(i.getItem(), Cset) ) |
---|
716 | { |
---|
717 | // CERR << "subset: " << i.getItem() << " is not a member of " << Cset << "\n"; |
---|
718 | return 0; |
---|
719 | } |
---|
720 | return 1; |
---|
721 | } |
---|
722 | |
---|
723 | // Union of two List of Lists |
---|
724 | ListCFList |
---|
725 | MyUnion( const ListCFList & a, const ListCFList &b ) |
---|
726 | { |
---|
727 | ListCFList output; |
---|
728 | ListCFListIterator i; |
---|
729 | CFList elem; |
---|
730 | |
---|
731 | for ( i = a ; i.hasItem(); i++ ) |
---|
732 | { |
---|
733 | elem=i.getItem(); |
---|
734 | if ( (! elem.isEmpty()) && ( ! member(elem,output)) ) |
---|
735 | { |
---|
736 | output.append(elem); |
---|
737 | } |
---|
738 | } |
---|
739 | |
---|
740 | for ( i = b ; i.hasItem(); i++ ) |
---|
741 | { |
---|
742 | elem=i.getItem(); |
---|
743 | if ( (! elem.isEmpty()) && ( ! member(elem,output)) ) |
---|
744 | { |
---|
745 | output.append(elem); |
---|
746 | } |
---|
747 | } |
---|
748 | return output; |
---|
749 | } |
---|
750 | |
---|
751 | //if list b is member of the list of lists remove b and return the rest |
---|
752 | ListCFList |
---|
753 | MyDifference( const ListCFList & a, const CFList &b) |
---|
754 | { |
---|
755 | ListCFList output; |
---|
756 | ListCFListIterator i; |
---|
757 | CFList elem; |
---|
758 | |
---|
759 | for ( i = a ; i.hasItem(); i++ ) |
---|
760 | { |
---|
761 | elem=i.getItem(); |
---|
762 | if ( (! elem.isEmpty()) && ( ! same(elem,b)) ) |
---|
763 | { |
---|
764 | output.append(elem); |
---|
765 | } |
---|
766 | } |
---|
767 | return output; |
---|
768 | } |
---|
769 | |
---|
770 | // remove all elements of b from list of lists a and return the rest |
---|
771 | ListCFList |
---|
772 | Minus( const ListCFList & a, const ListCFList & b) |
---|
773 | { |
---|
774 | ListCFList output=a; |
---|
775 | |
---|
776 | for ( ListCFListIterator i=b; i.hasItem(); i++ ) |
---|
777 | output = MyDifference(output, i.getItem() ); |
---|
778 | |
---|
779 | return output; |
---|
780 | } |
---|
781 | |
---|
782 | #if 0 |
---|
783 | static CanonicalForm alg_lc(const CanonicalForm &f, const CFList as) |
---|
784 | { |
---|
785 | for(CFListIterator i=as; i.hasItem(); i++) |
---|
786 | { |
---|
787 | if (f.mvar()==i.getItem().mvar()) return f; |
---|
788 | } |
---|
789 | if (f.level()>0) |
---|
790 | { |
---|
791 | return alg_lc(f.LC(),as); |
---|
792 | } |
---|
793 | return CanonicalForm(1); |
---|
794 | } |
---|
795 | #endif |
---|
796 | |
---|
797 | CanonicalForm alg_gcd(const CanonicalForm & fff, const CanonicalForm &ggg, |
---|
798 | const CFList &as) |
---|
799 | { |
---|
800 | CanonicalForm f=fff; |
---|
801 | CanonicalForm g=ggg; |
---|
802 | f=Prem(f,as); |
---|
803 | g=Prem(g,as); |
---|
804 | if ( f.isZero() ) |
---|
805 | { |
---|
806 | if ( g.lc().sign() < 0 ) return -g; |
---|
807 | else return g; |
---|
808 | } |
---|
809 | else if ( g.isZero() ) |
---|
810 | { |
---|
811 | if ( f.lc().sign() < 0 ) return -f; |
---|
812 | else return f; |
---|
813 | } |
---|
814 | //out_cf("alg_gcd(",fff," , "); |
---|
815 | //out_cf("",ggg,")\n"); |
---|
816 | CanonicalForm res; |
---|
817 | // does as appear in f and g ? |
---|
818 | bool has_alg_var=false; |
---|
819 | for ( CFListIterator j=as;j.hasItem(); j++ ) |
---|
820 | { |
---|
821 | Variable v=j.getItem().mvar(); |
---|
822 | if (hasVar(f,v)) {has_alg_var=true; /*break;*/} |
---|
823 | if (hasVar(g,v)) {has_alg_var=true; /*break;*/} |
---|
824 | //out_cf("as:",j.getItem(),"\n"); |
---|
825 | } |
---|
826 | if (!has_alg_var) |
---|
827 | { |
---|
828 | if ((hasAlgVar(f)) |
---|
829 | || (hasAlgVar(g))) |
---|
830 | { |
---|
831 | Varlist ord; |
---|
832 | for ( CFListIterator j=as;j.hasItem(); j++ ) |
---|
833 | ord.append(j.getItem().mvar()); |
---|
834 | res=algcd(f,g,as,ord); |
---|
835 | } |
---|
836 | else |
---|
837 | res=gcd(f,g); |
---|
838 | //out_cf("gcd=",res,"\n"); |
---|
839 | //out_cf("of f=",fff," , "); |
---|
840 | //out_cf("and g=",ggg,"\n"); |
---|
841 | |
---|
842 | return res; |
---|
843 | } |
---|
844 | |
---|
845 | int mvf=f.level(); |
---|
846 | int mvg=g.level(); |
---|
847 | if (mvg > mvf) |
---|
848 | { |
---|
849 | CanonicalForm tmp=f; f=g; g=tmp; |
---|
850 | int tmp2=mvf; mvf=mvg; mvg=tmp2; |
---|
851 | } |
---|
852 | if (g.inBaseDomain() || f.inBaseDomain()) |
---|
853 | { |
---|
854 | //printf("const\n"); |
---|
855 | //out_cf("of f=",fff," , "); |
---|
856 | //out_cf("and g=",ggg,"\n"); |
---|
857 | return CanonicalForm(1); |
---|
858 | } |
---|
859 | |
---|
860 | // gcd of all coefficients: |
---|
861 | CFIterator i=f; |
---|
862 | CanonicalForm c_gcd=i.coeff(); i++; |
---|
863 | while( i.hasTerms()) |
---|
864 | { |
---|
865 | c_gcd=alg_gcd(i.coeff(),c_gcd,as); |
---|
866 | if (c_gcd.inBaseDomain()) break; |
---|
867 | i++; |
---|
868 | } |
---|
869 | //printf("f.mvar=%d (%d), g.mvar=%d (%d)\n",f.level(),mvf,g.level(),mvg); |
---|
870 | if (mvf!=mvg) // => mvf > mvg |
---|
871 | { |
---|
872 | res=alg_gcd(g,c_gcd,as); |
---|
873 | //out_cf("alg_gcd1=",res,"\n"); |
---|
874 | //out_cf("of f=",fff," , "); |
---|
875 | //out_cf("and g=",ggg,"\n"); |
---|
876 | return res; |
---|
877 | } |
---|
878 | // now: mvf==mvg, f.level()==g.level() |
---|
879 | if (!c_gcd.inBaseDomain()) |
---|
880 | { |
---|
881 | i=g; |
---|
882 | while( i.hasTerms()) |
---|
883 | { |
---|
884 | c_gcd=alg_gcd(i.coeff(),c_gcd,as); |
---|
885 | if (c_gcd.inBaseDomain()) break; |
---|
886 | i++; |
---|
887 | } |
---|
888 | } |
---|
889 | |
---|
890 | //f/=c_gcd; |
---|
891 | //g/=c_gcd; |
---|
892 | if (!c_gcd.isOne()) |
---|
893 | { |
---|
894 | f=divide(f,c_gcd,as); |
---|
895 | g=divide(g,c_gcd,as); |
---|
896 | } |
---|
897 | |
---|
898 | CFList gg; |
---|
899 | CanonicalForm r=1; |
---|
900 | while (1) |
---|
901 | { |
---|
902 | //printf("f.mvar=%d, g.mvar=%d\n",f.level(),g.level()); |
---|
903 | gg=as; |
---|
904 | if (!g.inCoeffDomain()) gg.append(g); |
---|
905 | //out_cf("Prem(",f," , "); |
---|
906 | //out_cf("",g,")\n"); |
---|
907 | if (g.inCoeffDomain()|| g.isZero()) |
---|
908 | { |
---|
909 | //printf("in coeff domain:"); |
---|
910 | if (g.isZero()) { //printf("0\n"); |
---|
911 | i=f; |
---|
912 | CanonicalForm f_gcd=i.coeff(); i++; |
---|
913 | while( i.hasTerms()) |
---|
914 | { |
---|
915 | f_gcd=alg_gcd(i.coeff(),f_gcd,as); |
---|
916 | if (f_gcd.inBaseDomain()) break; |
---|
917 | i++; |
---|
918 | } |
---|
919 | //out_cf("g=0 -> f:",f,"\n"); |
---|
920 | //out_cf("f_gcd:",f_gcd,"\n"); |
---|
921 | //out_cf("c_gcd:",c_gcd,"\n"); |
---|
922 | //f/=f_gcd; |
---|
923 | f=divide(f,f_gcd,as); |
---|
924 | //out_cf("f/f_gcd:",f,"\n"); |
---|
925 | f*=c_gcd; |
---|
926 | //out_cf("f*c_gcd:",f,"\n"); |
---|
927 | CanonicalForm r_lc=alg_lc(f); |
---|
928 | //out_cf("r_lc:",r_lc,"\n"); |
---|
929 | //f/=r_lc; |
---|
930 | f=divide(f,r_lc,as); |
---|
931 | //out_cf(" -> gcd:",f,"\n"); |
---|
932 | return f; |
---|
933 | } |
---|
934 | else { //printf("c\n"); |
---|
935 | return c_gcd;} |
---|
936 | } |
---|
937 | else if (g.level()==f.level()) r=Prem(f,gg,as); |
---|
938 | else |
---|
939 | { |
---|
940 | //printf("diff. level: %d, %d\n", f.level(), g.level()); |
---|
941 | // g and f have different levels |
---|
942 | //out_cf("c_gcd=",c_gcd,"\n"); |
---|
943 | //out_cf("of f=",fff," , "); |
---|
944 | //out_cf("and g=",ggg,"\n"); |
---|
945 | return c_gcd; |
---|
946 | } |
---|
947 | //out_cf("->",r,"\n"); |
---|
948 | f=g; |
---|
949 | g=r; |
---|
950 | } |
---|
951 | if (degree(f,Variable(mvg))==0) |
---|
952 | { |
---|
953 | // remark: mvf == mvg == f.level() ==g.level() |
---|
954 | //out_cf("c_gcd=",c_gcd,"\n"); |
---|
955 | //out_cf("of f=",fff," , "); |
---|
956 | //out_cf("and g=",ggg,"\n"); |
---|
957 | return c_gcd; |
---|
958 | } |
---|
959 | //out_cf("f=",f,"\n"); |
---|
960 | i=f; |
---|
961 | CanonicalForm k=i.coeff(); i++; |
---|
962 | //out_cf("k=",k,"\n"); |
---|
963 | while( i.hasTerms()) |
---|
964 | { |
---|
965 | if (k.inCoeffDomain()) break; |
---|
966 | k=alg_gcd(i.coeff(),k,as); |
---|
967 | //out_cf("k=",k,"\n"); |
---|
968 | i++; |
---|
969 | } |
---|
970 | //out_cf("end-k=",k,"\n"); |
---|
971 | f/=k; |
---|
972 | //out_cf("f/k=",f,"\n"); |
---|
973 | res=c_gcd*f; |
---|
974 | CanonicalForm r_lc=alg_lc(res); |
---|
975 | res/=r_lc; |
---|
976 | //CanonicalForm r_lc=alg_lc(res,as); |
---|
977 | //res/=r_lc; |
---|
978 | //out_cf("alg_gcd2=",res,"\n"); |
---|
979 | // out_cf("of f=",fff," , "); |
---|
980 | // out_cf("and g=",ggg,"\n"); |
---|
981 | //return res; |
---|
982 | //if (res.level()<fff.level() && res.level() < ggg.level()) |
---|
983 | // return alg_gcd(alg_gcd(fff,res,as),ggg,as); |
---|
984 | //else |
---|
985 | return res; |
---|
986 | } |
---|
987 | |
---|
988 | /* |
---|
989 | $Log: not supported by cvs2svn $ |
---|
990 | Revision 1.14 2007/05/15 14:46:48 Singular |
---|
991 | *hannes: factorize in Zp(a)[x...] |
---|
992 | |
---|
993 | Revision 1.13 2006/06/19 13:37:47 Singular |
---|
994 | *hannes: more CS renamed |
---|
995 | |
---|
996 | Revision 1.12 2006/05/16 14:46:49 Singular |
---|
997 | *hannes: gcc 4.1 fixes |
---|
998 | |
---|
999 | Revision 1.11 2006/05/16 13:48:13 Singular |
---|
1000 | *hannes: gcc 4.1 fix: factory changes |
---|
1001 | |
---|
1002 | Revision 1.10 2006/04/28 13:45:29 Singular |
---|
1003 | *hannes: better tests for 0, 1 |
---|
1004 | |
---|
1005 | Revision 1.9 2003/05/28 11:52:52 Singular |
---|
1006 | *pfister/hannes: newfactoras, alg_gcd, divide (see bug_33) |
---|
1007 | |
---|
1008 | Revision 1.8 2002/10/24 17:22:22 Singular |
---|
1009 | * hannes: factoring in alg.ext., alg_gcd, NTL stuff |
---|
1010 | |
---|
1011 | Revision 1.7 2002/08/19 11:11:31 Singular |
---|
1012 | * hannes/pfister: alg_gcd etc. |
---|
1013 | |
---|
1014 | Revision 1.6 2002/01/21 09:11:07 Singular |
---|
1015 | * hannes: handle empty set in removecontent |
---|
1016 | |
---|
1017 | Revision 1.5 2001/06/21 14:57:04 Singular |
---|
1018 | *hannes/GP: Factorize, newfactoras, ... |
---|
1019 | |
---|
1020 | Revision 1.4 1998/03/12 12:34:35 schmidt |
---|
1021 | * charset/csutil.cc, charset/alg_factor.cc: all references to |
---|
1022 | `common_den()' replaced by `bCommonDen()' |
---|
1023 | |
---|
1024 | Revision 1.3 1997/09/12 07:19:41 Singular |
---|
1025 | * hannes/michael: libfac-0.3.0 |
---|
1026 | |
---|
1027 | Revision 1.3 1997/04/25 22:55:00 michael |
---|
1028 | Version for libfac-0.2.1 |
---|
1029 | |
---|
1030 | Revision 1.2 1996/12/13 05:53:42 michael |
---|
1031 | fixed a typo in nopower |
---|
1032 | |
---|
1033 | */ |
---|