source: git/factory/cf_factor.cc @ 37d038

spielwiese
Last change on this file since 37d038 was 37d038, checked in by Hans Schoenemann <hannes@…>, 4 years ago
fix: factorize
  • Property mode set to 100644
File size: 22.9 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3/**
4 *
5 * @file cf_factor.cc
6 *
7 * Interface to factorization and square free factorization algorithms.
8 *
9 * Used by: cf_irred.cc
10 *
11 * Header file: cf_algorithm.h
12 *
13**/
14
15
16#include "config.h"
17
18
19#include "cf_assert.h"
20
21#include "cf_defs.h"
22#include "canonicalform.h"
23#include "cf_iter.h"
24#include "fac_sqrfree.h"
25#include "cf_algorithm.h"
26#include "facFqFactorize.h"
27#include "facFqSquarefree.h"
28#include "cf_map.h"
29#include "facAlgExt.h"
30#include "facFactorize.h"
31#include "singext.h"
32#include "cf_util.h"
33#include "fac_berlekamp.h"
34#include "fac_cantzass.h"
35#include "fac_univar.h"
36#include "fac_multivar.h"
37
38#include "int_int.h"
39#ifdef HAVE_NTL
40#include "NTLconvert.h"
41#endif
42
43#include "factory/cf_gmp.h"
44#ifdef HAVE_FLINT
45#include "FLINTconvert.h"
46#if (__FLINT_RELEASE >= 20700)
47#include <flint/nmod_mpoly_factor.h>
48#include <flint/fmpq_mpoly_factor.h>
49#endif
50#endif
51
52//static bool isUnivariateBaseDomain( const CanonicalForm & f )
53//{
54//    CFIterator i = f;
55//    bool ok = i.coeff().inBaseDomain();
56//    i++;
57//    while ( i.hasTerms() && ( ok = ok && i.coeff().inBaseDomain() ) ) i++;
58//    return ok;
59//}
60
61void find_exp(const CanonicalForm & f, int * exp_f)
62{
63  if ( ! f.inCoeffDomain() )
64  {
65    int e=f.level();
66    CFIterator i = f;
67    if (e>=0)
68    {
69      if (i.exp() > exp_f[e]) exp_f[e]=i.exp();
70    }
71    for (; i.hasTerms(); i++ )
72    {
73      find_exp(i.coeff(), exp_f);
74    }
75  }
76}
77
78int find_mvar(const CanonicalForm & f)
79{
80  int mv=f.level();
81  int *exp_f=NEW_ARRAY(int,mv+1);
82  int i;
83  for(i=mv;i>0;i--) exp_f[i]=0;
84  find_exp(f,exp_f);
85  for(i=mv;i>0;i--)
86  {
87    if ((exp_f[i]>0) && (exp_f[i]<exp_f[mv]))
88    {
89      mv=i;
90    }
91  }
92  DELETE_ARRAY(exp_f);
93  return mv;
94}
95
96#if 1
97//#ifndef NOSTREAMIO
98void out_cf(const char *s1,const CanonicalForm &f,const char *s2)
99{
100  printf("%s",s1);
101  if (f.isZero()) printf("+0");
102  //else if (! f.inCoeffDomain() )
103  else if (! f.inBaseDomain() )
104  {
105    int l = f.level();
106    for ( CFIterator i = f; i.hasTerms(); i++ )
107    {
108      int e=i.exp();
109      if (i.coeff().isOne())
110      {
111        printf("+");
112        if (e==0) printf("1");
113        else
114        {
115          printf("%c",'a'+l-1);
116          if (e!=1) printf("^%d",e);
117        }
118      }
119      else
120      {
121        out_cf("+(",i.coeff(),")");
122        if (e!=0)
123        {
124          printf("*%c",'a'+l-1);
125          if (e!=1) printf("^%d",e);
126        }
127      }
128    }
129  }
130  else
131  {
132    if ( f.isImm() )
133    {
134      if (CFFactory::gettype()==GaloisFieldDomain)
135      {
136         long a= imm2int (f.getval());
137         if ( a == gf_q )
138           printf ("+%ld", a);
139         else  if ( a == 0L )
140           printf ("+1");
141         else  if ( a == 1L )
142           printf ("+%c",gf_name);
143         else
144         {
145           printf ("+%c",gf_name);
146           printf ("^%ld",a);
147         }
148      }
149      else
150      {
151        long l=f.intval();
152        if (l<0) printf("%ld",l);
153        else     printf("+%ld",l);
154      }
155    }
156    else
157    {
158    #ifdef NOSTREAMIO
159      if (f.inZ())
160      {
161        mpz_t m;
162        gmp_numerator(f,m);
163        char * str = new char[mpz_sizeinbase( m, 10 ) + 2];
164        str = mpz_get_str( str, 10, m );
165        puts(str);
166        delete[] str;
167        mpz_clear(m);
168      }
169      else if (f.inQ())
170      {
171        mpz_t m;
172        gmp_numerator(f,m);
173        char * str = new char[mpz_sizeinbase( m, 10 ) + 2];
174        str = mpz_get_str( str, 10, m );
175        while(str[strlen(str)]<' ') { str[strlen(str)]='\0'; }
176        puts(str);putchar('/');
177        delete[] str;
178        mpz_clear(m);
179        gmp_denominator(f,m);
180        str = new char[mpz_sizeinbase( m, 10 ) + 2];
181        str = mpz_get_str( str, 10, m );
182        while(str[strlen(str)]<' ') { str[strlen(str)]='\0'; }
183        puts(str);
184        delete[] str;
185        mpz_clear(m);
186      }
187    #else
188       std::cout << f;
189    #endif
190    }
191    //if (f.inZ()) printf("(Z)");
192    //else if (f.inQ()) printf("(Q)");
193    //else if (f.inFF()) printf("(FF)");
194    //else if (f.inPP()) printf("(PP)");
195    //else if (f.inGF()) printf("(PP)");
196    //else
197    if (f.inExtension()) printf("E(%d)",f.level());
198  }
199  printf("%s",s2);
200}
201void out_cff(CFFList &L)
202{
203  //int n = L.length();
204  CFFListIterator J=L;
205  int j=0;
206  for ( ; J.hasItem(); J++, j++ )
207  {
208    printf("F%d",j);out_cf(":",J.getItem().factor()," ^ ");
209    printf("%d\n", J.getItem().exp());
210  }
211}
212void test_cff(CFFList &L,const CanonicalForm & f)
213{
214  //int n = L.length();
215  CFFListIterator J=L;
216  CanonicalForm t=1;
217  int j=0;
218  if (!(L.getFirst().factor().inCoeffDomain()))
219    printf("first entry is not const\n");
220  for ( ; J.hasItem(); J++, j++ )
221  {
222    CanonicalForm tt=J.getItem().factor();
223    if (tt.inCoeffDomain() && (j!=0))
224      printf("other entry is const\n");
225    j=J.getItem().exp();
226    while(j>0) { t*=tt; j--; }
227  }
228  if (!(f-t).isZero()) { printf("problem:\n");out_cf("factor:",f," has problems\n");}
229}
230//#endif
231#endif
232
233bool isPurePoly_m(const CanonicalForm & f)
234{
235  if (f.inBaseDomain()) return true;
236  if (f.level()<0) return false;
237  for (CFIterator i=f;i.hasTerms();i++)
238  {
239    if (!isPurePoly_m(i.coeff())) return false;
240  }
241  return true;
242}
243bool isPurePoly(const CanonicalForm & f)
244{
245  if (f.level()<=0) return false;
246  for (CFIterator i=f;i.hasTerms();i++)
247  {
248    if (!(i.coeff().inBaseDomain())) return false;
249  }
250  return true;
251}
252
253
254/**
255 * get_max_degree_Variable returns Variable with
256 * highest degree. We assume f is *not* a constant!
257**/
258Variable
259get_max_degree_Variable(const CanonicalForm & f)
260{
261  ASSERT( ( ! f.inCoeffDomain() ), "no constants" );
262  int max=0, maxlevel=0, n=level(f);
263  for ( int i=1; i<=n; i++ )
264  {
265    if (degree(f,Variable(i)) >= max)
266    {
267      max= degree(f,Variable(i)); maxlevel= i;
268    }
269  }
270  return Variable(maxlevel);
271}
272
273/**
274 * get_Terms: Split the polynomial in the containing terms.
275 * getTerms: the real work is done here.
276**/
277void
278getTerms( const CanonicalForm & f, const CanonicalForm & t, CFList & result )
279{
280  if ( getNumVars(f) == 0 ) result.append(f*t);
281  else{
282    Variable x(level(f));
283    for ( CFIterator i=f; i.hasTerms(); i++ )
284      getTerms( i.coeff(), t*power(x,i.exp()), result);
285  }
286}
287CFList
288get_Terms( const CanonicalForm & f ){
289  CFList result,dummy,dummy2;
290  CFIterator i;
291  CFListIterator j;
292
293  if ( getNumVars(f) == 0 ) result.append(f);
294  else{
295    Variable _x(level(f));
296    for ( i=f; i.hasTerms(); i++ ){
297      getTerms(i.coeff(), 1, dummy);
298      for ( j=dummy; j.hasItem(); j++ )
299        result.append(j.getItem() * power(_x, i.exp()));
300
301      dummy= dummy2; // have to initalize new
302    }
303  }
304  return result;
305}
306
307
308/**
309 * homogenize homogenizes f with Variable x
310**/
311CanonicalForm
312homogenize( const CanonicalForm & f, const Variable & x)
313{
314#if 0
315  int maxdeg=totaldegree(f), deg;
316  CFIterator i;
317  CanonicalForm elem, result(0);
318
319  for (i=f; i.hasTerms(); i++)
320  {
321    elem= i.coeff()*power(f.mvar(),i.exp());
322    deg = totaldegree(elem);
323    if ( deg < maxdeg )
324      result += elem * power(x,maxdeg-deg);
325    else
326      result+=elem;
327  }
328  return result;
329#else
330  CFList Newlist, Termlist= get_Terms(f);
331  int maxdeg=totaldegree(f), deg;
332  CFListIterator i;
333  CanonicalForm elem, result(0);
334
335  for (i=Termlist; i.hasItem(); i++)
336  {
337    elem= i.getItem();
338    deg = totaldegree(elem);
339    if ( deg < maxdeg )
340      Newlist.append(elem * power(x,maxdeg-deg));
341    else
342      Newlist.append(elem);
343  }
344  for (i=Newlist; i.hasItem(); i++) // rebuild
345    result += i.getItem();
346
347  return result;
348#endif
349}
350
351CanonicalForm
352homogenize( const CanonicalForm & f, const Variable & x, const Variable & v1, const Variable & v2)
353{
354#if 0
355  int maxdeg=totaldegree(f), deg;
356  CFIterator i;
357  CanonicalForm elem, result(0);
358
359  for (i=f; i.hasTerms(); i++)
360  {
361    elem= i.coeff()*power(f.mvar(),i.exp());
362    deg = totaldegree(elem);
363    if ( deg < maxdeg )
364      result += elem * power(x,maxdeg-deg);
365    else
366      result+=elem;
367  }
368  return result;
369#else
370  CFList Newlist, Termlist= get_Terms(f);
371  int maxdeg=totaldegree(f), deg;
372  CFListIterator i;
373  CanonicalForm elem, result(0);
374
375  for (i=Termlist; i.hasItem(); i++)
376  {
377    elem= i.getItem();
378    deg = totaldegree(elem,v1,v2);
379    if ( deg < maxdeg )
380      Newlist.append(elem * power(x,maxdeg-deg));
381    else
382      Newlist.append(elem);
383  }
384  for (i=Newlist; i.hasItem(); i++) // rebuild
385    result += i.getItem();
386
387  return result;
388#endif
389}
390
391VAR int singular_homog_flag=1;
392
393int cmpCF( const CFFactor & f, const CFFactor & g )
394{
395  if (f.exp() > g.exp()) return 1;
396  if (f.exp() < g.exp()) return 0;
397  if (f.factor() > g.factor()) return 1;
398  return 0;
399}
400
401/**
402 * factorization over \f$ F_p \f$ or \f$ Q \f$
403**/
404CFFList factorize ( const CanonicalForm & f, bool issqrfree )
405{
406  if ( f.inCoeffDomain() )
407        return CFFList( f );
408#ifndef NOASSERT
409  Variable a;
410  ASSERT (!hasFirstAlgVar (f, a), "f has an algebraic variable use factorize \
411          ( const CanonicalForm & f, const Variable & alpha ) instead");
412#endif
413  //out_cf("factorize:",f,"==================================\n");
414  if (! f.isUnivariate() ) // preprocess homog. polys
415  {
416    if ( singular_homog_flag && f.isHomogeneous())
417    {
418      Variable xn = get_max_degree_Variable(f);
419      int d_xn = degree(f,xn);
420      CFMap n;
421      CanonicalForm F = compress(f(1,xn),n);
422      CFFList Intermediatelist;
423      Intermediatelist = factorize(F);
424      CFFList Homoglist;
425      CFFListIterator j;
426      for ( j=Intermediatelist; j.hasItem(); j++ )
427      {
428        Homoglist.append(
429            CFFactor( n(j.getItem().factor()), j.getItem().exp()) );
430      }
431      CFFList Unhomoglist;
432      CanonicalForm unhomogelem;
433      for ( j=Homoglist; j.hasItem(); j++ )
434      {
435        unhomogelem= homogenize(j.getItem().factor(),xn);
436        Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp()));
437        d_xn -= (degree(unhomogelem,xn)*j.getItem().exp());
438      }
439      if ( d_xn != 0 ) // have to append xn^(d_xn)
440        Unhomoglist.append(CFFactor(CanonicalForm(xn),d_xn));
441      if(isOn(SW_USE_NTL_SORT)) Unhomoglist.sort(cmpCF);
442      return Unhomoglist;
443    }
444  }
445  CFFList F;
446  if ( getCharacteristic() > 0 )
447  {
448    if (f.isUnivariate())
449    {
450#ifdef HAVE_FLINT
451#ifdef HAVE_NTL
452      if (degree (f) < 300)
453#endif
454      {
455        // use FLINT: char p, univariate
456        nmod_poly_t f1;
457        convertFacCF2nmod_poly_t (f1, f);
458        nmod_poly_factor_t result;
459        nmod_poly_factor_init (result);
460        mp_limb_t leadingCoeff= nmod_poly_factor (result, f1);
461        F= convertFLINTnmod_poly_factor2FacCFFList (result, leadingCoeff, f.mvar());
462        nmod_poly_factor_clear (result);
463        nmod_poly_clear (f1);
464        if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF);
465        return F;
466      }
467#endif
468#ifdef HAVE_NTL
469      { // NTL char 2, univariate
470        if (getCharacteristic()==2)
471        {
472          // Specialcase characteristic==2
473          if (fac_NTL_char != 2)
474          {
475            fac_NTL_char = 2;
476            zz_p::init(2);
477          }
478          // convert to NTL using the faster conversion routine for characteristic 2
479          GF2X f1=convertFacCF2NTLGF2X(f);
480          // no make monic necessary in GF2
481          //factorize
482          vec_pair_GF2X_long factors;
483          CanZass(factors,f1);
484
485          // convert back to factory again using the faster conversion routine for vectors over GF2X
486          F=convertNTLvec_pair_GF2X_long2FacCFFList(factors,LeadCoeff(f1),f.mvar());
487          if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF);
488          return F;
489        }
490      }
491#endif
492#ifdef HAVE_NTL
493      {
494        // use NTL char p, univariate
495        if (fac_NTL_char != getCharacteristic())
496        {
497          fac_NTL_char = getCharacteristic();
498          zz_p::init(getCharacteristic());
499        }
500
501        // convert to NTL
502        zz_pX f1=convertFacCF2NTLzzpX(f);
503        zz_p leadcoeff = LeadCoeff(f1);
504
505        //make monic
506        f1=f1 / LeadCoeff(f1);
507        // factorize
508        vec_pair_zz_pX_long factors;
509        CanZass(factors,f1);
510
511        F=convertNTLvec_pair_zzpX_long2FacCFFList(factors,leadcoeff,f.mvar());
512        //test_cff(F,f);
513        if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF);
514        return F;
515      }
516#endif
517#if !defined(HAVE_NTL) && !defined(HAVE_FLINT)
518      // Use Factory without NTL and without FLINT: char p, univariate
519      {
520        if ( isOn( SW_BERLEKAMP ) )
521          F=FpFactorizeUnivariateB( f, issqrfree );
522        else
523          F=FpFactorizeUnivariateCZ( f, issqrfree, 0, Variable(), Variable() );
524        return F;
525      }
526#endif
527    }
528    else // char p, multivariate
529    {
530      #if defined(HAVE_NTL)
531      if (issqrfree)
532      {
533        CFList factors;
534        Variable alpha;
535        if (CFFactory::gettype() == GaloisFieldDomain)
536          factors= GFSqrfFactorize (f);
537        else
538          factors= FpSqrfFactorize (f);
539        for (CFListIterator i= factors; i.hasItem(); i++)
540          F.append (CFFactor (i.getItem(), 1));
541      }
542      else
543      {
544        Variable alpha;
545        if (CFFactory::gettype() == GaloisFieldDomain)
546          F= GFFactorize (f);
547        else
548          F= FpFactorize (f);
549      }
550      #elif defined(HAVE_FLINT) && (__FLINT_RELEASE >= 20700)
551      #if 0
552      nmod_mpoly_ctx_t ctx;
553      nmod_mpoly_ctx_init(ctx,f.level(),ORD_LEX,getCharacteristic());
554      nmod_mpoly_t Flint_f;
555      nmod_mpoly_init(Flint_f,ctx);
556      convFactoryPFlintMP(f,Flint_f,ctx,f.level());
557      nmod_mpoly_factor_t factors;
558      nmod_mpoly_factor_init(factors,ctx);
559      if (issqrfree) nmod_mpoly_factor_squarefree(factors,Flint_f,ctx);
560      else           nmod_mpoly_factor(factors,Flint_f,ctx);
561      nmod_mpoly_t fac;
562      nmod_mpoly_init(fac,ctx);
563      CanonicalForm cf_fac;
564      int cf_exp;
565      cf_fac=nmod_mpoly_factor_get_constant_ui(factors,ctx);
566      F.append(CFFactor(cf_fac,1));
567      for(int i=nmod_mpoly_factor_length(factors,ctx)-1; i>=0; i--)
568      {
569         nmod_mpoly_factor_get_base(fac,factors,i,ctx);
570         cf_fac=convFlintMPFactoryP(fac,ctx,f.level());
571         cf_exp=nmod_mpoly_factor_get_exp_si(factors,i,ctx);
572         F.append(CFFactor(cf_fac,cf_exp));
573      }
574      nmod_mpoly_factor_clear(factors,ctx);
575      nmod_mpoly_clear(Flint_f,ctx);
576      nmod_mpoly_ctx_clear(ctx);
577      #endif
578      #else
579      factoryError ("multivariate factorization depends on NTL(missing)");
580      return CFFList (CFFactor (f, 1));
581      #endif
582    }
583  }
584  else // char 0
585  {
586    bool on_rational = isOn(SW_RATIONAL);
587    On(SW_RATIONAL);
588    CanonicalForm cd = bCommonDen( f );
589    CanonicalForm fz = f * cd;
590    Off(SW_RATIONAL);
591    if ( f.isUnivariate() )
592    {
593      CanonicalForm ic=icontent(fz);
594      fz/=ic;
595      if (fz.degree()==1)
596      {
597        F=CFFList(CFFactor(fz,1));
598        F.insert(CFFactor(ic,1));
599      }
600      else
601      #if defined(HAVE_FLINT) && (__FLINT_RELEASE>=20503)  && (__FLINT_RELEASE!= 20600)
602      {
603        // FLINT 2.6.0 has a bug:
604        // factorize x^12-13*x^10-13*x^8+13*x^4+13*x^2-1 runs forever
605        // use FLINT: char 0, univariate
606        fmpz_poly_t f1;
607        convertFacCF2Fmpz_poly_t (f1, fz);
608        fmpz_poly_factor_t result;
609        fmpz_poly_factor_init (result);
610        fmpz_poly_factor(result, f1);
611        F= convertFLINTfmpz_poly_factor2FacCFFList (result, fz.mvar());
612        fmpz_poly_factor_clear (result);
613        fmpz_poly_clear (f1);
614        if ( ! ic.isOne() )
615        {
616           // according to convertFLINTfmpz_polyfactor2FcaCFFlist,
617           //  first entry is in CoeffDomain
618          CFFactor new_first( F.getFirst().factor() * ic );
619          F.removeFirst();
620          F.insert( new_first );
621        }
622      }
623      goto end_char0;
624      #elif defined(HAVE_NTL)
625      {
626        //use NTL
627        ZZ c;
628        vec_pair_ZZX_long factors;
629        //factorize the converted polynomial
630        factor(c,factors,convertFacCF2NTLZZX(fz));
631
632        //convert the result back to Factory
633        F=convertNTLvec_pair_ZZX_long2FacCFFList(factors,c,fz.mvar());
634        if ( ! ic.isOne() )
635        {
636           // according to convertNTLvec_pair_ZZX_long2FacCFFList
637           //  first entry is in CoeffDomain
638          CFFactor new_first( F.getFirst().factor() * ic );
639          F.removeFirst();
640          F.insert( new_first );
641        }
642      }
643      goto end_char0;
644      #else
645      {
646        //Use Factory without NTL: char 0, univariate
647        F = ZFactorizeUnivariate( fz, issqrfree );
648        goto end_char0;
649      }
650      #endif
651    }
652    else // multivariate,  char 0
653    {
654      #if 0 //defined(HAVE_FLINT) && (__FLINT_RELEASE >= 20700)
655      On (SW_RATIONAL);
656      fmpq_mpoly_ctx_t ctx;
657      fmpq_mpoly_ctx_init(ctx,f.level(),ORD_LEX);
658      fmpq_mpoly_t Flint_f;
659      fmpq_mpoly_init(Flint_f,ctx);
660      convFactoryPFlintMP(fz,Flint_f,ctx,fz.level());
661      fmpq_mpoly_factor_t factors;
662      fmpq_mpoly_factor_init(factors,ctx);
663      int rr;
664      if (issqrfree) rr=fmpq_mpoly_factor_squarefree(factors,Flint_f,ctx);
665      else           rr=fmpq_mpoly_factor(factors,Flint_f,ctx);
666      if (rr==0) printf("fail\n");
667      fmpq_mpoly_t fac;
668      fmpq_mpoly_init(fac,ctx);
669      CanonicalForm cf_fac;
670      int cf_exp;
671      fmpq_t c;
672      fmpq_init(c);
673      fmpq_mpoly_factor_get_constant_fmpq(c,factors,ctx);
674      cf_fac=convertFmpq2CF(c);
675      fmpq_clear(c);
676      if (!cf_fac.isOne()) F.append(CFFactor(cf_fac,1));
677      for(int i=fmpq_mpoly_factor_length(factors,ctx)-1; i>=0; i--)
678      {
679         fmpq_mpoly_factor_get_base(fac,factors,i,ctx);
680         cf_fac=convFlintMPFactoryP(fac,ctx,f.level());
681         cf_exp=fmpq_mpoly_factor_get_exp_si(factors,i,ctx);
682         F.append(CFFactor(cf_fac,cf_exp));
683      }
684      fmpq_mpoly_factor_clear(factors,ctx);
685      fmpq_mpoly_clear(Flint_f,ctx);
686      fmpq_mpoly_ctx_clear(ctx);
687      #elif defined(HAVE_NTL)
688      On (SW_RATIONAL);
689      if (issqrfree)
690      {
691        CFList factors= ratSqrfFactorize (fz);
692        for (CFListIterator i= factors; i.hasItem(); i++)
693          F.append (CFFactor (i.getItem(), 1));
694      }
695      else
696      {
697        F = ratFactorize (fz);
698      }
699      #else
700      F=ZFactorizeMultivariate(fz, issqrfree);
701      #endif
702    }
703
704end_char0:
705    if ( on_rational )
706      On(SW_RATIONAL);
707    else
708      Off(SW_RATIONAL);
709    if ( ! cd.isOne() )
710    {
711      CFFactor new_first( F.getFirst().factor() / cd );
712      F.removeFirst();
713      F.insert( new_first );
714    }
715  }
716
717  if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF);
718  return F;
719}
720
721/**
722 * factorization over \f$ F_p(\alpha) \f$ or \f$ Q(\alpha) \f$
723**/
724CFFList factorize ( const CanonicalForm & f, const Variable & alpha )
725{
726  if ( f.inCoeffDomain() )
727    return CFFList( f );
728  //out_cf("factorize:",f,"==================================\n");
729  //out_cf("mipo:",getMipo(alpha),"\n");
730
731  CFFList F;
732  ASSERT( alpha.level() < 0 && getReduce (alpha), "not an algebraic extension" );
733#ifndef NOASSERT
734  Variable beta;
735  if (hasFirstAlgVar(f, beta))
736    ASSERT (beta == alpha, "f has an algebraic variable that \
737                            does not coincide with alpha");
738#endif
739  int ch=getCharacteristic();
740  if (ch>0)
741  {
742    if (f.isUnivariate())
743    {
744#ifdef HAVE_NTL
745      if (/*getCharacteristic()*/ch==2)
746      {
747        // special case : GF2
748
749        // remainder is two ==> nothing to do
750
751        // set minimal polynomial in NTL using the optimized conversion routines for characteristic 2
752        GF2X minPo=convertFacCF2NTLGF2X(getMipo(alpha,f.mvar()));
753        GF2E::init (minPo);
754
755        // convert to NTL again using the faster conversion routines
756        GF2EX f1;
757        if (isPurePoly(f))
758        {
759          GF2X f_tmp=convertFacCF2NTLGF2X(f);
760          f1=to_GF2EX(f_tmp);
761        }
762        else
763          f1=convertFacCF2NTLGF2EX(f,minPo);
764
765        // make monic (in Z/2(a))
766        GF2E f1_coef=LeadCoeff(f1);
767        MakeMonic(f1);
768
769        // factorize using NTL
770        vec_pair_GF2EX_long factors;
771        CanZass(factors,f1);
772
773        // return converted result
774        F=convertNTLvec_pair_GF2EX_long2FacCFFList(factors,f1_coef,f.mvar(),alpha);
775        if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF);
776        return F;
777      }
778#endif
779#if (HAVE_FLINT && __FLINT_RELEASE >= 20400)
780      {
781        // use FLINT
782        nmod_poly_t FLINTmipo, leadingCoeff;
783        fq_nmod_ctx_t fq_con;
784
785        nmod_poly_init (FLINTmipo, ch);
786        nmod_poly_init (leadingCoeff, ch);
787        convertFacCF2nmod_poly_t (FLINTmipo, getMipo (alpha));
788
789        fq_nmod_ctx_init_modulus (fq_con, FLINTmipo, "Z");
790        fq_nmod_poly_t FLINTF;
791        convertFacCF2Fq_nmod_poly_t (FLINTF, f, fq_con);
792        fq_nmod_poly_factor_t res;
793        fq_nmod_poly_factor_init (res, fq_con);
794        fq_nmod_poly_factor (res, leadingCoeff, FLINTF, fq_con);
795        F= convertFLINTFq_nmod_poly_factor2FacCFFList (res, f.mvar(), alpha, fq_con);
796        F.insert (CFFactor (Lc (f), 1));
797
798        fq_nmod_poly_factor_clear (res, fq_con);
799        fq_nmod_poly_clear (FLINTF, fq_con);
800        nmod_poly_clear (FLINTmipo);
801        nmod_poly_clear (leadingCoeff);
802        fq_nmod_ctx_clear (fq_con);
803        if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF);
804        return F;
805      }
806#endif
807#ifdef HAVE_NTL
808      {
809        // use NTL
810        if (fac_NTL_char != ch)
811        {
812          fac_NTL_char = ch;
813          zz_p::init(ch);
814        }
815
816        // set minimal polynomial in NTL
817        zz_pX minPo=convertFacCF2NTLzzpX(getMipo(alpha));
818        zz_pE::init (minPo);
819
820        // convert to NTL
821        zz_pEX f1=convertFacCF2NTLzz_pEX(f,minPo);
822        zz_pE leadcoeff= LeadCoeff(f1);
823
824        //make monic
825        f1=f1 / leadcoeff; //leadcoeff==LeadCoeff(f1);
826
827        // factorize
828        vec_pair_zz_pEX_long factors;
829        CanZass(factors,f1);
830
831        // return converted result
832        F=convertNTLvec_pair_zzpEX_long2FacCFFList(factors,leadcoeff,f.mvar(),alpha);
833        //test_cff(F,f);
834        if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF);
835        return F;
836      }
837#endif
838#if !defined(HAVE_NTL) && !defined(HAVE_FLINT)
839      // char p, extension, univariate
840      CanonicalForm c=Lc(f);
841      CanonicalForm fc=f/c;
842      F=FpFactorizeUnivariateCZ( fc, false, 1, alpha, Variable() );
843      F.insert (CFFactor (c, 1));
844#endif
845    }
846    else // char p, multivariate
847    {
848      #ifdef HAVE_NTL
849      F= FqFactorize (f, alpha);
850      #else
851      factoryError ("univariate factorization  depends on NTL(missing)");
852      return CFFList (CFFactor (f, 1));
853      #endif
854    }
855  }
856  else // Q(a)[x]
857  {
858    if (f.isUnivariate())
859    {
860      F= AlgExtFactorize (f, alpha);
861    }
862    else //Q(a)[x1,...,xn]
863    {
864      #ifdef HAVE_NTL
865      F= ratFactorize (f, alpha);
866      #else
867      factoryError ("multivariate factorization  depends on NTL(missing)");
868      return CFFList (CFFactor (f, 1));
869      #endif
870    }
871  }
872  if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF);
873  return F;
874}
875
876/**
877 * squarefree factorization
878**/
879CFFList sqrFree ( const CanonicalForm & f, bool sort )
880{
881//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
882    CFFList result;
883
884    if ( getCharacteristic() == 0 )
885        result = sqrFreeZ( f );
886    else
887    {
888        Variable alpha;
889        if (hasFirstAlgVar (f, alpha))
890          result = FqSqrf( f, alpha );
891        else
892          result= FpSqrf (f);
893    }
894    if (sort)
895    {
896      CFFactor buf= result.getFirst();
897      result.removeFirst();
898      result= sortCFFList (result);
899      result.insert (buf);
900    }
901    return result;
902}
903
Note: See TracBrowser for help on using the repository browser.