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

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