source: git/factory/cf_factor.cc @ 2667bc8

spielwiese
Last change on this file since 2667bc8 was 034eec, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes:ntl-gcd git-svn-id: file:///usr/local/Singular/svn/trunk@6997 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.0 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: cf_factor.cc,v 1.23 2004-01-20 15:39:52 Singular Exp $ */
3
4//{{{ docu
5//
6// cf_factor.cc - factorization and square free algorithms.
7//
8// Used by: fac_multivar.cc, fac_univar.cc, cf_irred.cc
9//
10// Header file: cf_algorithm.h
11//
12//}}}
13
14#include <config.h>
15
16#include "cf_gmp.h"
17
18#include "assert.h"
19
20#include "cf_defs.h"
21#include "canonicalform.h"
22#include "cf_iter.h"
23#include "fac_berlekamp.h"
24#include "fac_cantzass.h"
25#include "fac_univar.h"
26#include "fac_multivar.h"
27#include "fac_sqrfree.h"
28#include "cf_algorithm.h"
29
30#include "int_int.h"
31#ifdef HAVE_NTL
32#include "NTLconvert.h"
33#endif
34
35int getExp(); /* cf_char.cc */
36
37static bool isUnivariateBaseDomain( const CanonicalForm & f )
38{
39    CFIterator i = f;
40    bool ok = i.coeff().inBaseDomain();
41    i++;
42    while ( i.hasTerms() && ( ok = ok && i.coeff().inBaseDomain() ) ) i++;
43    return ok;
44}
45
46void find_exp(const CanonicalForm & f, int * exp_f)
47{
48  if ( ! f.inCoeffDomain() )
49  {
50    int e=f.level();
51    CFIterator i = f;
52    if (e>=0)
53    {
54      if (i.exp() > exp_f[e]) exp_f[e]=i.exp();
55    }
56    for (; i.hasTerms(); i++ )
57    {
58      find_exp(i.coeff(), exp_f);
59    }
60  }
61}
62
63int find_mvar(const CanonicalForm & f)
64{
65  int mv=f.level();
66  int *exp_f=new int[mv+1];
67  int i;
68  for(i=mv;i>0;i--) exp_f[i]=0;
69  find_exp(f,exp_f);
70  for(i=mv;i>0;i--)
71  {
72    if ((exp_f[i]>0) && (exp_f[i]<exp_f[mv]))
73    {
74      mv=i;
75    }
76  }
77  delete[] exp_f;
78  return mv;
79}
80
81#if 0
82void out_cf(char *s1,const CanonicalForm &f,char *s2)
83{
84  printf("%s",s1);
85  if (f==0) printf("+0");
86  //else if (! f.inCoeffDomain() )
87  else if (! f.inBaseDomain() )
88  {
89    int l = f.level();
90    for ( CFIterator i = f; i.hasTerms(); i++ )
91    {
92      int e=i.exp();
93      if (i.coeff().isOne())
94      {
95        printf("+");
96        if (e==0) printf("1");
97        else
98        {
99          printf("v(%d)",l);
100          if (e!=1) printf("^%d",e);
101        }
102      }
103      else
104      {
105        out_cf("+(",i.coeff(),")");
106        if (e!=0)
107        {
108          printf("*v(%d)",l);
109          if (e!=1) printf("^%d",e);
110        }
111      }
112    }
113  }
114  else
115  {
116    if ( f.isImm() )
117    {
118      printf("+%d",f.intval());
119    }
120    else printf("+...");
121    //if (f.inZ()) printf("(Z)");
122    //else if (f.inQ()) printf("(Q)");
123    //else if (f.inFF()) printf("(FF)");
124    //else if (f.inPP()) printf("(PP)");
125    //else if (f.inGF()) printf("(PP)");
126    //else
127    if (f.inExtension()) printf("E(%d)",f.level());
128  }
129  printf("%s",s2);
130}
131void out_cff(CFFList &L)
132{
133  int n = L.length();
134  CFFListIterator J=L;
135  int j=0;
136  for ( ; J.hasItem(); J++, j++ )
137  {
138    printf("F%d",j);out_cf(":",J.getItem().factor()," ^ ");
139    printf("%d\n", J.getItem().exp());
140  }
141}
142void test_cff(CFFList &L,const CanonicalForm & f)
143{
144  int n = L.length();
145  CFFListIterator J=L;
146  CanonicalForm t=1;
147  int j=0;
148  if (!(L.getFirst().factor().inCoeffDomain()))
149    printf("first entry is not const\n");
150  for ( ; J.hasItem(); J++, j++ )
151  {
152    CanonicalForm tt=J.getItem().factor();
153    if (tt.inCoeffDomain() && (j!=0))
154      printf("other entry is const\n");
155    j=J.getItem().exp();
156    while(j>0) { t*=tt; j--; }
157  }
158  if ((f-t)!=0) { printf("problem:\n");out_cf("factor:",f," has problems\n");}
159}
160#endif
161
162bool isPurePoly(const CanonicalForm & f)
163{
164  if (f.level()<=0) return false;
165  for (CFIterator i=f;i.hasTerms();i++)
166  {
167    if (!(i.coeff().inBaseDomain())) return false;
168  }
169  return true;
170}
171
172CFFList factorize ( const CanonicalForm & f, bool issqrfree )
173{
174  if ( f.inCoeffDomain() )
175        return CFFList( f );
176  //out_cf("factorize:",f,"==================================\n");
177  int mv=f.level();
178  int org_v=mv;
179  if (! f.isUnivariate() )
180  {
181    mv=find_mvar(f);
182    if ( getCharacteristic() == 0 )
183    {
184      if (mv!=f.level())
185      {
186        swapvar(f,Variable(mv),f.mvar());
187      }
188    }
189    else
190    {
191      if (mv!=1)
192      {
193        swapvar(f,Variable(mv),Variable(1));
194        org_v=1;
195      }
196    }
197  }
198  CFFList F;
199  if ( getCharacteristic() > 0 )
200  {
201    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
202    #ifdef HAVE_NTL
203    if (isOn(SW_USE_NTL) && (isPurePoly(f)))
204    {
205      // USE NTL
206      if (getCharacteristic()!=2)
207      {
208        // set remainder
209        #ifdef NTL_ZZ
210        ZZ r;
211        r=getCharacteristic();
212        ZZ_pContext ccc(r);
213        #else
214        zz_pContext ccc(getCharacteristic());
215        #endif
216        ccc.restore();
217        #ifdef NTL_ZZ
218        ZZ_p::init(r);
219        #else
220        zz_p::init(getCharacteristic());
221        #endif
222        // convert to NTL
223        #ifdef NTL_ZZ
224        ZZ_pX f1=convertFacCF2NTLZZpX(f);
225        ZZ_p leadcoeff = LeadCoeff(f1);
226        #else
227        zz_pX f1=convertFacCF2NTLzzpX(f);
228        zz_p leadcoeff = LeadCoeff(f1);
229        #endif
230        //make monic
231        f1=f1 / LeadCoeff(f1);
232
233        // factorize
234        #ifdef NTL_ZZ
235        vec_pair_ZZ_pX_long factors;
236        #else
237        vec_pair_zz_pX_long factors;
238        #endif
239        CanZass(factors,f1);
240
241        // convert back to factory
242        #ifdef NTL_ZZ
243        F=convertNTLvec_pair_ZZpX_long2FacCFFList(factors,leadcoeff,f.mvar());
244        #else
245        F=convertNTLvec_pair_zzpX_long2FacCFFList(factors,leadcoeff,f.mvar());
246        #endif
247        //test_cff(F,f);
248      }
249      else
250      {
251        // Specialcase characteristic==2
252        ZZ r;r=2;
253        ZZ_p::init(r);
254
255        // remainder is 2 --> nothing to set
256
257        // convert to NTL using the faster conversion routine for characteristic 2
258        GF2X f1=convertFacCF2NTLGF2X(f);
259        // no make monic necessary in GF2
260        //factorize
261        vec_pair_GF2X_long factors;
262        CanZass(factors,f1);
263
264        // convert back to factory again using the faster conversion routine for vectors over GF2X
265        F=convertNTLvec_pair_GF2X_long2FacCFFList(factors,LeadCoeff(f1),f.mvar());
266      }
267    }
268    else
269    #endif
270    {  // Use Factory without NTL
271      if ( isOn( SW_BERLEKAMP ) )
272         F=FpFactorizeUnivariateB( f, issqrfree );
273      else
274        F=FpFactorizeUnivariateCZ( f, issqrfree, 0, Variable(), Variable() );
275    }
276  }
277  else
278  {
279    CanonicalForm cd = bCommonDen( f );
280    CanonicalForm fz = f * cd;
281    bool on_rational = isOn(SW_RATIONAL);
282    Off(SW_RATIONAL);
283    if ( f.isUnivariate() )
284    {
285      #ifdef HAVE_NTL
286      if ((isOn(SW_USE_NTL)) && (isPurePoly(f)))
287      {
288        //USE NTL
289        CanonicalForm ic=icontent(fz);
290        fz/=ic;
291        ZZ c;
292        vec_pair_ZZX_long factors;
293        //factorize the converted polynomial
294        factor(c,factors,convertFacCF2NTLZZX(fz));
295
296        //convert the result back to Factory
297        F=convertNTLvec_pair_ZZX_long2FacCFFList(factors,c,fz.mvar());
298        if ( ! ic.isOne() )
299        {
300          if ( F.getFirst().factor().inCoeffDomain() )
301          {
302            CFFactor new_first( F.getFirst().factor() * ic );
303            F.removeFirst();
304            F.insert( new_first );
305          }
306          else
307            F.insert( CFFactor( ic ) );
308        }
309        else
310        {
311          if ( !F.getFirst().factor().inCoeffDomain() )
312          {
313            CFFactor new_first( 1 );
314            F.insert( new_first );
315          }
316        }
317        //if ( F.getFirst().factor().isOne() )
318        //{
319        //  F.removeFirst();
320        //}
321        //printf("NTL:\n");out_cff(F);
322        //F=ZFactorizeUnivariate( fz, issqrfree );
323        //printf("fac.:\n");out_cff(F);
324      }
325      else
326      #endif
327      {
328        //Use Factory without NTL
329        F = ZFactorizeUnivariate( fz, issqrfree );
330      }
331    }
332    else
333      F = ZFactorizeMultivariate( fz, issqrfree );
334
335    if ( on_rational )
336      On(SW_RATIONAL);
337    if ( ! cd.isOne() )
338    {
339      if ( F.getFirst().factor().inCoeffDomain() )
340      {
341        CFFactor new_first( F.getFirst().factor() / cd );
342        F.removeFirst();
343        F.insert( new_first );
344      }
345      else
346      {
347        F.insert( CFFactor( 1/cd ) );
348      }
349    }
350  }
351
352  if ((mv!=org_v) && (! f.isUnivariate() ))
353  {
354    CFFListIterator J=F;
355    for ( ; J.hasItem(); J++)
356    {
357      swapvar(J.getItem().factor(),Variable(mv),Variable(org_v));
358    }
359    swapvar(f,Variable(mv),Variable(org_v));
360  }
361  //out_cff(F);
362  return F;
363}
364
365#ifdef HAVE_NTL
366CanonicalForm fntl ( const CanonicalForm & f, int j )
367{
368  ZZX f1=convertFacCF2NTLZZX(f);
369  return convertZZ2CF(coeff(f1,j));
370}
371#endif
372
373CFFList factorize ( const CanonicalForm & f, const Variable & alpha )
374{
375  //out_cf("factorize:",f,"==================================\n");
376  //out_cf("mipo:",getMipo(alpha),"\n");
377  CFFList F;
378  ASSERT( alpha.level() < 0, "not an algebraic extension" );
379  ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
380  ASSERT( getCharacteristic() > 0, "char 0 factorization not implemented" );
381  #ifdef HAVE_NTL
382  if  (isOn(SW_USE_NTL))
383  {
384    //USE NTL
385    if (getCharacteristic()!=2)
386    {
387      // First all cases with characteristic !=2
388      // set remainder
389      ZZ r;
390      r=getCharacteristic();
391      ZZ_pContext ccc(r);
392      ccc.restore();
393
394      // set minimal polynomial in NTL
395      ZZ_pX minPo=convertFacCF2NTLZZpX(getMipo(alpha));
396      ZZ_pEContext c(minPo);
397
398      c.restore();
399
400      // convert to NTL
401      ZZ_pEX f1=convertFacCF2NTLZZ_pEX(f,minPo);
402
403      //make monic
404      ZZ_pE leadcoeff= LeadCoeff(f1);
405      f1=f1 / leadcoeff;
406
407      // factorize using NTL
408      vec_pair_ZZ_pEX_long factors;
409      CanZass(factors,f1);
410
411      // return converted result
412      F=convertNTLvec_pair_ZZpEX_long2FacCFFList(factors,leadcoeff,f.mvar(),alpha);
413    }
414    else
415    {
416      // special case : GF2
417
418      // remainder is two ==> nothing to do
419      // set remainder
420      ZZ r;
421      r=getCharacteristic();
422      ZZ_pContext ccc(r);
423      ccc.restore();
424
425      // set minimal polynomial in NTL using the optimized conversion routines for characteristic 2
426      GF2X minPo=convertFacCF2NTLGF2X(getMipo(alpha,f.mvar()));
427      GF2EContext c(minPo);
428      c.restore();
429
430      // convert to NTL again using the faster conversion routines
431      GF2EX f1;
432      if (isPurePoly(f))
433      {
434        GF2X f_tmp=convertFacCF2NTLGF2X(f);
435        f1=to_GF2EX(f_tmp);
436      }
437      else
438      {
439        f1=convertFacCF2NTLGF2EX(f,minPo);
440      }
441
442      // make monic (in Z/2(a))
443      GF2E f1_coef=LeadCoeff(f1);
444      MakeMonic(f1);
445
446      // factorize using NTL
447      vec_pair_GF2EX_long factors;
448      CanZass(factors,f1);
449
450      // return converted result
451      F=convertNTLvec_pair_GF2EX_long2FacCFFList(factors,f1_coef,f.mvar(),alpha);
452    }
453
454  }
455  else
456  #endif
457  {
458    F=FpFactorizeUnivariateCZ( f, false, 1, alpha, Variable() );
459  }
460  return F;
461}
462
463CFFList sqrFree ( const CanonicalForm & f, bool sort )
464{
465//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
466    CFFList result;
467
468    if ( getCharacteristic() == 0 )
469        result = sqrFreeZ( f );
470    else
471        result = sqrFreeFp( f );
472
473    return ( sort ? sortCFFList( result ) : result );
474}
475
476bool isSqrFree ( const CanonicalForm & f )
477{
478//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
479    if ( getCharacteristic() == 0 )
480        return isSqrFreeZ( f );
481    else
482        return isSqrFreeFp( f );
483}
484
Note: See TracBrowser for help on using the repository browser.