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

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