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

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