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

spielwiese
Last change on this file since ac2c30 was ac2c30, checked in by Martin Lee <martinlee84@…>, 10 years ago
chg: improve QuasiInverse
  • 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              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  return t1;
517}
518
519CanonicalForm
520evaluate (const CanonicalForm& f, const CanonicalForm& g, const CanonicalForm& h, const CanonicalForm& powH)
521{
522  if (f.inCoeffDomain())
523    return f;
524  CFIterator i= f;
525  int lastExp = i.exp();
526  CanonicalForm result = i.coeff()*powH;
527  i++;
528  while (i.hasTerms())
529  {
530    int i_exp= i.exp();
531    if ((lastExp - i_exp) == 1)
532    {
533      result *= g;
534      result /= h;
535    }
536    else
537    {
538      result *= power (g, lastExp - i_exp);
539      result /= power (h, lastExp - i_exp);
540    }
541    result += i.coeff()*powH;
542    lastExp = i_exp;
543    i++;
544  }
545  if (lastExp != 0)
546  {
547    result *= power (g, lastExp);
548    result /= power (h, lastExp);
549  }
550  return result;
551}
552
553
554/// evaluate f at g/h at v such that powH*f is integral i.e. powH is assumed to be h^degree(f,v)
555CanonicalForm
556evaluate (const CanonicalForm& f, const CanonicalForm& g,
557          const CanonicalForm& h, const CanonicalForm& powH,
558          const Variable& v)
559{
560  if (f.inCoeffDomain())
561  {
562    return f*powH;
563  }
564
565  Variable x = f.mvar();
566  if ( v > x )
567    return f*powH;
568  else  if ( v == x )
569    return evaluate (f, g, h, powH);
570
571  // v is less than main variable of f
572  CanonicalForm result= 0;
573  for (CFIterator i= f; i.hasTerms(); i++)
574    result += evaluate (i.coeff(), g, h, powH, v)*power (x, i.exp());
575  return result;
576}
577
578// calculate a "primitive element"
579// K must have more than S elements (->thesis, -> getextension)
580static CFList
581simpleextension(CFList& backSubst, const CFList & Astar,
582                const Variable & Extension, bool& isFunctionField,
583                CanonicalForm & R)
584{
585  CFList Returnlist, Bstar=Astar;
586  CanonicalForm s, g, ra, rb, oldR, h, denra, denrb=1;
587  Variable alpha;
588  CFList tmp;
589
590  bool isRat= isOn (SW_RATIONAL);
591
592  DEBOUTLN(CERR, "simpleextension: Astar= ", Astar);
593  DEBOUTLN(CERR, "simpleextension:     R= ", R);
594  DEBOUTLN(CERR, "simpleextension: Extension= ", Extension);
595  CFListIterator j;
596  if (Astar.length() == 1)
597  {
598    R= Astar.getFirst();
599    rb= R.mvar();
600  }
601  else
602  {
603    R=Bstar.getFirst();
604    Bstar.removeFirst();
605    for (CFListIterator i=Bstar; i.hasItem(); i++)
606    {
607      j= i;
608      j++;
609      Off (SW_RATIONAL);
610      R /= icontent (R);
611      On (SW_RATIONAL);
612      oldR= R;
613      sqrf_norm (i.getItem(), R, Extension, s, g, R);
614
615      backSubst.insert (s);
616
617      Off (SW_RATIONAL);
618      R /= icontent (R);
619
620      On (SW_RATIONAL);
621
622      if (!isFunctionField)
623      {
624        alpha= rootOf (R);
625        h= replacevar (g, g.mvar(), alpha);
626        On (SW_RATIONAL); //needed for GCD
627        h= gcd (h, oldR);
628        h /= Lc (h);
629        ra= -h[0];
630        ra= replacevar(ra, alpha, g.mvar());
631        rb= R.mvar()-s*ra;
632        for (; j.hasItem(); j++)
633        {
634          j.getItem()= j.getItem() (ra, oldR.mvar());
635          j.getItem()= j.getItem() (rb, i.getItem().mvar());
636        }
637      }
638      else
639      {
640        On (SW_RATIONAL);
641        h= swapvar (g, g.mvar(), oldR.mvar());
642        tmp= CFList (swapvar (R, g.mvar(), oldR.mvar()));
643        h= alg_gcd (h, swapvar (oldR, g.mvar(), oldR.mvar()), tmp);
644        CanonicalForm hh= replacevar (h, oldR.mvar(), alpha);
645
646        CanonicalForm numinv, deninv;
647        numinv= QuasiInverse (tmp.getFirst(), LC (h), tmp.getFirst().mvar());
648
649        Off (SW_RATIONAL);
650        h *= numinv;
651        h= reduce (h, tmp.getFirst());
652        deninv= LC(h);
653
654        ra= -h[0];
655        denra= gcd (ra, deninv);
656        ra /= denra;
657        denra= deninv/denra;
658        denra= replacevar (denra, ra.mvar(), g.mvar());
659        ra= replacevar(ra, ra.mvar(), g.mvar());
660        rb= R.mvar()*denra-s*ra;
661        denrb= denra;
662        for (; j.hasItem(); j++)
663        {
664          CanonicalForm powdenra= power (denra, degree (j.getItem(), oldR.mvar()));
665          j.getItem()= evaluate (j.getItem(),ra, denra, powdenra, oldR.mvar());
666          powdenra= power (denra, degree (j.getItem(), i.getItem().mvar()));
667          j.getItem()= evaluate (j.getItem(), rb, denrb, powdenra, i.getItem().mvar());
668        }
669      }
670
671      DEBOUTLN(CERR, "simpleextension: g= ", g);
672      DEBOUTLN(CERR, "simpleextension: s= ", s);
673      DEBOUTLN(CERR, "simpleextension: R= ", R);
674      Returnlist.append (ra);
675      if (isFunctionField)
676        Returnlist.append (denra);
677    }
678  }
679  Returnlist.append (rb);
680  if (isFunctionField)
681    Returnlist.append (denrb);
682
683  if (isRat)
684    On (SW_RATIONAL);
685  else
686    Off (SW_RATIONAL);
687
688  return Returnlist;
689}
690
691CanonicalForm alg_lc(const CanonicalForm &f)
692{
693  if (f.level()>0)
694  {
695    return alg_lc(f.LC());
696  }
697  //assert(f.inCoeffDomain());
698  return f;
699}
700
701CanonicalForm
702subst (const CanonicalForm& f, const CFList& a, const CFList& b,
703       const CanonicalForm& Rstar, bool isFunctionField)
704{
705  if (isFunctionField)
706    ASSERT (2*a.length() == b.length(), "wrong length of lists");
707  else
708    ASSERT (a.length() == b.length(), "lists of equal length expected");
709  CFListIterator j= b;
710  CanonicalForm result= f, tmp, powj;
711  for (CFListIterator i= a; i.hasItem() && j.hasItem(); i++, j++)
712  {
713    if (!isFunctionField)
714      result= result (j.getItem(), i.getItem().mvar());
715    else
716    {
717      tmp= j.getItem();
718      j++;
719      powj= power (j.getItem(), degree (result, i.getItem().mvar()));
720      result= evaluate (result, tmp, j.getItem(), powj, i.getItem().mvar());
721
722      if (fdivides (powj, result, tmp))
723        result= tmp;
724
725      result /= vcontent (result, Variable (i.getItem().level() + 1));
726    }
727  }
728  result= reduce (result, Rstar);
729  result /= vcontent (result, Variable (Rstar.level() + 1));
730  return result;
731}
732
733CanonicalForm
734backSubst (const CanonicalForm& F, const CFList& a, const CFList& b)
735{
736  ASSERT (a.length() == b.length() - 1, "wrong length of lists in backSubst");
737  CanonicalForm result= F;
738  Variable tmp;
739  CFList tmp2= b;
740  tmp= tmp2.getLast().mvar();
741  tmp2.removeLast();
742  for (CFListIterator iter= a; iter.hasItem(); iter++)
743  {
744    result= result (tmp+iter.getItem()*tmp2.getLast().mvar(), tmp);
745    tmp= tmp2.getLast().mvar();
746    tmp2.removeLast();
747  }
748  return result;
749}
750
751// the heart of the algorithm: the one from Trager
752#ifndef DEBUGOUTPUT
753static CFFList
754alg_factor( const CanonicalForm & F, const CFList & Astar, const Variable & vminpoly, const Varlist /*& oldord*/, const CFList & as, bool isFunctionField)
755#else
756static CFFList
757alg_factor( const CanonicalForm & F, const CFList & Astar, const Variable & vminpoly, const Varlist & oldord, const CFList & as, bool isFunctionField)
758#endif
759{
760  CFFList L, Factorlist;
761  CanonicalForm R, Rstar, s, g, h, f= F;
762  CFList substlist, backSubsts;
763
764  DEBINCLEVEL(CERR,"alg_factor");
765  DEBOUTLN(CERR, "alg_factor: f= ", f);
766
767  substlist= simpleextension (backSubsts, Astar, vminpoly, isFunctionField, Rstar);
768  DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
769  DEBOUTLN(CERR, "alg_factor: minpoly Rstar= ", Rstar);
770  DEBOUTLN(CERR, "alg_factor: vminpoly= ", vminpoly);
771
772  f= subst (f, Astar, substlist, Rstar, isFunctionField);
773
774  Variable alpha;
775  if (!isFunctionField)
776  {
777    alpha= rootOf (Rstar);
778    g= replacevar (f, Rstar.mvar(), alpha);
779
780    Factorlist= factorize (g, alpha);
781
782    for (CFFListIterator i= Factorlist; i.hasItem(); i++)
783    {
784      h= i.getItem().factor();
785      if (!h.inCoeffDomain())
786      {
787        h= replacevar (h, alpha, Rstar.mvar());
788        h *= bCommonDen(h);
789        h= backSubst (h, backSubsts, Astar);
790        h= Prem (h, as);
791        L.append (CFFactor (h, i.getItem().exp()));
792      }
793    }
794    return L;
795  }
796  // after here we are over an extension of a function field
797
798
799  // make quasi monic
800  CFList Rstarlist= CFList (Rstar);
801  CanonicalForm numinv;
802  On (SW_RATIONAL);
803  numinv= QuasiInverse (Rstar, LC(f), Rstar.mvar());
804
805  f *= numinv;
806  f= Prem (f, Rstarlist);
807  f /= vcontent (f, Rstar.mvar());
808  // end quasi monic
809
810  sqrf_norm(f, Rstar, vminpoly, s, g, R );
811  //out_cf("sqrf_norm R:",R,"\n");
812  //out_cf("sqrf_norm s:",s,"\n");
813  //out_cf("sqrf_norm g:",g,"\n");
814  DEBOUTLN(CERR, "alg_factor: g= ", g);
815  DEBOUTLN(CERR, "alg_factor: s= ", s);
816  DEBOUTLN(CERR, "alg_factor: R= ", R);
817  Off(SW_RATIONAL);
818  Variable X;
819  if (getAlgVar(R,X))
820  {
821    // factorize R over alg.extension with X
822    //CERR << "alg: "<< X << " mipo=" << getMipo(X,Variable('X')) <<"\n";
823    DEBOUTLN(CERR, "alg_factor: factorize( ", R);
824    Factorlist =  factorize( R, X );
825  }
826  else
827  {
828    // factor R over k
829    DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
830    Factorlist = factorize(R);
831  }
832
833  On(SW_RATIONAL);
834  DEBOUTLN(CERR, "alg_factor: Factorize(R)= ", Factorlist);
835  if ( !Factorlist.getFirst().factor().inCoeffDomain() )
836    Factorlist.insert(CFFactor(1,1));
837  if ( Factorlist.length() == 2 && Factorlist.getLast().exp()== 1)
838  { // irreduzibel (first entry is a constant)
839    L.append(CFFactor(f,1));
840  }
841  else
842  {
843    g= f;
844    DEBOUTLN(CERR, "alg_factor: g= ", g);
845    for ( CFFListIterator i=Factorlist; i.hasItem(); i++)
846    {
847      CanonicalForm fnew=i.getItem().factor();
848      if (fnew.level() < Rstar.level()) //factor is a constant from the function field
849        continue;
850      else
851      {
852        fnew= fnew (g.mvar()+s*Rstar.mvar(), g.mvar());
853        fnew= reduce (fnew, Rstar);
854      }
855
856      DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
857
858      h= alg_gcd (g, fnew, Rstarlist);
859      numinv= QuasiInverse(Rstar, LC(h), Rstar.mvar());
860      h *= numinv;
861      h= Prem (h, Rstarlist);
862      h /= vcontent (h, Rstar.mvar());
863
864      if (h.level() >= Rstar.level())
865      {
866        g= divide (g, h, Rstarlist);
867        h= backSubst (h, backSubsts, Astar);
868        h= Prem (h, as);
869        h *= bCommonDen (h);
870        h /= vcontent (h, as.getFirst().mvar());
871        L.append (CFFactor (h, 1));
872      }
873    }
874    // we are not interested in a
875    // constant (over K_r, which can be a polynomial!)
876    if (degree(g, f.mvar())>0){ L.append(CFFactor(g,1)); }
877  }
878  CFFList LL;
879  if (getCharacteristic()>0)
880  {
881    CFFListIterator i=L;
882    CanonicalForm c_fac=1;
883    CanonicalForm c;
884    for(;i.hasItem(); i++ )
885    {
886      CanonicalForm ff=i.getItem().factor();
887      c=alg_lc(ff);
888      int e=i.getItem().exp();
889      ff/=c;
890      if (!ff.isOne()) LL.append(CFFactor(ff,e));
891      while (e>0) { c_fac*=c;e--; }
892    }
893    if (!c_fac.isOne()) LL.insert(CFFactor(c_fac,1));
894  }
895  else
896  {
897    LL=L;
898  }
899  //CFFListIterator i=LL;
900  //for(;i.hasItem(); i++ )
901  //  out_cf("end alg_f:",i.getItem().factor(),"\n");
902  //printf("end alg_factor\n");
903  DEBOUTLN(CERR, "alg_factor: L= ", LL);
904  DEBDECLEVEL(CERR,"alg_factor");
905  return LL;
906}
907
908static CFFList
909endler( const CanonicalForm & f, const CFList & AS, const Varlist & uord )
910{
911  CanonicalForm F=f, g, q,r;
912  CFFList Output;
913  CFList One, Two, asnew, as=AS;
914  CFListIterator i,ii;
915  VarlistIterator j;
916  Variable vg;
917
918  for (i=as; i.hasItem(); i++)
919  {
920    g= i.getItem();
921    if (g.deriv() == 0 )
922    {
923      DEBOUTLN(CERR, "Inseperable extension detected: ", g);
924      for (j=uord; j.hasItem(); j++)
925      {
926        if ( degree(g,j.getItem()) > 0 ) vg= j.getItem();
927      }
928      // Now we have the highest transzendental in vg;
929      DEBOUTLN(CERR, "Transzendental is ", vg);
930      CanonicalForm gg=-1*g[0];
931      divrem(gg,vg,q,r); r= gg-q*vg;   gg= gg-r;
932      //DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
933      DEBOUTLN(CERR, "  that is ", gg);
934      DEBOUTLN(CERR, "  maps to ", g+gg);
935      One.insert(gg); Two.insert(g+gg);
936      // Now transform all remaining polys in as:
937      int x=0;
938      for (ii=i; ii.hasItem(); ii++)
939      {
940        if ( x != 0 )
941        {
942          divrem(ii.getItem(), gg, q,r);
943//          CERR << ii.getItem() << " divided by " << gg << "\n";
944          DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
945          ii.append(ii.getItem()+q*g); ii.remove(1);
946          DEBOUTLN(CERR, "as= ", as);
947        }
948        x+= 1;
949      }
950      // Now transform F:
951      divrem(F, gg, q,r);
952      F= F+q*g;
953      DEBOUTLN(CERR, "new F= ", F);
954    }
955    else{ asnew.append(i.getItem());  }// just the identity
956  }
957  // factor F with minimal polys given in asnew:
958  DEBOUTLN(CERR, "Factor F=  ", F);
959  DEBOUTLN(CERR, "  with as= ", asnew);
960  int success=0;
961  CFFList factorlist= newcfactor(F,asnew, success);
962  DEBOUTLN(CERR, "  gives =  ", factorlist);
963  DEBOUTLN(CERR, "One= ", One);
964  DEBOUTLN(CERR, "Two= ", Two);
965
966  // Transform back:
967  for ( CFFListIterator k=factorlist; k.hasItem(); k++)
968  {
969    CanonicalForm factor= k.getItem().factor();
970    ii=One;
971    for (i=Two; i.hasItem(); i++)
972    {
973      DEBOUTLN(CERR, "Mapping ", i.getItem());
974      DEBOUTLN(CERR, "     to ", ii.getItem());
975      DEBOUTLN(CERR, "     in ", factor);
976      divrem(factor,i.getItem(),q,r); r=factor -q*i.getItem();
977      DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
978      factor= ii.getItem()*q +r; //
979      ii++;
980    }
981    Output.append(CFFactor(factor,k.getItem().exp()));
982  }
983
984  return Output;
985}
986
987void
988multiplicity (CFFList& factors, const CanonicalForm& F, const CFList& as)
989{
990  CanonicalForm G= F;
991  Variable x= F.mvar();
992  CanonicalForm q, r;
993  int count= -1;
994  On (SW_RATIONAL);
995  for (CFFListIterator iter=factors; iter.hasItem(); iter++)
996  {
997    count= -1;
998    if (iter.getItem().factor().inCoeffDomain())
999      continue;
1000    while (1)
1001    {
1002      psqr (G, iter.getItem().factor(), q, r, x);
1003
1004      q= Prem (q, as);
1005      r= Prem (r, as);
1006      if (!r.isZero())
1007        break;
1008      On (SW_RATIONAL);
1009      count++;
1010      G= q;
1011    }
1012    iter.getItem()= CFFactor (iter.getItem().factor(), iter.getItem().exp()+count);
1013  }
1014}
1015
1016
1017// 1) prepares data
1018// 2) for char=p we distinguish 3 cases:
1019//           no transcendentals, seperable and inseperable extensions
1020CFFList
1021newfactoras( const CanonicalForm & f, const CFList & as, int &success)
1022{
1023  Variable vf=f.mvar();
1024  CFListIterator i;
1025  CFFListIterator jj;
1026  CFList reduceresult;
1027  CFFList result;
1028
1029  success=1;
1030  DEBINCLEVEL(CERR, "newfactoras");
1031  DEBOUTLN(CERR, "newfactoras called with f= ", f);
1032  DEBOUTLN(CERR, "               content(f)= ", content(f));
1033  DEBOUTLN(CERR, "                       as= ", as);
1034  DEBOUTLN(CERR, "newfactoras: cls(vf)= ", cls(vf));
1035  DEBOUTLN(CERR, "newfactoras: cls(as.getLast())= ", cls(as.getLast()));
1036  DEBOUTLN(CERR, "newfactoras: degree(f,vf)= ", degree(f,vf));
1037
1038// F1: [Test trivial cases]
1039// 1) first trivial cases:
1040  if ( (cls(vf) <= cls(as.getLast())) ||  degree(f,vf)<=1 ){
1041// ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) )
1042    DEBDECLEVEL(CERR,"newfactoras");
1043    return CFFList(CFFactor(f,1));
1044  }
1045
1046// 2) List of variables:
1047// 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1
1048// 2b) Setup variableordering
1049  CFList Astar;
1050  Variable x;
1051  CanonicalForm elem;
1052  Varlist ord, uord,oldord;
1053  for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii));  }
1054  oldord= uord; oldord.append(vf);
1055
1056  for ( i=as; i.hasItem(); i++ ){
1057    elem= i.getItem();
1058    x= elem.mvar();
1059    if ( degree(elem,x) > 1){ // otherwise it's not an extension
1060      Astar.append(elem);
1061      ord.append(x);
1062    }
1063  }
1064  uord= Difference(uord,ord);
1065  DEBOUTLN(CERR, "Astar is: ", Astar);
1066  DEBOUTLN(CERR, "ord is: ", ord);
1067  DEBOUTLN(CERR, "uord is: ", uord);
1068
1069// 3) second trivial cases: we already prooved irr. of f over no extensions
1070  if ( Astar.length() == 0 ){
1071    DEBDECLEVEL(CERR,"newfactoras");
1072    return CFFList(CFFactor(f,1));
1073  }
1074
1075// 4) Try to obtain a partial factorization using prop2 and prop3
1076//    Use with caution! We have to proof these propositions first!
1077  // Not yet implemented
1078
1079// 5) Look if elements in uord actually occure in any of the minimal
1080//    polynomials. If no element of uord occures in any of the minimal
1081//   polynomials, we don't have transzendentals.
1082  Varlist newuord=Var_is_in_AS(uord,Astar);
1083  DEBOUTLN(CERR, "newuord is: ", newuord);
1084
1085  CFFList Factorlist;
1086  Varlist gcdord= Union(ord,newuord); gcdord.append(f.mvar());
1087  bool isFunctionField= (newuord.length() > 0);
1088
1089  // This is for now. we need alg_sqrfree implemented!
1090  CanonicalForm Fgcd= 0;
1091  if (isFunctionField)
1092    Fgcd= alg_gcd(f,f.deriv(),Astar);
1093
1094  if ( Fgcd == 0 ) {DEBOUTMSG(CERR, "WARNING: p'th root ?");}
1095  if (isFunctionField && ( degree(Fgcd, f.mvar()) > 0) && (!(f.deriv().isZero())) )
1096  {
1097    DEBOUTLN(CERR, "Nontrivial GCD found of ", f);
1098    CanonicalForm Ggcd= divide(f, Fgcd,Astar);
1099    if (getCharacteristic() == 0)
1100    {
1101      CFFList result= newfactoras (Ggcd,as,success); //Ggcd is the squarefree part of f
1102      multiplicity (result, f, Astar);
1103      return result;
1104    }
1105    DEBOUTLN(CERR, "  split into ", Fgcd);
1106    DEBOUTLN(CERR, "         and ", Ggcd);
1107    Fgcd= pp(Fgcd); Ggcd= pp(Ggcd);
1108    DEBDECLEVEL(CERR,"newfactoras");
1109    return myUnion(newfactoras(Fgcd,as,success) , newfactoras(Ggcd,as,success));
1110  }
1111  if ( getCharacteristic() > 0 )
1112  {
1113    // First look for extension!
1114    IntList degreelist;
1115    Variable vminpoly;
1116    for (i=Astar; i.hasItem(); i++){degreelist.append(degree(i.getItem()));}
1117    int extdeg= getextension(degreelist, degree(f));
1118    DEBOUTLN(CERR, "Extension needed of degree ", extdeg);
1119
1120    // Now the real stuff!
1121    if ( newuord.length() == 0 ){ // no transzendentals
1122      DEBOUTMSG(CERR, "No transzendentals!");
1123      if ( extdeg > 1 ){
1124        CanonicalForm MIPO= generate_mipo( extdeg, vminpoly);
1125        DEBOUTLN(CERR, "Minpoly produced ", MIPO);
1126        vminpoly= rootOf(MIPO);
1127      }
1128      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1129      DEBDECLEVEL(CERR,"newfactoras");
1130      return Factorlist;
1131    }
1132    else if ( inseperable(Astar) > 0 ){ // Look if extensions are seperable
1133      // a) Use Endler
1134      DEBOUTMSG(CERR, "Inseperable extensions! Using Endler!");
1135      CFFList templist= endler(f,Astar, newuord);
1136      DEBOUTLN(CERR, "Endler gives: ", templist);
1137      return templist;
1138    }
1139    else{ // we are on the save side: Use trager
1140      DEBOUTMSG(CERR, "Only seperable extensions!");
1141      if (extdeg > 1 ){
1142        CanonicalForm MIPO=generate_mipo(extdeg, vminpoly );
1143        vminpoly= rootOf(MIPO);
1144        DEBOUTLN(CERR, "Minpoly generated: ", MIPO);
1145        DEBOUTLN(CERR, "vminpoly= ", vminpoly);
1146        DEBOUTLN(CERR, "degree(vminpoly)= ", degree(vminpoly));
1147      }
1148      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1149      DEBDECLEVEL(CERR,"newfactoras");
1150      return Factorlist;
1151    }
1152  }
1153  else{ // char=0 apply trager directly
1154    DEBOUTMSG(CERR, "Char=0! Apply Trager!");
1155    Variable vminpoly;
1156    Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1157      DEBDECLEVEL(CERR,"newfactoras");
1158      return Factorlist;
1159  }
1160
1161  DEBDECLEVEL(CERR,"newfactoras");
1162  return CFFList(CFFactor(f,1));
1163}
1164
1165CFFList
1166newcfactor(const CanonicalForm & f, const CFList & as, int & success )
1167{
1168  On (SW_RATIONAL);
1169  CFFList Output, output, Factors= factorize(f);
1170  if (Factors.getFirst().factor().inCoeffDomain())
1171    Factors.removeFirst();
1172
1173  if ( as.length() == 0 )
1174  {
1175    success=1;
1176    return Factors;
1177  }
1178  if ( cls(f) <= cls(as.getLast()) )
1179  {
1180    success=1;
1181    return Factors;
1182  }
1183
1184  success=1;
1185  for ( CFFListIterator i=Factors; i.hasItem(); i++ )
1186  {
1187    output=newfactoras(i.getItem().factor(),as, success);
1188    for ( CFFListIterator j=output; j.hasItem(); j++)
1189      Output = myappend(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp()));
1190  }
1191  return Output;
1192}
Note: See TracBrowser for help on using the repository browser.