source: git/libfac/charset/alg_factor.cc @ 625105

jengelh-datetimespielwiese
Last change on this file since 625105 was 625105, checked in by Hans Schönemann <hannes@…>, 18 years ago
*hannes: fixed call of resultant git-svn-id: file:///usr/local/Singular/svn/trunk@8426 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 27.3 KB
Line 
1/* Copyright 1997 Michael Messollen. All rights reserved. */
2////////////////////////////////////////////////////////////
3// emacs edit mode for this file is -*- C++ -*-
4////////////////////////////////////////////////////////////
5static char * rcsid = "$Id: alg_factor.cc,v 1.14 2005-07-08 09:18:15 Singular Exp $";
6////////////////////////////////////////////////////////////
7// FACTORY - Includes
8#include <factory.h>
9// Factor - Includes
10#include <tmpl_inst.h>
11#include <Factor.h>
12#include <SqrFree.h>
13#include <helpstuff.h>
14// Charset - Includes
15#include "csutil.h"
16#include "charset.h"
17#include "reorder.h"
18#include "algfactor.h"
19// some CC's need this:
20#include "alg_factor.h"
21
22void out_cf(char *s1,const CanonicalForm &f,char *s2);
23
24#ifdef ALGFACTORDEBUG
25#  define DEBUGOUTPUT
26#else
27#  undef DEBUGOUTPUT
28#endif
29
30#include "debug.h"
31#include "timing.h"
32TIMING_DEFINE_PRINT(newfactoras_time);
33
34static Varlist
35Var_is_in_AS(const Varlist & uord, const CFList & Astar);
36
37int getAlgVar(const CanonicalForm &f, Variable &X)
38{
39  if (f.inBaseDomain()) return 0;
40  if (f.inCoeffDomain())
41  {
42    if (f.level()!=0)
43    {
44      X= f.mvar();
45      return 1;
46    }
47    return getAlgVar(f.LC(),X);
48  }
49  if (f.inPolyDomain())
50  {
51    if (getAlgVar(f.LC(),X)) return 1;
52    for( CFIterator i=f; i.hasTerms(); i++)
53    {
54      if (getAlgVar(i.coeff(),X)) return 1;
55    }
56  }
57  return 0;
58}
59
60////////////////////////////////////////////////////////////////////////
61// This implements the algorithm of Trager for factorization of
62// (multivariate) polynomials over algebraic extensions and so called
63// function field extensions.
64////////////////////////////////////////////////////////////////////////
65
66// // missing class: IntGenerator:
67bool IntGenerator::hasItems() const
68{
69    return 1;
70}
71
72CanonicalForm IntGenerator::item() const
73//int IntGenerator::item() const
74{
75  //return current; //CanonicalForm( current );
76  return mapinto(CanonicalForm( current ));
77}
78
79void IntGenerator::next()
80{
81    current++;
82}
83
84// replacement for factory's broken psr
85static CanonicalForm
86mypsr ( const CanonicalForm &rr, const CanonicalForm &vv, const Variable & x ){
87  CanonicalForm r=rr, v=vv, l, test, lu, lv, t, retvalue;
88  int dr, dv, d,n=0;
89
90
91  dr = degree( r, x );
92  dv = degree( v, x );
93  if (dv <= dr) {l=LC(v,x); v = v -l*power(x,dv);}
94  else { l = 1; }
95  d= dr-dv+1;
96  while ( ( dv <= dr  ) && ( r != r.genZero()) ){
97    test = power(x,dr-dv)*v*LC(r,x);
98    if ( dr == 0 ) { r= CanonicalForm(0); }
99    else { r= r - LC(r,x)*power(x,dr); }
100    r= l*r -test;
101    dr= degree(r,x);
102    n+=1;
103  }
104  r= power(l, d-n)*r;
105  return r;
106}
107
108// replacement for factory's broken resultant
109static CanonicalForm
110resultante( const CanonicalForm & f, const CanonicalForm& g, const Variable & v ){
111  bool on_rational = isOn(SW_RATIONAL);
112  On(SW_RATIONAL);
113  CanonicalForm cd = bCommonDen( f );
114  CanonicalForm fz = f * cd;
115  cd = bCommonDen( g );
116  CanonicalForm gz = g * cd;
117  if (!on_rational)  Off(SW_RATIONAL);
118
119return resultant(fz,gz,v);
120
121  CanonicalForm h, beta, help, F, G;
122  int delta;
123
124  DEBOUTLN( cout, "resultante: called  f= ", f);
125  DEBOUTLN( cout, "resultante: called  g= ", g);
126  DEBOUTLN( cout, "resultante: called  v= ", v);
127  if ( f.mvar() < v || g.mvar() < v ){
128    DEBOUTMSG(cout, "resultante: f.mvar() < v || g.mvar() < v");
129    return 1;
130  }
131
132  if ( f.degree( v ) < 1 || g.degree( v ) < 1 ){
133    DEBOUTMSG(cout, "resultante: f.degree( v ) < 1 || g.degree( v ) < 1");
134    // If deg(F,v) == 0 , then resultante(F,G,v) = F^n, where n=deg(G,v)
135    if ( f.degree( v ) < 1 ) return power(f,degree(g,v));
136    else return power(g,degree(f,v));
137  }
138
139   if ( f.degree( v ) >= g.degree( v ) ) { F = f; G = g; }
140   else { G = f; F = g; }
141
142  h = CanonicalForm(1);
143  while ( G != G.genZero() ) {
144     delta= degree(F,v) -degree(G,v);
145     beta = power(CanonicalForm(-1), delta+1) * LC(F,v)* power(h, delta);
146     h= (h * power(LC(G,v), delta)) / power(h, delta);
147     help= G;
148     G= mypsr(F,G,v);
149     G= G/beta;
150     F=help;
151   }
152   if ( degree(F,v) != 0 ) F= CanonicalForm(0);
153   return F;
154}
155
156// sqr-free routine for algebraic extensions
157// we need it! Ex.: f=c^2+2*a*c-1; as=[a^2+1]; f=(c+a)^2
158static CFFList
159alg_sqrfree( const CanonicalForm & f ){
160  CFFList L;
161
162  L.append(CFFactor(f,1));
163  return L;
164}
165
166// Calculates a square free norm
167// Input: f(x, alpha) a square free polynomial over K(alpha),
168// alpha is defined by the minimal polynomial Palpha
169// K has more than S elements (S is defined in thesis; look getextension)
170static void
171sqrf_norm_sub( const CanonicalForm & f, const CanonicalForm & PPalpha,
172           CFGenerator & myrandom, CanonicalForm & s,  CanonicalForm & g,
173           CanonicalForm & R){
174  Variable y=PPalpha.mvar(),vf=f.mvar();
175  CanonicalForm temp, Palpha=PPalpha, t;
176  int sqfreetest=0;
177  CFFList testlist;
178  CFFListIterator i;
179
180  DEBOUTLN(cout, "sqrf_norm_sub:      f= ", f);
181  DEBOUTLN(cout, "sqrf_norm_sub: Palpha= ", Palpha);
182  myrandom.reset();   s=f.mvar()-myrandom.item()*Palpha.mvar();   g=f;
183  R= CanonicalForm(0);
184  DEBOUTLN(cout, "sqrf_norm_sub: myrandom s= ", s);
185
186  // Norm, resultante taken with respect to y
187  while ( !sqfreetest ){
188    DEBOUTLN(cout, "sqrf_norm_sub: Palpha= ", Palpha);
189    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
190    DEBOUTLN(cout, "sqrf_norm_sub: R= ", R);
191    // sqfree check ; R is a polynomial in K[x]
192    if ( getCharacteristic() == 0 )
193    {
194      temp= gcd(R, R.deriv(vf));
195      DEBOUTLN(cout, "sqrf_norm_sub: temp= ", temp);
196      if (degree(temp,vf) != 0 || temp == temp.genZero() ){ sqfreetest= 0; }
197      else { sqfreetest= 1; }
198      DEBOUTLN(cout, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
199    }
200    else{
201      DEBOUTMSG(cout, "Starting SqrFreeTest(R)!");
202      // Look at SqrFreeTest!
203      // (z+a^5+w)^4 with z<w<a should not give sqfreetest=1 !
204      // for now we use this workaround with Factorize...
205      // ...but it should go away soon!!!!
206      Variable X;
207      if (getAlgVar(R,X))
208      {
209        if (R.isUnivariate())
210          testlist=factorize( R, X );
211        else
212          testlist= Factorize(R, X, 0);
213      }
214      else
215        testlist= Factorize(R);
216      DEBOUTLN(cout, "testlist= ", testlist);
217      testlist.removeFirst();
218      sqfreetest=1;
219      for ( i=testlist; i.hasItem(); i++)
220        if ( i.getItem().exp() > 1 && degree(i.getItem().factor(), R.mvar()) > 0) { sqfreetest=0; break; }
221      DEBOUTLN(cout, "SqrFreeTest(R)= ", sqfreetest);
222    }
223    if ( ! sqfreetest ){
224      myrandom.next();
225      DEBOUTLN(cout, "sqrf_norm_sub generated new myrandom item: ", myrandom.item());
226      if ( getCharacteristic() == 0 ) t= CanonicalForm(mapinto(myrandom.item()));
227      else t= CanonicalForm(myrandom.item());
228      s= f.mvar()+t*Palpha.mvar(); // s defines backsubstitution
229      DEBOUTLN(cout, "sqrf_norm_sub: testing s= ", s);
230      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
231      DEBOUTLN(cout, "             gives g= ", g);
232    }
233  }
234}
235static void
236sqrf_agnorm_sub( const CanonicalForm & f, const CanonicalForm & PPalpha,
237           AlgExtGenerator & myrandom, CanonicalForm & s,  CanonicalForm & g,
238           CanonicalForm & R){
239  Variable y=PPalpha.mvar(),vf=f.mvar();
240  CanonicalForm temp, Palpha=PPalpha, t;
241  int sqfreetest=0;
242  CFFList testlist;
243  CFFListIterator i;
244
245  DEBOUTLN(cout, "sqrf_norm_sub:      f= ", f);
246  DEBOUTLN(cout, "sqrf_norm_sub: Palpha= ", Palpha);
247  myrandom.reset();   s=f.mvar()-myrandom.item()*Palpha.mvar();   g=f;
248  R= CanonicalForm(0);
249  DEBOUTLN(cout, "sqrf_norm_sub: myrandom s= ", s);
250
251  // Norm, resultante taken with respect to y
252  while ( !sqfreetest ){
253    DEBOUTLN(cout, "sqrf_norm_sub: Palpha= ", Palpha);
254    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
255    DEBOUTLN(cout, "sqrf_norm_sub: R= ", R);
256    // sqfree check ; R is a polynomial in K[x]
257    if ( getCharacteristic() == 0 )
258    {
259      temp= gcd(R, R.deriv(vf));
260      DEBOUTLN(cout, "sqrf_norm_sub: temp= ", temp);
261      if (degree(temp,vf) != 0 || temp == temp.genZero() ){ sqfreetest= 0; }
262      else { sqfreetest= 1; }
263      DEBOUTLN(cout, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
264    }
265    else{
266      DEBOUTMSG(cout, "Starting SqrFreeTest(R)!");
267      // Look at SqrFreeTest!
268      // (z+a^5+w)^4 with z<w<a should not give sqfreetest=1 !
269      // for now we use this workaround with Factorize...
270      // ...but it should go away soon!!!!
271      Variable X;
272      if (getAlgVar(R,X))
273      {
274        if (R.isUnivariate())
275          testlist=factorize( R, X );
276        else
277          testlist= Factorize(R, X, 0);
278      }
279      else
280        testlist= Factorize(R);
281      DEBOUTLN(cout, "testlist= ", testlist);
282      testlist.removeFirst();
283      sqfreetest=1;
284      for ( i=testlist; i.hasItem(); i++)
285        if ( i.getItem().exp() > 1 && degree(i.getItem().factor(), R.mvar()) > 0) { sqfreetest=0; break; }
286      DEBOUTLN(cout, "SqrFreeTest(R)= ", sqfreetest);
287    }
288    if ( ! sqfreetest ){
289      myrandom.next();
290      DEBOUTLN(cout, "sqrf_norm_sub generated new myrandom item: ", myrandom.item());
291      if ( getCharacteristic() == 0 ) t= CanonicalForm(mapinto(myrandom.item()));
292      else t= CanonicalForm(myrandom.item());
293      s= f.mvar()+t*Palpha.mvar(); // s defines backsubstitution
294      DEBOUTLN(cout, "sqrf_norm_sub: testing s= ", s);
295      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
296      DEBOUTLN(cout, "             gives g= ", g);
297    }
298  }
299}
300
301static void
302sqrf_norm( const CanonicalForm & f, const CanonicalForm & PPalpha,
303           const Variable & Extension, CanonicalForm & s,  CanonicalForm & g,
304           CanonicalForm & R){
305
306  DEBOUTLN(cout, "sqrf_norm:      f= ", f);
307  DEBOUTLN(cout, "sqrf_norm: Palpha= ", PPalpha);
308  if ( getCharacteristic() == 0 ) {
309    IntGenerator myrandom;
310    DEBOUTMSG(cout, "sqrf_norm: no extension, char=0");
311    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
312    DEBOUTLN(cout, "sqrf_norm:      f= ", f);
313    DEBOUTLN(cout, "sqrf_norm: Palpha= ", PPalpha);
314    DEBOUTLN(cout, "sqrf_norm:      s= ", s);
315    DEBOUTLN(cout, "sqrf_norm:      g= ", g);
316    DEBOUTLN(cout, "sqrf_norm:      R= ", R);
317  }
318  else if ( degree(Extension) > 0 ){ // working over Extensions
319    DEBOUTLN(cout, "sqrf_norm: degree of extension is ", degree(Extension));
320    AlgExtGenerator myrandom(Extension);
321    sqrf_agnorm_sub(f,PPalpha, myrandom, s,g,R);
322  }
323  else{
324    FFGenerator myrandom;
325    DEBOUTMSG(cout, "sqrf_norm: degree of extension is 0");
326    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
327  }
328}
329
330static Varlist
331Var_is_in_AS(const Varlist & uord, const CFList & Astar){
332  Varlist output;
333  CanonicalForm elem;
334  Variable x;
335
336  for ( VarlistIterator i=uord; i.hasItem(); i++){
337    x=i.getItem();
338    for ( CFListIterator j=Astar; j.hasItem(); j++ ){
339      elem= j.getItem();
340      if ( degree(elem,x) > 0 ){ // x actually occures in Astar
341        output.append(x);
342        break;
343      }
344    }
345  }
346  return output;
347}
348
349// Look if Minimalpolynomials in Astar define seperable Extensions
350// Must be a power of p: i.e. y^{p^e}-x
351static int
352inseperable(const CFList & Astar){
353  CanonicalForm elem;
354  int Counter= 1;
355
356  if ( Astar.length() == 0 ) return 0;
357  for ( CFListIterator i=Astar; i.hasItem(); i++){
358    elem= i.getItem();
359    if ( elem.deriv() == elem.genZero() ) return Counter;
360    else Counter += 1;
361  }
362  return 0;
363}
364
365// calculate gcd of f and g in char=0
366static CanonicalForm
367gcd0( CanonicalForm f, CanonicalForm g ){
368  int charac= getCharacteristic();
369  int save=isOn(SW_RATIONAL);
370  setCharacteristic(0); Off(SW_RATIONAL);
371  CanonicalForm ff= mapinto(f), gg= mapinto(g);
372  CanonicalForm result= gcd(ff,gg);
373  setCharacteristic(charac); 
374  if (save) On(SW_RATIONAL);
375  return mapinto(result);
376}
377
378// calculate big enough extension for finite fields
379// Idea: first calculate k, such that q^k > S (->thesis, -> getextension)
380// Second, search k with gcd(k,m_i)=1, where m_i is the degree of the i'th
381// minimal polynomial. Then the minpoly f_i remains irrd. over q^k and we
382// have enough elements to plug in.
383static int
384getextension( IntList & degreelist, int n){
385  int charac= getCharacteristic();
386  setCharacteristic(0); // need it for k !
387  int k=1, m=1, length=degreelist.length();
388  IntListIterator i;
389
390  for (i=degreelist; i.hasItem(); i++) m= m*i.getItem();
391  int q=charac;
392  while (q <= ((n*m)*(n*m)/2)) { k= k+1; q= q*charac;}
393  int l=0;
394  do {
395    for (i=degreelist; i.hasItem(); i++){
396      l= l+1;
397      if ( gcd0(k,i.getItem()) == 1){
398        DEBOUTLN(cout, "getextension: gcd == 1, l=",l);
399        if ( l==length ){ setCharacteristic(charac);  return k; }
400      }
401      else { DEBOUTMSG(cout, "getextension: Next iteration"); break; }
402    }
403    k= k+1; l=0;
404  }
405  while ( 1 );
406}
407
408// calculate a "primitive element"
409// K must have more than S elements (->thesis, -> getextension)
410static CFList
411simpleextension(const CFList & Astar, const Variable & Extension,
412                CanonicalForm & R){
413  CFList Returnlist, Bstar=Astar;
414  CanonicalForm s, g;
415
416  DEBOUTLN(cout, "simpleextension: Astar= ", Astar);
417  DEBOUTLN(cout, "simpleextension:     R= ", R);
418  DEBOUTLN(cout, "simpleextension: Extension= ", Extension);
419  if ( Astar.length() == 1 ){ R= Astar.getFirst();}
420  else{
421    R=Bstar.getFirst(); Bstar.removeFirst();
422    for ( CFListIterator i=Bstar; i.hasItem(); i++){
423      DEBOUTLN(cout, "simpleextension: f(x)= ", i.getItem());
424      DEBOUTLN(cout, "simpleextension: P(x)= ", R);
425      sqrf_norm(i.getItem(), R, Extension, s, g, R);
426      // spielt die Repraesentation eine Rolle?
427      // muessen wir die Nachfolger aendern, wenn s != 0 ?
428      DEBOUTLN(cout, "simpleextension: g= ", g);
429      if ( s != 0 ) DEBOUTLN(cout, "simpleextension: s= ", s);
430      else DEBOUTLN(cout, "simpleextension: s= ", s);
431      DEBOUTLN(cout, "simpleextension: R= ", R);
432      Returnlist.insert(s);
433    }
434  }
435
436  return Returnlist;
437}
438
439CanonicalForm alg_lc(const CanonicalForm &f)
440{
441  if (f.inCoeffDomain()) return f;
442  if (f.level()>0)
443  {
444    return alg_lc(f.LC());
445  }
446}
447
448// the heart of the algorithm: the one from Trager
449static CFFList
450alg_factor( const CanonicalForm & f, const CFList & Astar, const Variable & vminpoly, const Varlist & oldord, const CFList & as){
451  CFFList L, Factorlist;
452  CanonicalForm R, Rstar, s, g, h;
453  CFList substlist;
454
455  DEBINCLEVEL(cout,"alg_factor");
456  DEBOUTLN(cout, "alg_factor: f= ", f);
457  //out_cf("start alg_factor:",f,"\n");
458  substlist= simpleextension(Astar, vminpoly, Rstar);
459  DEBOUTLN(cout, "alg_factor: substlist= ", substlist);
460  DEBOUTLN(cout, "alg_factor: minpoly Rstar= ", Rstar);
461  DEBOUTLN(cout, "alg_factor: vminpoly= ", vminpoly);
462
463  sqrf_norm(f, Rstar, vminpoly, s, g, R );
464  //out_cf("sqrf_norm R:",R,"\n");
465  //out_cf("sqrf_norm s:",s,"\n");
466  //out_cf("sqrf_norm g:",g,"\n");
467  DEBOUTLN(cout, "alg_factor: g= ", g);
468  DEBOUTLN(cout, "alg_factor: s= ", s);
469  DEBOUTLN(cout, "alg_factor: R= ", R);
470  Off(SW_RATIONAL);
471  Variable X;
472  if (getAlgVar(R,X))
473  {
474    // factorize R over alg.extension with X
475//cout << "alg: "<< X << " mipo=" << getMipo(X,Variable('X')) <<endl;
476    if (R.isUnivariate())
477    {
478      DEBOUTLN(cout, "alg_factor: factorize( ", R);
479      Factorlist =  factorize( R, X );
480    }
481    else
482    {
483      #if 1
484      Variable XX;
485      CanonicalForm mipo=getMipo(X,XX);
486      CFList as(mipo);
487      DEBOUTLN(cout, "alg_factor: newfactoras( ", R);
488      Factorlist = newfactoras(R, as , 1);
489      #else
490      // factor R over k
491      DEBOUTLN(cout, "alg_factor: Factorize( ", R);
492      Factorlist = Factorize(R);
493      #endif
494    }
495  }
496  else
497  {
498    // factor R over k
499    DEBOUTLN(cout, "alg_factor: Factorize( ", R);
500    Factorlist = Factorize(R);
501  }
502  On(SW_RATIONAL);
503  DEBOUTLN(cout, "alg_factor: Factorize(R)= ", Factorlist);
504  if ( !Factorlist.getFirst().factor().inCoeffDomain() )
505    Factorlist.insert(CFFactor(1,1));
506  if ( Factorlist.length() == 2 && Factorlist.getLast().exp()== 1){ // irreduzibel (first entry is a constant)
507    L.append(CFFactor(f,1));
508  }
509  else{
510    DEBOUTLN(cout, "alg_factor: g= ", g);
511    CanonicalForm gnew= g(s,s.mvar());
512    DEBOUTLN(cout, "alg_factor: gnew= ", gnew);
513    g=gnew;
514    for ( CFFListIterator i=Factorlist; i.hasItem(); i++){
515      CanonicalForm fnew=i.getItem().factor();
516      fnew= fnew(s,s.mvar());
517      DEBOUTLN(cout, "alg_factor: fnew= ", fnew);
518      DEBOUTLN(cout, "alg_factor: substlist= ", substlist);
519      for ( CFListIterator ii=substlist; ii.hasItem(); ii++){
520        DEBOUTLN(cout, "alg_factor: item= ", ii.getItem());
521        fnew= fnew(ii.getItem(), ii.getItem().mvar());
522        DEBOUTLN(cout, "alg_factor: fnew= ", fnew);
523      }
524      if (degree(i.getItem().factor()) > 0 ){
525        // undo linear transformation!!!! and then gcd!
526        //cout << "algcd(" << g << "," << fnew << ",as" << as << ")" << endl;
527        //out_cf("algcd g=",g,"\n");
528        //out_cf("algcd fnew=",fnew,"\n");
529        //h= algcd(g,fnew, as, oldord);
530        //if (as.length() >1)
531        //  h= algcd(g,fnew, as, oldord);
532        //else
533          h=alg_gcd(g,fnew,as);
534        //out_cf(" -> algcd=",algcd(g,fnew, as, oldord),"\n");
535        //out_cf(" -> alg_gcd=",alg_gcd(g,fnew,as),"\n");
536        //cout << "algcd result:" << h << endl;
537        DEBOUTLN(cout, "  alg_factor: h= ", h);
538        DEBOUTLN(cout, "  alg_factor: oldord= ", oldord);
539        if ( degree(h) > 0 ){ //otherwise it's a constant
540          g= divide(g, h,as);
541          DEBOUTLN(cout, "alg_factor: g/h= ", g);
542          DEBOUTLN(cout, "alg_factor: s= ", s);
543          DEBOUTLN(cout, "alg_factor: substlist= ", substlist);
544          L.append(CFFactor(h,1));
545        }
546      }
547    }
548    // we are not interested in a
549    // constant (over K_r, which can be a polynomial!)
550    if (degree(g, f.mvar())>0){ L.append(CFFactor(g,1)); }
551  }
552  CFFList LL;
553  if (getCharacteristic()>0)
554  {
555    CFFListIterator i=L;
556    CanonicalForm c_fac=1;
557    CanonicalForm c;
558    for(;i.hasItem(); i++ )
559    {
560      CanonicalForm ff=i.getItem().factor();
561      c=alg_lc(ff);
562      int e=i.getItem().exp();
563      ff/=c;
564      if (!ff.isOne()) LL.append(CFFactor(ff,e));
565      while (e>0) { c_fac*=c;e--; }
566    }
567    if (!c_fac.isOne()) LL.insert(CFFactor(c_fac,1));
568  }
569  else
570  {
571    LL=L;
572  }
573  //CFFListIterator i=LL;
574  //for(;i.hasItem(); i++ )
575  //  out_cf("end alg_f:",i.getItem().factor(),"\n");
576  //printf("end alg_factor\n");
577  DEBOUTLN(cout, "alg_factor: L= ", LL);
578  DEBDECLEVEL(cout,"alg_factor");
579  return LL;
580}
581
582static CFFList
583endler( const CanonicalForm & f, const CFList & AS, const Varlist & uord ){
584  CanonicalForm F=f, g, q,r;
585  CFFList Output;
586  CFList One, Two, asnew, as=AS;
587  CFListIterator i,ii;
588  VarlistIterator j;
589  Variable vg;
590
591  for (i=as; i.hasItem(); i++){
592    g= i.getItem();
593    if (g.deriv() == 0 ){
594      DEBOUTLN(cout, "Inseperable extension detected: ", g);
595      for (j=uord; j.hasItem(); j++){
596        if ( degree(g,j.getItem()) > 0 ) vg= j.getItem();
597      }
598      // Now we have the highest transzendental in vg;
599      DEBOUTLN(cout, "Transzendental is ", vg);
600      CanonicalForm gg=-1*g[0];
601      divrem(gg,vg,q,r); r= gg-q*vg;   gg= gg-r;
602      //DEBOUTLN(cout, "q= ", q); DEBOUTLN(cout, "r= ", r);
603      DEBOUTLN(cout, "  that is ", gg);
604      DEBOUTLN(cout, "  maps to ", g+gg);
605      One.insert(gg); Two.insert(g+gg);
606      // Now transform all remaining polys in as:
607      int x=0;
608      for (ii=i; ii.hasItem(); ii++){
609        if ( x != 0 ){
610          divrem(ii.getItem(), gg, q,r);
611//          cout << ii.getItem() << " divided by " << gg << endl;
612          DEBOUTLN(cout, "q= ", q); DEBOUTLN(cout, "r= ", r);
613          ii.append(ii.getItem()+q*g); ii.remove(1);
614          DEBOUTLN(cout, "as= ", as);
615        }
616        x+= 1;
617      }
618      // Now transform F:
619      divrem(F, gg, q,r);
620      F= F+q*g;
621      DEBOUTLN(cout, "new F= ", F);
622    }
623    else{ asnew.append(i.getItem());  }// just the identity
624  }
625  // factor F with minimal polys given in asnew:
626  DEBOUTLN(cout, "Factor F=  ", F);
627  DEBOUTLN(cout, "  with as= ", asnew);
628  int success=0;
629  CFFList factorlist= newcfactor(F,asnew, success);
630  DEBOUTLN(cout, "  gives =  ", factorlist);
631  DEBOUTLN(cout, "One= ", One);
632  DEBOUTLN(cout, "Two= ", Two);
633
634  // Transform back:
635  for ( CFFListIterator k=factorlist; k.hasItem(); k++){
636    CanonicalForm factor= k.getItem().factor();
637    ii=One;
638    for (i=Two; i.hasItem(); i++){
639      DEBOUTLN(cout, "Mapping ", i.getItem());
640      DEBOUTLN(cout, "     to ", ii.getItem());
641      DEBOUTLN(cout, "     in ", factor);
642      divrem(factor,i.getItem(),q,r); r=factor -q*i.getItem();
643      DEBOUTLN(cout, "q= ", q); DEBOUTLN(cout, "r= ", r);
644      factor= ii.getItem()*q +r; //
645      ii++;
646    }
647    Output.append(CFFactor(factor,k.getItem().exp()));
648  }
649
650  return Output;
651}
652
653
654// 1) prepares data
655// 2) for char=p we distinguish 3 cases:
656//           no transcendentals, seperable and inseperable extensions
657CFFList
658newfactoras( const CanonicalForm & f, const CFList & as, int success){
659  Variable vf=f.mvar();
660  CFListIterator i;
661  CFFListIterator jj;
662  CFList reduceresult;
663  CFFList result;
664
665  success=1;
666  DEBINCLEVEL(cout, "newfactoras");
667  DEBOUTMSG(cerr, rcsid);
668  DEBOUTLN(cout, "newfactoras called with f= ", f);
669  DEBOUTLN(cout, "               content(f)= ", content(f));
670  DEBOUTLN(cout, "                       as= ", as);
671  DEBOUTLN(cout, "newfactoras: cls(vf)= ", cls(vf));
672  DEBOUTLN(cout, "newfactoras: cls(as.getLast())= ", cls(as.getLast()));
673  DEBOUTLN(cout, "newfactoras: degree(f,vf)= ", degree(f,vf));
674
675// F1: [Test trivial cases]
676// 1) first trivial cases:
677  if ( (cls(vf) <= cls(as.getLast())) ||  degree(f,vf)<=1 ){
678// ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) )
679    DEBDECLEVEL(cout,"newfactoras");
680    return CFFList(CFFactor(f,1));
681  }
682
683// 2) List of variables:
684// 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1
685// 2b) Setup variableordering
686  CFList Astar;
687  Variable x;
688  CanonicalForm elem;
689  Varlist ord, uord,oldord;
690  for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii));  }
691  oldord= uord; oldord.append(vf);
692
693  for ( i=as; i.hasItem(); i++ ){
694    elem= i.getItem();
695    x= elem.mvar();
696    if ( degree(elem,x) > 1){ // otherwise it's not an extension
697      //if ( degree(f,x) > 0 ){ // does it occure in f? RICHTIG?
698        Astar.append(elem);
699        ord.append(x);
700        //}
701    }
702  }
703  uord= Difference(uord,ord);
704  DEBOUTLN(cout, "Astar is: ", Astar);
705  DEBOUTLN(cout, "ord is: ", ord);
706  DEBOUTLN(cout, "uord is: ", uord);
707
708// 3) second trivial cases: we already prooved irr. of f over no extensions
709  if ( Astar.length() == 0 ){
710    DEBDECLEVEL(cout,"newfactoras");
711    return CFFList(CFFactor(f,1));
712  }
713
714// 4) Try to obtain a partial factorization using prop2 and prop3
715//    Use with caution! We have to proof these propositions first!
716  // Not yet implemented
717
718// 5) Look if elements in uord actually occure in any of the minimal
719//    polynomials. If no element of uord occures in any of the minimal
720//   polynomials, we don't have transzendentals.
721  Varlist newuord=Var_is_in_AS(uord,Astar);
722  DEBOUTLN(cout, "newuord is: ", newuord);
723
724  CFFList Factorlist;
725  Varlist gcdord= Union(ord,newuord); gcdord.append(f.mvar());
726  // This is for now. we need alg_sqrfree implemented!
727  //cout << "algcd(" << f << "," << f.deriv() << " as:" << Astar <<endl;
728  //CanonicalForm Fgcd= algcd(f,f.deriv(),Astar,gcdord);
729  CanonicalForm Fgcd;
730        //if (Astar.length() >1)
731        //  Fgcd= algcd(f,f.deriv(),Astar,gcdord);
732        //else
733          Fgcd= alg_gcd(f,f.deriv(),Astar);
734        //out_cf("algcd:",algcd(f,f.deriv(),Astar,gcdord),"\n");
735        //out_cf("alg_gcd:",alg_gcd(f,f.deriv(),Astar),"\n");
736 // cout << "algcd result:"  << Fgcd << endl;
737  if ( Fgcd == 0 ) DEBOUTMSG(cerr, "WARNING: p'th root ?");
738  if (( degree(Fgcd, f.mvar()) > 0) && (!(f.deriv().isZero())) ){
739    DEBOUTLN(cout, "Nontrivial GCD found of ", f);
740    CanonicalForm Ggcd= divide(f, Fgcd,Astar);
741    DEBOUTLN(cout, "  split into ", Fgcd);
742    DEBOUTLN(cout, "         and ", Ggcd);
743    Fgcd= pp(Fgcd); Ggcd= pp(Ggcd);
744    DEBDECLEVEL(cout,"newfactoras");
745    return myUnion(newfactoras(Fgcd,as,success) , newfactoras(Ggcd,as,success));
746  }
747  if ( getCharacteristic() > 0 ){
748
749    // First look for extension!
750    IntList degreelist;
751    Variable vminpoly;
752    for (i=Astar; i.hasItem(); i++){degreelist.append(degree(i.getItem()));}
753    int extdeg= getextension(degreelist, degree(f));
754    DEBOUTLN(cout, "Extension needed of degree ", extdeg);
755
756    // Now the real stuff!
757    if ( newuord.length() == 0 ){ // no transzendentals
758      DEBOUTMSG(cout, "No transzendentals!");
759      if ( extdeg > 1 ){
760        CanonicalForm MIPO= generate_mipo( extdeg, vminpoly);
761        DEBOUTLN(cout, "Minpoly produced ", MIPO);
762        vminpoly= rootOf(MIPO);
763      }
764      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
765      DEBDECLEVEL(cout,"newfactoras");
766      return Factorlist;
767    }
768    else if ( inseperable(Astar) > 0 ){ // Look if extensions are seperable
769      // a) Use Endler
770      DEBOUTMSG(cout, "Inseperable extensions! Using Endler!");
771      CFFList templist= endler(f,Astar, newuord);
772      DEBOUTLN(cout, "Endler gives: ", templist);
773      return templist;
774    }
775    else{ // we are on the save side: Use trager
776      DEBOUTMSG(cout, "Only seperable extensions!");
777      if (extdeg > 1 ){
778        CanonicalForm MIPO=generate_mipo(extdeg, vminpoly );
779        vminpoly= rootOf(MIPO);
780        DEBOUTLN(cout, "Minpoly generated: ", MIPO);
781        DEBOUTLN(cout, "vminpoly= ", vminpoly);
782        DEBOUTLN(cout, "degree(vminpoly)= ", degree(vminpoly));
783      }
784      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
785      DEBDECLEVEL(cout,"newfactoras");
786      return Factorlist;
787    }
788  }
789  else{ // char=0 apply trager directly
790    DEBOUTMSG(cout, "Char=0! Apply Trager!");
791    Variable vminpoly;
792    Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
793      DEBDECLEVEL(cout,"newfactoras");
794      return Factorlist;
795  }
796
797  DEBDECLEVEL(cout,"newfactoras");
798  return CFFList(CFFactor(f,1));
799}
800
801CFFList
802newcfactor(const CanonicalForm & f, const CFList & as, int success ){
803  Off(SW_RATIONAL);
804  CFFList Output, output, Factors=Factorize(f); On(SW_RATIONAL);
805  Factors.removeFirst();
806
807  if ( as.length() == 0 ){ success=1; return Factors;}
808  if ( cls(f) <= cls(as.getLast()) ) { success=1; return Factors;}
809
810  success=1;
811  for ( CFFListIterator i=Factors; i.hasItem(); i++ ){
812    output=newfactoras(i.getItem().factor(),as, success);
813    for ( CFFListIterator j=output; j.hasItem(); j++)
814      Output = myappend(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp()));
815  }
816  return Output;
817}
818
819/*
820$Log: not supported by cvs2svn $
821Revision 1.13  2004/12/10 10:15:05  Singular
822*pohl: AlgExtGenerator etc.
823
824Revision 1.12  2003/02/18 11:09:25  Singular
825* hannes: alg_gcd(f,f'=0) get a special handling
826
827Revision 1.11  2002/10/24 17:22:21  Singular
828* hannes: factoring in alg.ext., alg_gcd, NTL stuff
829
830Revision 1.10  2002/08/19 11:11:29  Singular
831* hannes/pfister: alg_gcd etc.
832
833Revision 1.9  2002/07/30 15:16:19  Singular
834*hannes: fix for alg. extension
835
836Revision 1.8  2001/08/06 08:32:53  Singular
837* hannes: code cleanup
838
839Revision 1.7  2001/06/27 13:58:05  Singular
840*hannes/GP: debug newfactoras, char_series, ...
841
842Revision 1.6  2001/06/21 14:57:04  Singular
843*hannes/GP: Factorize, newfactoras, ...
844
845Revision 1.5  2001/06/18 08:44:39  pfister
846* hannes/GP/michael: factory debug, Factorize
847
848Revision 1.4  1998/05/25 18:32:38  obachman
8491998-05-25  Olaf Bachmann  <obachman@mathematik.uni-kl.de>
850
851        * charset/alg_factor.cc: replaced identifiers 'random' by
852        'myrandom' to avoid name conflicts with the built-in (stdlib)
853        library function 'random' which might be a macro -- and, actually
854        is  under gcc v 2.6.3
855
856Revision 1.3  1998/03/12 12:34:24  schmidt
857        * charset/csutil.cc, charset/alg_factor.cc: all references to
858          `common_den()' replaced by `bCommonDen()'
859
860Revision 1.2  1997/09/12 07:19:37  Singular
861* hannes/michael: libfac-0.3.0
862
863*/
Note: See TracBrowser for help on using the repository browser.