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

spielwiese
Last change on this file since ea0a9d was ea0a9d, checked in by Martin Lee <martinlee84@…>, 10 years ago
chg: compute back substitutions
  • Property mode set to 100644
File size: 32.7 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      if (testlist.getFirst().factor().inCoeffDomain())
161        testlist.removeFirst();
162      sqfreetest=1;
163      for ( i=testlist; i.hasItem(); i++)
164      {
165        if ( i.getItem().exp() > 1 && degree(i.getItem().factor(), R.mvar()) > 0)
166        {
167          sqfreetest=0;
168          break;
169        }
170      }
171      DEBOUTLN(CERR, "SqrFreeTest(R)= ", sqfreetest);
172    }
173    if ( ! sqfreetest )
174    {
175      myrandom.next();
176      DEBOUTLN(CERR, "sqrf_norm_sub generated new myrandom item: ", myrandom.item());
177      if ( getCharacteristic() == 0 )
178        t= CanonicalForm(mapinto(myrandom.item()));
179      else
180        t= CanonicalForm(myrandom.item());
181      s= t;
182      DEBOUTLN(CERR, "sqrf_norm_sub: testing s= ", s);
183      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
184      DEBOUTLN(CERR, "             gives g= ", g);
185    }
186  }
187}
188static void
189sqrf_agnorm_sub( const CanonicalForm & f, const CanonicalForm & PPalpha,
190           AlgExtGenerator & myrandom, CanonicalForm & s,  CanonicalForm & g,
191           CanonicalForm & R)
192{
193  Variable y=PPalpha.mvar(),vf=f.mvar();
194  CanonicalForm temp, Palpha=PPalpha, t;
195  int sqfreetest=0;
196  CFFList testlist;
197  CFFListIterator i;
198
199  DEBOUTLN(CERR, "sqrf_norm_sub:      f= ", f);
200  DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
201  myrandom.reset();   s=myrandom.item();   g=f;
202  R= CanonicalForm(0);
203  DEBOUTLN(CERR, "sqrf_norm_sub: myrandom s= ", s);
204
205  // Norm, resultante taken with respect to y
206  while ( !sqfreetest )
207  {
208    DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
209    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
210    DEBOUTLN(CERR, "sqrf_norm_sub: R= ", R);
211    // sqfree check ; R is a polynomial in K[x]
212    if ( getCharacteristic() == 0 )
213    {
214      temp= gcd(R, R.deriv(vf));
215      DEBOUTLN(CERR, "sqrf_norm_sub: temp= ", temp);
216      if (degree(temp,vf) != 0 || temp == temp.genZero() )
217        sqfreetest= 0;
218      else
219        sqfreetest= 1;
220      DEBOUTLN(CERR, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
221    }
222    else
223    {
224      DEBOUTMSG(CERR, "Starting SqrFreeTest(R)!");
225      // Look at SqrFreeTest!
226      // (z+a^5+w)^4 with z<w<a should not give sqfreetest=1 !
227      // for now we use this workaround with Factorize...
228      // ...but it should go away soon!!!!
229      Variable X;
230      if (getAlgVar(R,X))
231        testlist= factorize( R, X );
232      else
233        testlist= factorize(R);
234      DEBOUTLN(CERR, "testlist= ", testlist);
235      if (testlist.getFirst().factor().inCoeffDomain())
236        testlist.removeFirst();
237      sqfreetest=1;
238      for ( i=testlist; i.hasItem(); i++)
239      {
240        if ( i.getItem().exp() > 1 && degree(i.getItem().factor(), R.mvar()) > 0)
241        {
242          sqfreetest=0;
243          break;
244        }
245      }
246      DEBOUTLN(CERR, "SqrFreeTest(R)= ", sqfreetest);
247    }
248    if ( ! sqfreetest )
249    {
250      myrandom.next();
251      DEBOUTLN(CERR, "sqrf_norm_sub generated new myrandom item: ", myrandom.item());
252      if ( getCharacteristic() == 0 )
253        t= CanonicalForm(mapinto(myrandom.item()));
254      else
255        t= CanonicalForm(myrandom.item());
256      s= t;
257      DEBOUTLN(CERR, "sqrf_norm_sub: testing s= ", s);
258      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
259      DEBOUTLN(CERR, "             gives g= ", g);
260    }
261  }
262}
263
264static void
265sqrf_norm( const CanonicalForm & f, const CanonicalForm & PPalpha,
266           const Variable & Extension, CanonicalForm & s,  CanonicalForm & g,
267           CanonicalForm & R)
268{
269  DEBOUTLN(CERR, "sqrf_norm:      f= ", f);
270  DEBOUTLN(CERR, "sqrf_norm: Palpha= ", PPalpha);
271  if ( getCharacteristic() == 0 )
272  {
273    IntGenerator myrandom;
274    DEBOUTMSG(CERR, "sqrf_norm: no extension, char=0");
275    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
276    DEBOUTLN(CERR, "sqrf_norm:      f= ", f);
277    DEBOUTLN(CERR, "sqrf_norm: Palpha= ", PPalpha);
278    DEBOUTLN(CERR, "sqrf_norm:      s= ", s);
279    DEBOUTLN(CERR, "sqrf_norm:      g= ", g);
280    DEBOUTLN(CERR, "sqrf_norm:      R= ", R);
281  }
282  else if ( degree(Extension) > 0 ) // working over Extensions
283  {
284    DEBOUTLN(CERR, "sqrf_norm: degree of extension is ", degree(Extension));
285    AlgExtGenerator myrandom(Extension);
286    sqrf_agnorm_sub(f,PPalpha, myrandom, s,g,R);
287  }
288  else
289  {
290    FFGenerator myrandom;
291    DEBOUTMSG(CERR, "sqrf_norm: degree of extension is 0");
292    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
293  }
294}
295
296static Varlist
297Var_is_in_AS(const Varlist & uord, const CFList & Astar){
298  Varlist output;
299  CanonicalForm elem;
300  Variable x;
301
302  for ( VarlistIterator i=uord; i.hasItem(); i++)
303  {
304    x=i.getItem();
305    for ( CFListIterator j=Astar; j.hasItem(); j++ )
306    {
307      elem= j.getItem();
308      if ( degree(elem,x) > 0 ) // x actually occures in Astar
309      {
310        output.append(x);
311        break;
312      }
313    }
314  }
315  return output;
316}
317
318// Look if Minimalpolynomials in Astar define seperable Extensions
319// Must be a power of p: i.e. y^{p^e}-x
320static int
321inseperable(const CFList & Astar)
322{
323  CanonicalForm elem;
324  int Counter= 1;
325
326  if ( Astar.length() == 0 ) return 0;
327  for ( CFListIterator i=Astar; i.hasItem(); i++)
328  {
329    elem= i.getItem();
330    if ( elem.deriv() == elem.genZero() ) return Counter;
331    else Counter += 1;
332  }
333  return 0;
334}
335
336// calculate gcd of f and g in char=0
337static CanonicalForm
338gcd0( CanonicalForm f, CanonicalForm g )
339{
340  int charac= getCharacteristic();
341  int save=isOn(SW_RATIONAL);
342  setCharacteristic(0); Off(SW_RATIONAL);
343  CanonicalForm ff= mapinto(f), gg= mapinto(g);
344  CanonicalForm result= gcd(ff,gg);
345  setCharacteristic(charac);
346  if (save) On(SW_RATIONAL);
347  return mapinto(result);
348}
349
350// calculate big enough extension for finite fields
351// Idea: first calculate k, such that q^k > S (->thesis, -> getextension)
352// Second, search k with gcd(k,m_i)=1, where m_i is the degree of the i'th
353// minimal polynomial. Then the minpoly f_i remains irrd. over q^k and we
354// have enough elements to plug in.
355static int
356getextension( IntList & degreelist, int n)
357{
358  int charac= getCharacteristic();
359  setCharacteristic(0); // need it for k !
360  int k=1, m=1, length=degreelist.length();
361  IntListIterator i;
362
363  for (i=degreelist; i.hasItem(); i++) m= m*i.getItem();
364  int q=charac;
365  while (q <= ((n*m)*(n*m)/2)) { k= k+1; q= q*charac;}
366  int l=0;
367  do {
368    for (i=degreelist; i.hasItem(); i++){
369      l= l+1;
370      if ( gcd0(k,i.getItem()) == 1){
371        DEBOUTLN(CERR, "getextension: gcd == 1, l=",l);
372        if ( l==length ){ setCharacteristic(charac);  return k; }
373      }
374      else { DEBOUTMSG(CERR, "getextension: Next iteration"); break; }
375    }
376    k= k+1; l=0;
377  }
378  while ( 1 );
379}
380
381
382/// pseudo division of f and g wrt. x s.t. multiplier*f=q*g+r
383/// but only if the leading coefficient of g is of level lower than coeffLevel
384void
385psqr (const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q,
386      CanonicalForm & r, CanonicalForm& multiplier, const Variable& x,
387      int coeffLevel)
388{
389  ASSERT( x.level() > 0, "type error: polynomial variable expected" );
390  ASSERT( ! g.isZero(), "math error: division by zero" );
391
392  // swap variables such that x's level is larger or equal
393  // than both f's and g's levels.
394  Variable X;
395  if (f.level() > g.level())
396    X= f.mvar();
397  else
398    X= g.mvar();
399  if (X.level() < x.level())
400    X= x;
401  CanonicalForm F= swapvar (f, x, X);
402  CanonicalForm G= swapvar (g, x, X);
403
404  // now, we have to calculate the pseudo remainder of F and G
405  // w.r.t. X
406  int fDegree= degree (F, X);
407  int gDegree= degree (G, X);
408  if (fDegree < 0 || fDegree < gDegree)
409  {
410    q= 0;
411    r= f;
412  }
413  else
414  {
415    CanonicalForm LCG= LC (G, X);
416    if (LCG.level() < coeffLevel)
417    {
418      multiplier= power (LCG, fDegree - gDegree + 1);
419      divrem (multiplier*F, G, q, r);
420      q= swapvar (q, x, X);
421      r= swapvar (r, x, X);
422    }
423    else
424    {
425      q= 0;
426      r= f;
427    }
428  }
429}
430
431/// pseudo division of f and g wrt. x s.t. multiplier*f=q*g+r
432void
433psqr (const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, 
434      CanonicalForm & r, CanonicalForm& multiplier, const Variable& x)
435{
436    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
437    ASSERT( ! g.isZero(), "math error: division by zero" );
438
439    // swap variables such that x's level is larger or equal
440    // than both f's and g's levels.
441    Variable X;
442    if (f.level() > g.level())
443      X= f.mvar();
444    else
445      X= g.mvar();
446    if (X.level() < x.level())
447      X= x;
448    CanonicalForm F= swapvar (f, x, X);
449    CanonicalForm G= swapvar (g, x, X);
450
451    // now, we have to calculate the pseudo remainder of F and G
452    // w.r.t. X
453    int fDegree= degree (F, X);
454    int gDegree= degree (G, X);
455    if (fDegree < 0 || fDegree < gDegree)
456    {
457      q= 0;
458      r= f;
459    }
460    else
461    {
462      CanonicalForm LCG= LC (G, X);
463      multiplier= power (LCG, fDegree - gDegree + 1);
464      divrem (multiplier*F, G, q, r);
465      q= swapvar (q, x, X);
466      r= swapvar (r, x, X);
467    }
468}
469
470CanonicalForm
471QuasiInverse (const CanonicalForm& f, const CanonicalForm& g,
472      CanonicalForm& numt, const Variable& x)
473{
474  CanonicalForm pi, pi1, q, t0, t1, Hi, bi, pi2;
475  bool isRat= isOn (SW_RATIONAL);
476  CanonicalForm m,tmp;
477  if (isRat)
478    Off (SW_RATIONAL);
479  pi= f/content (f,x);
480  pi1= g/content (g,x);
481
482  t0= 0;
483  t1= 1;
484  bi= 1;
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    psqr( pi, pi1, q, pi2, m, x);
496    pi2 /= bi;
497
498    tmp= t1;
499    t1= t0*m - t1*q;
500    t0= tmp;
501    t1 /= bi;
502    pi = pi1; pi1 = pi2;
503    if ( degree ( pi1, x ) > 0 )
504    {
505      delta = degree( pi, x ) - degree( pi1, x );
506      if ( (delta+1) % 2 )
507        bi = LC( pi, x ) * power( Hi, delta );
508      else
509        bi = -LC( pi, x ) * power( Hi, delta );
510      Hi = power( LC( pi1, x ), delta ) / power( Hi, delta-1 );
511    }
512  }
513  t1 /= gcd (pi1, t1);
514  if (!isRat)
515    Off (SW_RATIONAL);
516  numt= t1;
517  return pi;
518}
519
520CanonicalForm
521evaluate (const CanonicalForm& f, const CanonicalForm& g, const CanonicalForm& h, const CanonicalForm& powH)
522{
523  if (f.inCoeffDomain())
524    return f;
525  CFIterator i= f;
526  int lastExp = i.exp();
527  CanonicalForm result = i.coeff()*powH;
528  i++;
529  while (i.hasTerms())
530  {
531    int i_exp= i.exp();
532    if ((lastExp - i_exp) == 1)
533    {
534      result *= g;
535      result /= h;
536    }
537    else
538    {
539      result *= power (g, lastExp - i_exp);
540      result /= power (h, lastExp - i_exp);
541    }
542    result += i.coeff()*powH;
543    lastExp = i_exp;
544    i++;
545  }
546  if (lastExp != 0)
547  {
548    result *= power (g, lastExp);
549    result /= power (h, lastExp);
550  }
551  return result;
552}
553
554
555/// evaluate f at g/h at v such that powH*f is integral i.e. powH is assumed to be h^degree(f,v)
556CanonicalForm
557evaluate (const CanonicalForm& f, const CanonicalForm& g,
558          const CanonicalForm& h, const CanonicalForm& powH,
559          const Variable& v)
560{
561  if (f.inCoeffDomain())
562  {
563    return f*powH;
564  }
565
566  Variable x = f.mvar();
567  if ( v > x )
568    return f*powH;
569  else  if ( v == x )
570    return evaluate (f, g, h, powH);
571
572  // v is less than main variable of f
573  CanonicalForm result= 0;
574  for (CFIterator i= f; i.hasTerms(); i++)
575    result += evaluate (i.coeff(), g, h, powH, v)*power (x, i.exp());
576  return result;
577}
578
579// calculate a "primitive element"
580// K must have more than S elements (->thesis, -> getextension)
581static CFList
582simpleextension(CFList& backSubst, const CFList & Astar,
583                const Variable & Extension, bool& isFunctionField,
584                CanonicalForm & R)
585{
586  CFList Returnlist, Bstar=Astar;
587  CanonicalForm s, g, ra, rb, oldR, h, denra, denrb=1;
588  Variable alpha;
589  CFList tmp;
590
591  bool isRat= isOn (SW_RATIONAL);
592
593  DEBOUTLN(CERR, "simpleextension: Astar= ", Astar);
594  DEBOUTLN(CERR, "simpleextension:     R= ", R);
595  DEBOUTLN(CERR, "simpleextension: Extension= ", Extension);
596  CFListIterator j;
597  if (Astar.length() == 1)
598  {
599    R= Astar.getFirst();
600    rb= R.mvar();
601  }
602  else
603  {
604    R=Bstar.getFirst();
605    Bstar.removeFirst();
606    for (CFListIterator i=Bstar; i.hasItem(); i++)
607    {
608      j= i;
609      j++;
610      Off (SW_RATIONAL);
611      R /= icontent (R);
612      On (SW_RATIONAL);
613      oldR= R;
614      sqrf_norm (i.getItem(), R, Extension, s, g, R);
615
616      backSubst.insert (s);
617
618      Off (SW_RATIONAL);
619      R /= icontent (R);
620
621      On (SW_RATIONAL);
622
623      if (!isFunctionField)
624      {
625        alpha= rootOf (R);
626        h= replacevar (g, g.mvar(), alpha);
627        On (SW_RATIONAL); //needed for GCD
628        h= gcd (h, oldR);
629        h /= Lc (h);
630        ra= -h[0];
631        ra= replacevar(ra, alpha, g.mvar());
632        rb= R.mvar()-s*ra;
633        for (; j.hasItem(); j++)
634        {
635          j.getItem()= j.getItem() (ra, oldR.mvar());
636          j.getItem()= j.getItem() (rb, i.getItem().mvar());
637        }
638      }
639      else
640      {
641        On (SW_RATIONAL);
642        h= swapvar (g, g.mvar(), oldR.mvar());
643        tmp= CFList (swapvar (R, g.mvar(), oldR.mvar()));
644        h= alg_gcd (h, swapvar (oldR, g.mvar(), oldR.mvar()), tmp);
645        CanonicalForm hh= replacevar (h, oldR.mvar(), alpha);
646
647        CanonicalForm numt, dent;
648        QuasiInverse (tmp.getFirst(), LC (h), numt, tmp.getFirst().mvar());
649
650        Off (SW_RATIONAL);
651        h *= numt;
652        h= reduce (h, tmp.getFirst());
653        dent= LC(h);
654
655        ra= -h[0];
656        denra= gcd (ra, dent);
657        ra /= denra;
658        denra= dent/denra;
659        denra= replacevar (denra, ra.mvar(), g.mvar());
660        ra= replacevar(ra, ra.mvar(), g.mvar());
661        rb= R.mvar()*denra-s*ra;
662        denrb= denra;
663        for (; j.hasItem(); j++)
664        {
665          CanonicalForm powdenra= power (denra, degree (j.getItem(), oldR.mvar()));
666          j.getItem()= evaluate (j.getItem(),ra, denra, powdenra, oldR.mvar());
667          powdenra= power (denra, degree (j.getItem(), i.getItem().mvar()));
668          j.getItem()= evaluate (j.getItem(), rb, denrb, powdenra, i.getItem().mvar());
669        }
670      }
671
672      DEBOUTLN(CERR, "simpleextension: g= ", g);
673      DEBOUTLN(CERR, "simpleextension: s= ", s);
674      DEBOUTLN(CERR, "simpleextension: R= ", R);
675      Returnlist.append (ra);
676      if (isFunctionField)
677        Returnlist.append (denra);
678    }
679  }
680  Returnlist.append (rb);
681  if (isFunctionField)
682    Returnlist.append (denrb);
683
684  if (isRat)
685    On (SW_RATIONAL);
686  else
687    Off (SW_RATIONAL);
688
689  return Returnlist;
690}
691
692CanonicalForm alg_lc(const CanonicalForm &f)
693{
694  if (f.level()>0)
695  {
696    return alg_lc(f.LC());
697  }
698  //assert(f.inCoeffDomain());
699  return f;
700}
701
702CanonicalForm
703subst (const CanonicalForm& f, const CFList& a, const CFList& b,
704       const CanonicalForm& Rstar, bool isFunctionField)
705{
706  if (isFunctionField)
707    ASSERT (2*a.length() == b.length(), "wrong length of lists");
708  else
709    ASSERT (a.length() == b.length(), "lists of equal length expected");
710  CFListIterator j= b;
711  CanonicalForm result= f, tmp, powj;
712  for (CFListIterator i= a; i.hasItem() && j.hasItem(); i++, j++)
713  {
714    if (!isFunctionField)
715      result= result (j.getItem(), i.getItem().mvar());
716    else
717    {
718      tmp= j.getItem();
719      j++;
720      powj= power (j.getItem(), degree (result, i.getItem().mvar()));
721      result= evaluate (result, tmp, j.getItem(), powj, i.getItem().mvar());
722
723      if (fdivides (powj, result, tmp))
724        result= tmp;
725
726      result /= vcontent (result, Variable (i.getItem().level() + 1));
727    }
728  }
729  result= reduce (result, Rstar);
730  result /= vcontent (result, Variable (Rstar.level() + 1));
731  return result;
732}
733
734CanonicalForm
735backSubst (const CanonicalForm& F, const CFList& a, const CFList& b)
736{
737  ASSERT (a.length() == b.length() - 1, "wrong length of lists in backSubst");
738  CanonicalForm result= F;
739  Variable tmp;
740  CFList tmp2= b;
741  tmp= tmp2.getLast().mvar();
742  tmp2.removeLast();
743  for (CFListIterator iter= a; iter.hasItem(); iter++)
744  {
745    result= result (tmp+iter.getItem()*tmp2.getLast().mvar(), tmp);
746    tmp= tmp2.getLast().mvar();
747    tmp2.removeLast();
748  }
749  return result;
750}
751
752// the heart of the algorithm: the one from Trager
753#ifndef DEBUGOUTPUT
754static CFFList
755alg_factor( const CanonicalForm & F, const CFList & Astar, const Variable & vminpoly, const Varlist /*& oldord*/, const CFList & as, bool isFunctionField)
756#else
757static CFFList
758alg_factor( const CanonicalForm & F, const CFList & Astar, const Variable & vminpoly, const Varlist & oldord, const CFList & as, bool isFunctionField)
759#endif
760{
761  CFFList L, Factorlist;
762  CanonicalForm R, Rstar, s, g, h, f= F;
763  CFList substlist, backSubsts;
764
765  DEBINCLEVEL(CERR,"alg_factor");
766  DEBOUTLN(CERR, "alg_factor: f= ", f);
767
768  substlist= simpleextension (backSubsts, Astar, vminpoly, isFunctionField, Rstar);
769  DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
770  DEBOUTLN(CERR, "alg_factor: minpoly Rstar= ", Rstar);
771  DEBOUTLN(CERR, "alg_factor: vminpoly= ", vminpoly);
772
773  f= subst (f, Astar, substlist, Rstar, isFunctionField);
774
775  Variable alpha;
776  if (!isFunctionField)
777  {
778    alpha= rootOf (Rstar);
779    g= replacevar (f, Rstar.mvar(), alpha);
780
781    Factorlist= factorize (g, alpha);
782
783    for (CFFListIterator i= Factorlist; i.hasItem(); i++)
784    {
785      h= i.getItem().factor();
786      if (!h.inCoeffDomain())
787      {
788        h= replacevar (h, alpha, Rstar.mvar());
789        h *= bCommonDen(h);
790        h= backSubst (h, backSubsts, Astar);
791        h= Prem (h, as);
792        L.append (CFFactor (h, i.getItem().exp()));
793      }
794    }
795    return L;
796  }
797  // after here we are over an extension of a function field
798
799
800  // make quasi monic
801  CFList Rstarlist= CFList (Rstar);
802  CanonicalForm numt, dent;
803  On (SW_RATIONAL);
804  QuasiInverse (Rstar, LC(f), numt, Rstar.mvar());
805
806  f *= numt;
807  f= Prem (f, Rstarlist);
808  f /= vcontent (f, Rstar.mvar());
809  // end quasi monic
810
811  sqrf_norm(f, Rstar, vminpoly, s, g, R );
812  //out_cf("sqrf_norm R:",R,"\n");
813  //out_cf("sqrf_norm s:",s,"\n");
814  //out_cf("sqrf_norm g:",g,"\n");
815  DEBOUTLN(CERR, "alg_factor: g= ", g);
816  DEBOUTLN(CERR, "alg_factor: s= ", s);
817  DEBOUTLN(CERR, "alg_factor: R= ", R);
818  Off(SW_RATIONAL);
819  Variable X;
820  if (getAlgVar(R,X))
821  {
822    // factorize R over alg.extension with X
823    //CERR << "alg: "<< X << " mipo=" << getMipo(X,Variable('X')) <<"\n";
824    DEBOUTLN(CERR, "alg_factor: factorize( ", R);
825    Factorlist =  factorize( R, X );
826  }
827  else
828  {
829    // factor R over k
830    DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
831    Factorlist = factorize(R);
832  }
833
834  On(SW_RATIONAL);
835  DEBOUTLN(CERR, "alg_factor: Factorize(R)= ", Factorlist);
836  if ( !Factorlist.getFirst().factor().inCoeffDomain() )
837    Factorlist.insert(CFFactor(1,1));
838  if ( Factorlist.length() == 2 && Factorlist.getLast().exp()== 1)
839  { // irreduzibel (first entry is a constant)
840    L.append(CFFactor(f,1));
841  }
842  else
843  {
844    g= f;
845    DEBOUTLN(CERR, "alg_factor: g= ", g);
846    for ( CFFListIterator i=Factorlist; i.hasItem(); i++)
847    {
848      CanonicalForm fnew=i.getItem().factor();
849      if (fnew.level() < Rstar.level()) //factor is a constant from the function field
850        continue;
851      else
852      {
853        fnew= fnew (g.mvar()+s*Rstar.mvar(), g.mvar());
854        fnew= reduce (fnew, Rstar);
855      }
856
857      DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
858
859      h= alg_gcd (g, fnew, Rstarlist);
860      QuasiInverse(Rstar, LC(h), numt, Rstar.mvar());
861      dent= 1;
862      h *= numt;
863      h= Prem (h, Rstarlist);
864      h /= vcontent (h, Rstar.mvar());
865
866      if (h.level() >= Rstar.level())
867      {
868        g= divide (g, h, Rstarlist);
869        h= backSubst (h, backSubsts, Astar);
870        h= Prem (h, as);
871        h *= bCommonDen (h);
872        h /= vcontent (h, as.getFirst().mvar());
873        L.append (CFFactor (h, 1));
874      }
875    }
876    // we are not interested in a
877    // constant (over K_r, which can be a polynomial!)
878    if (degree(g, f.mvar())>0){ L.append(CFFactor(g,1)); }
879  }
880  CFFList LL;
881  if (getCharacteristic()>0)
882  {
883    CFFListIterator i=L;
884    CanonicalForm c_fac=1;
885    CanonicalForm c;
886    for(;i.hasItem(); i++ )
887    {
888      CanonicalForm ff=i.getItem().factor();
889      c=alg_lc(ff);
890      int e=i.getItem().exp();
891      ff/=c;
892      if (!ff.isOne()) LL.append(CFFactor(ff,e));
893      while (e>0) { c_fac*=c;e--; }
894    }
895    if (!c_fac.isOne()) LL.insert(CFFactor(c_fac,1));
896  }
897  else
898  {
899    LL=L;
900  }
901  //CFFListIterator i=LL;
902  //for(;i.hasItem(); i++ )
903  //  out_cf("end alg_f:",i.getItem().factor(),"\n");
904  //printf("end alg_factor\n");
905  DEBOUTLN(CERR, "alg_factor: L= ", LL);
906  DEBDECLEVEL(CERR,"alg_factor");
907  return LL;
908}
909
910static CFFList
911endler( const CanonicalForm & f, const CFList & AS, const Varlist & uord )
912{
913  CanonicalForm F=f, g, q,r;
914  CFFList Output;
915  CFList One, Two, asnew, as=AS;
916  CFListIterator i,ii;
917  VarlistIterator j;
918  Variable vg;
919
920  for (i=as; i.hasItem(); i++)
921  {
922    g= i.getItem();
923    if (g.deriv() == 0 )
924    {
925      DEBOUTLN(CERR, "Inseperable extension detected: ", g);
926      for (j=uord; j.hasItem(); j++)
927      {
928        if ( degree(g,j.getItem()) > 0 ) vg= j.getItem();
929      }
930      // Now we have the highest transzendental in vg;
931      DEBOUTLN(CERR, "Transzendental is ", vg);
932      CanonicalForm gg=-1*g[0];
933      divrem(gg,vg,q,r); r= gg-q*vg;   gg= gg-r;
934      //DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
935      DEBOUTLN(CERR, "  that is ", gg);
936      DEBOUTLN(CERR, "  maps to ", g+gg);
937      One.insert(gg); Two.insert(g+gg);
938      // Now transform all remaining polys in as:
939      int x=0;
940      for (ii=i; ii.hasItem(); ii++)
941      {
942        if ( x != 0 )
943        {
944          divrem(ii.getItem(), gg, q,r);
945//          CERR << ii.getItem() << " divided by " << gg << "\n";
946          DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
947          ii.append(ii.getItem()+q*g); ii.remove(1);
948          DEBOUTLN(CERR, "as= ", as);
949        }
950        x+= 1;
951      }
952      // Now transform F:
953      divrem(F, gg, q,r);
954      F= F+q*g;
955      DEBOUTLN(CERR, "new F= ", F);
956    }
957    else{ asnew.append(i.getItem());  }// just the identity
958  }
959  // factor F with minimal polys given in asnew:
960  DEBOUTLN(CERR, "Factor F=  ", F);
961  DEBOUTLN(CERR, "  with as= ", asnew);
962  int success=0;
963  CFFList factorlist= newcfactor(F,asnew, success);
964  DEBOUTLN(CERR, "  gives =  ", factorlist);
965  DEBOUTLN(CERR, "One= ", One);
966  DEBOUTLN(CERR, "Two= ", Two);
967
968  // Transform back:
969  for ( CFFListIterator k=factorlist; k.hasItem(); k++)
970  {
971    CanonicalForm factor= k.getItem().factor();
972    ii=One;
973    for (i=Two; i.hasItem(); i++)
974    {
975      DEBOUTLN(CERR, "Mapping ", i.getItem());
976      DEBOUTLN(CERR, "     to ", ii.getItem());
977      DEBOUTLN(CERR, "     in ", factor);
978      divrem(factor,i.getItem(),q,r); r=factor -q*i.getItem();
979      DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
980      factor= ii.getItem()*q +r; //
981      ii++;
982    }
983    Output.append(CFFactor(factor,k.getItem().exp()));
984  }
985
986  return Output;
987}
988
989void
990multiplicity (CFFList& factors, const CanonicalForm& F, const CFList& as)
991{
992  CanonicalForm G= F;
993  Variable x= F.mvar();
994  CanonicalForm q, r;
995  int count= -1;
996  On (SW_RATIONAL);
997  for (CFFListIterator iter=factors; iter.hasItem(); iter++)
998  {
999    count= -1;
1000    if (iter.getItem().factor().inCoeffDomain())
1001      continue;
1002    while (1)
1003    {
1004      psqr (G, iter.getItem().factor(), q, r, x);
1005
1006      q= Prem (q, as);
1007      r= Prem (r, as);
1008      if (!r.isZero())
1009        break;
1010      On (SW_RATIONAL);
1011      count++;
1012      G= q;
1013    }
1014    iter.getItem()= CFFactor (iter.getItem().factor(), iter.getItem().exp()+count);
1015  }
1016}
1017
1018
1019// 1) prepares data
1020// 2) for char=p we distinguish 3 cases:
1021//           no transcendentals, seperable and inseperable extensions
1022CFFList
1023newfactoras( const CanonicalForm & f, const CFList & as, int &success)
1024{
1025  Variable vf=f.mvar();
1026  CFListIterator i;
1027  CFFListIterator jj;
1028  CFList reduceresult;
1029  CFFList result;
1030
1031  success=1;
1032  DEBINCLEVEL(CERR, "newfactoras");
1033  DEBOUTLN(CERR, "newfactoras called with f= ", f);
1034  DEBOUTLN(CERR, "               content(f)= ", content(f));
1035  DEBOUTLN(CERR, "                       as= ", as);
1036  DEBOUTLN(CERR, "newfactoras: cls(vf)= ", cls(vf));
1037  DEBOUTLN(CERR, "newfactoras: cls(as.getLast())= ", cls(as.getLast()));
1038  DEBOUTLN(CERR, "newfactoras: degree(f,vf)= ", degree(f,vf));
1039
1040// F1: [Test trivial cases]
1041// 1) first trivial cases:
1042  if ( (cls(vf) <= cls(as.getLast())) ||  degree(f,vf)<=1 ){
1043// ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) )
1044    DEBDECLEVEL(CERR,"newfactoras");
1045    return CFFList(CFFactor(f,1));
1046  }
1047
1048// 2) List of variables:
1049// 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1
1050// 2b) Setup variableordering
1051  CFList Astar;
1052  Variable x;
1053  CanonicalForm elem;
1054  Varlist ord, uord,oldord;
1055  for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii));  }
1056  oldord= uord; oldord.append(vf);
1057
1058  for ( i=as; i.hasItem(); i++ ){
1059    elem= i.getItem();
1060    x= elem.mvar();
1061    if ( degree(elem,x) > 1){ // otherwise it's not an extension
1062      Astar.append(elem);
1063      ord.append(x);
1064    }
1065  }
1066  uord= Difference(uord,ord);
1067  DEBOUTLN(CERR, "Astar is: ", Astar);
1068  DEBOUTLN(CERR, "ord is: ", ord);
1069  DEBOUTLN(CERR, "uord is: ", uord);
1070
1071// 3) second trivial cases: we already prooved irr. of f over no extensions
1072  if ( Astar.length() == 0 ){
1073    DEBDECLEVEL(CERR,"newfactoras");
1074    return CFFList(CFFactor(f,1));
1075  }
1076
1077// 4) Try to obtain a partial factorization using prop2 and prop3
1078//    Use with caution! We have to proof these propositions first!
1079  // Not yet implemented
1080
1081// 5) Look if elements in uord actually occure in any of the minimal
1082//    polynomials. If no element of uord occures in any of the minimal
1083//   polynomials, we don't have transzendentals.
1084  Varlist newuord=Var_is_in_AS(uord,Astar);
1085  DEBOUTLN(CERR, "newuord is: ", newuord);
1086
1087  CFFList Factorlist;
1088  Varlist gcdord= Union(ord,newuord); gcdord.append(f.mvar());
1089  bool isFunctionField= (newuord.length() > 0);
1090
1091  // This is for now. we need alg_sqrfree implemented!
1092  CanonicalForm Fgcd= 0;
1093  if (isFunctionField)
1094    Fgcd= alg_gcd(f,f.deriv(),Astar);
1095
1096  if ( Fgcd == 0 ) {DEBOUTMSG(CERR, "WARNING: p'th root ?");}
1097  if (isFunctionField && ( degree(Fgcd, f.mvar()) > 0) && (!(f.deriv().isZero())) )
1098  {
1099    DEBOUTLN(CERR, "Nontrivial GCD found of ", f);
1100    CanonicalForm Ggcd= divide(f, Fgcd,Astar);
1101    if (getCharacteristic() == 0)
1102    {
1103      CFFList result= newfactoras (Ggcd,as,success); //Ggcd is the squarefree part of f
1104      multiplicity (result, f, Astar);
1105      return result;
1106    }
1107    DEBOUTLN(CERR, "  split into ", Fgcd);
1108    DEBOUTLN(CERR, "         and ", Ggcd);
1109    Fgcd= pp(Fgcd); Ggcd= pp(Ggcd);
1110    DEBDECLEVEL(CERR,"newfactoras");
1111    return myUnion(newfactoras(Fgcd,as,success) , newfactoras(Ggcd,as,success));
1112  }
1113  if ( getCharacteristic() > 0 )
1114  {
1115    // First look for extension!
1116    IntList degreelist;
1117    Variable vminpoly;
1118    for (i=Astar; i.hasItem(); i++){degreelist.append(degree(i.getItem()));}
1119    int extdeg= getextension(degreelist, degree(f));
1120    DEBOUTLN(CERR, "Extension needed of degree ", extdeg);
1121
1122    // Now the real stuff!
1123    if ( newuord.length() == 0 ){ // no transzendentals
1124      DEBOUTMSG(CERR, "No transzendentals!");
1125      if ( extdeg > 1 ){
1126        CanonicalForm MIPO= generate_mipo( extdeg, vminpoly);
1127        DEBOUTLN(CERR, "Minpoly produced ", MIPO);
1128        vminpoly= rootOf(MIPO);
1129      }
1130      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1131      DEBDECLEVEL(CERR,"newfactoras");
1132      return Factorlist;
1133    }
1134    else if ( inseperable(Astar) > 0 ){ // Look if extensions are seperable
1135      // a) Use Endler
1136      DEBOUTMSG(CERR, "Inseperable extensions! Using Endler!");
1137      CFFList templist= endler(f,Astar, newuord);
1138      DEBOUTLN(CERR, "Endler gives: ", templist);
1139      return templist;
1140    }
1141    else{ // we are on the save side: Use trager
1142      DEBOUTMSG(CERR, "Only seperable extensions!");
1143      if (extdeg > 1 ){
1144        CanonicalForm MIPO=generate_mipo(extdeg, vminpoly );
1145        vminpoly= rootOf(MIPO);
1146        DEBOUTLN(CERR, "Minpoly generated: ", MIPO);
1147        DEBOUTLN(CERR, "vminpoly= ", vminpoly);
1148        DEBOUTLN(CERR, "degree(vminpoly)= ", degree(vminpoly));
1149      }
1150      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1151      DEBDECLEVEL(CERR,"newfactoras");
1152      return Factorlist;
1153    }
1154  }
1155  else{ // char=0 apply trager directly
1156    DEBOUTMSG(CERR, "Char=0! Apply Trager!");
1157    Variable vminpoly;
1158    Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1159      DEBDECLEVEL(CERR,"newfactoras");
1160      return Factorlist;
1161  }
1162
1163  DEBDECLEVEL(CERR,"newfactoras");
1164  return CFFList(CFFactor(f,1));
1165}
1166
1167CFFList
1168newcfactor(const CanonicalForm & f, const CFList & as, int & success )
1169{
1170  On (SW_RATIONAL);
1171  CFFList Output, output, Factors= factorize(f);
1172  if (Factors.getFirst().factor().inCoeffDomain())
1173    Factors.removeFirst();
1174
1175  if ( as.length() == 0 )
1176  {
1177    success=1;
1178    return Factors;
1179  }
1180  if ( cls(f) <= cls(as.getLast()) )
1181  {
1182    success=1;
1183    return Factors;
1184  }
1185
1186  success=1;
1187  for ( CFFListIterator i=Factors; i.hasItem(); i++ )
1188  {
1189    output=newfactoras(i.getItem().factor(),as, success);
1190    for ( CFFListIterator j=output; j.hasItem(); j++)
1191      Output = myappend(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp()));
1192  }
1193  return Output;
1194}
Note: See TracBrowser for help on using the repository browser.