source: git/factory/cf_factor.cc @ e89e56

spielwiese
Last change on this file since e89e56 was e89e56, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: fact.tst git-svn-id: file:///usr/local/Singular/svn/trunk@10621 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.8 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: cf_factor.cc,v 1.40 2008-03-17 17:44:04 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.isZero()) 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  {
275    elem= i.getItem();
276    deg = totaldegree(elem);
277    if ( deg < maxdeg )
278      Newlist.append(elem * power(x,maxdeg-deg));
279    else
280      Newlist.append(elem);
281  }
282  for (i=Newlist; i.hasItem(); i++) // rebuild
283    result += i.getItem();
284
285  return result;
286#endif
287}
288
289CanonicalForm
290homogenize( const CanonicalForm & f, const Variable & x, const Variable & v1, const Variable & v2)
291{
292#if 0
293  int maxdeg=totaldegree(f), deg;
294  CFIterator i;
295  CanonicalForm elem, result(0);
296 
297  for (i=f; i.hasTerms(); i++)
298  {
299    elem= i.coeff()*power(f.mvar(),i.exp());
300    deg = totaldegree(elem);
301    if ( deg < maxdeg )
302      result += elem * power(x,maxdeg-deg);
303    else
304      result+=elem;
305  }
306  return result;
307#else
308  CFList Newlist, Termlist= get_Terms(f);
309  int maxdeg=totaldegree(f), deg;
310  CFListIterator i;
311  CanonicalForm elem, result(0);
312
313  for (i=Termlist; i.hasItem(); i++)
314  {
315    elem= i.getItem();
316    deg = totaldegree(elem,v1,v2);
317    if ( deg < maxdeg )
318      Newlist.append(elem * power(x,maxdeg-deg));
319    else
320      Newlist.append(elem);
321  }
322  for (i=Newlist; i.hasItem(); i++) // rebuild
323    result += i.getItem();
324
325  return result;
326#endif
327}
328
329#ifdef SINGULAR
330extern int singular_homog_flag;
331#else
332#define singular_homog_flag 1
333#endif
334int cmpCF( const CFFactor & f, const CFFactor & g )
335{
336  if (f.exp() > g.exp()) return 1;
337  if (f.exp() < g.exp()) return 0;
338  if (f.factor() > g.factor()) return 1;
339  return 0;
340}
341
342CFFList factorize ( const CanonicalForm & f, bool issqrfree )
343{
344  if ( f.inCoeffDomain() )
345        return CFFList( f );
346  int mv=f.level();
347  int org_v=mv;
348  //out_cf("factorize:",f,"==================================\n");
349  if (! f.isUnivariate() )
350  {
351    if ( singular_homog_flag && f.isHomogeneous())
352    {
353      Variable xn = get_max_degree_Variable(f);
354      int d_xn = degree(f,xn);
355      CFMap n;
356      CanonicalForm F = compress(f(1,xn),n);
357      CFFList Intermediatelist;
358      Intermediatelist = factorize(F);
359      CFFList Homoglist;
360      CFFListIterator j;
361      for ( j=Intermediatelist; j.hasItem(); j++ )
362      {
363        Homoglist.append(
364            CFFactor( n(j.getItem().factor()), j.getItem().exp()) );
365      }
366      CFFList Unhomoglist;
367      CanonicalForm unhomogelem;
368      for ( j=Homoglist; j.hasItem(); j++ )
369      {
370        unhomogelem= homogenize(j.getItem().factor(),xn);
371        Unhomoglist.append(CFFactor(unhomogelem,j.getItem().exp()));
372        d_xn -= (degree(unhomogelem,xn)*j.getItem().exp());
373      }
374      if ( d_xn != 0 ) // have to append xn^(d_xn)
375        Unhomoglist.append(CFFactor(CanonicalForm(xn),d_xn)); 
376      if(isOn(SW_USE_NTL_SORT)) Unhomoglist.sort(cmpCF); 
377      return Unhomoglist;
378    }
379    mv=find_mvar(f);
380    if ( getCharacteristic() == 0 )
381    {
382      if (mv!=f.level())
383      {
384        swapvar(f,Variable(mv),f.mvar());
385      }
386    }
387    else
388    {
389      if (mv!=1)
390      {
391        swapvar(f,Variable(mv),Variable(1));
392        org_v=1;
393      }
394    }
395  }
396  CFFList F;
397  if ( getCharacteristic() > 0 )
398  {
399    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
400    #ifdef HAVE_NTL
401    if (isOn(SW_USE_NTL) && (isPurePoly(f)))
402    {
403      // USE NTL
404      if (getCharacteristic()!=2)
405      {
406        // set remainder
407        if (fac_NTL_char!=getCharacteristic())
408        {
409          fac_NTL_char=getCharacteristic();
410          #ifdef NTL_ZZ
411          ZZ r;
412          r=getCharacteristic();
413          ZZ_pContext ccc(r);
414          #else
415          zz_pContext ccc(getCharacteristic());
416          #endif
417          ccc.restore();
418          #ifdef NTL_ZZ
419          ZZ_p::init(r);
420          #else
421          zz_p::init(getCharacteristic());
422          #endif
423        }
424        // convert to NTL
425        #ifdef NTL_ZZ
426        ZZ_pX f1=convertFacCF2NTLZZpX(f);
427        ZZ_p leadcoeff = LeadCoeff(f1);
428        #else
429        zz_pX f1=convertFacCF2NTLzzpX(f);
430        zz_p leadcoeff = LeadCoeff(f1);
431        #endif
432        //make monic
433        f1=f1 / LeadCoeff(f1);
434
435        // factorize
436        #ifdef NTL_ZZ
437        vec_pair_ZZ_pX_long factors;
438        #else
439        vec_pair_zz_pX_long factors;
440        #endif
441        CanZass(factors,f1);
442
443        // convert back to factory
444        #ifdef NTL_ZZ
445        F=convertNTLvec_pair_ZZpX_long2FacCFFList(factors,leadcoeff,f.mvar());
446        #else
447        F=convertNTLvec_pair_zzpX_long2FacCFFList(factors,leadcoeff,f.mvar());
448        #endif
449        //test_cff(F,f);
450      }
451      else
452      {
453        // Specialcase characteristic==2
454        if (fac_NTL_char!=2)
455        {
456          fac_NTL_char=2;
457          zz_p::init(2);
458        }
459        // convert to NTL using the faster conversion routine for characteristic 2
460        GF2X f1=convertFacCF2NTLGF2X(f);
461        // no make monic necessary in GF2
462        //factorize
463        vec_pair_GF2X_long factors;
464        CanZass(factors,f1);
465
466        // convert back to factory again using the faster conversion routine for vectors over GF2X
467        F=convertNTLvec_pair_GF2X_long2FacCFFList(factors,LeadCoeff(f1),f.mvar());
468      }
469    }
470    else
471    #endif
472    {  // Use Factory without NTL
473      if ( isOn( SW_BERLEKAMP ) )
474         F=FpFactorizeUnivariateB( f, issqrfree );
475      else
476        F=FpFactorizeUnivariateCZ( f, issqrfree, 0, Variable(), Variable() );
477    }
478  }
479  else
480  {
481    bool on_rational = isOn(SW_RATIONAL);
482    On(SW_RATIONAL);
483    CanonicalForm cd = bCommonDen( f );
484    CanonicalForm fz = f * cd;
485    Off(SW_RATIONAL);
486    if ( f.isUnivariate() )
487    {
488      #ifdef HAVE_NTL
489      if ((isOn(SW_USE_NTL)) && (isPurePoly(f)))
490      {
491        //USE NTL
492        CanonicalForm ic=icontent(fz);
493        fz/=ic;
494        ZZ c;
495        vec_pair_ZZX_long factors;
496        //factorize the converted polynomial
497        factor(c,factors,convertFacCF2NTLZZX(fz));
498
499        //convert the result back to Factory
500        F=convertNTLvec_pair_ZZX_long2FacCFFList(factors,c,fz.mvar());
501        if ( ! ic.isOne() )
502        {
503          if ( F.getFirst().factor().inCoeffDomain() )
504          {
505            CFFactor new_first( F.getFirst().factor() * ic );
506            F.removeFirst();
507            F.insert( new_first );
508          }
509          else
510            F.insert( CFFactor( ic ) );
511        }
512        else
513        {
514          if ( !F.getFirst().factor().inCoeffDomain() )
515          {
516            CFFactor new_first( 1 );
517            F.insert( new_first );
518          }
519        }
520        //if ( F.getFirst().factor().isOne() )
521        //{
522        //  F.removeFirst();
523        //}
524        //printf("NTL:\n");out_cff(F);
525        //F=ZFactorizeUnivariate( fz, issqrfree );
526        //printf("fac.:\n");out_cff(F);
527      }
528      else
529      #endif
530      {
531        //Use Factory without NTL
532        F = ZFactorizeUnivariate( fz, issqrfree );
533      }
534    }
535    else
536    {
537      F = ZFactorizeMultivariate( fz, issqrfree );
538    }
539
540    if ( on_rational )
541      On(SW_RATIONAL);
542    if ( ! cd.isOne() )
543    {
544      if ( F.getFirst().factor().inCoeffDomain() )
545      {
546        CFFactor new_first( F.getFirst().factor() / cd );
547        F.removeFirst();
548        F.insert( new_first );
549      }
550      else
551      {
552        F.insert( CFFactor( 1/cd ) );
553      }
554    }
555  }
556
557  if ((mv!=org_v) && (! f.isUnivariate() ))
558  {
559    CFFListIterator J=F;
560    for ( ; J.hasItem(); J++)
561    {
562      swapvar(J.getItem().factor(),Variable(mv),Variable(org_v));
563    }
564    swapvar(f,Variable(mv),Variable(org_v));
565  }
566  //out_cff(F);
567  if(isOn(SW_USE_NTL_SORT)) F.sort(cmpCF); 
568  return F;
569}
570
571#ifdef HAVE_NTL
572CanonicalForm fntl ( const CanonicalForm & f, int j )
573{
574  ZZX f1=convertFacCF2NTLZZX(f);
575  return convertZZ2CF(coeff(f1,j));
576}
577#endif
578
579CFFList factorize ( const CanonicalForm & f, const Variable & alpha )
580{
581  //out_cf("factorize:",f,"==================================\n");
582  //out_cf("mipo:",getMipo(alpha),"\n");
583  CFFList F;
584  ASSERT( alpha.level() < 0, "not an algebraic extension" );
585  ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
586  ASSERT( getCharacteristic() > 0, "char 0 factorization not implemented" );
587  #ifdef HAVE_NTL
588  if  (isOn(SW_USE_NTL))
589  {
590    //USE NTL
591    if (getCharacteristic()!=2)
592    {
593      // First all cases with characteristic !=2
594      // set remainder
595      if (fac_NTL_char!=getCharacteristic())
596      {
597        fac_NTL_char=getCharacteristic();
598        #ifdef NTL_ZZ
599        ZZ r;
600        r=getCharacteristic();
601        ZZ_pContext ccc(r);
602        #else
603        zz_pContext ccc(getCharacteristic());
604        #endif
605        ccc.restore();
606        #ifdef NTL_ZZ
607        ZZ_p::init(r);
608        #else
609        zz_p::init(getCharacteristic());
610        #endif
611      }
612
613      // set minimal polynomial in NTL
614      #ifdef NTL_ZZ
615      ZZ_pX minPo=convertFacCF2NTLZZpX(getMipo(alpha));
616      ZZ_pEContext c(minPo);
617      #else
618      zz_pX minPo=convertFacCF2NTLzzpX(getMipo(alpha));
619      zz_pEContext c(minPo);
620      #endif
621
622      c.restore();
623
624      // convert to NTL
625      #ifdef NTL_ZZ
626      ZZ_pEX f1=convertFacCF2NTLZZ_pEX(f,minPo);
627      ZZ_pE leadcoeff= LeadCoeff(f1);
628      #else
629      zz_pEX f1=convertFacCF2NTLzz_pEX(f,minPo);
630      zz_pE leadcoeff= LeadCoeff(f1);
631      #endif
632
633      //make monic
634      f1=f1 / leadcoeff;
635
636      // factorize using NTL
637      #ifdef NTL_ZZ
638      vec_pair_ZZ_pEX_long factors;
639      #else
640      vec_pair_zz_pEX_long factors;
641      #endif
642      CanZass(factors,f1);
643
644      // return converted result
645      F=convertNTLvec_pair_zzpEX_long2FacCFFList(factors,leadcoeff,f.mvar(),alpha);
646    }
647    else
648    {
649      // special case : GF2
650
651      // remainder is two ==> nothing to do
652      // set remainder
653      ZZ r;
654      r=getCharacteristic();
655      ZZ_pContext ccc(r);
656      ccc.restore();
657
658      // set minimal polynomial in NTL using the optimized conversion routines for characteristic 2
659      GF2X minPo=convertFacCF2NTLGF2X(getMipo(alpha,f.mvar()));
660      GF2EContext c(minPo);
661      c.restore();
662
663      // convert to NTL again using the faster conversion routines
664      GF2EX f1;
665      if (isPurePoly(f))
666      {
667        GF2X f_tmp=convertFacCF2NTLGF2X(f);
668        f1=to_GF2EX(f_tmp);
669      }
670      else
671      {
672        f1=convertFacCF2NTLGF2EX(f,minPo);
673      }
674
675      // make monic (in Z/2(a))
676      GF2E f1_coef=LeadCoeff(f1);
677      MakeMonic(f1);
678
679      // factorize using NTL
680      vec_pair_GF2EX_long factors;
681      CanZass(factors,f1);
682
683      // return converted result
684      F=convertNTLvec_pair_GF2EX_long2FacCFFList(factors,f1_coef,f.mvar(),alpha);
685    }
686
687  }
688  else
689  #endif
690  {
691    F=FpFactorizeUnivariateCZ( f, false, 1, alpha, Variable() );
692  }
693  return F;
694}
695
696CFFList sqrFree ( const CanonicalForm & f )
697{
698//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
699    CFFList result;
700
701    if ( getCharacteristic() == 0 )
702        result = sqrFreeZ( f );
703    else
704        result = sqrFreeFp( f );
705
706    //return ( sort ? sortCFFList( result ) : result );
707    return result;
708}
709
710bool isSqrFree ( const CanonicalForm & f )
711{
712//    ASSERT( f.isUnivariate(), "multivariate factorization not implemented" );
713    if ( getCharacteristic() == 0 )
714        return isSqrFreeZ( f );
715    else
716        return isSqrFreeFp( f );
717}
718
Note: See TracBrowser for help on using the repository browser.