1 | /* Copyright 1997 Michael Messollen. All rights reserved. */ |
---|
2 | //////////////////////////////////////////////////////////// |
---|
3 | // emacs edit mode for this file is -*- C++ -*- |
---|
4 | //////////////////////////////////////////////////////////// |
---|
5 | static char * rcsid = "$Id: algfactor.cc,v 1.1.1.1 1997-09-11 10:58:22 Singular Exp $"; |
---|
6 | //////////////////////////////////////////////////////////// |
---|
7 | // FACTORY - Includes |
---|
8 | #include <factory.h> |
---|
9 | // Factor - Includes |
---|
10 | #include <tmpl_inst.h> |
---|
11 | #include <Factor.h> |
---|
12 | #include <helpstuff.h> |
---|
13 | // Charset - Includes |
---|
14 | #include "csutil.h" |
---|
15 | #include "charset.h" |
---|
16 | #include "reorder.h" |
---|
17 | #include "alg_factor.h" |
---|
18 | // some CC's need this: |
---|
19 | #include "algfactor.h" |
---|
20 | |
---|
21 | #ifdef ALGFACTORDEBUG |
---|
22 | # define DEBUGOUTPUT |
---|
23 | #else |
---|
24 | # undef DEBUGOUTPUT |
---|
25 | #endif |
---|
26 | |
---|
27 | #include "debug.h" |
---|
28 | #include "timing.h" |
---|
29 | TIMING_DEFINE_PRINT(newfactoras_time); |
---|
30 | |
---|
31 | static CFFList |
---|
32 | myminus( const CFFList & Inputlist, const CFFactor & TheFactor){ |
---|
33 | CFFList Outputlist ; |
---|
34 | CFFactor copy; |
---|
35 | |
---|
36 | for ( CFFListIterator i=Inputlist ; i.hasItem() ; i++ ){ |
---|
37 | copy = i.getItem(); |
---|
38 | if ( copy.factor() != TheFactor.factor() ){ |
---|
39 | Outputlist.append( copy ); |
---|
40 | } |
---|
41 | } |
---|
42 | return Outputlist; |
---|
43 | } |
---|
44 | |
---|
45 | static CFFList |
---|
46 | myDifference(const CFFList & Inputlist1,const CFFList & Inputlist2){ |
---|
47 | CFFList Outputlist=Inputlist1; |
---|
48 | |
---|
49 | for ( CFFListIterator i=Inputlist2 ; i.hasItem() ; i++ ) |
---|
50 | Outputlist = myminus(Outputlist, i.getItem() ); |
---|
51 | |
---|
52 | return Outputlist; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | // return 1 if as is quasilinear; 0 if not. |
---|
57 | static int |
---|
58 | isquasilinear( const CFList & as ){ |
---|
59 | if (as.length() <= 1) { return 1; } |
---|
60 | else { |
---|
61 | CFList AS=as; |
---|
62 | AS.removeFirst(); // we are not interested in the first elem |
---|
63 | for ( CFListIterator i=AS; i.hasItem(); i++) |
---|
64 | if ( degree(i.getItem()) > 1 ) return 0; |
---|
65 | } |
---|
66 | return 1; |
---|
67 | } |
---|
68 | |
---|
69 | #ifdef CHARSETNADEBUG |
---|
70 | # define DEBUGOUTPUT |
---|
71 | #else |
---|
72 | # undef DEBUGOUTPUT |
---|
73 | #endif |
---|
74 | #include "debug.h" |
---|
75 | static CFList |
---|
76 | charsetnA(const CFList & AS, const CFList &PS, PremForm & Remembern, const Variable & vf ){ |
---|
77 | CFList QS = PS, RS = PS, CS; |
---|
78 | int nas= AS.length() +1; |
---|
79 | |
---|
80 | DEBOUTLN(cout, "charsetnA: called with ps= ", PS); |
---|
81 | while ( ! RS.isEmpty() ) { |
---|
82 | CS = BasicSet( QS ); |
---|
83 | DEBOUTLN(cout, "charsetnA: CS= ", CS); |
---|
84 | CS=Union(CS,AS); |
---|
85 | DEBOUTLN(cout, "charsetnA: CS= ", CS); |
---|
86 | Remembern.FS1 = Union(Remembern.FS1, initalset1(CS)); |
---|
87 | DEBOUTLN(cout, "charsetnA: Remembern.FS1= ", Remembern.FS1); |
---|
88 | DEBOUTLN(cout, "charsetnA: Remembern.FS2= ", Remembern.FS2); |
---|
89 | RS = CFList(); |
---|
90 | if ( CS.length() == nas && degree(CS.getLast(),vf) > 0 ) { |
---|
91 | CFList D = Difference( QS, CS ); |
---|
92 | DEBOUT(cout, "charsetnA: Difference( ", QS); |
---|
93 | DEBOUT(cout, " , ", CS); |
---|
94 | DEBOUTLN(cout, " ) = ", D); |
---|
95 | for ( CFListIterator i = D; i.hasItem(); ++i ) { |
---|
96 | CanonicalForm r = Prem( i.getItem(), CS ); |
---|
97 | DEBOUT(cout,"charsetnA: Prem(", i.getItem() ); |
---|
98 | DEBOUT(cout, ",", CS); |
---|
99 | DEBOUTLN(cout,") = ", r); |
---|
100 | if ( r != 0 ){ |
---|
101 | //removefactor( r, Remembern ); |
---|
102 | RS=Union(RS,CFList(r)); |
---|
103 | } |
---|
104 | } |
---|
105 | if ( ! checkok(RS,Remembern.FS2)) return CFList(CanonicalForm(1)); |
---|
106 | DEBOUTLN(cout, "charsetnA: RS= ", RS); |
---|
107 | //QS = Union( QS, RS ); |
---|
108 | QS=Union(AS,RS); QS.append(CS.getLast()); |
---|
109 | DEBOUTLN(cout, "charsetnA: QS= Union(QS,RS)= ", QS); |
---|
110 | } |
---|
111 | else{ return CFList(CanonicalForm(1)); } |
---|
112 | } |
---|
113 | DEBOUTLN(cout, "charsetnA: Removed factors: ", Remembern.FS2); |
---|
114 | DEBOUTLN(cout, "charsetnA: Remembern.FS1: ", Remembern.FS1); |
---|
115 | |
---|
116 | return CS; |
---|
117 | } |
---|
118 | |
---|
119 | #ifdef ALGFACTORDEBUG |
---|
120 | # define DEBUGOUTPUT |
---|
121 | #else |
---|
122 | # undef DEBUGOUTPUT |
---|
123 | #endif |
---|
124 | #include "debug.h" |
---|
125 | // compute the GCD of f and g over the algebraic field having |
---|
126 | // adjoing ascending set as |
---|
127 | CanonicalForm |
---|
128 | algcd(const CanonicalForm & F, const CanonicalForm & g, const CFList & as, const Varlist & order){ |
---|
129 | CanonicalForm f=F; |
---|
130 | int nas= as.length()+1; // the length the new char. set should have! |
---|
131 | Variable vf=f.mvar(); |
---|
132 | |
---|
133 | // for ( VarlistIterator i=order; i.hasItem() ; i++ ){ |
---|
134 | // vf= i.getItem(); |
---|
135 | // nas--; |
---|
136 | // if ( nas==0 ) break; |
---|
137 | // } |
---|
138 | // nas= as.length()+1; |
---|
139 | |
---|
140 | DEBOUTLN(cout, "algcd called with f= ", f); |
---|
141 | DEBOUTLN(cout, " g= ", g); |
---|
142 | DEBOUTLN(cout, " as= ", as); |
---|
143 | DEBOUTLN(cout, " order= ", order); |
---|
144 | DEBOUTLN(cout, " choosen vf= ", vf); |
---|
145 | |
---|
146 | // check trivial case: |
---|
147 | if ( degree(f, order.getLast())==0 || degree(g, order.getLast())==0) |
---|
148 | return CanonicalForm(1); |
---|
149 | |
---|
150 | |
---|
151 | CFList bs; bs.append(f); bs.append(g); |
---|
152 | PremForm Remembern; |
---|
153 | CFList cs=charsetnA(as,bs,Remembern,vf); |
---|
154 | DEBOUTLN(cout, "CharSetA((as,bs))= ", cs); |
---|
155 | |
---|
156 | // for ( VarlistIterator i=order; i.hasItem() ; i++ ){ |
---|
157 | // DEBOUTLN(cout, "vf= ", i.getItem()); |
---|
158 | // DEBOUTLN(cout, "CharSetA((as,bs))= " , charsetnA(as,bs,Remembern,i.getItem())); |
---|
159 | // } |
---|
160 | |
---|
161 | CanonicalForm result; |
---|
162 | if (cs.length()==nas) result= cs.getLast(); |
---|
163 | else result= CanonicalForm(1); |
---|
164 | |
---|
165 | return result; |
---|
166 | } |
---|
167 | |
---|
168 | CFFList |
---|
169 | factoras( const CanonicalForm & f, const CFList & as, int & success ){ |
---|
170 | Variable vf=f.mvar(); |
---|
171 | CFListIterator i; |
---|
172 | CFFListIterator jj; |
---|
173 | CFList reduceresult; |
---|
174 | CFFList result; |
---|
175 | |
---|
176 | DEBINCLEVEL(cout, "factoras"); |
---|
177 | DEBOUTMSG(cerr, rcsid); |
---|
178 | DEBOUTLN(cout, "factoras called with f= ", f); |
---|
179 | DEBOUTLN(cout, " content(f)= ", content(f)); |
---|
180 | DEBOUTLN(cout, " as= ", as); |
---|
181 | DEBOUTLN(cout, "factoras: cls(vf)= ", cls(vf)); |
---|
182 | DEBOUTLN(cout, "factoras: cls(as.getLast())= ", cls(as.getLast())); |
---|
183 | DEBOUTLN(cout, "factoras: degree(f,vf)= ", degree(f,vf)); |
---|
184 | |
---|
185 | // F1: [Test trivial cases] |
---|
186 | // 1) first trivial cases: |
---|
187 | if ( (cls(vf) <= cls(as.getLast())) || |
---|
188 | degree(f,vf)<=1 |
---|
189 | // ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) ) |
---|
190 | ){ |
---|
191 | success=1; |
---|
192 | DEBDECLEVEL(cout,"factoras"); |
---|
193 | return CFFList(CFFactor(f,1)); |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | // 2) List of variables: |
---|
198 | // 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1 |
---|
199 | // 2b) Setup variableordering |
---|
200 | CFList Astar; |
---|
201 | Variable x; |
---|
202 | CanonicalForm elem; |
---|
203 | Varlist ord, uord,oldord; |
---|
204 | for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii)); } |
---|
205 | oldord= uord; oldord.append(vf); |
---|
206 | |
---|
207 | for ( i=as; i.hasItem(); i++ ){ |
---|
208 | elem= i.getItem(); |
---|
209 | x= elem.mvar(); |
---|
210 | if ( degree(elem,x) > 1){ // otherwise it's not an extension |
---|
211 | //if ( degree(f,x) > 0 ){ // does it occure in f? RICHTIG? |
---|
212 | Astar.append(elem); |
---|
213 | ord.append(x); |
---|
214 | //} |
---|
215 | } |
---|
216 | } |
---|
217 | uord= Difference(uord,ord); |
---|
218 | DEBOUTLN(cout, "Astar is: ", Astar); |
---|
219 | DEBOUTLN(cout, "ord is: ", ord); |
---|
220 | DEBOUTLN(cout, "uord is: ", uord); |
---|
221 | |
---|
222 | // 3) second trivial cases |
---|
223 | if ( Astar.length() == 0 ){ |
---|
224 | success=1; |
---|
225 | DEBDECLEVEL(cout,"factoras"); |
---|
226 | return CFFList(CFFactor(f,1)); |
---|
227 | } |
---|
228 | |
---|
229 | // 4) Try to obtain a partial factorization using prop2 and prop3 |
---|
230 | // Not yet implemented |
---|
231 | |
---|
232 | // 5) choose (d_1, ..., d_s) |
---|
233 | FFRandom gen; |
---|
234 | if ( getCharacteristic() == 0 ) IntRandom gen(100); |
---|
235 | REvaluation D(1, ord.length(), gen); |
---|
236 | //D.nextpoint(); // first we use 0 as evaluation point |
---|
237 | |
---|
238 | F2: //[Characteristic set computation] |
---|
239 | // 6) Compute f^* (<- fstar) |
---|
240 | CanonicalForm substhin=CanonicalForm(vf), substback=CanonicalForm(vf); |
---|
241 | CanonicalForm monom; |
---|
242 | DEBOUTLN(cout, "substhin= ", substhin); |
---|
243 | DEBOUTLN(cout, "substback= ", substback); |
---|
244 | int j=1; |
---|
245 | for ( VarlistIterator jjj=ord; jjj.hasItem(); jjj++){ |
---|
246 | if ( getCharacteristic() > 0 ) monom= jjj.getItem()*D[j]; |
---|
247 | else monom= mapinto(jjj.getItem()*mapinto(D[j])); |
---|
248 | j++; |
---|
249 | substhin-= monom; substback+= monom; |
---|
250 | } |
---|
251 | DEBOUTLN(cout, "substhin= ", substhin); |
---|
252 | DEBOUTLN(cout, "substback= ", substback); |
---|
253 | CanonicalForm fstar=f(substhin,vf); |
---|
254 | DEBOUTLN(cout, "fstar= ", fstar); |
---|
255 | |
---|
256 | // 7) Set up Variable ordering from ord,vf to vf,ord ! i.e. vf is the variable |
---|
257 | // with lowest level. |
---|
258 | Varlist nord=uord; |
---|
259 | nord.append(vf); nord= Union(ord,nord); |
---|
260 | DEBOUTLN(cout, " nord= ", nord); |
---|
261 | CFList Astarnord= Astar; Astarnord.insert(fstar); |
---|
262 | DEBOUTLN(cout, " Astarnord= ", Astarnord); |
---|
263 | Astarnord= reorder(nord,Astarnord); |
---|
264 | DEBOUTLN(cout, "original Astar= ", Astar); |
---|
265 | DEBOUTLN(cout, "reorderd Astar= ", Astarnord); |
---|
266 | DEBOUTLN(cout, " f= ", f); |
---|
267 | DEBOUTLN(cout, " fstar= ", fstar); |
---|
268 | |
---|
269 | // 8) Compute Charset Cstar of Astar \cup { fstar } wrt. ordering {vf, ord} |
---|
270 | PremForm Remembern; |
---|
271 | CFList Cstar= MCharSetN(Astarnord, Remembern); |
---|
272 | DEBOUTLN(cout, " Cstar= ", Cstar ); |
---|
273 | DEBOUTLN(cout, " Factors removed= ", Remembern.FS2 ); |
---|
274 | DEBOUTLN(cout, " Possible Factors considered:= ", Remembern.FS1 ); |
---|
275 | DEBOUTLN(cout, " Reorderd Cstar= ", reorder(nord,Cstar)); |
---|
276 | |
---|
277 | // 9) Compute Delta: the set of all irr. factors (over K_0) of initials of |
---|
278 | // Cstar |
---|
279 | CFList iniset= initalset1(Cstar); |
---|
280 | DEBOUTLN(cout, "Set of initials: ", iniset); |
---|
281 | CFFList Delta; |
---|
282 | CFFList temp; |
---|
283 | for ( i=iniset; i.hasItem(); i++){ |
---|
284 | Off(SW_RATIONAL); |
---|
285 | temp= Factorize(i.getItem()); |
---|
286 | On(SW_RATIONAL); |
---|
287 | temp.removeFirst(); |
---|
288 | Delta= Union(temp,Delta); |
---|
289 | } |
---|
290 | DEBOUTLN(cout, "Delta= ", Delta); |
---|
291 | |
---|
292 | // 10) Compute Psi: the irreduzible factors (over K_0) of the first polynomial |
---|
293 | // in Cstar, which are not factors of any polynomial in Delta |
---|
294 | Off(SW_RATIONAL); |
---|
295 | CFFList Psi=Factorize(Cstar.getFirst()); |
---|
296 | On(SW_RATIONAL); |
---|
297 | Psi.removeFirst(); |
---|
298 | Psi= myDifference(Psi,Delta); |
---|
299 | DEBOUTLN(cout, "Psi= ", Psi); |
---|
300 | |
---|
301 | // F3: [Test quasilinearity] |
---|
302 | // If Cstar is quasilinear -> F4 |
---|
303 | // else if Psi.length() > 1 then Delta<- Delta \cup Psi; Psi<- \emptyset -> F4 |
---|
304 | // else D.nextpoint() -> F2 |
---|
305 | if ( isquasilinear(Cstar) ) { |
---|
306 | DEBOUTLN(cout, "Cstar is quasilinear; going to F4, Cstar= ", Cstar); |
---|
307 | goto F4; |
---|
308 | } |
---|
309 | else if ( Psi.length() > 1 ){ |
---|
310 | Delta= Union(Psi,Delta); |
---|
311 | Psi= CFFList(); |
---|
312 | DEBOUTMSG(cout, "Psi.length() == 1; going to F4"); |
---|
313 | goto F4; |
---|
314 | } |
---|
315 | else{ |
---|
316 | D.nextpoint(); |
---|
317 | DEBOUTMSG(cout, "Choosing next evaluation point. going to F2"); |
---|
318 | goto F2; |
---|
319 | } |
---|
320 | |
---|
321 | F4: //[GCD Computation] |
---|
322 | CanonicalForm g=f; |
---|
323 | Delta= reorder(nord,Delta); |
---|
324 | Psi= reorder(nord,Psi); |
---|
325 | DEBOUTLN(cout, "Reordered: Delta= ", Delta); |
---|
326 | DEBOUTLN(cout, " Psi= ", Psi); |
---|
327 | CanonicalForm fp; |
---|
328 | |
---|
329 | DEBOUTMSG(cout, "Testing Psi: this gives irreducible Factors!"); |
---|
330 | for (jj=Psi; jj.hasItem(); jj++){ |
---|
331 | if ( degree(g,vf) == 1 ) { // g is linear |
---|
332 | break; |
---|
333 | } |
---|
334 | fp= jj.getItem().factor(); |
---|
335 | DEBOUT(cout, "Calculating fp= gcd(", g); |
---|
336 | DEBOUT(cout, ",", fp(substback,vf)); |
---|
337 | DEBOUT(cout, ") over K_r wrt ", vf); |
---|
338 | fp= algcd(g,fp(substback,vf), as, oldord); |
---|
339 | DEBOUTLN(cout, " = ", fp); |
---|
340 | if ( degree(fp,vf) > 0 ){ //otherwise it's a constant |
---|
341 | g= divide(g, fp,as); |
---|
342 | DEBOUTLN(cout, "f/fp= ", g); |
---|
343 | result.append(CFFactor(fp,1)); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | DEBOUTMSG(cout, "Testing Delta: this gives Factors (not nec. irreduzible!)"); |
---|
348 | for (jj=Delta; jj.hasItem(); jj++){ |
---|
349 | if ( degree(g,vf) <= 1 ) { // g is linear (or a constant..) |
---|
350 | break; |
---|
351 | } |
---|
352 | fp= jj.getItem().factor(); |
---|
353 | DEBOUT(cout, "Calculating fp= gcd(", g); |
---|
354 | DEBOUT(cout, ",", fp(substback,vf)); |
---|
355 | DEBOUT(cout, ") over K_r wrt ", vf); |
---|
356 | fp= algcd(g,fp(substback,vf), as, oldord); |
---|
357 | DEBOUTLN(cout, " = ", fp); |
---|
358 | if ( degree(fp,vf) > 0 ){ //otherwise it's a constant |
---|
359 | g= divide(g, fp,as); |
---|
360 | DEBOUTLN(cout, "f/fp= ", g); |
---|
361 | // reduceresult.append(fp); // a facctor but not nec. irreduzible |
---|
362 | result.append(CFFactor(fp,1)); |
---|
363 | success=0; |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | // if (reduceresult.length() > 0){ //we have factors; |
---|
368 | // for ( CFListIterator I=reduceresult; I.hasItem(); I++ ){ |
---|
369 | // // try to obtain an irreducible factorization |
---|
370 | // result= Union( result, |
---|
371 | // factoras(I.getItem(), Astar, success) ); |
---|
372 | // } |
---|
373 | // } |
---|
374 | |
---|
375 | if ( result.length()==0 ){ |
---|
376 | if ( isquasilinear(Cstar) ){ // Cstar quasilinear => f is irreduzible |
---|
377 | success=1; |
---|
378 | DEBDECLEVEL(cout,"factoras"); |
---|
379 | return CFFList(CFFactor(f,1)); |
---|
380 | } |
---|
381 | else { |
---|
382 | DEBOUTMSG(cout, "Going to F2!"); |
---|
383 | D.nextpoint(); // choose a new set of int's. |
---|
384 | goto F2; |
---|
385 | } |
---|
386 | } |
---|
387 | // result.length() > 0 ==> we have factors! |
---|
388 | success=1; |
---|
389 | result.append(CFFactor(g,1)); //append the rest |
---|
390 | DEBDECLEVEL(cout,"factoras"); |
---|
391 | return result; |
---|
392 | } |
---|
393 | |
---|
394 | // for now: success=-1 no success, success=1 factored poly, |
---|
395 | // success = 0 factored poly partially |
---|
396 | CFFList |
---|
397 | cfactor(const CanonicalForm & f, const CFList & as, int success ){ |
---|
398 | Off(SW_RATIONAL); |
---|
399 | CFFList Output, output, Factors=Factorize(f); On(SW_RATIONAL); |
---|
400 | int csuccess=0; |
---|
401 | Factors.removeFirst(); |
---|
402 | |
---|
403 | if ( as.length() == 0 ){ success=1; return Factors;} |
---|
404 | if ( cls(f) <= cls(as.getLast()) ) { success=1; return Factors;} |
---|
405 | |
---|
406 | success=1; |
---|
407 | for ( CFFListIterator i=Factors; i.hasItem(); i++ ){ |
---|
408 | CFFList coutput=factoras(i.getItem().factor(),as,csuccess); |
---|
409 | success= min(success,csuccess); |
---|
410 | for ( CFFListIterator j=coutput; j.hasItem(); j++) |
---|
411 | Output = myappend(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp())); |
---|
412 | } |
---|
413 | return Output; |
---|
414 | } |
---|
415 | |
---|
416 | // Gegenbeispiel: char(k)=0 (original maple von wang): |
---|
417 | // f:= x^2*z^2-6909*x*z^2-3537*z^2-14344*x^2; |
---|
418 | // as:= [x^3-9977*x^2+15570*x-961] |
---|
419 | // ord:= [x,z] |
---|
420 | // cfactor(f,as,ord)= |
---|
421 | // -1/153600624478379*(-153600624478379*z^2+96784194432*x^2-246879593179080*x+ |
---|
422 | // 13599716437688)*(x^2-6909*x-3537) |
---|
423 | // |
---|
424 | // maple: |
---|
425 | // alias(alpha=RootOf(x^3-9977*x^2+15570*x-961,x)); |
---|
426 | // factor(f,alpha); // faktorisierung in char=0 |
---|
427 | // x^2*z^2-6909*x*z^2-3537*z^2-14344*x^2 |
---|
428 | |
---|
429 | |
---|
430 | /* |
---|
431 | $Log: not supported by cvs2svn $ |
---|
432 | */ |
---|