source: git/factory/libfac/charset/alg_factor.cc @ 36aaf8

spielwiese
Last change on this file since 36aaf8 was 36aaf8, checked in by Martin Lee <martinlee84@…>, 10 years ago
chg: added alg_LC
  • Property mode set to 100644
File size: 33.0 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 alg_LC (const CanonicalForm& f, int lev)
702{
703  CanonicalForm result= f;
704  while (result.level() > lev)
705    result= LC (result);
706  return result;
707}
708
709CanonicalForm
710subst (const CanonicalForm& f, const CFList& a, const CFList& b,
711       const CanonicalForm& Rstar, bool isFunctionField)
712{
713  if (isFunctionField)
714    ASSERT (2*a.length() == b.length(), "wrong length of lists");
715  else
716    ASSERT (a.length() == b.length(), "lists of equal length expected");
717  CFListIterator j= b;
718  CanonicalForm result= f, tmp, powj;
719  for (CFListIterator i= a; i.hasItem() && j.hasItem(); i++, j++)
720  {
721    if (!isFunctionField)
722      result= result (j.getItem(), i.getItem().mvar());
723    else
724    {
725      tmp= j.getItem();
726      j++;
727      powj= power (j.getItem(), degree (result, i.getItem().mvar()));
728      result= evaluate (result, tmp, j.getItem(), powj, i.getItem().mvar());
729
730      if (fdivides (powj, result, tmp))
731        result= tmp;
732
733      result /= vcontent (result, Variable (i.getItem().level() + 1));
734    }
735  }
736  result= reduce (result, Rstar);
737  result /= vcontent (result, Variable (Rstar.level() + 1));
738  return result;
739}
740
741CanonicalForm
742backSubst (const CanonicalForm& F, const CFList& a, const CFList& b)
743{
744  ASSERT (a.length() == b.length() - 1, "wrong length of lists in backSubst");
745  CanonicalForm result= F;
746  Variable tmp;
747  CFList tmp2= b;
748  tmp= tmp2.getLast().mvar();
749  tmp2.removeLast();
750  for (CFListIterator iter= a; iter.hasItem(); iter++)
751  {
752    result= result (tmp+iter.getItem()*tmp2.getLast().mvar(), tmp);
753    tmp= tmp2.getLast().mvar();
754    tmp2.removeLast();
755  }
756  return result;
757}
758
759// the heart of the algorithm: the one from Trager
760#ifndef DEBUGOUTPUT
761static CFFList
762alg_factor( const CanonicalForm & F, const CFList & Astar, const Variable & vminpoly, const Varlist /*& oldord*/, const CFList & as, bool isFunctionField)
763#else
764static CFFList
765alg_factor( const CanonicalForm & F, const CFList & Astar, const Variable & vminpoly, const Varlist & oldord, const CFList & as, bool isFunctionField)
766#endif
767{
768  CFFList L, Factorlist;
769  CanonicalForm R, Rstar, s, g, h, f= F;
770  CFList substlist, backSubsts;
771
772  DEBINCLEVEL(CERR,"alg_factor");
773  DEBOUTLN(CERR, "alg_factor: f= ", f);
774
775  substlist= simpleextension (backSubsts, Astar, vminpoly, isFunctionField, Rstar);
776  DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
777  DEBOUTLN(CERR, "alg_factor: minpoly Rstar= ", Rstar);
778  DEBOUTLN(CERR, "alg_factor: vminpoly= ", vminpoly);
779
780  f= subst (f, Astar, substlist, Rstar, isFunctionField);
781
782  Variable alpha;
783  if (!isFunctionField)
784  {
785    alpha= rootOf (Rstar);
786    g= replacevar (f, Rstar.mvar(), alpha);
787
788    Factorlist= factorize (g, alpha);
789
790    for (CFFListIterator i= Factorlist; i.hasItem(); i++)
791    {
792      h= i.getItem().factor();
793      if (!h.inCoeffDomain())
794      {
795        h= replacevar (h, alpha, Rstar.mvar());
796        h *= bCommonDen(h);
797        h= backSubst (h, backSubsts, Astar);
798        h= Prem (h, as);
799        L.append (CFFactor (h, i.getItem().exp()));
800      }
801    }
802    return L;
803  }
804  // after here we are over an extension of a function field
805
806
807  // make quasi monic
808  CFList Rstarlist= CFList (Rstar);
809  int algExtLevel= Astar.getLast().level(); //highest level of algebraic variables
810  CanonicalForm numinv;
811  On (SW_RATIONAL);
812  numinv= QuasiInverse (Rstar, alg_LC(f, algExtLevel), Rstar.mvar());
813
814  f *= numinv;
815  f= Prem (f, Rstarlist);
816  f /= vcontent (f, Rstar.mvar());
817  // end quasi monic
818
819  sqrf_norm(f, Rstar, vminpoly, s, g, R );
820  //out_cf("sqrf_norm R:",R,"\n");
821  //out_cf("sqrf_norm s:",s,"\n");
822  //out_cf("sqrf_norm g:",g,"\n");
823  DEBOUTLN(CERR, "alg_factor: g= ", g);
824  DEBOUTLN(CERR, "alg_factor: s= ", s);
825  DEBOUTLN(CERR, "alg_factor: R= ", R);
826  Off(SW_RATIONAL);
827  Variable X;
828  if (getAlgVar(R,X))
829  {
830    // factorize R over alg.extension with X
831    //CERR << "alg: "<< X << " mipo=" << getMipo(X,Variable('X')) <<"\n";
832    DEBOUTLN(CERR, "alg_factor: factorize( ", R);
833    Factorlist =  factorize( R, X );
834  }
835  else
836  {
837    // factor R over k
838    DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
839    Factorlist = factorize(R);
840  }
841
842  On(SW_RATIONAL);
843  DEBOUTLN(CERR, "alg_factor: Factorize(R)= ", Factorlist);
844  if ( !Factorlist.getFirst().factor().inCoeffDomain() )
845    Factorlist.insert(CFFactor(1,1));
846  if ( Factorlist.length() == 2 && Factorlist.getLast().exp()== 1)
847  { // irreduzibel (first entry is a constant)
848    L.append(CFFactor(f,1));
849  }
850  else
851  {
852    g= f;
853    DEBOUTLN(CERR, "alg_factor: g= ", g);
854    for ( CFFListIterator i=Factorlist; i.hasItem(); i++)
855    {
856      CanonicalForm fnew=i.getItem().factor();
857      if (fnew.level() < Rstar.level()) //factor is a constant from the function field
858        continue;
859      else
860      {
861        fnew= fnew (g.mvar()+s*Rstar.mvar(), g.mvar());
862        fnew= reduce (fnew, Rstar);
863      }
864
865      DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
866
867      h= alg_gcd (g, fnew, Rstarlist);
868      numinv= QuasiInverse(Rstar, alg_LC(h, algExtLevel), Rstar.mvar());
869      h *= numinv;
870      h= Prem (h, Rstarlist);
871      h /= vcontent (h, Rstar.mvar());
872
873      if (h.level() >= Rstar.level())
874      {
875        g= divide (g, h, Rstarlist);
876        h= backSubst (h, backSubsts, Astar);
877        h= Prem (h, as);
878        h *= bCommonDen (h);
879        h /= vcontent (h, as.getFirst().mvar());
880        L.append (CFFactor (h, 1));
881      }
882    }
883    // we are not interested in a
884    // constant (over K_r, which can be a polynomial!)
885    if (degree(g, f.mvar())>0){ L.append(CFFactor(g,1)); }
886  }
887  CFFList LL;
888  if (getCharacteristic()>0)
889  {
890    CFFListIterator i=L;
891    CanonicalForm c_fac=1;
892    CanonicalForm c;
893    for(;i.hasItem(); i++ )
894    {
895      CanonicalForm ff=i.getItem().factor();
896      c=alg_lc(ff);
897      int e=i.getItem().exp();
898      ff/=c;
899      if (!ff.isOne()) LL.append(CFFactor(ff,e));
900      while (e>0) { c_fac*=c;e--; }
901    }
902    if (!c_fac.isOne()) LL.insert(CFFactor(c_fac,1));
903  }
904  else
905  {
906    LL=L;
907  }
908  //CFFListIterator i=LL;
909  //for(;i.hasItem(); i++ )
910  //  out_cf("end alg_f:",i.getItem().factor(),"\n");
911  //printf("end alg_factor\n");
912  DEBOUTLN(CERR, "alg_factor: L= ", LL);
913  DEBDECLEVEL(CERR,"alg_factor");
914  return LL;
915}
916
917static CFFList
918endler( const CanonicalForm & f, const CFList & AS, const Varlist & uord )
919{
920  CanonicalForm F=f, g, q,r;
921  CFFList Output;
922  CFList One, Two, asnew, as=AS;
923  CFListIterator i,ii;
924  VarlistIterator j;
925  Variable vg;
926
927  for (i=as; i.hasItem(); i++)
928  {
929    g= i.getItem();
930    if (g.deriv() == 0 )
931    {
932      DEBOUTLN(CERR, "Inseperable extension detected: ", g);
933      for (j=uord; j.hasItem(); j++)
934      {
935        if ( degree(g,j.getItem()) > 0 ) vg= j.getItem();
936      }
937      // Now we have the highest transzendental in vg;
938      DEBOUTLN(CERR, "Transzendental is ", vg);
939      CanonicalForm gg=-1*g[0];
940      divrem(gg,vg,q,r); r= gg-q*vg;   gg= gg-r;
941      //DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
942      DEBOUTLN(CERR, "  that is ", gg);
943      DEBOUTLN(CERR, "  maps to ", g+gg);
944      One.insert(gg); Two.insert(g+gg);
945      // Now transform all remaining polys in as:
946      int x=0;
947      for (ii=i; ii.hasItem(); ii++)
948      {
949        if ( x != 0 )
950        {
951          divrem(ii.getItem(), gg, q,r);
952//          CERR << ii.getItem() << " divided by " << gg << "\n";
953          DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
954          ii.append(ii.getItem()+q*g); ii.remove(1);
955          DEBOUTLN(CERR, "as= ", as);
956        }
957        x+= 1;
958      }
959      // Now transform F:
960      divrem(F, gg, q,r);
961      F= F+q*g;
962      DEBOUTLN(CERR, "new F= ", F);
963    }
964    else{ asnew.append(i.getItem());  }// just the identity
965  }
966  // factor F with minimal polys given in asnew:
967  DEBOUTLN(CERR, "Factor F=  ", F);
968  DEBOUTLN(CERR, "  with as= ", asnew);
969  int success=0;
970  CFFList factorlist= newcfactor(F,asnew, success);
971  DEBOUTLN(CERR, "  gives =  ", factorlist);
972  DEBOUTLN(CERR, "One= ", One);
973  DEBOUTLN(CERR, "Two= ", Two);
974
975  // Transform back:
976  for ( CFFListIterator k=factorlist; k.hasItem(); k++)
977  {
978    CanonicalForm factor= k.getItem().factor();
979    ii=One;
980    for (i=Two; i.hasItem(); i++)
981    {
982      DEBOUTLN(CERR, "Mapping ", i.getItem());
983      DEBOUTLN(CERR, "     to ", ii.getItem());
984      DEBOUTLN(CERR, "     in ", factor);
985      divrem(factor,i.getItem(),q,r); r=factor -q*i.getItem();
986      DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
987      factor= ii.getItem()*q +r; //
988      ii++;
989    }
990    Output.append(CFFactor(factor,k.getItem().exp()));
991  }
992
993  return Output;
994}
995
996void
997multiplicity (CFFList& factors, const CanonicalForm& F, const CFList& as)
998{
999  CanonicalForm G= F;
1000  Variable x= F.mvar();
1001  CanonicalForm q, r;
1002  int count= -1;
1003  On (SW_RATIONAL);
1004  for (CFFListIterator iter=factors; iter.hasItem(); iter++)
1005  {
1006    count= -1;
1007    if (iter.getItem().factor().inCoeffDomain())
1008      continue;
1009    while (1)
1010    {
1011      psqr (G, iter.getItem().factor(), q, r, x);
1012
1013      q= Prem (q, as);
1014      r= Prem (r, as);
1015      if (!r.isZero())
1016        break;
1017      On (SW_RATIONAL);
1018      count++;
1019      G= q;
1020    }
1021    iter.getItem()= CFFactor (iter.getItem().factor(), iter.getItem().exp()+count);
1022  }
1023}
1024
1025
1026// 1) prepares data
1027// 2) for char=p we distinguish 3 cases:
1028//           no transcendentals, seperable and inseperable extensions
1029CFFList
1030newfactoras( const CanonicalForm & f, const CFList & as, int &success)
1031{
1032  Variable vf=f.mvar();
1033  CFListIterator i;
1034  CFFListIterator jj;
1035  CFList reduceresult;
1036  CFFList result;
1037
1038  success=1;
1039  DEBINCLEVEL(CERR, "newfactoras");
1040  DEBOUTLN(CERR, "newfactoras called with f= ", f);
1041  DEBOUTLN(CERR, "               content(f)= ", content(f));
1042  DEBOUTLN(CERR, "                       as= ", as);
1043  DEBOUTLN(CERR, "newfactoras: cls(vf)= ", cls(vf));
1044  DEBOUTLN(CERR, "newfactoras: cls(as.getLast())= ", cls(as.getLast()));
1045  DEBOUTLN(CERR, "newfactoras: degree(f,vf)= ", degree(f,vf));
1046
1047// F1: [Test trivial cases]
1048// 1) first trivial cases:
1049  if ( (cls(vf) <= cls(as.getLast())) ||  degree(f,vf)<=1 ){
1050// ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) )
1051    DEBDECLEVEL(CERR,"newfactoras");
1052    return CFFList(CFFactor(f,1));
1053  }
1054
1055// 2) List of variables:
1056// 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1
1057// 2b) Setup variableordering
1058  CFList Astar;
1059  Variable x;
1060  CanonicalForm elem;
1061  Varlist ord, uord,oldord;
1062  for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii));  }
1063  oldord= uord; oldord.append(vf);
1064
1065  for ( i=as; i.hasItem(); i++ ){
1066    elem= i.getItem();
1067    x= elem.mvar();
1068    if ( degree(elem,x) > 1){ // otherwise it's not an extension
1069      Astar.append(elem);
1070      ord.append(x);
1071    }
1072  }
1073  uord= Difference(uord,ord);
1074  DEBOUTLN(CERR, "Astar is: ", Astar);
1075  DEBOUTLN(CERR, "ord is: ", ord);
1076  DEBOUTLN(CERR, "uord is: ", uord);
1077
1078// 3) second trivial cases: we already prooved irr. of f over no extensions
1079  if ( Astar.length() == 0 ){
1080    DEBDECLEVEL(CERR,"newfactoras");
1081    return CFFList(CFFactor(f,1));
1082  }
1083
1084// 4) Try to obtain a partial factorization using prop2 and prop3
1085//    Use with caution! We have to proof these propositions first!
1086  // Not yet implemented
1087
1088// 5) Look if elements in uord actually occure in any of the minimal
1089//    polynomials. If no element of uord occures in any of the minimal
1090//   polynomials, we don't have transzendentals.
1091  Varlist newuord=Var_is_in_AS(uord,Astar);
1092  DEBOUTLN(CERR, "newuord is: ", newuord);
1093
1094  CFFList Factorlist;
1095  Varlist gcdord= Union(ord,newuord); gcdord.append(f.mvar());
1096  bool isFunctionField= (newuord.length() > 0);
1097
1098  // This is for now. we need alg_sqrfree implemented!
1099  CanonicalForm Fgcd= 0;
1100  if (isFunctionField)
1101    Fgcd= alg_gcd(f,f.deriv(),Astar);
1102
1103  if ( Fgcd == 0 ) {DEBOUTMSG(CERR, "WARNING: p'th root ?");}
1104  if (isFunctionField && ( degree(Fgcd, f.mvar()) > 0) && (!(f.deriv().isZero())) )
1105  {
1106    DEBOUTLN(CERR, "Nontrivial GCD found of ", f);
1107    CanonicalForm Ggcd= divide(f, Fgcd,Astar);
1108    if (getCharacteristic() == 0)
1109    {
1110      CFFList result= newfactoras (Ggcd,as,success); //Ggcd is the squarefree part of f
1111      multiplicity (result, f, Astar);
1112      return result;
1113    }
1114    DEBOUTLN(CERR, "  split into ", Fgcd);
1115    DEBOUTLN(CERR, "         and ", Ggcd);
1116    Fgcd= pp(Fgcd); Ggcd= pp(Ggcd);
1117    DEBDECLEVEL(CERR,"newfactoras");
1118    return myUnion(newfactoras(Fgcd,as,success) , newfactoras(Ggcd,as,success));
1119  }
1120  if ( getCharacteristic() > 0 )
1121  {
1122    // First look for extension!
1123    IntList degreelist;
1124    Variable vminpoly;
1125    for (i=Astar; i.hasItem(); i++){degreelist.append(degree(i.getItem()));}
1126    int extdeg= getextension(degreelist, degree(f));
1127    DEBOUTLN(CERR, "Extension needed of degree ", extdeg);
1128
1129    // Now the real stuff!
1130    if ( newuord.length() == 0 ){ // no transzendentals
1131      DEBOUTMSG(CERR, "No transzendentals!");
1132      if ( extdeg > 1 ){
1133        CanonicalForm MIPO= generate_mipo( extdeg, vminpoly);
1134        DEBOUTLN(CERR, "Minpoly produced ", MIPO);
1135        vminpoly= rootOf(MIPO);
1136      }
1137      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1138      DEBDECLEVEL(CERR,"newfactoras");
1139      return Factorlist;
1140    }
1141    else if ( inseperable(Astar) > 0 ){ // Look if extensions are seperable
1142      // a) Use Endler
1143      DEBOUTMSG(CERR, "Inseperable extensions! Using Endler!");
1144      CFFList templist= endler(f,Astar, newuord);
1145      DEBOUTLN(CERR, "Endler gives: ", templist);
1146      return templist;
1147    }
1148    else{ // we are on the save side: Use trager
1149      DEBOUTMSG(CERR, "Only seperable extensions!");
1150      if (extdeg > 1 ){
1151        CanonicalForm MIPO=generate_mipo(extdeg, vminpoly );
1152        vminpoly= rootOf(MIPO);
1153        DEBOUTLN(CERR, "Minpoly generated: ", MIPO);
1154        DEBOUTLN(CERR, "vminpoly= ", vminpoly);
1155        DEBOUTLN(CERR, "degree(vminpoly)= ", degree(vminpoly));
1156      }
1157      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1158      DEBDECLEVEL(CERR,"newfactoras");
1159      return Factorlist;
1160    }
1161  }
1162  else{ // char=0 apply trager directly
1163    DEBOUTMSG(CERR, "Char=0! Apply Trager!");
1164    Variable vminpoly;
1165    Factorlist= alg_factor(f, Astar, vminpoly, oldord, as, isFunctionField);
1166      DEBDECLEVEL(CERR,"newfactoras");
1167      return Factorlist;
1168  }
1169
1170  DEBDECLEVEL(CERR,"newfactoras");
1171  return CFFList(CFFactor(f,1));
1172}
1173
1174CFFList
1175newcfactor(const CanonicalForm & f, const CFList & as, int & success )
1176{
1177  On (SW_RATIONAL);
1178  CFFList Output, output, Factors= factorize(f);
1179  if (Factors.getFirst().factor().inCoeffDomain())
1180    Factors.removeFirst();
1181
1182  if ( as.length() == 0 )
1183  {
1184    success=1;
1185    return Factors;
1186  }
1187  if ( cls(f) <= cls(as.getLast()) )
1188  {
1189    success=1;
1190    return Factors;
1191  }
1192
1193  success=1;
1194  for ( CFFListIterator i=Factors; i.hasItem(); i++ )
1195  {
1196    output=newfactoras(i.getItem().factor(),as, success);
1197    for ( CFFListIterator j=output; j.hasItem(); j++)
1198      Output = myappend(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp()));
1199  }
1200  return Output;
1201}
Note: See TracBrowser for help on using the repository browser.