source: git/factory/libfac/charset/alg_factor.cc @ 36914e

fieker-DuValspielwiese
Last change on this file since 36914e was 36914e, checked in by Martin Lee <martinlee84@…>, 10 years ago
chg: added QuasiInverse over alg. function field
  • Property mode set to 100644
File size: 27.3 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
380/// pseudo division of f and g wrt. x s.t. multiplier*f=q*g+r
381/// but only if the leading coefficient of g is of level lower than coeffLevel
382void
383psqr (const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q,
384      CanonicalForm & r, CanonicalForm& multiplier, const Variable& x,
385      int coeffLevel)
386{
387  ASSERT( x.level() > 0, "type error: polynomial variable expected" );
388  ASSERT( ! g.isZero(), "math error: division by zero" );
389
390  // swap variables such that x's level is larger or equal
391  // than both f's and g's levels.
392  Variable X;
393  if (f.level() > g.level())
394    X= f.mvar();
395  else
396    X= g.mvar();
397  if (X.level() < x.level())
398    X= x;
399  CanonicalForm F= swapvar (f, x, X);
400  CanonicalForm G= swapvar (g, x, X);
401
402  // now, we have to calculate the pseudo remainder of F and G
403  // w.r.t. X
404  int fDegree= degree (F, X);
405  int gDegree= degree (G, X);
406  if (fDegree < 0 || fDegree < gDegree)
407  {
408    q= 0;
409    r= f;
410  }
411  else
412  {
413    CanonicalForm LCG= LC (G, X);
414    if (LCG.level() < coeffLevel)
415    {
416      multiplier= power (LCG, fDegree - gDegree + 1);
417      divrem (multiplier*F, G, q, r);
418      q= swapvar (q, x, X);
419      r= swapvar (r, x, X);
420    }
421    else
422    {
423      q= 0;
424      r= f;
425    }
426  }
427}
428
429/// pseudo division of f and g wrt. x s.t. multiplier*f=q*g+r
430void
431psqr (const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, 
432      CanonicalForm & r, CanonicalForm& multiplier, const Variable& x)
433{
434    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
435    ASSERT( ! g.isZero(), "math error: division by zero" );
436
437    // swap variables such that x's level is larger or equal
438    // than both f's and g's levels.
439    Variable X;
440    if (f.level() > g.level())
441      X= f.mvar();
442    else
443      X= g.mvar();
444    if (X.level() < x.level())
445      X= x;
446    CanonicalForm F= swapvar (f, x, X);
447    CanonicalForm G= swapvar (g, x, X);
448
449    // now, we have to calculate the pseudo remainder of F and G
450    // w.r.t. X
451    int fDegree= degree (F, X);
452    int gDegree= degree (G, X);
453    if (fDegree < 0 || fDegree < gDegree)
454    {
455      q= 0;
456      r= f;
457    }
458    else
459    {
460      CanonicalForm LCG= LC (G, X);
461      multiplier= power (LCG, fDegree - gDegree + 1);
462      divrem (multiplier*F, G, q, r);
463      q= swapvar (q, x, X);
464      r= swapvar (r, x, X);
465    }
466}
467
468CanonicalForm
469QuasiInverse (const CanonicalForm& f, const CanonicalForm& g,
470      CanonicalForm& numt, const Variable& x)
471{
472  CanonicalForm pi, pi1, q, t0, t1, Hi, bi, pi2;
473  bool isRat= isOn (SW_RATIONAL);
474  CanonicalForm m,tmp;
475  if (isRat)
476    Off (SW_RATIONAL);
477  pi= f/content (f,x);
478  pi1= g/content (g,x);
479
480  t0= 0;
481  t1= 1;
482  bi= 1;
483
484  On (SW_RATIONAL);
485
486  int delta= degree (f, x) - degree (g, x);
487  Hi= power (LC (pi1, x), delta);
488  if ( (delta+1) % 2 )
489      bi = 1;
490  else
491      bi = -1;
492
493  while (degree (pi1,x) > 0)
494  {
495    On (SW_RATIONAL);
496    psqr( pi, pi1, q, pi2, m, x);
497    pi2 /= bi;
498
499    tmp= t1;
500    t1= t0*m - t1*q;
501    t0= tmp;
502    t1 /= bi;
503    pi = pi1; pi1 = pi2;
504    if ( degree ( pi1, x ) > 0 )
505    {
506      delta = degree( pi, x ) - degree( pi1, x );
507      if ( (delta+1) % 2 )
508        bi = LC( pi, x ) * power( Hi, delta );
509      else
510        bi = -LC( pi, x ) * power( Hi, delta );
511      Hi = power( LC( pi1, x ), delta ) / power( Hi, delta-1 );
512    }
513  }
514  t1 /= gcd (pi1, t1);
515  if (!isRat)
516    Off (SW_RATIONAL);
517  numt= t1;
518  return pi;
519}
520
521// calculate a "primitive element"
522// K must have more than S elements (->thesis, -> getextension)
523static CFList
524simpleextension(const CFList & Astar, const Variable & Extension,
525                CanonicalForm & R){
526  CFList Returnlist, Bstar=Astar;
527  CanonicalForm s, g;
528
529  DEBOUTLN(CERR, "simpleextension: Astar= ", Astar);
530  DEBOUTLN(CERR, "simpleextension:     R= ", R);
531  DEBOUTLN(CERR, "simpleextension: Extension= ", Extension);
532  if ( Astar.length() == 1 ){ R= Astar.getFirst();}
533  else{
534    R=Bstar.getFirst(); Bstar.removeFirst();
535    for ( CFListIterator i=Bstar; i.hasItem(); i++){
536      DEBOUTLN(CERR, "simpleextension: f(x)= ", i.getItem());
537      DEBOUTLN(CERR, "simpleextension: P(x)= ", R);
538      sqrf_norm(i.getItem(), R, Extension, s, g, R);
539      // spielt die Repraesentation eine Rolle?
540      // muessen wir die Nachfolger aendern, wenn s != 0 ?
541      DEBOUTLN(CERR, "simpleextension: g= ", g);
542      DEBOUTLN(CERR, "simpleextension: s= ", s);
543      DEBOUTLN(CERR, "simpleextension: R= ", R);
544      Returnlist.insert(s);
545    }
546  }
547
548  return Returnlist;
549}
550
551CanonicalForm alg_lc(const CanonicalForm &f)
552{
553  if (f.level()>0)
554  {
555    return alg_lc(f.LC());
556  }
557  //assert(f.inCoeffDomain());
558  return f;
559}
560
561// the heart of the algorithm: the one from Trager
562#ifndef DEBUGOUTPUT
563static CFFList
564alg_factor( const CanonicalForm & f, const CFList & Astar, const Variable & vminpoly, const Varlist /*& oldord*/, const CFList & as)
565#else
566static CFFList
567alg_factor( const CanonicalForm & f, const CFList & Astar, const Variable & vminpoly, const Varlist & oldord, const CFList & as)
568#endif
569{
570  CFFList L, Factorlist;
571  CanonicalForm R, Rstar, s, g, h;
572  CFList substlist;
573
574  DEBINCLEVEL(CERR,"alg_factor");
575  DEBOUTLN(CERR, "alg_factor: f= ", f);
576
577  //out_cf("start alg_factor:",f,"\n");
578  substlist= simpleextension(Astar, vminpoly, Rstar);
579  DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
580  DEBOUTLN(CERR, "alg_factor: minpoly Rstar= ", Rstar);
581  DEBOUTLN(CERR, "alg_factor: vminpoly= ", vminpoly);
582
583  sqrf_norm(f, Rstar, vminpoly, s, g, R );
584  //out_cf("sqrf_norm R:",R,"\n");
585  //out_cf("sqrf_norm s:",s,"\n");
586  //out_cf("sqrf_norm g:",g,"\n");
587  DEBOUTLN(CERR, "alg_factor: g= ", g);
588  DEBOUTLN(CERR, "alg_factor: s= ", s);
589  DEBOUTLN(CERR, "alg_factor: R= ", R);
590  Off(SW_RATIONAL);
591  Variable X;
592  if (getAlgVar(R,X))
593  {
594    // factorize R over alg.extension with X
595//CERR << "alg: "<< X << " mipo=" << getMipo(X,Variable('X')) <<"\n";
596    if (R.isUnivariate())
597    {
598      DEBOUTLN(CERR, "alg_factor: factorize( ", R);
599      Factorlist =  factorize( R, X );
600    }
601    else
602    {
603      #if 1
604      Variable XX;
605      CanonicalForm mipo=getMipo(X,XX);
606      CFList as(mipo);
607      DEBOUTLN(CERR, "alg_factor: newfactoras( ", R);
608      int success=1;
609      Factorlist = newfactoras(R, as , success);
610      #else
611      // factor R over k
612      DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
613      Factorlist = Factorize(R);
614      #endif
615    }
616  }
617  else
618  {
619    // factor R over k
620    DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
621    Factorlist = Factorize(R);
622  }
623  On(SW_RATIONAL);
624  DEBOUTLN(CERR, "alg_factor: Factorize(R)= ", Factorlist);
625  if ( !Factorlist.getFirst().factor().inCoeffDomain() )
626    Factorlist.insert(CFFactor(1,1));
627  if ( Factorlist.length() == 2 && Factorlist.getLast().exp()== 1)
628  { // irreduzibel (first entry is a constant)
629    L.append(CFFactor(f,1));
630  }
631  else
632  {
633    DEBOUTLN(CERR, "alg_factor: g= ", g);
634    CanonicalForm gnew= g(s,s.mvar());
635    DEBOUTLN(CERR, "alg_factor: gnew= ", gnew);
636    g=gnew;
637    for ( CFFListIterator i=Factorlist; i.hasItem(); i++)
638    {
639      CanonicalForm fnew=i.getItem().factor();
640      fnew= fnew(s,s.mvar());
641      DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
642      DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
643      for ( CFListIterator ii=substlist; ii.hasItem(); ii++){
644        DEBOUTLN(CERR, "alg_factor: item= ", ii.getItem());
645        fnew= fnew(ii.getItem(), ii.getItem().mvar());
646        DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
647      }
648      if (degree(i.getItem().factor()) > 0 )
649      {
650          h=alg_gcd(g,fnew,as);
651        DEBOUTLN(CERR, "  alg_factor: h= ", h);
652        DEBOUTLN(CERR, "  alg_factor: oldord= ", oldord);
653        if ( degree(h) > 0 )
654        { //otherwise it's a constant
655          g= divide(g, h,as);
656          DEBOUTLN(CERR, "alg_factor: g/h= ", g);
657          DEBOUTLN(CERR, "alg_factor: s= ", s);
658          DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
659          //out_cf("new g:",g,"\n");
660          L.append(CFFactor(h,1));
661        }
662        //else printf("degree <=1\n");
663      }
664    }
665    // we are not interested in a
666    // constant (over K_r, which can be a polynomial!)
667    if (degree(g, f.mvar())>0){ L.append(CFFactor(g,1)); }
668  }
669  CFFList LL;
670  if (getCharacteristic()>0)
671  {
672    CFFListIterator i=L;
673    CanonicalForm c_fac=1;
674    CanonicalForm c;
675    for(;i.hasItem(); i++ )
676    {
677      CanonicalForm ff=i.getItem().factor();
678      c=alg_lc(ff);
679      int e=i.getItem().exp();
680      ff/=c;
681      if (!ff.isOne()) LL.append(CFFactor(ff,e));
682      while (e>0) { c_fac*=c;e--; }
683    }
684    if (!c_fac.isOne()) LL.insert(CFFactor(c_fac,1));
685  }
686  else
687  {
688    LL=L;
689  }
690  //CFFListIterator i=LL;
691  //for(;i.hasItem(); i++ )
692  //  out_cf("end alg_f:",i.getItem().factor(),"\n");
693  //printf("end alg_factor\n");
694  DEBOUTLN(CERR, "alg_factor: L= ", LL);
695  DEBDECLEVEL(CERR,"alg_factor");
696  return LL;
697}
698
699static CFFList
700endler( const CanonicalForm & f, const CFList & AS, const Varlist & uord )
701{
702  CanonicalForm F=f, g, q,r;
703  CFFList Output;
704  CFList One, Two, asnew, as=AS;
705  CFListIterator i,ii;
706  VarlistIterator j;
707  Variable vg;
708
709  for (i=as; i.hasItem(); i++)
710  {
711    g= i.getItem();
712    if (g.deriv() == 0 )
713    {
714      DEBOUTLN(CERR, "Inseperable extension detected: ", g);
715      for (j=uord; j.hasItem(); j++)
716      {
717        if ( degree(g,j.getItem()) > 0 ) vg= j.getItem();
718      }
719      // Now we have the highest transzendental in vg;
720      DEBOUTLN(CERR, "Transzendental is ", vg);
721      CanonicalForm gg=-1*g[0];
722      divrem(gg,vg,q,r); r= gg-q*vg;   gg= gg-r;
723      //DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
724      DEBOUTLN(CERR, "  that is ", gg);
725      DEBOUTLN(CERR, "  maps to ", g+gg);
726      One.insert(gg); Two.insert(g+gg);
727      // Now transform all remaining polys in as:
728      int x=0;
729      for (ii=i; ii.hasItem(); ii++)
730      {
731        if ( x != 0 )
732        {
733          divrem(ii.getItem(), gg, q,r);
734//          CERR << ii.getItem() << " divided by " << gg << "\n";
735          DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
736          ii.append(ii.getItem()+q*g); ii.remove(1);
737          DEBOUTLN(CERR, "as= ", as);
738        }
739        x+= 1;
740      }
741      // Now transform F:
742      divrem(F, gg, q,r);
743      F= F+q*g;
744      DEBOUTLN(CERR, "new F= ", F);
745    }
746    else{ asnew.append(i.getItem());  }// just the identity
747  }
748  // factor F with minimal polys given in asnew:
749  DEBOUTLN(CERR, "Factor F=  ", F);
750  DEBOUTLN(CERR, "  with as= ", asnew);
751  int success=0;
752  CFFList factorlist= newcfactor(F,asnew, success);
753  DEBOUTLN(CERR, "  gives =  ", factorlist);
754  DEBOUTLN(CERR, "One= ", One);
755  DEBOUTLN(CERR, "Two= ", Two);
756
757  // Transform back:
758  for ( CFFListIterator k=factorlist; k.hasItem(); k++)
759  {
760    CanonicalForm factor= k.getItem().factor();
761    ii=One;
762    for (i=Two; i.hasItem(); i++)
763    {
764      DEBOUTLN(CERR, "Mapping ", i.getItem());
765      DEBOUTLN(CERR, "     to ", ii.getItem());
766      DEBOUTLN(CERR, "     in ", factor);
767      divrem(factor,i.getItem(),q,r); r=factor -q*i.getItem();
768      DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
769      factor= ii.getItem()*q +r; //
770      ii++;
771    }
772    Output.append(CFFactor(factor,k.getItem().exp()));
773  }
774
775  return Output;
776}
777
778void
779multiplicity (CFFList& factors, const CanonicalForm& F, const CFList& as)
780{
781  CanonicalForm G= F;
782  Variable x= F.mvar();
783  CanonicalForm q, r;
784  int count= -1;
785  On (SW_RATIONAL);
786  for (CFFListIterator iter=factors; iter.hasItem(); iter++)
787  {
788    count= -1;
789    if (iter.getItem().factor().inCoeffDomain())
790      continue;
791    while (1)
792    {
793      psqr (G, iter.getItem().factor(), q, r, x);
794
795      q= Prem (q, as);
796      r= Prem (r, as);
797      if (!r.isZero())
798        break;
799      On (SW_RATIONAL);
800      count++;
801      G= q;
802    }
803    iter.getItem()= CFFactor (iter.getItem().factor(), iter.getItem().exp()+count);
804  }
805}
806
807
808// 1) prepares data
809// 2) for char=p we distinguish 3 cases:
810//           no transcendentals, seperable and inseperable extensions
811CFFList
812newfactoras( const CanonicalForm & f, const CFList & as, int &success)
813{
814  Variable vf=f.mvar();
815  CFListIterator i;
816  CFFListIterator jj;
817  CFList reduceresult;
818  CFFList result;
819
820  success=1;
821  DEBINCLEVEL(CERR, "newfactoras");
822  DEBOUTLN(CERR, "newfactoras called with f= ", f);
823  DEBOUTLN(CERR, "               content(f)= ", content(f));
824  DEBOUTLN(CERR, "                       as= ", as);
825  DEBOUTLN(CERR, "newfactoras: cls(vf)= ", cls(vf));
826  DEBOUTLN(CERR, "newfactoras: cls(as.getLast())= ", cls(as.getLast()));
827  DEBOUTLN(CERR, "newfactoras: degree(f,vf)= ", degree(f,vf));
828
829// F1: [Test trivial cases]
830// 1) first trivial cases:
831  if ( (cls(vf) <= cls(as.getLast())) ||  degree(f,vf)<=1 ){
832// ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) )
833    DEBDECLEVEL(CERR,"newfactoras");
834    return CFFList(CFFactor(f,1));
835  }
836
837// 2) List of variables:
838// 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1
839// 2b) Setup variableordering
840  CFList Astar;
841  Variable x;
842  CanonicalForm elem;
843  Varlist ord, uord,oldord;
844  for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii));  }
845  oldord= uord; oldord.append(vf);
846
847  for ( i=as; i.hasItem(); i++ ){
848    elem= i.getItem();
849    x= elem.mvar();
850    if ( degree(elem,x) > 1){ // otherwise it's not an extension
851      Astar.append(elem);
852      ord.append(x);
853    }
854  }
855  uord= Difference(uord,ord);
856  DEBOUTLN(CERR, "Astar is: ", Astar);
857  DEBOUTLN(CERR, "ord is: ", ord);
858  DEBOUTLN(CERR, "uord is: ", uord);
859
860// 3) second trivial cases: we already prooved irr. of f over no extensions
861  if ( Astar.length() == 0 ){
862    DEBDECLEVEL(CERR,"newfactoras");
863    return CFFList(CFFactor(f,1));
864  }
865
866// 4) Try to obtain a partial factorization using prop2 and prop3
867//    Use with caution! We have to proof these propositions first!
868  // Not yet implemented
869
870// 5) Look if elements in uord actually occure in any of the minimal
871//    polynomials. If no element of uord occures in any of the minimal
872//   polynomials, we don't have transzendentals.
873  Varlist newuord=Var_is_in_AS(uord,Astar);
874  DEBOUTLN(CERR, "newuord is: ", newuord);
875
876  CFFList Factorlist;
877  Varlist gcdord= Union(ord,newuord); gcdord.append(f.mvar());
878  // This is for now. we need alg_sqrfree implemented!
879  CanonicalForm Fgcd;
880          Fgcd= alg_gcd(f,f.deriv(),Astar);
881  if ( Fgcd == 0 ) {DEBOUTMSG(CERR, "WARNING: p'th root ?");}
882  if (( degree(Fgcd, f.mvar()) > 0) && (!(f.deriv().isZero())) ){
883    DEBOUTLN(CERR, "Nontrivial GCD found of ", f);
884    CanonicalForm Ggcd= divide(f, Fgcd,Astar);
885    DEBOUTLN(CERR, "  split into ", Fgcd);
886    DEBOUTLN(CERR, "         and ", Ggcd);
887    Fgcd= pp(Fgcd); Ggcd= pp(Ggcd);
888    DEBDECLEVEL(CERR,"newfactoras");
889    return myUnion(newfactoras(Fgcd,as,success) , newfactoras(Ggcd,as,success));
890  }
891  if ( getCharacteristic() > 0 ){
892
893    // First look for extension!
894    IntList degreelist;
895    Variable vminpoly;
896    for (i=Astar; i.hasItem(); i++){degreelist.append(degree(i.getItem()));}
897    int extdeg= getextension(degreelist, degree(f));
898    DEBOUTLN(CERR, "Extension needed of degree ", extdeg);
899
900    // Now the real stuff!
901    if ( newuord.length() == 0 ){ // no transzendentals
902      DEBOUTMSG(CERR, "No transzendentals!");
903      if ( extdeg > 1 ){
904        CanonicalForm MIPO= generate_mipo( extdeg, vminpoly);
905        DEBOUTLN(CERR, "Minpoly produced ", MIPO);
906        vminpoly= rootOf(MIPO);
907      }
908      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
909      DEBDECLEVEL(CERR,"newfactoras");
910      return Factorlist;
911    }
912    else if ( inseperable(Astar) > 0 ){ // Look if extensions are seperable
913      // a) Use Endler
914      DEBOUTMSG(CERR, "Inseperable extensions! Using Endler!");
915      CFFList templist= endler(f,Astar, newuord);
916      DEBOUTLN(CERR, "Endler gives: ", templist);
917      return templist;
918    }
919    else{ // we are on the save side: Use trager
920      DEBOUTMSG(CERR, "Only seperable extensions!");
921      if (extdeg > 1 ){
922        CanonicalForm MIPO=generate_mipo(extdeg, vminpoly );
923        vminpoly= rootOf(MIPO);
924        DEBOUTLN(CERR, "Minpoly generated: ", MIPO);
925        DEBOUTLN(CERR, "vminpoly= ", vminpoly);
926        DEBOUTLN(CERR, "degree(vminpoly)= ", degree(vminpoly));
927      }
928      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
929      DEBDECLEVEL(CERR,"newfactoras");
930      return Factorlist;
931    }
932  }
933  else{ // char=0 apply trager directly
934    DEBOUTMSG(CERR, "Char=0! Apply Trager!");
935    Variable vminpoly;
936    Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
937      DEBDECLEVEL(CERR,"newfactoras");
938      return Factorlist;
939  }
940
941  DEBDECLEVEL(CERR,"newfactoras");
942  return CFFList(CFFactor(f,1));
943}
944
945CFFList
946newcfactor(const CanonicalForm & f, const CFList & as, int & success )
947{
948  Off(SW_RATIONAL);
949  CFFList Output, output, Factors=Factorize(f);
950  On(SW_RATIONAL);
951  Factors.removeFirst();
952
953  if ( as.length() == 0 ){ success=1; return Factors;}
954  if ( cls(f) <= cls(as.getLast()) ) { success=1; return Factors;}
955
956  success=1;
957  for ( CFFListIterator i=Factors; i.hasItem(); i++ )
958  {
959    output=newfactoras(i.getItem().factor(),as, success);
960    for ( CFFListIterator j=output; j.hasItem(); j++)
961      Output = myappend(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp()));
962  }
963  return Output;
964}
Note: See TracBrowser for help on using the repository browser.