source: git/factory/cf_factor.cc @ ec989c

spielwiese
Last change on this file since ec989c was ec989c, checked in by Hans Schönemann <hannes@…>, 22 years ago
* hannes: NTL stuff git-svn-id: file:///usr/local/Singular/svn/trunk@6290 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.2 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: cf_factor.cc,v 1.17 2002-10-24 12:17:46 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
162static bool 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        ZZ r;
210        r=getCharacteristic();
211        ZZ_pContext ccc(r);
212        ccc.restore();
213        ZZ_p::init(r);
214        // convert to NTL
215        ZZ_pX f1=convertFacCF2NTLZZpX(f);
216        ZZ_p leadcoeff = LeadCoeff(f1);
217        //make monic
218        f1=f1 / LeadCoeff(f1);
219
220        // factorize
221        vec_pair_ZZ_pX_long factors;
222        CanZass(factors,f1);
223
224        // convert back to factory
225        F=convertNTLvec_pair_ZZpX_long2FacCFFList(factors,leadcoeff,f.mvar());
226        //test_cff(F,f);
227      }
228      else
229      {
230        // Specialcase characteristic==2
231        ZZ r;r=2;
232        ZZ_p::init(r);
233
234        // remainder is 2 --> nothing to set
235
236        // convert to NTL using the faster conversion routine for characteristic 2
237        GF2X f1=convertFacCF2NTLGF2X(f);
238        // no make monic necessary in GF2
239        //factorize
240        vec_pair_GF2X_long factors;
241        CanZass(factors,f1);
242
243        // convert back to factory again using the faster conversion routine for vectors over GF2X
244        F=convertNTLvec_pair_GF2X_long2FacCFFList(factors,LeadCoeff(f1),f.mvar());
245      }
246    }
247    else
248    #endif
249    {  // Use Factory without NTL
250      if ( isOn( SW_BERLEKAMP ) )
251         F=FpFactorizeUnivariateB( f, issqrfree );
252      else
253        F=FpFactorizeUnivariateCZ( f, issqrfree, 0, Variable(), Variable() );
254    }
255  }
256  else
257  {
258    CanonicalForm cd = bCommonDen( f );
259    CanonicalForm fz = f * cd;
260    bool on_rational = isOn(SW_RATIONAL);
261    Off(SW_RATIONAL);
262    if ( f.isUnivariate() )
263    {
264      #ifdef HAVE_NTL
265      if ((isOn(SW_USE_NTL)) && (isPurePoly(f)))
266      {
267        //USE NTL
268        CanonicalForm ic=icontent(fz);
269        fz/=ic;
270        ZZ c;
271        vec_pair_ZZX_long factors;
272        //factorize the converted polynomial
273        factor(c,factors,convertFacCF2NTLZZX(fz));
274
275        //convert the result back to Factory
276        F=convertNTLvec_pair_ZZX_long2FacCFFList(factors,c,fz.mvar());
277        if ( ! ic.isOne() )
278        {
279          if ( F.getFirst().factor().inCoeffDomain() )
280          {
281            CFFactor new_first( F.getFirst().factor() * ic );
282            F.removeFirst();
283            F.insert( new_first );
284          }
285          else
286            F.insert( CFFactor( ic ) );
287        }
288        else
289        {
290          if ( !F.getFirst().factor().inCoeffDomain() )
291          {
292            CFFactor new_first( 1 );
293            F.insert( new_first );
294          }
295        }
296        //if ( F.getFirst().factor().isOne() )
297        //{
298        //  F.removeFirst();
299        //}
300        //printf("NTL:\n");out_cff(F);
301        //F=ZFactorizeUnivariate( fz, issqrfree );
302        //printf("fac.:\n");out_cff(F);
303      }
304      else
305      #endif
306      {
307        //Use Factory without NTL
308        F = ZFactorizeUnivariate( fz, issqrfree );
309      }
310    }
311    else
312      F = ZFactorizeMultivariate( fz, issqrfree );
313
314    if ( on_rational )
315      On(SW_RATIONAL);
316    if ( ! cd.isOne() )
317    {
318      if ( F.getFirst().factor().inCoeffDomain() )
319      {
320        CFFactor new_first( F.getFirst().factor() / cd );
321        F.removeFirst();
322        F.insert( new_first );
323      }
324      else
325      {
326        F.insert( CFFactor( 1/cd ) );
327      }
328    }
329  }
330
331  if ((mv!=org_v) && (! f.isUnivariate() ))
332  {
333    CFFListIterator J=F;
334    for ( ; J.hasItem(); J++)
335    {
336      swapvar(J.getItem().factor(),Variable(mv),Variable(org_v));
337    }
338    swapvar(f,Variable(mv),Variable(org_v));
339  }
340  //out_cff(F);
341  return F;
342}
343
344#ifdef HAVE_NTL
345CanonicalForm fntl ( const CanonicalForm & f, int j )
346{
347  ZZX f1=convertFacCF2NTLZZX(f);
348  return convertZZ2CF(coeff(f1,j));
349} 
350#endif
351
352CFFList factorize ( const CanonicalForm & f, const Variable & alpha )
353{
354    CFFList F;
355    ASSERT( alpha.level() < 0, "not an algebraic extension" );
356    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
357    ASSERT( getCharacteristic() > 0, "char 0 factorization not implemented" );
358    #ifdef HAVE_NTL
359    if  (isOn(SW_USE_NTL))
360    {
361      //USE NTL
362      if (1 ) //getCharacteristic()!=2)
363      {
364        // First all cases with characteristic !=2
365        // set remainder
366        ZZ r;
367        r=getCharacteristic();
368        ZZ_pContext ccc(r);
369        ccc.restore();
370
371        // set minimal polynomial in NTL
372        ZZ_pX minPo=convertFacCF2NTLZZpX(getMipo(alpha));
373        ZZ_pEContext c(minPo);
374
375        c.restore();
376
377        // convert to NTL
378        ZZ_pEX f1=convertFacCF2NTLZZ_pEX(f,minPo);
379
380        //make monic
381        ZZ_pE leadcoeff= LeadCoeff(f1);
382        f1=f1 / leadcoeff;
383
384        // factorize using NTL
385        vec_pair_ZZ_pEX_long factors;
386        CanZass(factors,f1);
387
388        // return converted result
389        F=convertNTLvec_pair_ZZpEX_long2FacCFFList(factors,leadcoeff,f.mvar(),alpha);
390      }
391      else
392      {
393        // special case : GF2
394
395        // remainder is two ==> nothing to do
396
397        // set minimal polynomial in NTL using the optimized conversion routines for characteristic 2
398        GF2X minPo=convertFacCF2NTLGF2X(getMipo(alpha,f.mvar()));
399        GF2EContext c(minPo);
400        c.restore();
401
402        // convert to NTL again using the faster conversion routines
403        GF2X f_tmp=convertFacCF2NTLGF2X(f);
404        GF2EX f1=to_GF2EX(f_tmp);
405
406        // no make monic necessary in GF2
407
408        // factorize using NTL
409        vec_pair_GF2EX_long factors;
410        CanZass(factors,f1);
411
412        // return converted result
413        F=convertNTLvec_pair_GF2EX_long2FacCFFList(factors,LeadCoeff(f1),f.mvar(),alpha);
414      }
415
416    }
417    else
418    #endif
419    {
420        printf("factorize without NTL: alg. ext.\n");
421      F=FpFactorizeUnivariateCZ( f, false, 1, alpha, Variable() );
422    }
423    return F;
424}
425
426CFFList sqrFree ( const CanonicalForm & f, bool sort )
427{
428//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
429    CFFList result;
430
431    if ( getCharacteristic() == 0 )
432        result = sqrFreeZ( f );
433    else
434        result = sqrFreeFp( f );
435
436    return ( sort ? sortCFFList( result ) : result );
437}
438
439bool isSqrFree ( const CanonicalForm & f )
440{
441//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
442    if ( getCharacteristic() == 0 )
443        return isSqrFreeZ( f );
444    else
445        return isSqrFreeFp( f );
446}
447
Note: See TracBrowser for help on using the repository browser.