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