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

spielwiese
Last change on this file since dbcaa4 was dbcaa4, checked in by Martin Lee <martinlee84@…>, 10 years ago
fix: simpleextension
  • Property mode set to 100644
File size: 30.5 KB
Line 
1////////////////////////////////////////////////////////////
2// emacs edit mode for this file is -*- C++ -*-
3////////////////////////////////////////////////////////////
4
5// FACTORY - Includes
6#include <factory.h>
7// Factor - Includes
8#include <tmpl_inst.h>
9#include <Factor.h>
10#include <SqrFree.h>
11#include <helpstuff.h>
12// Charset - Includes
13#include "csutil.h"
14#include "charset.h"
15#include "reorder.h"
16#include "algfactor.h"
17// some CC's need this:
18#include "alg_factor.h"
19
20void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
21
22#ifdef ALGFACTORDEBUG
23#  define DEBUGOUTPUT
24#else
25#  undef DEBUGOUTPUT
26#endif
27
28#include <libfac/factor/debug.h>
29
30static Varlist
31Var_is_in_AS(const Varlist & uord, const CFList & Astar);
32
33int getAlgVar(const CanonicalForm &f, Variable &X)
34{
35  if (f.inBaseDomain()) return 0;
36  if (f.inCoeffDomain())
37  {
38    if (f.level()!=0)
39    {
40      X= f.mvar();
41      return 1;
42    }
43    return getAlgVar(f.LC(),X);
44  }
45  if (f.inPolyDomain())
46  {
47    if (getAlgVar(f.LC(),X)) return 1;
48    for( CFIterator i=f; i.hasTerms(); i++)
49    {
50      if (getAlgVar(i.coeff(),X)) return 1;
51    }
52  }
53  return 0;
54}
55
56////////////////////////////////////////////////////////////////////////
57// This implements the algorithm of Trager for factorization of
58// (multivariate) polynomials over algebraic extensions and so called
59// function field extensions.
60////////////////////////////////////////////////////////////////////////
61
62// // missing class: IntGenerator:
63bool IntGenerator::hasItems() const
64{
65    return 1;
66}
67
68CanonicalForm IntGenerator::item() const
69//int IntGenerator::item() const
70{
71  //return current; //CanonicalForm( current );
72  return mapinto(CanonicalForm( current ));
73}
74
75void IntGenerator::next()
76{
77    current++;
78}
79
80static CanonicalForm
81resultante( const CanonicalForm & f, const CanonicalForm& g, const Variable & v )
82{
83  bool on_rational = isOn(SW_RATIONAL);
84  On(SW_RATIONAL);
85  CanonicalForm cd = bCommonDen( f );
86  CanonicalForm fz = f * cd;
87  cd = bCommonDen( g );
88  CanonicalForm gz = g * cd;
89  if (!on_rational)  Off(SW_RATIONAL);
90  CanonicalForm result;
91  if (getCharacteristic() == 0)
92    result= resultantZ (fz, gz,v);
93  else
94    result= resultant (fz,gz,v);
95
96  return result;
97}
98
99// sqr-free routine for algebraic extensions
100// we need it! Ex.: f=c^2+2*a*c-1; as=[a^2+1]; f=(c+a)^2
101//static CFFList alg_sqrfree( const CanonicalForm & f )
102//{
103//  CFFList L;
104//
105//  L.append(CFFactor(f,1));
106//  return L;
107//}
108
109// Calculates a square free norm
110// Input: f(x, alpha) a square free polynomial over K(alpha),
111// alpha is defined by the minimal polynomial Palpha
112// K has more than S elements (S is defined in thesis; look getextension)
113static void
114sqrf_norm_sub( const CanonicalForm & f, const CanonicalForm & PPalpha,
115           CFGenerator & myrandom, CanonicalForm & s,  CanonicalForm & g,
116           CanonicalForm & R)
117{
118  Variable y=PPalpha.mvar(),vf=f.mvar();
119  CanonicalForm temp, Palpha=PPalpha, t;
120  int sqfreetest=0;
121  CFFList testlist;
122  CFFListIterator i;
123
124  DEBOUTLN(CERR, "sqrf_norm_sub:      f= ", f);
125  DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
126  myrandom.reset();   s=myrandom.item();   g=f;
127  R= CanonicalForm(0);
128  DEBOUTLN(CERR, "sqrf_norm_sub: myrandom s= ", s);
129
130  // Norm, resultante taken with respect to y
131  while ( !sqfreetest )
132  {
133    DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
134    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
135    DEBOUTLN(CERR, "sqrf_norm_sub: R= ", R);
136    // sqfree check ; R is a polynomial in K[x]
137    if ( getCharacteristic() == 0 )
138    {
139      temp= gcd(R, R.deriv(vf));
140      DEBOUTLN(CERR, "sqrf_norm_sub: temp= ", temp);
141      if (degree(temp,vf) != 0 || temp == temp.genZero() )
142        sqfreetest= 0;
143      else
144        sqfreetest= 1;
145      DEBOUTLN(CERR, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
146    }
147    else
148    {
149      DEBOUTMSG(CERR, "Starting SqrFreeTest(R)!");
150      // Look at SqrFreeTest!
151      // (z+a^5+w)^4 with z<w<a should not give sqfreetest=1 !
152      // for now we use this workaround with Factorize...
153      // ...but it should go away soon!!!!
154      Variable X;
155      if (getAlgVar(R,X))
156        testlist=factorize( R, X );
157      else
158        testlist= factorize(R);
159      DEBOUTLN(CERR, "testlist= ", testlist);
160      testlist.removeFirst();
161      sqfreetest=1;
162      for ( i=testlist; i.hasItem(); i++)
163      {
164        if ( i.getItem().exp() > 1 && degree(i.getItem().factor(), R.mvar()) > 0)
165        {
166          sqfreetest=0;
167          break;
168        }
169      }
170      DEBOUTLN(CERR, "SqrFreeTest(R)= ", sqfreetest);
171    }
172    if ( ! sqfreetest )
173    {
174      myrandom.next();
175      DEBOUTLN(CERR, "sqrf_norm_sub generated new myrandom item: ", myrandom.item());
176      if ( getCharacteristic() == 0 )
177        t= CanonicalForm(mapinto(myrandom.item()));
178      else
179        t= CanonicalForm(myrandom.item());
180      s= t;
181      DEBOUTLN(CERR, "sqrf_norm_sub: testing s= ", s);
182      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
183      DEBOUTLN(CERR, "             gives g= ", g);
184    }
185  }
186}
187static void
188sqrf_agnorm_sub( const CanonicalForm & f, const CanonicalForm & PPalpha,
189           AlgExtGenerator & myrandom, CanonicalForm & s,  CanonicalForm & g,
190           CanonicalForm & R)
191{
192  Variable y=PPalpha.mvar(),vf=f.mvar();
193  CanonicalForm temp, Palpha=PPalpha, t;
194  int sqfreetest=0;
195  CFFList testlist;
196  CFFListIterator i;
197
198  DEBOUTLN(CERR, "sqrf_norm_sub:      f= ", f);
199  DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
200  myrandom.reset();   s=myrandom.item();   g=f;
201  R= CanonicalForm(0);
202  DEBOUTLN(CERR, "sqrf_norm_sub: myrandom s= ", s);
203
204  // Norm, resultante taken with respect to y
205  while ( !sqfreetest )
206  {
207    DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
208    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
209    DEBOUTLN(CERR, "sqrf_norm_sub: R= ", R);
210    // sqfree check ; R is a polynomial in K[x]
211    if ( getCharacteristic() == 0 )
212    {
213      temp= gcd(R, R.deriv(vf));
214      DEBOUTLN(CERR, "sqrf_norm_sub: temp= ", temp);
215      if (degree(temp,vf) != 0 || temp == temp.genZero() )
216        sqfreetest= 0;
217      else
218        sqfreetest= 1;
219      DEBOUTLN(CERR, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
220    }
221    else
222    {
223      DEBOUTMSG(CERR, "Starting SqrFreeTest(R)!");
224      // Look at SqrFreeTest!
225      // (z+a^5+w)^4 with z<w<a should not give sqfreetest=1 !
226      // for now we use this workaround with Factorize...
227      // ...but it should go away soon!!!!
228      Variable X;
229      if (getAlgVar(R,X))
230        testlist= factorize( R, X );
231      else
232        testlist= factorize(R);
233      DEBOUTLN(CERR, "testlist= ", testlist);
234      testlist.removeFirst();
235      sqfreetest=1;
236      for ( i=testlist; i.hasItem(); i++)
237      {
238        if ( i.getItem().exp() > 1 && degree(i.getItem().factor(), R.mvar()) > 0)
239        {
240          sqfreetest=0;
241          break;
242        }
243      }
244      DEBOUTLN(CERR, "SqrFreeTest(R)= ", sqfreetest);
245    }
246    if ( ! sqfreetest )
247    {
248      myrandom.next();
249      DEBOUTLN(CERR, "sqrf_norm_sub generated new myrandom item: ", myrandom.item());
250      if ( getCharacteristic() == 0 )
251        t= CanonicalForm(mapinto(myrandom.item()));
252      else
253        t= CanonicalForm(myrandom.item());
254      s= t;
255      DEBOUTLN(CERR, "sqrf_norm_sub: testing s= ", s);
256      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
257      DEBOUTLN(CERR, "             gives g= ", g);
258    }
259  }
260}
261
262static void
263sqrf_norm( const CanonicalForm & f, const CanonicalForm & PPalpha,
264           const Variable & Extension, CanonicalForm & s,  CanonicalForm & g,
265           CanonicalForm & R)
266{
267  DEBOUTLN(CERR, "sqrf_norm:      f= ", f);
268  DEBOUTLN(CERR, "sqrf_norm: Palpha= ", PPalpha);
269  if ( getCharacteristic() == 0 )
270  {
271    IntGenerator myrandom;
272    DEBOUTMSG(CERR, "sqrf_norm: no extension, char=0");
273    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
274    DEBOUTLN(CERR, "sqrf_norm:      f= ", f);
275    DEBOUTLN(CERR, "sqrf_norm: Palpha= ", PPalpha);
276    DEBOUTLN(CERR, "sqrf_norm:      s= ", s);
277    DEBOUTLN(CERR, "sqrf_norm:      g= ", g);
278    DEBOUTLN(CERR, "sqrf_norm:      R= ", R);
279  }
280  else if ( degree(Extension) > 0 ) // working over Extensions
281  {
282    DEBOUTLN(CERR, "sqrf_norm: degree of extension is ", degree(Extension));
283    AlgExtGenerator myrandom(Extension);
284    sqrf_agnorm_sub(f,PPalpha, myrandom, s,g,R);
285  }
286  else
287  {
288    FFGenerator myrandom;
289    DEBOUTMSG(CERR, "sqrf_norm: degree of extension is 0");
290    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
291  }
292}
293
294static Varlist
295Var_is_in_AS(const Varlist & uord, const CFList & Astar){
296  Varlist output;
297  CanonicalForm elem;
298  Variable x;
299
300  for ( VarlistIterator i=uord; i.hasItem(); i++)
301  {
302    x=i.getItem();
303    for ( CFListIterator j=Astar; j.hasItem(); j++ )
304    {
305      elem= j.getItem();
306      if ( degree(elem,x) > 0 ) // x actually occures in Astar
307      {
308        output.append(x);
309        break;
310      }
311    }
312  }
313  return output;
314}
315
316// Look if Minimalpolynomials in Astar define seperable Extensions
317// Must be a power of p: i.e. y^{p^e}-x
318static int
319inseperable(const CFList & Astar)
320{
321  CanonicalForm elem;
322  int Counter= 1;
323
324  if ( Astar.length() == 0 ) return 0;
325  for ( CFListIterator i=Astar; i.hasItem(); i++)
326  {
327    elem= i.getItem();
328    if ( elem.deriv() == elem.genZero() ) return Counter;
329    else Counter += 1;
330  }
331  return 0;
332}
333
334// calculate gcd of f and g in char=0
335static CanonicalForm
336gcd0( CanonicalForm f, CanonicalForm g )
337{
338  int charac= getCharacteristic();
339  int save=isOn(SW_RATIONAL);
340  setCharacteristic(0); Off(SW_RATIONAL);
341  CanonicalForm ff= mapinto(f), gg= mapinto(g);
342  CanonicalForm result= gcd(ff,gg);
343  setCharacteristic(charac);
344  if (save) On(SW_RATIONAL);
345  return mapinto(result);
346}
347
348// calculate big enough extension for finite fields
349// Idea: first calculate k, such that q^k > S (->thesis, -> getextension)
350// Second, search k with gcd(k,m_i)=1, where m_i is the degree of the i'th
351// minimal polynomial. Then the minpoly f_i remains irrd. over q^k and we
352// have enough elements to plug in.
353static int
354getextension( IntList & degreelist, int n)
355{
356  int charac= getCharacteristic();
357  setCharacteristic(0); // need it for k !
358  int k=1, m=1, length=degreelist.length();
359  IntListIterator i;
360
361  for (i=degreelist; i.hasItem(); i++) m= m*i.getItem();
362  int q=charac;
363  while (q <= ((n*m)*(n*m)/2)) { k= k+1; q= q*charac;}
364  int l=0;
365  do {
366    for (i=degreelist; i.hasItem(); i++){
367      l= l+1;
368      if ( gcd0(k,i.getItem()) == 1){
369        DEBOUTLN(CERR, "getextension: gcd == 1, l=",l);
370        if ( l==length ){ setCharacteristic(charac);  return k; }
371      }
372      else { DEBOUTMSG(CERR, "getextension: Next iteration"); break; }
373    }
374    k= k+1; l=0;
375  }
376  while ( 1 );
377}
378
379
380/// pseudo division of f and g wrt. x s.t. multiplier*f=q*g+r
381/// but only if the leading coefficient of g is of level lower than coeffLevel
382void
383psqr (const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q,
384      CanonicalForm & r, CanonicalForm& multiplier, const Variable& x,
385      int coeffLevel)
386{
387  ASSERT( x.level() > 0, "type error: polynomial variable expected" );
388  ASSERT( ! g.isZero(), "math error: division by zero" );
389
390  // swap variables such that x's level is larger or equal
391  // than both f's and g's levels.
392  Variable X;
393  if (f.level() > g.level())
394    X= f.mvar();
395  else
396    X= g.mvar();
397  if (X.level() < x.level())
398    X= x;
399  CanonicalForm F= swapvar (f, x, X);
400  CanonicalForm G= swapvar (g, x, X);
401
402  // now, we have to calculate the pseudo remainder of F and G
403  // w.r.t. X
404  int fDegree= degree (F, X);
405  int gDegree= degree (G, X);
406  if (fDegree < 0 || fDegree < gDegree)
407  {
408    q= 0;
409    r= f;
410  }
411  else
412  {
413    CanonicalForm LCG= LC (G, X);
414    if (LCG.level() < coeffLevel)
415    {
416      multiplier= power (LCG, fDegree - gDegree + 1);
417      divrem (multiplier*F, G, q, r);
418      q= swapvar (q, x, X);
419      r= swapvar (r, x, X);
420    }
421    else
422    {
423      q= 0;
424      r= f;
425    }
426  }
427}
428
429/// pseudo division of f and g wrt. x s.t. multiplier*f=q*g+r
430void
431psqr (const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, 
432      CanonicalForm & r, CanonicalForm& multiplier, const Variable& x)
433{
434    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
435    ASSERT( ! g.isZero(), "math error: division by zero" );
436
437    // swap variables such that x's level is larger or equal
438    // than both f's and g's levels.
439    Variable X;
440    if (f.level() > g.level())
441      X= f.mvar();
442    else
443      X= g.mvar();
444    if (X.level() < x.level())
445      X= x;
446    CanonicalForm F= swapvar (f, x, X);
447    CanonicalForm G= swapvar (g, x, X);
448
449    // now, we have to calculate the pseudo remainder of F and G
450    // w.r.t. X
451    int fDegree= degree (F, X);
452    int gDegree= degree (G, X);
453    if (fDegree < 0 || fDegree < gDegree)
454    {
455      q= 0;
456      r= f;
457    }
458    else
459    {
460      CanonicalForm LCG= LC (G, X);
461      multiplier= power (LCG, fDegree - gDegree + 1);
462      divrem (multiplier*F, G, q, r);
463      q= swapvar (q, x, X);
464      r= swapvar (r, x, X);
465    }
466}
467
468CanonicalForm
469QuasiInverse (const CanonicalForm& f, const CanonicalForm& g,
470      CanonicalForm& numt, const Variable& x)
471{
472  CanonicalForm pi, pi1, q, t0, t1, Hi, bi, pi2;
473  bool isRat= isOn (SW_RATIONAL);
474  CanonicalForm m,tmp;
475  if (isRat)
476    Off (SW_RATIONAL);
477  pi= f/content (f,x);
478  pi1= g/content (g,x);
479
480  t0= 0;
481  t1= 1;
482  bi= 1;
483
484  On (SW_RATIONAL);
485
486  int delta= degree (f, x) - degree (g, x);
487  Hi= power (LC (pi1, x), delta);
488  if ( (delta+1) % 2 )
489      bi = 1;
490  else
491      bi = -1;
492
493  while (degree (pi1,x) > 0)
494  {
495    On (SW_RATIONAL);
496    psqr( pi, pi1, q, pi2, m, x);
497    pi2 /= bi;
498
499    tmp= t1;
500    t1= t0*m - t1*q;
501    t0= tmp;
502    t1 /= bi;
503    pi = pi1; pi1 = pi2;
504    if ( degree ( pi1, x ) > 0 )
505    {
506      delta = degree( pi, x ) - degree( pi1, x );
507      if ( (delta+1) % 2 )
508        bi = LC( pi, x ) * power( Hi, delta );
509      else
510        bi = -LC( pi, x ) * power( Hi, delta );
511      Hi = power( LC( pi1, x ), delta ) / power( Hi, delta-1 );
512    }
513  }
514  t1 /= gcd (pi1, t1);
515  if (!isRat)
516    Off (SW_RATIONAL);
517  numt= t1;
518  return pi;
519}
520
521CanonicalForm
522evaluate (const CanonicalForm& f, const CanonicalForm& g, const CanonicalForm& h, const CanonicalForm& powH)
523{
524  if (f.inCoeffDomain())
525    return f;
526  CFIterator i= f;
527  int lastExp = i.exp();
528  CanonicalForm result = i.coeff()*powH;
529  i++;
530  while (i.hasTerms())
531  {
532    int i_exp= i.exp();
533    if ((lastExp - i_exp) == 1)
534    {
535      result *= g;
536      result /= h;
537    }
538    else
539    {
540      result *= power (g, lastExp - i_exp);
541      result /= power (h, lastExp - i_exp);
542    }
543    result += i.coeff()*powH;
544    lastExp = i_exp;
545    i++;
546  }
547  if (lastExp != 0)
548  {
549    result *= power (g, lastExp);
550    result /= power (h, lastExp);
551  }
552  return result;
553}
554
555
556/// evaluate f at g/h at v such that powH*f is integral i.e. powH is assumed to be h^degree(f,v)
557CanonicalForm
558evaluate (const CanonicalForm& f, const CanonicalForm& g,
559          const CanonicalForm& h, const CanonicalForm& powH,
560          const Variable& v)
561{
562  if (f.inCoeffDomain())
563  {
564    return f*powH;
565  }
566
567  Variable x = f.mvar();
568  if ( v > x )
569    return f*powH;
570  else  if ( v == x )
571    return evaluate (f, g, h, powH);
572
573  // v is less than main variable of f
574  CanonicalForm result= 0;
575  for (CFIterator i= f; i.hasTerms(); i++)
576    result += evaluate (i.coeff(), g, h, powH, v)*power (x, i.exp());
577  return result;
578}
579
580// calculate a "primitive element"
581// K must have more than S elements (->thesis, -> getextension)
582static CFList
583simpleextension(const CFList & Astar, 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      Off (SW_RATIONAL);
617      R /= icontent (R);
618
619      On (SW_RATIONAL);
620
621      if (!isFunctionField)
622      {
623        alpha= rootOf (R);
624        h= replacevar (g, g.mvar(), alpha);
625        On (SW_RATIONAL); //needed for GCD
626        h= gcd (h, oldR);
627        h /= Lc (h);
628        ra= -h[0];
629        ra= replacevar(ra, alpha, g.mvar());
630        rb= R.mvar()-s*ra;
631        for (; j.hasItem(); j++)
632        {
633          j.getItem()= j.getItem() (ra, oldR.mvar());
634          j.getItem()= j.getItem() (rb, i.getItem().mvar());
635        }
636      }
637      else
638      {
639        On (SW_RATIONAL);
640        h= swapvar (g, g.mvar(), oldR.mvar());
641        tmp= CFList (swapvar (R, g.mvar(), oldR.mvar()));
642        h= alg_gcd (h, swapvar (oldR, g.mvar(), oldR.mvar()), tmp);
643        CanonicalForm hh= replacevar (h, oldR.mvar(), alpha);
644
645        CanonicalForm numt, dent;
646        QuasiInverse (tmp.getFirst(), LC (h), numt, tmp.getFirst().mvar());
647
648        Off (SW_RATIONAL);
649        h *= numt;
650        h= reduce (h, tmp.getFirst());
651        dent= LC(h);
652
653        ra= -h[0];
654        denra= gcd (ra, dent);
655        ra /= denra;
656        denra= dent/denra;
657        denra= replacevar (denra, ra.mvar(), g.mvar());
658        ra= replacevar(ra, ra.mvar(), g.mvar());
659        rb= R.mvar()*denra-s*ra;
660        denrb= denra;
661        for (; j.hasItem(); j++)
662        {
663          CanonicalForm powdenra= power (denra, degree (j.getItem(), oldR.mvar()));
664          j.getItem()= evaluate (j.getItem(),ra, denra, powdenra, oldR.mvar());
665          powdenra= power (denra, degree (j.getItem(), i.getItem().mvar()));
666          j.getItem()= evaluate (j.getItem(), rb, denrb, powdenra, i.getItem().mvar());
667        }
668      }
669
670      DEBOUTLN(CERR, "simpleextension: g= ", g);
671      DEBOUTLN(CERR, "simpleextension: s= ", s);
672      DEBOUTLN(CERR, "simpleextension: R= ", R);
673      Returnlist.append (ra);
674      if (isFunctionField)
675        Returnlist.append (denra);
676    }
677  }
678  Returnlist.append (rb);
679  if (isFunctionField)
680    Returnlist.append (denrb);
681
682  if (isRat)
683    On (SW_RATIONAL);
684  else
685    Off (SW_RATIONAL);
686
687  return Returnlist;
688}
689
690CanonicalForm alg_lc(const CanonicalForm &f)
691{
692  if (f.level()>0)
693  {
694    return alg_lc(f.LC());
695  }
696  //assert(f.inCoeffDomain());
697  return f;
698}
699
700// the heart of the algorithm: the one from Trager
701#ifndef DEBUGOUTPUT
702static CFFList
703alg_factor( const CanonicalForm & f, const CFList & Astar, const Variable & vminpoly, const Varlist /*& oldord*/, const CFList & as)
704#else
705static CFFList
706alg_factor( const CanonicalForm & f, const CFList & Astar, const Variable & vminpoly, const Varlist & oldord, const CFList & as)
707#endif
708{
709  CFFList L, Factorlist;
710  CanonicalForm R, Rstar, s, g, h;
711  CFList substlist;
712
713  DEBINCLEVEL(CERR,"alg_factor");
714  DEBOUTLN(CERR, "alg_factor: f= ", f);
715
716  //out_cf("start alg_factor:",f,"\n");
717  substlist= simpleextension(Astar, vminpoly, Rstar);
718  DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
719  DEBOUTLN(CERR, "alg_factor: minpoly Rstar= ", Rstar);
720  DEBOUTLN(CERR, "alg_factor: vminpoly= ", vminpoly);
721
722  sqrf_norm(f, Rstar, vminpoly, s, g, R );
723  //out_cf("sqrf_norm R:",R,"\n");
724  //out_cf("sqrf_norm s:",s,"\n");
725  //out_cf("sqrf_norm g:",g,"\n");
726  DEBOUTLN(CERR, "alg_factor: g= ", g);
727  DEBOUTLN(CERR, "alg_factor: s= ", s);
728  DEBOUTLN(CERR, "alg_factor: R= ", R);
729  Off(SW_RATIONAL);
730  Variable X;
731  if (getAlgVar(R,X))
732  {
733    // factorize R over alg.extension with X
734//CERR << "alg: "<< X << " mipo=" << getMipo(X,Variable('X')) <<"\n";
735    if (R.isUnivariate())
736    {
737      DEBOUTLN(CERR, "alg_factor: factorize( ", R);
738      Factorlist =  factorize( R, X );
739    }
740    else
741    {
742      #if 1
743      Variable XX;
744      CanonicalForm mipo=getMipo(X,XX);
745      CFList as(mipo);
746      DEBOUTLN(CERR, "alg_factor: newfactoras( ", R);
747      int success=1;
748      Factorlist = newfactoras(R, as , success);
749      #else
750      // factor R over k
751      DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
752      Factorlist = Factorize(R);
753      #endif
754    }
755  }
756  else
757  {
758    // factor R over k
759    DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
760    Factorlist = Factorize(R);
761  }
762  On(SW_RATIONAL);
763  DEBOUTLN(CERR, "alg_factor: Factorize(R)= ", Factorlist);
764  if ( !Factorlist.getFirst().factor().inCoeffDomain() )
765    Factorlist.insert(CFFactor(1,1));
766  if ( Factorlist.length() == 2 && Factorlist.getLast().exp()== 1)
767  { // irreduzibel (first entry is a constant)
768    L.append(CFFactor(f,1));
769  }
770  else
771  {
772    DEBOUTLN(CERR, "alg_factor: g= ", g);
773    CanonicalForm gnew= g(s,s.mvar());
774    DEBOUTLN(CERR, "alg_factor: gnew= ", gnew);
775    g=gnew;
776    for ( CFFListIterator i=Factorlist; i.hasItem(); i++)
777    {
778      CanonicalForm fnew=i.getItem().factor();
779      fnew= fnew(s,s.mvar());
780      DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
781      DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
782      for ( CFListIterator ii=substlist; ii.hasItem(); ii++){
783        DEBOUTLN(CERR, "alg_factor: item= ", ii.getItem());
784        fnew= fnew(ii.getItem(), ii.getItem().mvar());
785        DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
786      }
787      if (degree(i.getItem().factor()) > 0 )
788      {
789          h=alg_gcd(g,fnew,as);
790        DEBOUTLN(CERR, "  alg_factor: h= ", h);
791        DEBOUTLN(CERR, "  alg_factor: oldord= ", oldord);
792        if ( degree(h) > 0 )
793        { //otherwise it's a constant
794          g= divide(g, h,as);
795          DEBOUTLN(CERR, "alg_factor: g/h= ", g);
796          DEBOUTLN(CERR, "alg_factor: s= ", s);
797          DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
798          //out_cf("new g:",g,"\n");
799          L.append(CFFactor(h,1));
800        }
801        //else printf("degree <=1\n");
802      }
803    }
804    // we are not interested in a
805    // constant (over K_r, which can be a polynomial!)
806    if (degree(g, f.mvar())>0){ L.append(CFFactor(g,1)); }
807  }
808  CFFList LL;
809  if (getCharacteristic()>0)
810  {
811    CFFListIterator i=L;
812    CanonicalForm c_fac=1;
813    CanonicalForm c;
814    for(;i.hasItem(); i++ )
815    {
816      CanonicalForm ff=i.getItem().factor();
817      c=alg_lc(ff);
818      int e=i.getItem().exp();
819      ff/=c;
820      if (!ff.isOne()) LL.append(CFFactor(ff,e));
821      while (e>0) { c_fac*=c;e--; }
822    }
823    if (!c_fac.isOne()) LL.insert(CFFactor(c_fac,1));
824  }
825  else
826  {
827    LL=L;
828  }
829  //CFFListIterator i=LL;
830  //for(;i.hasItem(); i++ )
831  //  out_cf("end alg_f:",i.getItem().factor(),"\n");
832  //printf("end alg_factor\n");
833  DEBOUTLN(CERR, "alg_factor: L= ", LL);
834  DEBDECLEVEL(CERR,"alg_factor");
835  return LL;
836}
837
838static CFFList
839endler( const CanonicalForm & f, const CFList & AS, const Varlist & uord )
840{
841  CanonicalForm F=f, g, q,r;
842  CFFList Output;
843  CFList One, Two, asnew, as=AS;
844  CFListIterator i,ii;
845  VarlistIterator j;
846  Variable vg;
847
848  for (i=as; i.hasItem(); i++)
849  {
850    g= i.getItem();
851    if (g.deriv() == 0 )
852    {
853      DEBOUTLN(CERR, "Inseperable extension detected: ", g);
854      for (j=uord; j.hasItem(); j++)
855      {
856        if ( degree(g,j.getItem()) > 0 ) vg= j.getItem();
857      }
858      // Now we have the highest transzendental in vg;
859      DEBOUTLN(CERR, "Transzendental is ", vg);
860      CanonicalForm gg=-1*g[0];
861      divrem(gg,vg,q,r); r= gg-q*vg;   gg= gg-r;
862      //DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
863      DEBOUTLN(CERR, "  that is ", gg);
864      DEBOUTLN(CERR, "  maps to ", g+gg);
865      One.insert(gg); Two.insert(g+gg);
866      // Now transform all remaining polys in as:
867      int x=0;
868      for (ii=i; ii.hasItem(); ii++)
869      {
870        if ( x != 0 )
871        {
872          divrem(ii.getItem(), gg, q,r);
873//          CERR << ii.getItem() << " divided by " << gg << "\n";
874          DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
875          ii.append(ii.getItem()+q*g); ii.remove(1);
876          DEBOUTLN(CERR, "as= ", as);
877        }
878        x+= 1;
879      }
880      // Now transform F:
881      divrem(F, gg, q,r);
882      F= F+q*g;
883      DEBOUTLN(CERR, "new F= ", F);
884    }
885    else{ asnew.append(i.getItem());  }// just the identity
886  }
887  // factor F with minimal polys given in asnew:
888  DEBOUTLN(CERR, "Factor F=  ", F);
889  DEBOUTLN(CERR, "  with as= ", asnew);
890  int success=0;
891  CFFList factorlist= newcfactor(F,asnew, success);
892  DEBOUTLN(CERR, "  gives =  ", factorlist);
893  DEBOUTLN(CERR, "One= ", One);
894  DEBOUTLN(CERR, "Two= ", Two);
895
896  // Transform back:
897  for ( CFFListIterator k=factorlist; k.hasItem(); k++)
898  {
899    CanonicalForm factor= k.getItem().factor();
900    ii=One;
901    for (i=Two; i.hasItem(); i++)
902    {
903      DEBOUTLN(CERR, "Mapping ", i.getItem());
904      DEBOUTLN(CERR, "     to ", ii.getItem());
905      DEBOUTLN(CERR, "     in ", factor);
906      divrem(factor,i.getItem(),q,r); r=factor -q*i.getItem();
907      DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
908      factor= ii.getItem()*q +r; //
909      ii++;
910    }
911    Output.append(CFFactor(factor,k.getItem().exp()));
912  }
913
914  return Output;
915}
916
917void
918multiplicity (CFFList& factors, const CanonicalForm& F, const CFList& as)
919{
920  CanonicalForm G= F;
921  Variable x= F.mvar();
922  CanonicalForm q, r;
923  int count= -1;
924  On (SW_RATIONAL);
925  for (CFFListIterator iter=factors; iter.hasItem(); iter++)
926  {
927    count= -1;
928    if (iter.getItem().factor().inCoeffDomain())
929      continue;
930    while (1)
931    {
932      psqr (G, iter.getItem().factor(), q, r, x);
933
934      q= Prem (q, as);
935      r= Prem (r, as);
936      if (!r.isZero())
937        break;
938      On (SW_RATIONAL);
939      count++;
940      G= q;
941    }
942    iter.getItem()= CFFactor (iter.getItem().factor(), iter.getItem().exp()+count);
943  }
944}
945
946
947// 1) prepares data
948// 2) for char=p we distinguish 3 cases:
949//           no transcendentals, seperable and inseperable extensions
950CFFList
951newfactoras( const CanonicalForm & f, const CFList & as, int &success)
952{
953  Variable vf=f.mvar();
954  CFListIterator i;
955  CFFListIterator jj;
956  CFList reduceresult;
957  CFFList result;
958
959  success=1;
960  DEBINCLEVEL(CERR, "newfactoras");
961  DEBOUTLN(CERR, "newfactoras called with f= ", f);
962  DEBOUTLN(CERR, "               content(f)= ", content(f));
963  DEBOUTLN(CERR, "                       as= ", as);
964  DEBOUTLN(CERR, "newfactoras: cls(vf)= ", cls(vf));
965  DEBOUTLN(CERR, "newfactoras: cls(as.getLast())= ", cls(as.getLast()));
966  DEBOUTLN(CERR, "newfactoras: degree(f,vf)= ", degree(f,vf));
967
968// F1: [Test trivial cases]
969// 1) first trivial cases:
970  if ( (cls(vf) <= cls(as.getLast())) ||  degree(f,vf)<=1 ){
971// ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) )
972    DEBDECLEVEL(CERR,"newfactoras");
973    return CFFList(CFFactor(f,1));
974  }
975
976// 2) List of variables:
977// 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1
978// 2b) Setup variableordering
979  CFList Astar;
980  Variable x;
981  CanonicalForm elem;
982  Varlist ord, uord,oldord;
983  for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii));  }
984  oldord= uord; oldord.append(vf);
985
986  for ( i=as; i.hasItem(); i++ ){
987    elem= i.getItem();
988    x= elem.mvar();
989    if ( degree(elem,x) > 1){ // otherwise it's not an extension
990      Astar.append(elem);
991      ord.append(x);
992    }
993  }
994  uord= Difference(uord,ord);
995  DEBOUTLN(CERR, "Astar is: ", Astar);
996  DEBOUTLN(CERR, "ord is: ", ord);
997  DEBOUTLN(CERR, "uord is: ", uord);
998
999// 3) second trivial cases: we already prooved irr. of f over no extensions
1000  if ( Astar.length() == 0 ){
1001    DEBDECLEVEL(CERR,"newfactoras");
1002    return CFFList(CFFactor(f,1));
1003  }
1004
1005// 4) Try to obtain a partial factorization using prop2 and prop3
1006//    Use with caution! We have to proof these propositions first!
1007  // Not yet implemented
1008
1009// 5) Look if elements in uord actually occure in any of the minimal
1010//    polynomials. If no element of uord occures in any of the minimal
1011//   polynomials, we don't have transzendentals.
1012  Varlist newuord=Var_is_in_AS(uord,Astar);
1013  DEBOUTLN(CERR, "newuord is: ", newuord);
1014
1015  CFFList Factorlist;
1016  Varlist gcdord= Union(ord,newuord); gcdord.append(f.mvar());
1017  // This is for now. we need alg_sqrfree implemented!
1018  CanonicalForm Fgcd;
1019          Fgcd= alg_gcd(f,f.deriv(),Astar);
1020  if ( Fgcd == 0 ) {DEBOUTMSG(CERR, "WARNING: p'th root ?");}
1021  if (( degree(Fgcd, f.mvar()) > 0) && (!(f.deriv().isZero())) ){
1022    DEBOUTLN(CERR, "Nontrivial GCD found of ", f);
1023    CanonicalForm Ggcd= divide(f, Fgcd,Astar);
1024    DEBOUTLN(CERR, "  split into ", Fgcd);
1025    DEBOUTLN(CERR, "         and ", Ggcd);
1026    Fgcd= pp(Fgcd); Ggcd= pp(Ggcd);
1027    DEBDECLEVEL(CERR,"newfactoras");
1028    return myUnion(newfactoras(Fgcd,as,success) , newfactoras(Ggcd,as,success));
1029  }
1030  if ( getCharacteristic() > 0 ){
1031
1032    // First look for extension!
1033    IntList degreelist;
1034    Variable vminpoly;
1035    for (i=Astar; i.hasItem(); i++){degreelist.append(degree(i.getItem()));}
1036    int extdeg= getextension(degreelist, degree(f));
1037    DEBOUTLN(CERR, "Extension needed of degree ", extdeg);
1038
1039    // Now the real stuff!
1040    if ( newuord.length() == 0 ){ // no transzendentals
1041      DEBOUTMSG(CERR, "No transzendentals!");
1042      if ( extdeg > 1 ){
1043        CanonicalForm MIPO= generate_mipo( extdeg, vminpoly);
1044        DEBOUTLN(CERR, "Minpoly produced ", MIPO);
1045        vminpoly= rootOf(MIPO);
1046      }
1047      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
1048      DEBDECLEVEL(CERR,"newfactoras");
1049      return Factorlist;
1050    }
1051    else if ( inseperable(Astar) > 0 ){ // Look if extensions are seperable
1052      // a) Use Endler
1053      DEBOUTMSG(CERR, "Inseperable extensions! Using Endler!");
1054      CFFList templist= endler(f,Astar, newuord);
1055      DEBOUTLN(CERR, "Endler gives: ", templist);
1056      return templist;
1057    }
1058    else{ // we are on the save side: Use trager
1059      DEBOUTMSG(CERR, "Only seperable extensions!");
1060      if (extdeg > 1 ){
1061        CanonicalForm MIPO=generate_mipo(extdeg, vminpoly );
1062        vminpoly= rootOf(MIPO);
1063        DEBOUTLN(CERR, "Minpoly generated: ", MIPO);
1064        DEBOUTLN(CERR, "vminpoly= ", vminpoly);
1065        DEBOUTLN(CERR, "degree(vminpoly)= ", degree(vminpoly));
1066      }
1067      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
1068      DEBDECLEVEL(CERR,"newfactoras");
1069      return Factorlist;
1070    }
1071  }
1072  else{ // char=0 apply trager directly
1073    DEBOUTMSG(CERR, "Char=0! Apply Trager!");
1074    Variable vminpoly;
1075    Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
1076      DEBDECLEVEL(CERR,"newfactoras");
1077      return Factorlist;
1078  }
1079
1080  DEBDECLEVEL(CERR,"newfactoras");
1081  return CFFList(CFFactor(f,1));
1082}
1083
1084CFFList
1085newcfactor(const CanonicalForm & f, const CFList & as, int & success )
1086{
1087  Off(SW_RATIONAL);
1088  CFFList Output, output, Factors=Factorize(f);
1089  On(SW_RATIONAL);
1090  Factors.removeFirst();
1091
1092  if ( as.length() == 0 ){ success=1; return Factors;}
1093  if ( cls(f) <= cls(as.getLast()) ) { success=1; return Factors;}
1094
1095  success=1;
1096  for ( CFFListIterator i=Factors; i.hasItem(); i++ )
1097  {
1098    output=newfactoras(i.getItem().factor(),as, success);
1099    for ( CFFListIterator j=output; j.hasItem(); j++)
1100      Output = myappend(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp()));
1101  }
1102  return Output;
1103}
Note: See TracBrowser for help on using the repository browser.