source: git/factory/cf_factor.cc @ b1326b

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