source: git/factory/cf_factor.cc @ 6f62c3

spielwiese
Last change on this file since 6f62c3 was 6f62c3, checked in by Hans Schönemann <hannes@…>, 17 years ago
*hannes: chin.remainder/farey for gcd in char 0 git-svn-id: file:///usr/local/Singular/svn/trunk@10310 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.0 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: cf_factor.cc,v 1.34 2007-09-26 09:17:39 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#include "cf_map.h"
30
31#include "int_int.h"
32#ifdef HAVE_NTL
33#include "NTLconvert.h"
34#endif
35
36int getExp(); /* cf_char.cc */
37
38static bool isUnivariateBaseDomain( const CanonicalForm & f )
39{
40    CFIterator i = f;
41    bool ok = i.coeff().inBaseDomain();
42    i++;
43    while ( i.hasTerms() && ( ok = ok && i.coeff().inBaseDomain() ) ) i++;
44    return ok;
45}
46
47void find_exp(const CanonicalForm & f, int * exp_f)
48{
49  if ( ! f.inCoeffDomain() )
50  {
51    int e=f.level();
52    CFIterator i = f;
53    if (e>=0)
54    {
55      if (i.exp() > exp_f[e]) exp_f[e]=i.exp();
56    }
57    for (; i.hasTerms(); i++ )
58    {
59      find_exp(i.coeff(), exp_f);
60    }
61  }
62}
63
64int find_mvar(const CanonicalForm & f)
65{
66  int mv=f.level();
67  int *exp_f=new int[mv+1];
68  int i;
69  for(i=mv;i>0;i--) exp_f[i]=0;
70  find_exp(f,exp_f);
71  for(i=mv;i>0;i--)
72  {
73    if ((exp_f[i]>0) && (exp_f[i]<exp_f[mv]))
74    {
75      mv=i;
76    }
77  }
78  delete[] exp_f;
79  return mv;
80}
81
82#if 1
83//#ifndef NOSTREAMIO
84void out_cf(char *s1,const CanonicalForm &f,char *s2)
85{
86  printf("%s",s1);
87  if (f==0) printf("+0");
88  //else if (! f.inCoeffDomain() )
89  else if (! f.inBaseDomain() )
90  {
91    int l = f.level();
92    for ( CFIterator i = f; i.hasTerms(); i++ )
93    {
94      int e=i.exp();
95      if (i.coeff().isOne())
96      {
97        printf("+");
98        if (e==0) printf("1");
99        else
100        {
101          printf("v(%d)",l);
102          if (e!=1) printf("^%d",e);
103        }
104      }
105      else
106      {
107        out_cf("+(",i.coeff(),")");
108        if (e!=0)
109        {
110          printf("*v(%d)",l);
111          if (e!=1) printf("^%d",e);
112        }
113      }
114    }
115  }
116  else
117  {
118    if ( f.isImm() )
119    {
120      printf("+%d",f.intval());
121    }
122    else 
123    #ifdef NOSTREAMIO
124      printf("+...");
125    #else
126       std::cout << f;
127    #endif
128    //if (f.inZ()) printf("(Z)");
129    //else if (f.inQ()) printf("(Q)");
130    //else if (f.inFF()) printf("(FF)");
131    //else if (f.inPP()) printf("(PP)");
132    //else if (f.inGF()) printf("(PP)");
133    //else
134    if (f.inExtension()) printf("E(%d)",f.level());
135  }
136  printf("%s",s2);
137}
138void out_cff(CFFList &L)
139{
140  int n = L.length();
141  CFFListIterator J=L;
142  int j=0;
143  for ( ; J.hasItem(); J++, j++ )
144  {
145    printf("F%d",j);out_cf(":",J.getItem().factor()," ^ ");
146    printf("%d\n", J.getItem().exp());
147  }
148}
149void test_cff(CFFList &L,const CanonicalForm & f)
150{
151  int n = L.length();
152  CFFListIterator J=L;
153  CanonicalForm t=1;
154  int j=0;
155  if (!(L.getFirst().factor().inCoeffDomain()))
156    printf("first entry is not const\n");
157  for ( ; J.hasItem(); J++, j++ )
158  {
159    CanonicalForm tt=J.getItem().factor();
160    if (tt.inCoeffDomain() && (j!=0))
161      printf("other entry is const\n");
162    j=J.getItem().exp();
163    while(j>0) { t*=tt; j--; }
164  }
165  if ((f-t)!=0) { printf("problem:\n");out_cf("factor:",f," has problems\n");}
166}
167//#endif
168#endif
169
170bool isPurePoly_m(const CanonicalForm & f)
171{
172  if (f.inBaseDomain()) return true;
173  if (f.level()<0) return false;
174  for (CFIterator i=f;i.hasTerms();i++)
175  {
176    if (!isPurePoly_m(i.coeff())) return false;
177  }
178  return true;
179}
180bool isPurePoly(const CanonicalForm & f)
181{
182  if (f.level()<=0) return false;
183  for (CFIterator i=f;i.hasTerms();i++)
184  {
185    if (!(i.coeff().inBaseDomain())) return false;
186  }
187  return true;
188}
189
190
191///////////////////////////////////////////////////////////////
192// get_max_degree_Variable returns Variable with             //
193// highest degree. We assume f is *not* a constant!          //
194///////////////////////////////////////////////////////////////
195Variable
196get_max_degree_Variable(const CanonicalForm & f)
197{
198  ASSERT( ( ! f.inCoeffDomain() ), "no constants" );
199  int max=0, maxlevel=0, n=level(f);
200  for ( int i=1; i<=n; i++ )
201  {
202    if (degree(f,Variable(i)) >= max)
203    {
204      max= degree(f,Variable(i)); maxlevel= i;
205    }
206  }
207  return Variable(maxlevel);
208}
209
210///////////////////////////////////////////////////////////////
211// get_Terms: Split the polynomial in the containing terms.  //
212// getTerms: the real work is done here.                     //
213///////////////////////////////////////////////////////////////
214void
215getTerms( const CanonicalForm & f, const CanonicalForm & t, CFList & result )
216{
217  if ( getNumVars(f) == 0 ) result.append(f*t);
218  else{
219    Variable x(level(f));
220    for ( CFIterator i=f; i.hasTerms(); i++ )
221      getTerms( i.coeff(), t*power(x,i.exp()), result);
222  }
223}
224CFList
225get_Terms( const CanonicalForm & f ){
226  CFList result,dummy,dummy2;
227  CFIterator i;
228  CFListIterator j;
229
230  if ( getNumVars(f) == 0 ) result.append(f);
231  else{
232    Variable _x(level(f));
233    for ( i=f; i.hasTerms(); i++ ){
234      getTerms(i.coeff(), 1, dummy);
235      for ( j=dummy; j.hasItem(); j++ )
236        result.append(j.getItem() * power(_x, i.exp()));
237
238      dummy= dummy2; // have to initalize new
239    }
240  }
241  return result;
242}
243
244
245///////////////////////////////////////////////////////////////
246// homogenize homogenizes f with Variable x                  //
247///////////////////////////////////////////////////////////////
248
249CanonicalForm
250homogenize( const CanonicalForm & f, const Variable & x)
251{
252#if 0
253  int maxdeg=totaldegree(f), deg;
254  CFIterator i;
255  CanonicalForm elem, result(0);
256 
257  for (i=f; i.hasTerms(); i++)
258  {
259    elem= i.coeff()*power(f.mvar(),i.exp());
260    deg = totaldegree(elem);
261    if ( deg < maxdeg )
262      result += elem * power(x,maxdeg-deg);
263    else
264      result+=elem;
265  }
266  return result;
267#else
268  CFList Newlist, Termlist= get_Terms(f);
269  int maxdeg=totaldegree(f), deg;
270  CFListIterator i;
271  CanonicalForm elem, result(0);
272
273  for (i=Termlist; i.hasItem(); i++){
274    elem= i.getItem();
275    deg = totaldegree(elem);
276    if ( deg < maxdeg )
277      Newlist.append(elem * power(x,maxdeg-deg));
278    else
279      Newlist.append(elem);
280  }
281  for (i=Newlist; i.hasItem(); i++) // rebuild
282    result += i.getItem();
283
284  return result;
285#endif
286}
287
288#ifdef SINGULAR
289extern int singular_homog_flag;
290#else
291#define singular_homog_flag 1
292#endif
293int cmpCF( const CFFactor & f, const CFFactor & g )
294{
295  if (f.exp() > g.exp()) return 1;
296  if (f.exp() < g.exp()) return 0;
297  if (f.factor() > g.factor()) return 1;
298  return 0;
299}
300
301CFFList factorize ( const CanonicalForm & f, bool issqrfree )
302{
303  if ( f.inCoeffDomain() )
304        return CFFList( f );
305  int mv=f.level();
306  int org_v=mv;
307  //out_cf("factorize:",f,"==================================\n");
308  if (! f.isUnivariate() )
309  {
310    if ( singular_homog_flag && f.isHomogeneous())
311    {
312      Variable xn = get_max_degree_Variable(f);
313      int d_xn = degree(f,xn);
314      CFMap n;
315      CanonicalForm F = compress(f(1,xn),n);
316      CFFList Intermediatelist;
317      Intermediatelist = factorize(F);
318      CFFList Homoglist;
319      CFFListIterator j;
320      for ( j=Intermediatelist; j.hasItem(); j++ )
321      {
322        Homoglist.append(
323            CFFactor( n(j.getItem().factor()), j.getItem().exp()) );
324      }
325      CFFList Unhomoglist;
326      CanonicalForm unhomogelem;
327      for ( j=Homoglist; j.hasItem(); j++ )
328      {
329        unhomogelem= homogenize(j.getItem().factor(),xn);
330        Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp()));
331        d_xn -= (degree(unhomogelem,xn)*j.getItem().exp());
332      }
333      if ( d_xn != 0 ) // have to append xn^(d_xn)
334        Unhomoglist.append(CFFactor(CanonicalForm(xn),d_xn)); 
335      if(isOn(SW_USE_NTL_SORT)) Unhomoglist.sort(cmpCF); 
336      return Unhomoglist;
337    }
338    mv=find_mvar(f);
339    if ( getCharacteristic() == 0 )
340    {
341      if (mv!=f.level())
342      {
343        swapvar(f,Variable(mv),f.mvar());
344      }
345    }
346    else
347    {
348      if (mv!=1)
349      {
350        swapvar(f,Variable(mv),Variable(1));
351        org_v=1;
352      }
353    }
354  }
355  CFFList F;
356  if ( getCharacteristic() > 0 )
357  {
358    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
359    #ifdef HAVE_NTL
360    if (isOn(SW_USE_NTL) && (isPurePoly(f)))
361    {
362      // USE NTL
363      if (getCharacteristic()!=2)
364      {
365        // set remainder
366        if (fac_NTL_char!=getCharacteristic())
367        {
368          fac_NTL_char=getCharacteristic();
369          #ifdef NTL_ZZ
370          ZZ r;
371          r=getCharacteristic();
372          ZZ_pContext ccc(r);
373          #else
374          zz_pContext ccc(getCharacteristic());
375          #endif
376          ccc.restore();
377          #ifdef NTL_ZZ
378          ZZ_p::init(r);
379          #else
380          zz_p::init(getCharacteristic());
381          #endif
382        }
383        // convert to NTL
384        #ifdef NTL_ZZ
385        ZZ_pX f1=convertFacCF2NTLZZpX(f);
386        ZZ_p leadcoeff = LeadCoeff(f1);
387        #else
388        zz_pX f1=convertFacCF2NTLzzpX(f);
389        zz_p leadcoeff = LeadCoeff(f1);
390        #endif
391        //make monic
392        f1=f1 / LeadCoeff(f1);
393
394        // factorize
395        #ifdef NTL_ZZ
396        vec_pair_ZZ_pX_long factors;
397        #else
398        vec_pair_zz_pX_long factors;
399        #endif
400        CanZass(factors,f1);
401
402        // convert back to factory
403        #ifdef NTL_ZZ
404        F=convertNTLvec_pair_ZZpX_long2FacCFFList(factors,leadcoeff,f.mvar());
405        #else
406        F=convertNTLvec_pair_zzpX_long2FacCFFList(factors,leadcoeff,f.mvar());
407        #endif
408        //test_cff(F,f);
409      }
410      else
411      {
412        // Specialcase characteristic==2
413        if (fac_NTL_char!=2)
414        {
415          fac_NTL_char=2;
416          zz_p::init(2);
417        }
418        // convert to NTL using the faster conversion routine for characteristic 2
419        GF2X f1=convertFacCF2NTLGF2X(f);
420        // no make monic necessary in GF2
421        //factorize
422        vec_pair_GF2X_long factors;
423        CanZass(factors,f1);
424
425        // convert back to factory again using the faster conversion routine for vectors over GF2X
426        F=convertNTLvec_pair_GF2X_long2FacCFFList(factors,LeadCoeff(f1),f.mvar());
427      }
428    }
429    else
430    #endif
431    {  // Use Factory without NTL
432      if ( isOn( SW_BERLEKAMP ) )
433         F=FpFactorizeUnivariateB( f, issqrfree );
434      else
435        F=FpFactorizeUnivariateCZ( f, issqrfree, 0, Variable(), Variable() );
436    }
437  }
438  else
439  {
440    bool on_rational = isOn(SW_RATIONAL);
441    On(SW_RATIONAL);
442    CanonicalForm cd = bCommonDen( f );
443    CanonicalForm fz = f * cd;
444    Off(SW_RATIONAL);
445    if ( f.isUnivariate() )
446    {
447      #ifdef HAVE_NTL
448      if ((isOn(SW_USE_NTL)) && (isPurePoly(f)))
449      {
450        //USE NTL
451        CanonicalForm ic=icontent(fz);
452        fz/=ic;
453        ZZ c;
454        vec_pair_ZZX_long factors;
455        //factorize the converted polynomial
456        factor(c,factors,convertFacCF2NTLZZX(fz));
457
458        //convert the result back to Factory
459        F=convertNTLvec_pair_ZZX_long2FacCFFList(factors,c,fz.mvar());
460        if ( ! ic.isOne() )
461        {
462          if ( F.getFirst().factor().inCoeffDomain() )
463          {
464            CFFactor new_first( F.getFirst().factor() * ic );
465            F.removeFirst();
466            F.insert( new_first );
467          }
468          else
469            F.insert( CFFactor( ic ) );
470        }
471        else
472        {
473          if ( !F.getFirst().factor().inCoeffDomain() )
474          {
475            CFFactor new_first( 1 );
476            F.insert( new_first );
477          }
478        }
479        //if ( F.getFirst().factor().isOne() )
480        //{
481        //  F.removeFirst();
482        //}
483        //printf("NTL:\n");out_cff(F);
484        //F=ZFactorizeUnivariate( fz, issqrfree );
485        //printf("fac.:\n");out_cff(F);
486      }
487      else
488      #endif
489      {
490        //Use Factory without NTL
491        F = ZFactorizeUnivariate( fz, issqrfree );
492      }
493    }
494    else
495    {
496      F = ZFactorizeMultivariate( fz, issqrfree );
497    }
498
499    if ( on_rational )
500      On(SW_RATIONAL);
501    if ( ! cd.isOne() )
502    {
503      if ( F.getFirst().factor().inCoeffDomain() )
504      {
505        CFFactor new_first( F.getFirst().factor() / cd );
506        F.removeFirst();
507        F.insert( new_first );
508      }
509      else
510      {
511        F.insert( CFFactor( 1/cd ) );
512      }
513    }
514  }
515
516  if ((mv!=org_v) && (! f.isUnivariate() ))
517  {
518    CFFListIterator J=F;
519    for ( ; J.hasItem(); J++)
520    {
521      swapvar(J.getItem().factor(),Variable(mv),Variable(org_v));
522    }
523    swapvar(f,Variable(mv),Variable(org_v));
524  }
525  //out_cff(F);
526  if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF); 
527  return F;
528}
529
530#ifdef HAVE_NTL
531CanonicalForm fntl ( const CanonicalForm & f, int j )
532{
533  ZZX f1=convertFacCF2NTLZZX(f);
534  return convertZZ2CF(coeff(f1,j));
535}
536#endif
537
538CFFList factorize ( const CanonicalForm & f, const Variable & alpha )
539{
540  //out_cf("factorize:",f,"==================================\n");
541  //out_cf("mipo:",getMipo(alpha),"\n");
542  CFFList F;
543  ASSERT( alpha.level() < 0, "not an algebraic extension" );
544  ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
545  ASSERT( getCharacteristic() > 0, "char 0 factorization not implemented" );
546  #ifdef HAVE_NTL
547  if  (isOn(SW_USE_NTL))
548  {
549    //USE NTL
550    if (getCharacteristic()!=2)
551    {
552      // First all cases with characteristic !=2
553      // set remainder
554      if (fac_NTL_char!=getCharacteristic())
555      {
556        fac_NTL_char=getCharacteristic();
557        #ifdef NTL_ZZ
558        ZZ r;
559        r=getCharacteristic();
560        ZZ_pContext ccc(r);
561        #else
562        zz_pContext ccc(getCharacteristic());
563        #endif
564        ccc.restore();
565        #ifdef NTL_ZZ
566        ZZ_p::init(r);
567        #else
568        zz_p::init(getCharacteristic());
569        #endif
570      }
571
572      // set minimal polynomial in NTL
573      #ifdef NTL_ZZ
574      ZZ_pX minPo=convertFacCF2NTLZZpX(getMipo(alpha));
575      ZZ_pEContext c(minPo);
576      #else
577      zz_pX minPo=convertFacCF2NTLzzpX(getMipo(alpha));
578      zz_pEContext c(minPo);
579      #endif
580
581      c.restore();
582
583      // convert to NTL
584      #ifdef NTL_ZZ
585      ZZ_pEX f1=convertFacCF2NTLZZ_pEX(f,minPo);
586      ZZ_pE leadcoeff= LeadCoeff(f1);
587      #else
588      zz_pEX f1=convertFacCF2NTLzz_pEX(f,minPo);
589      zz_pE leadcoeff= LeadCoeff(f1);
590      #endif
591
592      //make monic
593      f1=f1 / leadcoeff;
594
595      // factorize using NTL
596      #ifdef NTL_ZZ
597      vec_pair_ZZ_pEX_long factors;
598      #else
599      vec_pair_zz_pEX_long factors;
600      #endif
601      CanZass(factors,f1);
602
603      // return converted result
604      F=convertNTLvec_pair_zzpEX_long2FacCFFList(factors,leadcoeff,f.mvar(),alpha);
605    }
606    else
607    {
608      // special case : GF2
609
610      // remainder is two ==> nothing to do
611      // set remainder
612      ZZ r;
613      r=getCharacteristic();
614      ZZ_pContext ccc(r);
615      ccc.restore();
616
617      // set minimal polynomial in NTL using the optimized conversion routines for characteristic 2
618      GF2X minPo=convertFacCF2NTLGF2X(getMipo(alpha,f.mvar()));
619      GF2EContext c(minPo);
620      c.restore();
621
622      // convert to NTL again using the faster conversion routines
623      GF2EX f1;
624      if (isPurePoly(f))
625      {
626        GF2X f_tmp=convertFacCF2NTLGF2X(f);
627        f1=to_GF2EX(f_tmp);
628      }
629      else
630      {
631        f1=convertFacCF2NTLGF2EX(f,minPo);
632      }
633
634      // make monic (in Z/2(a))
635      GF2E f1_coef=LeadCoeff(f1);
636      MakeMonic(f1);
637
638      // factorize using NTL
639      vec_pair_GF2EX_long factors;
640      CanZass(factors,f1);
641
642      // return converted result
643      F=convertNTLvec_pair_GF2EX_long2FacCFFList(factors,f1_coef,f.mvar(),alpha);
644    }
645
646  }
647  else
648  #endif
649  {
650    F=FpFactorizeUnivariateCZ( f, false, 1, alpha, Variable() );
651  }
652  return F;
653}
654
655CFFList sqrFree ( const CanonicalForm & f, bool sort )
656{
657//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
658    CFFList result;
659
660    if ( getCharacteristic() == 0 )
661        result = sqrFreeZ( f );
662    else
663        result = sqrFreeFp( f );
664
665    return ( sort ? sortCFFList( result ) : result );
666}
667
668bool isSqrFree ( const CanonicalForm & f )
669{
670//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
671    if ( getCharacteristic() == 0 )
672        return isSqrFreeZ( f );
673    else
674        return isSqrFreeFp( f );
675}
676
Note: See TracBrowser for help on using the repository browser.