source: git/factory/libfac/charset/alg_factor.cc @ ba6139

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