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

spielwiese
Last change on this file since aadd442 was aadd442, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: SqrFreeTest -> isSqrFree git-svn-id: file:///usr/local/Singular/svn/trunk@10524 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 27.9 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.19 2008-01-25 14:19:39 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( CERR, "resultante: called  f= ", f);
125  DEBOUTLN( CERR, "resultante: called  g= ", g);
126  DEBOUTLN( CERR, "resultante: called  v= ", v);
127  if ( f.mvar() < v || g.mvar() < v ){
128    DEBOUTMSG(CERR, "resultante: f.mvar() < v || g.mvar() < v");
129    return 1;
130  }
131
132  if ( f.degree( v ) < 1 || g.degree( v ) < 1 ){
133    DEBOUTMSG(CERR, "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(CERR, "sqrf_norm_sub:      f= ", f);
181  DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
182  myrandom.reset();   s=f.mvar()-myrandom.item()*Palpha.mvar();   g=f;
183  R= CanonicalForm(0);
184  DEBOUTLN(CERR, "sqrf_norm_sub: myrandom s= ", s);
185
186  // Norm, resultante taken with respect to y
187  while ( !sqfreetest ){
188    DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
189    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
190    DEBOUTLN(CERR, "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(CERR, "sqrf_norm_sub: temp= ", temp);
196      if (degree(temp,vf) != 0 || temp == temp.genZero() ){ sqfreetest= 0; }
197      else { sqfreetest= 1; }
198      DEBOUTLN(CERR, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
199    }
200    else{
201      DEBOUTMSG(CERR, "Starting isSqrFree(R)!");
202      // Look at isSqrFree!
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(CERR, "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(CERR, "isSqrFree(R)= ", sqfreetest);
222    }
223    if ( ! sqfreetest ){
224      myrandom.next();
225      DEBOUTLN(CERR, "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(CERR, "sqrf_norm_sub: testing s= ", s);
230      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
231      DEBOUTLN(CERR, "             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(CERR, "sqrf_norm_sub:      f= ", f);
246  DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
247  myrandom.reset();   s=f.mvar()-myrandom.item()*Palpha.mvar();   g=f;
248  R= CanonicalForm(0);
249  DEBOUTLN(CERR, "sqrf_norm_sub: myrandom s= ", s);
250
251  // Norm, resultante taken with respect to y
252  while ( !sqfreetest ){
253    DEBOUTLN(CERR, "sqrf_norm_sub: Palpha= ", Palpha);
254    R = resultante(Palpha, g, y); R= R* bCommonDen(R);
255    DEBOUTLN(CERR, "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(CERR, "sqrf_norm_sub: temp= ", temp);
261      if (degree(temp,vf) != 0 || temp == temp.genZero() ){ sqfreetest= 0; }
262      else { sqfreetest= 1; }
263      DEBOUTLN(CERR, "sqrf_norm_sub: sqfreetest= ", sqfreetest);
264    }
265    else{
266      DEBOUTMSG(CERR, "Starting isSqrFree(R)!");
267      // Look at isSqrFree!
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(CERR, "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(CERR, "isSqrFree(R)= ", sqfreetest);
287    }
288    if ( ! sqfreetest ){
289      myrandom.next();
290      DEBOUTLN(CERR, "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(CERR, "sqrf_norm_sub: testing s= ", s);
295      g= f(f.mvar()-t*Palpha.mvar(), f.mvar());
296      DEBOUTLN(CERR, "             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(CERR, "sqrf_norm:      f= ", f);
307  DEBOUTLN(CERR, "sqrf_norm: Palpha= ", PPalpha);
308  if ( getCharacteristic() == 0 ) {
309    IntGenerator myrandom;
310    DEBOUTMSG(CERR, "sqrf_norm: no extension, char=0");
311    sqrf_norm_sub(f,PPalpha, myrandom, s,g,R);
312    DEBOUTLN(CERR, "sqrf_norm:      f= ", f);
313    DEBOUTLN(CERR, "sqrf_norm: Palpha= ", PPalpha);
314    DEBOUTLN(CERR, "sqrf_norm:      s= ", s);
315    DEBOUTLN(CERR, "sqrf_norm:      g= ", g);
316    DEBOUTLN(CERR, "sqrf_norm:      R= ", R);
317  }
318  else if ( degree(Extension) > 0 ){ // working over Extensions
319    DEBOUTLN(CERR, "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(CERR, "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(CERR, "getextension: gcd == 1, l=",l);
399        if ( l==length ){ setCharacteristic(charac);  return k; }
400      }
401      else { DEBOUTMSG(CERR, "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(CERR, "simpleextension: Astar= ", Astar);
417  DEBOUTLN(CERR, "simpleextension:     R= ", R);
418  DEBOUTLN(CERR, "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(CERR, "simpleextension: f(x)= ", i.getItem());
424      DEBOUTLN(CERR, "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(CERR, "simpleextension: g= ", g);
429      if ( s != 0 ) DEBOUTLN(CERR, "simpleextension: s= ", s);
430      else DEBOUTLN(CERR, "simpleextension: s= ", s);
431      DEBOUTLN(CERR, "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{
452  CFFList L, Factorlist;
453  CanonicalForm R, Rstar, s, g, h;
454  CFList substlist;
455
456  DEBINCLEVEL(CERR,"alg_factor");
457  DEBOUTLN(CERR, "alg_factor: f= ", f);
458
459  //out_cf("start alg_factor:",f,"\n");
460  substlist= simpleextension(Astar, vminpoly, Rstar);
461  DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
462  DEBOUTLN(CERR, "alg_factor: minpoly Rstar= ", Rstar);
463  DEBOUTLN(CERR, "alg_factor: vminpoly= ", vminpoly);
464
465  sqrf_norm(f, Rstar, vminpoly, s, g, R );
466  //out_cf("sqrf_norm R:",R,"\n");
467  //out_cf("sqrf_norm s:",s,"\n");
468  //out_cf("sqrf_norm g:",g,"\n");
469  DEBOUTLN(CERR, "alg_factor: g= ", g);
470  DEBOUTLN(CERR, "alg_factor: s= ", s);
471  DEBOUTLN(CERR, "alg_factor: R= ", R);
472  Off(SW_RATIONAL);
473  Variable X;
474  if (getAlgVar(R,X))
475  {
476    // factorize R over alg.extension with X
477//CERR << "alg: "<< X << " mipo=" << getMipo(X,Variable('X')) <<"\n";
478    if (R.isUnivariate())
479    {
480      DEBOUTLN(CERR, "alg_factor: factorize( ", R);
481      Factorlist =  factorize( R, X );
482    }
483    else
484    {
485      #if 1
486      Variable XX;
487      CanonicalForm mipo=getMipo(X,XX);
488      CFList as(mipo);
489      DEBOUTLN(CERR, "alg_factor: newfactoras( ", R);
490      Factorlist = newfactoras(R, as , 1);
491      #else
492      // factor R over k
493      DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
494      Factorlist = Factorize(R);
495      #endif
496    }
497  }
498  else
499  {
500    // factor R over k
501    DEBOUTLN(CERR, "alg_factor: Factorize( ", R);
502    Factorlist = Factorize(R);
503  }
504  On(SW_RATIONAL);
505  DEBOUTLN(CERR, "alg_factor: Factorize(R)= ", Factorlist);
506  if ( !Factorlist.getFirst().factor().inCoeffDomain() )
507    Factorlist.insert(CFFactor(1,1));
508  if ( Factorlist.length() == 2 && Factorlist.getLast().exp()== 1)
509  { // irreduzibel (first entry is a constant)
510    L.append(CFFactor(f,1));
511  }
512  else
513  {
514    DEBOUTLN(CERR, "alg_factor: g= ", g);
515    CanonicalForm gnew= g(s,s.mvar());
516    DEBOUTLN(CERR, "alg_factor: gnew= ", gnew);
517    g=gnew;
518    for ( CFFListIterator i=Factorlist; i.hasItem(); i++)
519    {
520      CanonicalForm fnew=i.getItem().factor();
521      fnew= fnew(s,s.mvar());
522      DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
523      DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
524      for ( CFListIterator ii=substlist; ii.hasItem(); ii++){
525        DEBOUTLN(CERR, "alg_factor: item= ", ii.getItem());
526        fnew= fnew(ii.getItem(), ii.getItem().mvar());
527        DEBOUTLN(CERR, "alg_factor: fnew= ", fnew);
528      }
529      if (degree(i.getItem().factor()) > 0 )
530      {
531        // undo linear transformation!!!! and then gcd!
532        //CERR << "algcd(" << g << "," << fnew << ",as" << as << ")" << "\n";
533        //out_cf("g=",g,"\n");
534        //out_cf("fnew=",fnew,"\n");
535        //h= algcd(g,fnew, as, oldord);
536        //if (as.length() >1)
537        //  h= algcd(g,fnew, as, oldord);
538        //else
539          h=alg_gcd(g,fnew,as);
540        //out_cf(" -> algcd=",algcd(g,fnew, as, oldord),"\n");
541        //out_cf(" -> alg_gcd=",alg_gcd(g,fnew,as),"\n");
542        //CERR << "algcd result:" << h << "\n";
543        DEBOUTLN(CERR, "  alg_factor: h= ", h);
544        DEBOUTLN(CERR, "  alg_factor: oldord= ", oldord);
545        if ( degree(h) > 0 )
546        { //otherwise it's a constant
547          //CanonicalForm c=LC(h,g.mvar());
548          //out_cf("new lc h:",c,"\n");
549          //h= divide(h,c,as);
550          //out_cf("new factor h/c:",h,"\n");
551          g= divide(g, h,as);
552          DEBOUTLN(CERR, "alg_factor: g/h= ", g);
553          DEBOUTLN(CERR, "alg_factor: s= ", s);
554          DEBOUTLN(CERR, "alg_factor: substlist= ", substlist);
555          //out_cf("new g:",g,"\n");
556          L.append(CFFactor(h,1));
557        }
558        //else printf("degree <=1\n");
559      }
560    }
561    // we are not interested in a
562    // constant (over K_r, which can be a polynomial!)
563    if (degree(g, f.mvar())>0){ L.append(CFFactor(g,1)); }
564  }
565  CFFList LL;
566  if (getCharacteristic()>0)
567  {
568    CFFListIterator i=L;
569    CanonicalForm c_fac=1;
570    CanonicalForm c;
571    for(;i.hasItem(); i++ )
572    {
573      CanonicalForm ff=i.getItem().factor();
574      c=alg_lc(ff);
575      int e=i.getItem().exp();
576      ff/=c;
577      if (!ff.isOne()) LL.append(CFFactor(ff,e));
578      while (e>0) { c_fac*=c;e--; }
579    }
580    if (!c_fac.isOne()) LL.insert(CFFactor(c_fac,1));
581  }
582  else
583  {
584    LL=L;
585  }
586  //CFFListIterator i=LL;
587  //for(;i.hasItem(); i++ )
588  //  out_cf("end alg_f:",i.getItem().factor(),"\n");
589  //printf("end alg_factor\n");
590  DEBOUTLN(CERR, "alg_factor: L= ", LL);
591  DEBDECLEVEL(CERR,"alg_factor");
592  return LL;
593}
594
595static CFFList
596endler( const CanonicalForm & f, const CFList & AS, const Varlist & uord ){
597  CanonicalForm F=f, g, q,r;
598  CFFList Output;
599  CFList One, Two, asnew, as=AS;
600  CFListIterator i,ii;
601  VarlistIterator j;
602  Variable vg;
603
604  for (i=as; i.hasItem(); i++){
605    g= i.getItem();
606    if (g.deriv() == 0 ){
607      DEBOUTLN(CERR, "Inseperable extension detected: ", g);
608      for (j=uord; j.hasItem(); j++){
609        if ( degree(g,j.getItem()) > 0 ) vg= j.getItem();
610      }
611      // Now we have the highest transzendental in vg;
612      DEBOUTLN(CERR, "Transzendental is ", vg);
613      CanonicalForm gg=-1*g[0];
614      divrem(gg,vg,q,r); r= gg-q*vg;   gg= gg-r;
615      //DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
616      DEBOUTLN(CERR, "  that is ", gg);
617      DEBOUTLN(CERR, "  maps to ", g+gg);
618      One.insert(gg); Two.insert(g+gg);
619      // Now transform all remaining polys in as:
620      int x=0;
621      for (ii=i; ii.hasItem(); ii++){
622        if ( x != 0 ){
623          divrem(ii.getItem(), gg, q,r);
624//          CERR << ii.getItem() << " divided by " << gg << "\n";
625          DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
626          ii.append(ii.getItem()+q*g); ii.remove(1);
627          DEBOUTLN(CERR, "as= ", as);
628        }
629        x+= 1;
630      }
631      // Now transform F:
632      divrem(F, gg, q,r);
633      F= F+q*g;
634      DEBOUTLN(CERR, "new F= ", F);
635    }
636    else{ asnew.append(i.getItem());  }// just the identity
637  }
638  // factor F with minimal polys given in asnew:
639  DEBOUTLN(CERR, "Factor F=  ", F);
640  DEBOUTLN(CERR, "  with as= ", asnew);
641  int success=0;
642  CFFList factorlist= newcfactor(F,asnew, success);
643  DEBOUTLN(CERR, "  gives =  ", factorlist);
644  DEBOUTLN(CERR, "One= ", One);
645  DEBOUTLN(CERR, "Two= ", Two);
646
647  // Transform back:
648  for ( CFFListIterator k=factorlist; k.hasItem(); k++){
649    CanonicalForm factor= k.getItem().factor();
650    ii=One;
651    for (i=Two; i.hasItem(); i++){
652      DEBOUTLN(CERR, "Mapping ", i.getItem());
653      DEBOUTLN(CERR, "     to ", ii.getItem());
654      DEBOUTLN(CERR, "     in ", factor);
655      divrem(factor,i.getItem(),q,r); r=factor -q*i.getItem();
656      DEBOUTLN(CERR, "q= ", q); DEBOUTLN(CERR, "r= ", r);
657      factor= ii.getItem()*q +r; //
658      ii++;
659    }
660    Output.append(CFFactor(factor,k.getItem().exp()));
661  }
662
663  return Output;
664}
665
666
667// 1) prepares data
668// 2) for char=p we distinguish 3 cases:
669//           no transcendentals, seperable and inseperable extensions
670CFFList
671newfactoras( const CanonicalForm & f, const CFList & as, int success){
672  Variable vf=f.mvar();
673  CFListIterator i;
674  CFFListIterator jj;
675  CFList reduceresult;
676  CFFList result;
677
678  success=1;
679  DEBINCLEVEL(CERR, "newfactoras");
680  DEBOUTMSG(CERR, rcsid);
681  DEBOUTLN(CERR, "newfactoras called with f= ", f);
682  DEBOUTLN(CERR, "               content(f)= ", content(f));
683  DEBOUTLN(CERR, "                       as= ", as);
684  DEBOUTLN(CERR, "newfactoras: cls(vf)= ", cls(vf));
685  DEBOUTLN(CERR, "newfactoras: cls(as.getLast())= ", cls(as.getLast()));
686  DEBOUTLN(CERR, "newfactoras: degree(f,vf)= ", degree(f,vf));
687
688// F1: [Test trivial cases]
689// 1) first trivial cases:
690  if ( (cls(vf) <= cls(as.getLast())) ||  degree(f,vf)<=1 ){
691// ||( (as.length()==1) && (degree(f,vf)==3) && (degree(as.getFirst()==2)) )
692    DEBDECLEVEL(CERR,"newfactoras");
693    return CFFList(CFFactor(f,1));
694  }
695
696// 2) List of variables:
697// 2a) Setup list of those polys in AS having degree(AS[i], AS[i].mvar()) > 1
698// 2b) Setup variableordering
699  CFList Astar;
700  Variable x;
701  CanonicalForm elem;
702  Varlist ord, uord,oldord;
703  for ( int ii=1; ii< level(vf) ; ii++ ) { uord.append(Variable(ii));  }
704  oldord= uord; oldord.append(vf);
705
706  for ( i=as; i.hasItem(); i++ ){
707    elem= i.getItem();
708    x= elem.mvar();
709    if ( degree(elem,x) > 1){ // otherwise it's not an extension
710      Astar.append(elem);
711      ord.append(x);
712    }
713  }
714  uord= Difference(uord,ord);
715  DEBOUTLN(CERR, "Astar is: ", Astar);
716  DEBOUTLN(CERR, "ord is: ", ord);
717  DEBOUTLN(CERR, "uord is: ", uord);
718
719// 3) second trivial cases: we already prooved irr. of f over no extensions
720  if ( Astar.length() == 0 ){
721    DEBDECLEVEL(CERR,"newfactoras");
722    return CFFList(CFFactor(f,1));
723  }
724
725// 4) Try to obtain a partial factorization using prop2 and prop3
726//    Use with caution! We have to proof these propositions first!
727  // Not yet implemented
728
729// 5) Look if elements in uord actually occure in any of the minimal
730//    polynomials. If no element of uord occures in any of the minimal
731//   polynomials, we don't have transzendentals.
732  Varlist newuord=Var_is_in_AS(uord,Astar);
733  DEBOUTLN(CERR, "newuord is: ", newuord);
734
735  CFFList Factorlist;
736  Varlist gcdord= Union(ord,newuord); gcdord.append(f.mvar());
737  // This is for now. we need alg_sqrfree implemented!
738  //CERR << "algcd(" << f << "," << f.deriv() << " as:" << Astar <<"\n";
739  //CanonicalForm Fgcd= algcd(f,f.deriv(),Astar,gcdord);
740  CanonicalForm Fgcd;
741        //if (Astar.length() >1)
742        //  Fgcd= algcd(f,f.deriv(),Astar,gcdord);
743        //else
744          Fgcd= alg_gcd(f,f.deriv(),Astar);
745        //out_cf("algcd:",algcd(f,f.deriv(),Astar,gcdord),"\n");
746        //out_cf("alg_gcd:",alg_gcd(f,f.deriv(),Astar),"\n");
747 // CERR << "algcd result:"  << Fgcd << "\n";
748  if ( Fgcd == 0 ) DEBOUTMSG(CERR, "WARNING: p'th root ?");
749  if (( degree(Fgcd, f.mvar()) > 0) && (!(f.deriv().isZero())) ){
750    DEBOUTLN(CERR, "Nontrivial GCD found of ", f);
751    CanonicalForm Ggcd= divide(f, Fgcd,Astar);
752    DEBOUTLN(CERR, "  split into ", Fgcd);
753    DEBOUTLN(CERR, "         and ", Ggcd);
754    Fgcd= pp(Fgcd); Ggcd= pp(Ggcd);
755    DEBDECLEVEL(CERR,"newfactoras");
756    return UnionCFFL(newfactoras(Fgcd,as,success) , newfactoras(Ggcd,as,success));
757  }
758  if ( getCharacteristic() > 0 ){
759
760    // First look for extension!
761    IntList degreelist;
762    Variable vminpoly;
763    for (i=Astar; i.hasItem(); i++){degreelist.append(degree(i.getItem()));}
764    int extdeg= getextension(degreelist, degree(f));
765    DEBOUTLN(CERR, "Extension needed of degree ", extdeg);
766
767    // Now the real stuff!
768    if ( newuord.length() == 0 ){ // no transzendentals
769      DEBOUTMSG(CERR, "No transzendentals!");
770      if ( extdeg > 1 ){
771        CanonicalForm MIPO= generate_mipo( extdeg, vminpoly);
772        DEBOUTLN(CERR, "Minpoly produced ", MIPO);
773        vminpoly= rootOf(MIPO);
774      }
775      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
776      DEBDECLEVEL(CERR,"newfactoras");
777      return Factorlist;
778    }
779    else if ( inseperable(Astar) > 0 ){ // Look if extensions are seperable
780      // a) Use Endler
781      DEBOUTMSG(CERR, "Inseperable extensions! Using Endler!");
782      CFFList templist= endler(f,Astar, newuord);
783      DEBOUTLN(CERR, "Endler gives: ", templist);
784      return templist;
785    }
786    else{ // we are on the save side: Use trager
787      DEBOUTMSG(CERR, "Only seperable extensions!");
788      if (extdeg > 1 ){
789        CanonicalForm MIPO=generate_mipo(extdeg, vminpoly );
790        vminpoly= rootOf(MIPO);
791        DEBOUTLN(CERR, "Minpoly generated: ", MIPO);
792        DEBOUTLN(CERR, "vminpoly= ", vminpoly);
793        DEBOUTLN(CERR, "degree(vminpoly)= ", degree(vminpoly));
794      }
795      Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
796      DEBDECLEVEL(CERR,"newfactoras");
797      return Factorlist;
798    }
799  }
800  else{ // char=0 apply trager directly
801    DEBOUTMSG(CERR, "Char=0! Apply Trager!");
802    Variable vminpoly;
803    Factorlist= alg_factor(f, Astar, vminpoly, oldord, as);
804      DEBDECLEVEL(CERR,"newfactoras");
805      return Factorlist;
806  }
807
808  DEBDECLEVEL(CERR,"newfactoras");
809  return CFFList(CFFactor(f,1));
810}
811
812CFFList
813newcfactor(const CanonicalForm & f, const CFList & as, int success ){
814  Off(SW_RATIONAL);
815  CFFList Output, output, Factors=Factorize(f); On(SW_RATIONAL);
816  Factors.removeFirst();
817
818  if ( as.length() == 0 ){ success=1; return Factors;}
819  if ( cls(f) <= cls(as.getLast()) ) { success=1; return Factors;}
820
821  success=1;
822  for ( CFFListIterator i=Factors; i.hasItem(); i++ ){
823    output=newfactoras(i.getItem().factor(),as, success);
824    for ( CFFListIterator j=output; j.hasItem(); j++)
825      Output = appendCFFL(Output,CFFactor(j.getItem().factor(),j.getItem().exp()*i.getItem().exp()));
826  }
827  return Output;
828}
829
830/*
831$Log: not supported by cvs2svn $
832Revision 1.18  2008/01/22 09:51:36  Singular
833*hannes: sqrFree/InternalSqrFree -> factory
834
835Revision 1.17  2007/05/15 14:46:48  Singular
836*hannes: factorize in Zp(a)[x...]
837
838Revision 1.16  2006/05/16 14:46:48  Singular
839*hannes: gcc 4.1 fixes
840
841Revision 1.15  2005/10/17 13:16:18  Singular
842*hannes: code cleanup
843
844Revision 1.14  2005/07/08 09:18:15  Singular
845*hannes: fixed call of resultant
846
847Revision 1.13  2004/12/10 10:15:05  Singular
848*pohl: AlgExtGenerator etc.
849
850Revision 1.12  2003/02/18 11:09:25  Singular
851* hannes: alg_gcd(f,f'=0) get a special handling
852
853Revision 1.11  2002/10/24 17:22:21  Singular
854* hannes: factoring in alg.ext., alg_gcd, NTL stuff
855
856Revision 1.10  2002/08/19 11:11:29  Singular
857* hannes/pfister: alg_gcd etc.
858
859Revision 1.9  2002/07/30 15:16:19  Singular
860*hannes: fix for alg. extension
861
862Revision 1.8  2001/08/06 08:32:53  Singular
863* hannes: code cleanup
864
865Revision 1.7  2001/06/27 13:58:05  Singular
866*hannes/GP: debug newfactoras, char_series, ...
867
868Revision 1.6  2001/06/21 14:57:04  Singular
869*hannes/GP: Factorize, newfactoras, ...
870
871Revision 1.5  2001/06/18 08:44:39  pfister
872* hannes/GP/michael: factory debug, Factorize
873
874Revision 1.4  1998/05/25 18:32:38  obachman
8751998-05-25  Olaf Bachmann  <obachman@mathematik.uni-kl.de>
876
877        * charset/alg_factor.cc: replaced identifiers 'random' by
878        'myrandom' to avoid name conflicts with the built-in (stdlib)
879        library function 'random' which might be a macro -- and, actually
880        is  under gcc v 2.6.3
881
882Revision 1.3  1998/03/12 12:34:24  schmidt
883        * charset/csutil.cc, charset/alg_factor.cc: all references to
884          `common_den()' replaced by `bCommonDen()'
885
886Revision 1.2  1997/09/12 07:19:37  Singular
887* hannes/michael: libfac-0.3.0
888
889*/
Note: See TracBrowser for help on using the repository browser.