source: git/libfac/charset/alg_factor.cc @ 070c1b4

jengelh-datetimespielwiese
Last change on this file since 070c1b4 was 070c1b4, checked in by Hans Schoenemann <hannes@…>, 11 years ago
fix: error about choosing field extensions (Tst/Short/bug_charseries2) update version in libfac and factory
  • Property mode set to 100644
File size: 23.8 KB
Line 
1////////////////////////////////////////////////////////////
2// emacs edit mode for this file is -*- C++ -*-
3////////////////////////////////////////////////////////////
4
5// FACTORY - Includes
6#include <factory.h>
7// Factor - Includes
8#include <tmpl_inst.h>
9#include <Factor.h>
10#include <SqrFree.h>
11#include <helpstuff.h>
12// Charset - Includes
13#include "csutil.h"
14#include "charset.h"
15#include "reorder.h"
16#include "algfactor.h"
17// some CC's need this:
18#include "alg_factor.h"
19
20void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
21
22#ifdef ALGFACTORDEBUG
23#  define DEBUGOUTPUT
24#else
25#  undef DEBUGOUTPUT
26#endif
27
28#include <libfac/factor/debug.h>
29
30static Varlist
31Var_is_in_AS(const Varlist & uord, const CFList & Astar);
32
33int getAlgVar(const CanonicalForm &f, Variable &X)
34{
35  if (f.inBaseDomain()) return 0;
36  if (f.inCoeffDomain())
37  {
38    if (f.level()!=0)
39    {
40      X= f.mvar();
41      return 1;
42    }
43    return getAlgVar(f.LC(),X);
44  }
45  if (f.inPolyDomain())
46  {
47    if (getAlgVar(f.LC(),X)) return 1;
48    for( CFIterator i=f; i.hasTerms(); i++)
49    {
50      if (getAlgVar(i.coeff(),X)) return 1;
51    }
52  }
53  return 0;
54}
55
56////////////////////////////////////////////////////////////////////////
57// This implements the algorithm of Trager for factorization of
58// (multivariate) polynomials over algebraic extensions and so called
59// function field extensions.
60////////////////////////////////////////////////////////////////////////
61
62// // missing class: IntGenerator:
63bool IntGenerator::hasItems() const
64{
65    return 1;
66}
67
68CanonicalForm IntGenerator::item() const
69//int IntGenerator::item() const
70{
71  //return current; //CanonicalForm( current );
72  return mapinto(CanonicalForm( current ));
73}
74
75void IntGenerator::next()
76{
77    current++;
78}
79
80// replacement for factory's broken psr
81static CanonicalForm
82mypsr ( const CanonicalForm &rr, const CanonicalForm &vv, const Variable & x )
83{
84  CanonicalForm r=rr, v=vv, l, test, lu, lv, t, retvalue;
85  int dr, dv, d,n=0;
86
87
88  dr = degree( r, x );
89  dv = degree( v, x );
90  if (dv <= dr) {l=LC(v,x); v = v -l*power(x,dv);}
91  else { l = 1; }
92  d= dr-dv+1;
93  while ( ( dv <= dr  ) && ( r != r.genZero()) ){
94    test = power(x,dr-dv)*v*LC(r,x);
95    if ( dr == 0 ) { r= CanonicalForm(0); }
96    else { r= r - LC(r,x)*power(x,dr); }
97    r= l*r -test;
98    dr= degree(r,x);
99    n+=1;
100  }
101  r= power(l, d-n)*r;
102  return r;
103}
104
105// replacement for factory's broken resultant
106static CanonicalForm
107resultante( const CanonicalForm & f, const CanonicalForm& g, const Variable & v )
108{
109  bool on_rational = isOn(SW_RATIONAL);
110  On(SW_RATIONAL);
111  CanonicalForm cd = bCommonDen( f );
112  CanonicalForm fz = f * cd;
113  cd = bCommonDen( g );
114  CanonicalForm gz = g * cd;
115  if (!on_rational)  Off(SW_RATIONAL);
116
117  return resultant(fz,gz,v);
118}
119
120// sqr-free routine for algebraic extensions
121// we need it! Ex.: f=c^2+2*a*c-1; as=[a^2+1]; f=(c+a)^2
122//static CFFList alg_sqrfree( const CanonicalForm & f )
123//{
124//  CFFList L;
125//
126//  L.append(CFFactor(f,1));
127//  return L;
128//}
129
130// Calculates a square free norm
131// Input: f(x, alpha) a square free polynomial over K(alpha),
132// alpha is defined by the minimal polynomial Palpha
133// K has more than S elements (S is defined in thesis; look getextension)
134static void
135sqrf_norm_sub( const CanonicalForm & f, const CanonicalForm & PPalpha,
136           CFGenerator & myrandom, CanonicalForm & s,  CanonicalForm & g,
137           CanonicalForm & R)
138{
139  Variable y=PPalpha.mvar(),vf=f.mvar();
140  CanonicalForm temp, Palpha=PPalpha, t;
141  int sqfreetest=0;
142  CFFList testlist;
143  CFFListIterator i;
144
145  DEBOUTLN(CERR, "sqrf_norm_sub:      f= ", f);
146  DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
147  myrandom.reset();   s=f.mvar()-myrandom.item()*Palpha.mvar();   g=f;
148  R= CanonicalForm(0);
149  DEBOUTLN(CERR, "sqrf_norm_sub: myrandom s= ", s);
150
151  // Norm, resultante taken with respect to y
152  while ( !sqfreetest )
153  {
154    DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
155    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
156    DEBOUTLN(CERR, "sqrf_norm_sub: R= ", R);
157    // sqfree check ; R is a polynomial in K[x]
158    if ( getCharacteristic() == 0 )
159    {
160      temp= gcd(R, R.deriv(vf));
161      DEBOUTLN(CERR, "sqrf_norm_sub: temp= ", temp);
162      if (degree(temp,vf) != 0 || temp == temp.genZero() ){ sqfreetest= 0; }
163      else { sqfreetest= 1; }
164      DEBOUTLN(CERR, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
165    }
166    else
167    {
168      DEBOUTMSG(CERR, "Starting SqrFreeTest(R)!");
169      // Look at SqrFreeTest!
170      // (z+a^5+w)^4 with z<w<a should not give sqfreetest=1 !
171      // for now we use this workaround with Factorize...
172      // ...but it should go away soon!!!!
173      Variable X;
174      if (getAlgVar(R,X))
175      {
176          testlist=factorize( R, X );
177      }
178      else
179        testlist= Factorize(R);
180      DEBOUTLN(CERR, "testlist= ", testlist);
181      testlist.removeFirst();
182      sqfreetest=1;
183      for ( i=testlist; i.hasItem(); i++)
184        if ( i.getItem().exp() > 1 && degree(i.getItem().factor(), R.mvar()) > 0) { sqfreetest=0; break; }
185      DEBOUTLN(CERR, "SqrFreeTest(R)= ", sqfreetest);
186    }
187    if ( ! sqfreetest )
188    {
189      myrandom.next();
190      DEBOUTLN(CERR, "sqrf_norm_sub generated new myrandom item: ", myrandom.item());
191      if ( getCharacteristic() == 0 ) t= CanonicalForm(mapinto(myrandom.item()));
192      else t= CanonicalForm(myrandom.item());
193      s= f.mvar()+t*Palpha.mvar(); // s defines backsubstitution
194      DEBOUTLN(CERR, "sqrf_norm_sub: testing s= ", s);
195      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
196      DEBOUTLN(CERR, "             gives g= ", g);
197    }
198  }
199}
200static void
201sqrf_agnorm_sub( const CanonicalForm & f, const CanonicalForm & PPalpha,
202           AlgExtGenerator & myrandom, CanonicalForm & s,  CanonicalForm & g,
203           CanonicalForm & R)
204{
205  Variable y=PPalpha.mvar(),vf=f.mvar();
206  CanonicalForm temp, Palpha=PPalpha, t;
207  int sqfreetest=0;
208  CFFList testlist;
209  CFFListIterator i;
210
211  DEBOUTLN(CERR, "sqrf_norm_sub:      f= ", f);
212  DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
213  myrandom.reset();   s=f.mvar()-myrandom.item()*Palpha.mvar();   g=f;
214  R= CanonicalForm(0);
215  DEBOUTLN(CERR, "sqrf_norm_sub: myrandom s= ", s);
216
217  // Norm, resultante taken with respect to y
218  while ( !sqfreetest )
219  {
220    DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
221    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
222    DEBOUTLN(CERR, "sqrf_norm_sub: R= ", R);
223    // sqfree check ; R is a polynomial in K[x]
224    if ( getCharacteristic() == 0 )
225    {
226      temp= gcd(R, R.deriv(vf));
227      DEBOUTLN(CERR, "sqrf_norm_sub: temp= ", temp);
228      if (degree(temp,vf) != 0 || temp == temp.genZero() ){ sqfreetest= 0; }
229      else { sqfreetest= 1; }
230      DEBOUTLN(CERR, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
231    }
232    else
233    {
234      DEBOUTMSG(CERR, "Starting SqrFreeTest(R)!");
235      // Look at SqrFreeTest!
236      // (z+a^5+w)^4 with z<w<a should not give sqfreetest=1 !
237      // for now we use this workaround with Factorize...
238      // ...but it should go away soon!!!!
239      Variable X;
240      if (getAlgVar(R,X))
241      {
242          testlist=factorize( R, X );
243      }
244      else
245        testlist= Factorize(R);
246      DEBOUTLN(CERR, "testlist= ", testlist);
247      testlist.removeFirst();
248      sqfreetest=1;
249      for ( i=testlist; i.hasItem(); i++)
250        if ( i.getItem().exp() > 1 && degree(i.getItem().factor(), R.mvar()) > 0) { sqfreetest=0; break; }
251      DEBOUTLN(CERR, "SqrFreeTest(R)= ", sqfreetest);
252    }
253    if ( ! sqfreetest )
254    {
255      myrandom.next();
256      DEBOUTLN(CERR, "sqrf_norm_sub generated new myrandom item: ", myrandom.item());
257      if ( getCharacteristic() == 0 ) t= CanonicalForm(mapinto(myrandom.item()));
258      else t= CanonicalForm(myrandom.item());
259      s= f.mvar()+t*Palpha.mvar(); // s defines backsubstitution
260      DEBOUTLN(CERR, "sqrf_norm_sub: testing s= ", s);
261      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
262      DEBOUTLN(CERR, "             gives g= ", g);
263    }
264  }
265}
266
267static void
268sqrf_norm( const CanonicalForm & f, const CanonicalForm & PPalpha,
269           const Variable & Extension, CanonicalForm & s,  CanonicalForm & g,
270           CanonicalForm & R)
271{
272  DEBOUTLN(CERR, "sqrf_norm:      f= ", f);
273  DEBOUTLN(CERR, "sqrf_norm: Palpha= ", PPalpha);
274  if ( getCharacteristic() == 0 )
275  {
276    IntGenerator myrandom;
277    DEBOUTMSG(CERR, "sqrf_norm: no extension, char=0");
278    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
279    DEBOUTLN(CERR, "sqrf_norm:      f= ", f);
280    DEBOUTLN(CERR, "sqrf_norm: Palpha= ", PPalpha);
281    DEBOUTLN(CERR, "sqrf_norm:      s= ", s);
282    DEBOUTLN(CERR, "sqrf_norm:      g= ", g);
283    DEBOUTLN(CERR, "sqrf_norm:      R= ", R);
284  }
285  else if ( degree(Extension) > 0 ) // working over Extensions
286  {
287    DEBOUTLN(CERR, "sqrf_norm: degree of extension is ", degree(Extension));
288    AlgExtGenerator myrandom(Extension);
289    sqrf_agnorm_sub(f,PPalpha, myrandom, s,g,R);
290  }
291  else
292  {
293    FFGenerator myrandom;
294    DEBOUTMSG(CERR, "sqrf_norm: degree of extension is 0");
295    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
296  }
297}
298
299static Varlist
300Var_is_in_AS(const Varlist & uord, const CFList & Astar){
301  Varlist output;
302  CanonicalForm elem;
303  Variable x;
304
305  for ( VarlistIterator i=uord; i.hasItem(); i++)
306  {
307    x=i.getItem();
308    for ( CFListIterator j=Astar; j.hasItem(); j++ )
309    {
310      elem= j.getItem();
311      if ( degree(elem,x) > 0 ) // x actually occures in Astar
312      {
313        output.append(x);
314        break;
315      }
316    }
317  }
318  return output;
319}
320
321// Look if Minimalpolynomials in Astar define seperable Extensions
322// Must be a power of p: i.e. y^{p^e}-x
323static int
324inseperable(const CFList & Astar)
325{
326  CanonicalForm elem;
327  int Counter= 1;
328
329  if ( Astar.length() == 0 ) return 0;
330  for ( CFListIterator i=Astar; i.hasItem(); i++)
331  {
332    elem= i.getItem();
333    if ( elem.deriv() == elem.genZero() ) return Counter;
334    else Counter += 1;
335  }
336  return 0;
337}
338
339// calculate gcd of f and g in char=0
340static CanonicalForm
341gcd0( CanonicalForm f, CanonicalForm g )
342{
343  int charac= getCharacteristic();
344  int save=isOn(SW_RATIONAL);
345  setCharacteristic(0); Off(SW_RATIONAL);
346  CanonicalForm ff= mapinto(f), gg= mapinto(g);
347  CanonicalForm result= gcd(ff,gg);
348  setCharacteristic(charac);
349  if (save) On(SW_RATIONAL);
350  return mapinto(result);
351}
352
353// calculate big enough extension for finite fields
354// Idea: first calculate k, such that q^k > S (->thesis, -> getextension)
355// Second, search k with gcd(k,m_i)=1, where m_i is the degree of the i'th
356// minimal polynomial. Then the minpoly f_i remains irrd. over q^k and we
357// have enough elements to plug in.
358static int
359getextension( IntList & degreelist, int n)
360{
361  int charac= getCharacteristic();
362  setCharacteristic(0); // need it for k !
363  int k=1, m=1, length=degreelist.length();
364  IntListIterator i;
365
366  for (i=degreelist; i.hasItem(); i++) m= m*i.getItem();
367  int q=charac;
368  while (q <= ((n*m)*(n*m)/2)) { k= k+1; q= q*charac;}
369  int l=0;
370  do {
371    for (i=degreelist; i.hasItem(); i++){
372      l= l+1;
373      if ( gcd0(k,i.getItem()) == 1){
374        DEBOUTLN(CERR, "getextension: gcd == 1, l=",l);
375        if ( l==length ){ setCharacteristic(charac);  return k; }
376      }
377      else { DEBOUTMSG(CERR, "getextension: Next iteration"); break; }
378    }
379    k= k+1; l=0;
380  }
381  while ( 1 );
382}
383
384// calculate a "primitive element"
385// K must have more than S elements (->thesis, -> getextension)
386static CFList
387simpleextension(const CFList & Astar, const Variable & Extension,
388                CanonicalForm & R){
389  CFList Returnlist, Bstar=Astar;
390  CanonicalForm s, g;
391
392  DEBOUTLN(CERR, "simpleextension: Astar= ", Astar);
393  DEBOUTLN(CERR, "simpleextension:     R= ", R);
394  DEBOUTLN(CERR, "simpleextension: Extension= ", Extension);
395  if ( Astar.length() == 1 ){ R= Astar.getFirst();}
396  else{
397    R=Bstar.getFirst(); Bstar.removeFirst();
398    for ( CFListIterator i=Bstar; i.hasItem(); i++){
399      DEBOUTLN(CERR, "simpleextension: f(x)= ", i.getItem());
400      DEBOUTLN(CERR, "simpleextension: P(x)= ", R);
401      sqrf_norm(i.getItem(), R, Extension, s, g, R);
402      // spielt die Repraesentation eine Rolle?
403      // muessen wir die Nachfolger aendern, wenn s != 0 ?
404      DEBOUTLN(CERR, "simpleextension: g= ", g);
405      if ( s != 0 ) DEBOUTLN(CERR, "simpleextension: s= ", s);
406      else DEBOUTLN(CERR, "simpleextension: s= ", s);
407      DEBOUTLN(CERR, "simpleextension: R= ", R);
408      Returnlist.insert(s);
409    }
410  }
411
412  return Returnlist;
413}
414
415CanonicalForm alg_lc(const CanonicalForm &f)
416{
417  if (f.level()>0)
418  {
419    return alg_lc(f.LC());
420  }
421  //assert(f.inCoeffDomain());
422  return f;
423}
424
425// the heart of the algorithm: the one from Trager
426static CFFList
427alg_factor( const CanonicalForm & f, const CFList & Astar, const Variable & vminpoly, const Varlist & oldord, const CFList & as)
428{
429  CFFList L, Factorlist;
430  CanonicalForm R, Rstar, s, g, h;
431  CFList substlist;
432
433  DEBINCLEVEL(CERR,"alg_factor");
434  DEBOUTLN(CERR, "alg_factor: f= ", f);
435
436  //out_cf("start alg_factor:",f,"\n");
437  substlist= simpleextension(Astar, vminpoly, Rstar);
438  DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
439  DEBOUTLN(CERR, "alg_factor: minpoly Rstar= ", Rstar);
440  DEBOUTLN(CERR, "alg_factor: vminpoly= ", vminpoly);
441
442  sqrf_norm(f, Rstar, vminpoly, s, g, R );
443  //out_cf("sqrf_norm R:",R,"\n");
444  //out_cf("sqrf_norm s:",s,"\n");
445  //out_cf("sqrf_norm g:",g,"\n");
446  DEBOUTLN(CERR, "alg_factor: g= ", g);
447  DEBOUTLN(CERR, "alg_factor: s= ", s);
448  DEBOUTLN(CERR, "alg_factor: R= ", R);
449  Off(SW_RATIONAL);
450  Variable X;
451  if (getAlgVar(R,X))
452  {
453    // factorize R over alg.extension with X
454//CERR << "alg: "<< X << " mipo=" << getMipo(X,Variable('X')) <<"\n";
455    if (R.isUnivariate())
456    {
457      DEBOUTLN(CERR, "alg_factor: factorize( ", R);
458      Factorlist =  factorize( R, X );
459    }
460    else
461    {
462      #if 1
463      Variable XX;
464      CanonicalForm mipo=getMipo(X,XX);
465      CFList as(mipo);
466      DEBOUTLN(CERR, "alg_factor: newfactoras( ", R);
467      int success=1;
468      Factorlist = newfactoras(R, as , success);
469      #else
470      // factor R over k
471      DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
472      Factorlist = Factorize(R);
473      #endif
474    }
475  }
476  else
477  {
478    // factor R over k
479    DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
480    Factorlist = Factorize(R);
481  }
482  On(SW_RATIONAL);
483  DEBOUTLN(CERR, "alg_factor: Factorize(R)= ", Factorlist);
484  if ( !Factorlist.getFirst().factor().inCoeffDomain() )
485    Factorlist.insert(CFFactor(1,1));
486  if ( Factorlist.length() == 2 && Factorlist.getLast().exp()== 1)
487  { // irreduzibel (first entry is a constant)
488    L.append(CFFactor(f,1));
489  }
490  else
491  {
492    DEBOUTLN(CERR, "alg_factor: g= ", g);
493    CanonicalForm gnew= g(s,s.mvar());
494    DEBOUTLN(CERR, "alg_factor: gnew= ", gnew);
495    g=gnew;
496    for ( CFFListIterator i=Factorlist; i.hasItem(); i++)
497    {
498      CanonicalForm fnew=i.getItem().factor();
499      fnew= fnew(s,s.mvar());
500      DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
501      DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
502      for ( CFListIterator ii=substlist; ii.hasItem(); ii++){
503        DEBOUTLN(CERR, "alg_factor: item= ", ii.getItem());
504        fnew= fnew(ii.getItem(), ii.getItem().mvar());
505        DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
506      }
507      if (degree(i.getItem().factor()) > 0 )
508      {
509          h=alg_gcd(g,fnew,as);
510        DEBOUTLN(CERR, "  alg_factor: h= ", h);
511        DEBOUTLN(CERR, "  alg_factor: oldord= ", oldord);
512        if ( degree(h) > 0 )
513        { //otherwise it's a constant
514          g= divide(g, h,as);
515          DEBOUTLN(CERR, "alg_factor: g/h= ", g);
516          DEBOUTLN(CERR, "alg_factor: s= ", s);
517          DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
518          //out_cf("new g:",g,"\n");
519          L.append(CFFactor(h,1));
520        }
521        //else printf("degree <=1\n");
522      }
523    }
524    // we are not interested in a
525    // constant (over K_r, which can be a polynomial!)
526    if (degree(g, f.mvar())>0){ L.append(CFFactor(g,1)); }
527  }
528  CFFList LL;
529  if (getCharacteristic()>0)
530  {
531    CFFListIterator i=L;
532    CanonicalForm c_fac=1;
533    CanonicalForm c;
534    for(;i.hasItem(); i++ )
535    {
536      CanonicalForm ff=i.getItem().factor();
537      c=alg_lc(ff);
538      int e=i.getItem().exp();
539      ff/=c;
540      if (!ff.isOne()) LL.append(CFFactor(ff,e));
541      while (e>0) { c_fac*=c;e--; }
542    }
543    if (!c_fac.isOne()) LL.insert(CFFactor(c_fac,1));
544  }
545  else
546  {
547    LL=L;
548  }
549  //CFFListIterator i=LL;
550  //for(;i.hasItem(); i++ )
551  //  out_cf("end alg_f:",i.getItem().factor(),"\n");
552  //printf("end alg_factor\n");
553  DEBOUTLN(CERR, "alg_factor: L= ", LL);
554  DEBDECLEVEL(CERR,"alg_factor");
555  return LL;
556}
557
558static CFFList
559endler( const CanonicalForm & f, const CFList & AS, const Varlist & uord )
560{
561  CanonicalForm F=f, g, q,r;
562  CFFList Output;
563  CFList One, Two, asnew, as=AS;
564  CFListIterator i,ii;
565  VarlistIterator j;
566  Variable vg;
567
568  for (i=as; i.hasItem(); i++)
569  {
570    g= i.getItem();
571    if (g.deriv() == 0 )
572    {
573      DEBOUTLN(CERR, "Inseperable extension detected: ", g);
574      for (j=uord; j.hasItem(); j++)
575      {
576        if ( degree(g,j.getItem()) > 0 ) vg= j.getItem();
577      }
578      // Now we have the highest transzendental in vg;
579      DEBOUTLN(CERR, "Transzendental is ", vg);
580      CanonicalForm gg=-1*g[0];
581      divrem(gg,vg,q,r); r= gg-q*vg;   gg= gg-r;
582      //DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
583      DEBOUTLN(CERR, "  that is ", gg);
584      DEBOUTLN(CERR, "  maps to ", g+gg);
585      One.insert(gg); Two.insert(g+gg);
586      // Now transform all remaining polys in as:
587      int x=0;
588      for (ii=i; ii.hasItem(); ii++)
589      {
590        if ( x != 0 )
591        {
592          divrem(ii.getItem(), gg, q,r);
593//          CERR << ii.getItem() << " divided by " << gg << "\n";
594          DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
595          ii.append(ii.getItem()+q*g); ii.remove(1);
596          DEBOUTLN(CERR, "as= ", as);
597        }
598        x+= 1;
599      }
600      // Now transform F:
601      divrem(F, gg, q,r);
602      F= F+q*g;
603      DEBOUTLN(CERR, "new F= ", F);
604    }
605    else{ asnew.append(i.getItem());  }// just the identity
606  }
607  // factor F with minimal polys given in asnew:
608  DEBOUTLN(CERR, "Factor F=  ", F);
609  DEBOUTLN(CERR, "  with as= ", asnew);
610  int success=0;
611  CFFList factorlist= newcfactor(F,asnew, success);
612  DEBOUTLN(CERR, "  gives =  ", factorlist);
613  DEBOUTLN(CERR, "One= ", One);
614  DEBOUTLN(CERR, "Two= ", Two);
615
616  // Transform back:
617  for ( CFFListIterator k=factorlist; k.hasItem(); k++)
618  {
619    CanonicalForm factor= k.getItem().factor();
620    ii=One;
621    for (i=Two; i.hasItem(); i++)
622    {
623      DEBOUTLN(CERR, "Mapping ", i.getItem());
624      DEBOUTLN(CERR, "     to ", ii.getItem());
625      DEBOUTLN(CERR, "     in ", factor);
626      divrem(factor,i.getItem(),q,r); r=factor -q*i.getItem();
627      DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
628      factor= ii.getItem()*q +r; //
629      ii++;
630    }
631    Output.append(CFFactor(factor,k.getItem().exp()));
632  }
633
634  return Output;
635}
636
637
638// 1) prepares data
639// 2) for char=p we distinguish 3 cases:
640//           no transcendentals, seperable and inseperable extensions
641CFFList
642newfactoras( const CanonicalForm & f, const CFList & as, int &success)
643{
644  Variable vf=f.mvar();
645  CFListIterator i;
646  CFFListIterator jj;
647  CFList reduceresult;
648  CFFList result;
649
650  success=1;
651  DEBINCLEVEL(CERR, "newfactoras");
652  DEBOUTLN(CERR, "newfactoras called with f= ", f);
653  DEBOUTLN(CERR, "               content(f)= ", content(f));
654  DEBOUTLN(CERR, "                       as= ", as);
655  DEBOUTLN(CERR, "newfactoras: cls(vf)= ", cls(vf));
656  DEBOUTLN(CERR, "newfactoras: cls(as.getLast())= ", cls(as.getLast()));
657  DEBOUTLN(CERR, "newfactoras: degree(f,vf)= ", degree(f,vf));
658
659// F1: [Test trivial cases]
660// 1) first trivial cases:
661  if ( (cls(vf) <= cls(as.getLast())) ||  degree(f,vf)<=1 ){
662// ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) )
663    DEBDECLEVEL(CERR,"newfactoras");
664    return CFFList(CFFactor(f,1));
665  }
666
667// 2) List of variables:
668// 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1
669// 2b) Setup variableordering
670  CFList Astar;
671  Variable x;
672  CanonicalForm elem;
673  Varlist ord, uord,oldord;
674  for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii));  }
675  oldord= uord; oldord.append(vf);
676
677  for ( i=as; i.hasItem(); i++ ){
678    elem= i.getItem();
679    x= elem.mvar();
680    if ( degree(elem,x) > 1){ // otherwise it's not an extension
681      Astar.append(elem);
682      ord.append(x);
683    }
684  }
685  uord= Difference(uord,ord);
686  DEBOUTLN(CERR, "Astar is: ", Astar);
687  DEBOUTLN(CERR, "ord is: ", ord);
688  DEBOUTLN(CERR, "uord is: ", uord);
689
690// 3) second trivial cases: we already prooved irr. of f over no extensions
691  if ( Astar.length() == 0 ){
692    DEBDECLEVEL(CERR,"newfactoras");
693    return CFFList(CFFactor(f,1));
694  }
695
696// 4) Try to obtain a partial factorization using prop2 and prop3
697//    Use with caution! We have to proof these propositions first!
698  // Not yet implemented
699
700// 5) Look if elements in uord actually occure in any of the minimal
701//    polynomials. If no element of uord occures in any of the minimal
702//   polynomials, we don't have transzendentals.
703  Varlist newuord=Var_is_in_AS(uord,Astar);
704  DEBOUTLN(CERR, "newuord is: ", newuord);
705
706  CFFList Factorlist;
707  Varlist gcdord= Union(ord,newuord); gcdord.append(f.mvar());
708  // This is for now. we need alg_sqrfree implemented!
709  CanonicalForm Fgcd;
710          Fgcd= alg_gcd(f,f.deriv(),Astar);
711  if ( Fgcd == 0 ) DEBOUTMSG(CERR, "WARNING: p'th root ?");
712  if (( degree(Fgcd, f.mvar()) > 0) && (!(f.deriv().isZero())) ){
713    DEBOUTLN(CERR, "Nontrivial GCD found of ", f);
714    CanonicalForm Ggcd= divide(f, Fgcd,Astar);
715    DEBOUTLN(CERR, "  split into ", Fgcd);
716    DEBOUTLN(CERR, "         and ", Ggcd);
717    Fgcd= pp(Fgcd); Ggcd= pp(Ggcd);
718    DEBDECLEVEL(CERR,"newfactoras");
719    return myUnion(newfactoras(Fgcd,as,success) , newfactoras(Ggcd,as,success));
720  }
721  if ( getCharacteristic() > 0 ){
722
723    // First look for extension!
724    IntList degreelist;
725    Variable vminpoly;
726    for (i=Astar; i.hasItem(); i++){degreelist.append(degree(i.getItem()));}
727    int extdeg= getextension(degreelist, degree(f));
728    DEBOUTLN(CERR, "Extension needed of degree ", extdeg);
729
730    // Now the real stuff!
731    if ( newuord.length() == 0 ){ // no transzendentals
732      DEBOUTMSG(CERR, "No transzendentals!");
733      if ( extdeg > 1 ){
734        CanonicalForm MIPO= generate_mipo( extdeg, vminpoly);
735        DEBOUTLN(CERR, "Minpoly produced ", MIPO);
736        vminpoly= rootOf(MIPO);
737      }
738      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
739      DEBDECLEVEL(CERR,"newfactoras");
740      return Factorlist;
741    }
742    else if ( inseperable(Astar) > 0 ){ // Look if extensions are seperable
743      // a) Use Endler
744      DEBOUTMSG(CERR, "Inseperable extensions! Using Endler!");
745      CFFList templist= endler(f,Astar, newuord);
746      DEBOUTLN(CERR, "Endler gives: ", templist);
747      return templist;
748    }
749    else{ // we are on the save side: Use trager
750      DEBOUTMSG(CERR, "Only seperable extensions!");
751      if (extdeg > 1 ){
752        CanonicalForm MIPO=generate_mipo(extdeg, vminpoly );
753        vminpoly= rootOf(MIPO);
754        DEBOUTLN(CERR, "Minpoly generated: ", MIPO);
755        DEBOUTLN(CERR, "vminpoly= ", vminpoly);
756        DEBOUTLN(CERR, "degree(vminpoly)= ", degree(vminpoly));
757      }
758      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
759      DEBDECLEVEL(CERR,"newfactoras");
760      return Factorlist;
761    }
762  }
763  else{ // char=0 apply trager directly
764    DEBOUTMSG(CERR, "Char=0! Apply Trager!");
765    Variable vminpoly;
766    Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
767      DEBDECLEVEL(CERR,"newfactoras");
768      return Factorlist;
769  }
770
771  DEBDECLEVEL(CERR,"newfactoras");
772  return CFFList(CFFactor(f,1));
773}
774
775CFFList
776newcfactor(const CanonicalForm & f, const CFList & as, int & success )
777{
778  Off(SW_RATIONAL);
779  CFFList Output, output, Factors=Factorize(f);
780  On(SW_RATIONAL);
781  Factors.removeFirst();
782
783  if ( as.length() == 0 ){ success=1; return Factors;}
784  if ( cls(f) <= cls(as.getLast()) ) { success=1; return Factors;}
785
786  success=1;
787  for ( CFFListIterator i=Factors; i.hasItem(); i++ )
788  {
789    output=newfactoras(i.getItem().factor(),as, success);
790    for ( CFFListIterator j=output; j.hasItem(); j++)
791      Output = myappend(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp()));
792  }
793  return Output;
794}
Note: See TracBrowser for help on using the repository browser.