source: git/factory/cf_gcd.cc @ ad8e1b

spielwiese
Last change on this file since ad8e1b was ad8e1b, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: QGCD, fieldGCD, etc git-svn-id: file:///usr/local/Singular/svn/trunk@10949 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 28.1 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: cf_gcd.cc,v 1.65 2008-08-06 13:53:15 Singular Exp $ */
3
4#include <config.h>
5
6#include "assert.h"
7#include "debug.h"
8
9#include "cf_defs.h"
10#include "canonicalform.h"
11#include "cf_iter.h"
12#include "cf_reval.h"
13#include "cf_primes.h"
14#include "cf_algorithm.h"
15#include "fac_util.h"
16#include "ftmpl_functions.h"
17#include "ffreval.h"
18#include "algext.h"
19#include "fieldGCD.h"
20
21#ifdef HAVE_NTL
22#include <NTL/ZZX.h>
23#include "NTLconvert.h"
24bool isPurePoly(const CanonicalForm & );
25static CanonicalForm gcd_univar_ntl0( const CanonicalForm &, const CanonicalForm & );
26static CanonicalForm gcd_univar_ntlp( const CanonicalForm &, const CanonicalForm & );
27#endif
28
29static CanonicalForm cf_content ( const CanonicalForm &, const CanonicalForm & );
30static bool gcd_avoid_mtaildegree ( CanonicalForm &, CanonicalForm &, CanonicalForm & );
31static void cf_prepgcd( const CanonicalForm &, const CanonicalForm &, int &, int &, int & );
32
33void out_cf(char *s1,const CanonicalForm &f,char *s2);
34
35CanonicalForm chinrem_gcd(const CanonicalForm & FF,const CanonicalForm & GG);
36CanonicalForm newGCD(CanonicalForm A, CanonicalForm B);
37
38bool
39gcd_test_one ( const CanonicalForm & f, const CanonicalForm & g, bool swap )
40{
41    int count = 0;
42    // assume polys have same level;
43    CFRandom * sample = CFRandomFactory::generate();
44    REvaluation e( 2, tmax( f.level(), g.level() ), *sample );
45    delete sample;
46    CanonicalForm lcf, lcg;
47    if ( swap )
48    {
49        lcf = swapvar( LC( f ), Variable(1), f.mvar() );
50        lcg = swapvar( LC( g ), Variable(1), f.mvar() );
51    }
52    else
53    {
54        lcf = LC( f, Variable(1) );
55        lcg = LC( g, Variable(1) );
56    }
57    #define TEST_ONE_MAX 50
58    while ( ( e( lcf ).isZero() || e( lcg ).isZero() ) && count < TEST_ONE_MAX )
59    {
60        e.nextpoint();
61        count++;
62    }
63    if ( count == TEST_ONE_MAX )
64        return false;
65    CanonicalForm F, G;
66    if ( swap )
67    {
68        F=swapvar( f, Variable(1), f.mvar() );
69        G=swapvar( g, Variable(1), g.mvar() );
70    }
71    else
72    {
73        F = f;
74        G = g;
75    }
76    if ( e(F).taildegree() > 0 && e(G).taildegree() > 0 )
77        return false;
78    return gcd( e( F ), e( G ) ).degree() < 1;
79}
80
81//{{{ static CanonicalForm icontent ( const CanonicalForm & f, const CanonicalForm & c )
82//{{{ docu
83//
84// icontent() - return gcd of c and all coefficients of f which
85//   are in a coefficient domain.
86//
87// Used by icontent().
88//
89//}}}
90static CanonicalForm
91icontent ( const CanonicalForm & f, const CanonicalForm & c )
92{
93    if ( f.inBaseDomain() )
94    {
95      if (c.isZero()) return abs(f);
96      return bgcd( f, c );
97    }
98    //else if ( f.inCoeffDomain() )
99    //   return gcd(f,c);
100    else
101    {
102        CanonicalForm g = c;
103        for ( CFIterator i = f; i.hasTerms() && ! g.isOne(); i++ )
104            g = icontent( i.coeff(), g );
105        return g;
106    }
107}
108//}}}
109
110//{{{ CanonicalForm icontent ( const CanonicalForm & f )
111//{{{ docu
112//
113// icontent() - return gcd over all coefficients of f which are
114//   in a coefficient domain.
115//
116//}}}
117CanonicalForm
118icontent ( const CanonicalForm & f )
119{
120    return icontent( f, 0 );
121}
122//}}}
123
124//{{{ CanonicalForm extgcd ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & a, CanonicalForm & b )
125//{{{ docu
126//
127// extgcd() - returns polynomial extended gcd of f and g.
128//
129// Returns gcd(f, g) and a and b sucht that f*a+g*b=gcd(f, g).
130// The gcd is calculated using an extended euclidean polynomial
131// remainder sequence, so f and g should be polynomials over an
132// euclidean domain.  Normalizes result.
133//
134// Note: be sure that f and g have the same level!
135//
136//}}}
137CanonicalForm
138extgcd ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & a, CanonicalForm & b )
139{
140#ifdef HAVE_NTL
141  if (isOn(SW_USE_NTL_GCD_P) && ( getCharacteristic() > 0 )
142  && isPurePoly(f) && isPurePoly(g))
143  {
144    if (fac_NTL_char!=getCharacteristic())
145    {
146      fac_NTL_char=getCharacteristic();
147      #ifdef NTL_ZZ
148      ZZ r;
149      r=getCharacteristic();
150      ZZ_pContext ccc(r);
151      #else
152      zz_pContext ccc(getCharacteristic());
153      #endif
154      ccc.restore();
155      #ifdef NTL_ZZ
156      ZZ_p::init(r);
157      #else
158      zz_p::init(getCharacteristic());
159      #endif
160    }
161    #ifdef NTL_ZZ
162    ZZ_pX F1=convertFacCF2NTLZZpX(f);
163    ZZ_pX G1=convertFacCF2NTLZZpX(g);
164    ZZ_pX R;
165    ZZ_pX A,B;
166    XGCD(R,A,B,F1,G1);
167    a=convertNTLZZpX2CF(A,f.mvar());
168    b=convertNTLZZpX2CF(B,f.mvar());
169    return convertNTLZZpX2CF(R,f.mvar());
170    #else
171    zz_pX F1=convertFacCF2NTLzzpX(f);
172    zz_pX G1=convertFacCF2NTLzzpX(g);
173    zz_pX R;
174    zz_pX A,B;
175    XGCD(R,A,B,F1,G1);
176    a=convertNTLzzpX2CF(A,f.mvar());
177    b=convertNTLzzpX2CF(B,f.mvar());
178    return convertNTLzzpX2CF(R,f.mvar());
179    #endif
180  }
181#endif
182  CanonicalForm contf = content( f );
183  CanonicalForm contg = content( g );
184
185  CanonicalForm p0 = f / contf, p1 = g / contg;
186  CanonicalForm f0 = 1, f1 = 0, g0 = 0, g1 = 1, q, r;
187
188  while ( ! p1.isZero() )
189  {
190      divrem( p0, p1, q, r );
191      p0 = p1; p1 = r;
192      r = g0 - g1 * q;
193      g0 = g1; g1 = r;
194      r = f0 - f1 * q;
195      f0 = f1; f1 = r;
196  }
197  CanonicalForm contp0 = content( p0 );
198  a = f0 / ( contf * contp0 );
199  b = g0 / ( contg * contp0 );
200  p0 /= contp0;
201  if ( p0.sign() < 0 )
202  {
203      p0 = -p0;
204      a = -a;
205      b = -b;
206  }
207  return p0;
208}
209//}}}
210
211//{{{ static CanonicalForm balance ( const CanonicalForm & f, const CanonicalForm & q )
212//{{{ docu
213//
214// balance() - map f from positive to symmetric representation
215//   mod q.
216//
217// This makes sense for univariate polynomials over Z only.
218// q should be an integer.
219//
220// Used by gcd_poly_univar0().
221//
222//}}}
223static CanonicalForm
224balance ( const CanonicalForm & f, const CanonicalForm & q )
225{
226    Variable x = f.mvar();
227    CanonicalForm result = 0, qh = q / 2;
228    CanonicalForm c;
229    CFIterator i;
230    for ( i = f; i.hasTerms(); i++ ) {
231        c = mod( i.coeff(), q );
232        if ( c > qh )
233            result += power( x, i.exp() ) * (c - q);
234        else
235            result += power( x, i.exp() ) * c;
236    }
237    return result;
238}
239//}}}
240
241static CanonicalForm
242gcd_poly_univar0( const CanonicalForm & F, const CanonicalForm & G, bool primitive )
243{
244  CanonicalForm f, g, c, cg, cl, BB, B, M, q, Dp, newD, D, newq;
245  int p, i, n;
246
247  if ( primitive )
248  {
249    f = F;
250    g = G;
251    c = 1;
252  }
253  else
254  {
255    CanonicalForm cF = content( F ), cG = content( G );
256    f = F / cF;
257    g = G / cG;
258    c = bgcd( cF, cG );
259  }
260  cg = gcd( f.lc(), g.lc() );
261  cl = ( f.lc() / cg ) * g.lc();
262//     B = 2 * cg * tmin(
263//         maxnorm(f)*power(CanonicalForm(2),f.degree())*isqrt(f.degree()+1),
264//         maxnorm(g)*power(CanonicalForm(2),g.degree())*isqrt(g.degree()+1)
265//         )+1;
266  M = tmin( maxNorm(f), maxNorm(g) );
267  BB = power(CanonicalForm(2),tmin(f.degree(),g.degree()))*M;
268  q = 0;
269  i = cf_getNumSmallPrimes() - 1;
270  while ( true )
271  {
272    B = BB;
273    while ( i >= 0 && q < B )
274    {
275      p = cf_getSmallPrime( i );
276      i--;
277      while ( i >= 0 && mod( cl, p ) == 0 )
278      {
279        p = cf_getSmallPrime( i );
280        i--;
281      }
282      setCharacteristic( p );
283      Dp = gcd( mapinto( f ), mapinto( g ) );
284      Dp = ( Dp / Dp.lc() ) * mapinto( cg );
285      setCharacteristic( 0 );
286      if ( Dp.degree() == 0 )
287        return c;
288      if ( q.isZero() )
289      {
290        D = mapinto( Dp );
291        q = p;
292        B = power(CanonicalForm(2),D.degree())*M+1;
293      }
294      else
295      {
296        if ( Dp.degree() == D.degree() )
297        {
298          chineseRemainder( D, q, mapinto( Dp ), p, newD, newq );
299          q = newq;
300          D = newD;
301        }
302        else if ( Dp.degree() < D.degree() )
303        {
304          // all previous p's are bad primes
305          q = p;
306          D = mapinto( Dp );
307          B = power(CanonicalForm(2),D.degree())*M+1;
308        }
309        // else p is a bad prime
310      }
311    }
312    if ( i >= 0 )
313    {
314      // now balance D mod q
315      D = pp( balance( D, q ) );
316      if ( fdivides( D, f ) && fdivides( D, g ) )
317        return D * c;
318      else
319        q = 0;
320    }
321    else
322      return gcd_poly( F, G );
323    DEBOUTLN( cerr, "another try ..." );
324  }
325}
326
327static CanonicalForm
328gcd_poly_p( const CanonicalForm & f, const CanonicalForm & g )
329{
330    CanonicalForm pi, pi1;
331    CanonicalForm C, Ci, Ci1, Hi, bi, pi2;
332    bool bpure;
333    int delta = degree( f ) - degree( g );
334
335    if ( delta >= 0 )
336    {
337        pi = f; pi1 = g;
338    }
339    else
340    {
341        pi = g; pi1 = f; delta = -delta;
342    }
343    Ci = content( pi ); Ci1 = content( pi1 );
344    pi1 = pi1 / Ci1; pi = pi / Ci;
345    C = gcd( Ci, Ci1 );
346    if ( !( pi.isUnivariate() && pi1.isUnivariate() ) )
347    {
348        //out_cf("F:",f,"\n");
349        //out_cf("G:",g,"\n");
350        //out_cf("newGCD:",newGCD(f,g),"\n");
351        if (isOn(SW_USE_GCD_P) && (getCharacteristic()>0))
352        {
353          return newGCD(f,g);
354        }
355        if ( gcd_test_one( pi1, pi, true ) )
356        {
357          C=abs(C);
358          //out_cf("GCD:",C,"\n");
359          return C;
360        }
361        bpure = false;
362    }
363    else
364    {
365        bpure = isPurePoly(pi) && isPurePoly(pi1);
366#ifdef HAVE_NTL
367        if ( isOn(SW_USE_NTL_GCD_P) && bpure )
368            return gcd_univar_ntlp(pi, pi1 ) * C;
369#endif
370    }
371    Variable v = f.mvar();
372    Hi = power( LC( pi1, v ), delta );
373    if ( (delta+1) % 2 )
374        bi = 1;
375    else
376        bi = -1;
377    while ( degree( pi1, v ) > 0 ) {
378        pi2 = psr( pi, pi1, v );
379        pi2 = pi2 / bi;
380        pi = pi1; pi1 = pi2;
381        if ( degree( pi1, v ) > 0 ) {
382            delta = degree( pi, v ) - degree( pi1, v );
383            if ( (delta+1) % 2 )
384                bi = LC( pi, v ) * power( Hi, delta );
385            else
386                bi = -LC( pi, v ) * power( Hi, delta );
387            Hi = power( LC( pi1, v ), delta ) / power( Hi, delta-1 );
388        }
389    }
390    if ( degree( pi1, v ) == 0 )
391    {
392      C=abs(C);
393      //out_cf("GCD:",C,"\n");
394      return C;
395    }
396    pi /= content( pi );
397    if ( bpure )
398        pi /= pi.lc();
399    C=abs(C*pi);
400    //out_cf("GCD:",C,"\n");
401    return C;
402}
403
404static CanonicalForm
405gcd_poly_0( const CanonicalForm & f, const CanonicalForm & g )
406{
407    CanonicalForm pi, pi1;
408    CanonicalForm C, Ci, Ci1, Hi, bi, pi2;
409    int delta = degree( f ) - degree( g );
410
411    if ( delta >= 0 )
412    {
413        pi = f; pi1 = g;
414    }
415    else
416    {
417        pi = g; pi1 = f; delta = -delta;
418    }
419    Ci = content( pi ); Ci1 = content( pi1 );
420    pi1 = pi1 / Ci1; pi = pi / Ci;
421    C = gcd( Ci, Ci1 );
422    if ( pi.isUnivariate() && pi1.isUnivariate() )
423    {
424#ifdef HAVE_NTL
425        if ( isOn(SW_USE_NTL_GCD_0) && isPurePoly(pi) && isPurePoly(pi1) )
426            return gcd_univar_ntl0(pi, pi1 ) * C;
427#endif
428        return gcd_poly_univar0( pi, pi1, true ) * C;
429    }
430    else if ( gcd_test_one( pi1, pi, true ) )
431      return C;
432    Variable v = f.mvar();
433    Hi = power( LC( pi1, v ), delta );
434    if ( (delta+1) % 2 )
435        bi = 1;
436    else
437        bi = -1;
438    while ( degree( pi1, v ) > 0 )
439    {
440        pi2 = psr( pi, pi1, v );
441        pi2 = pi2 / bi;
442        pi = pi1; pi1 = pi2;
443        if ( degree( pi1, v ) > 0 )
444        {
445            delta = degree( pi, v ) - degree( pi1, v );
446            if ( (delta+1) % 2 )
447                bi = LC( pi, v ) * power( Hi, delta );
448            else
449                bi = -LC( pi, v ) * power( Hi, delta );
450            Hi = power( LC( pi1, v ), delta ) / power( Hi, delta-1 );
451        }
452    }
453    if ( degree( pi1, v ) == 0 )
454        return C;
455    else
456        return C * pp( pi );
457}
458
459//{{{ CanonicalForm gcd_poly ( const CanonicalForm & f, const CanonicalForm & g )
460//{{{ docu
461//
462// gcd_poly() - calculate polynomial gcd.
463//
464// This is the dispatcher for polynomial gcd calculation.  We call either
465// ezgcd(), sparsemod() or gcd_poly1() in dependecy on the current
466// characteristic and settings of SW_USE_EZGCD and SW_USE_SPARSEMOD, resp.
467//
468// Used by gcd() and gcd_poly_univar0().
469//
470//}}}
471#if 0
472int si_factor_reminder=1;
473#endif
474CanonicalForm gcd_poly ( const CanonicalForm & f, const CanonicalForm & g )
475{
476  CanonicalForm fc, gc, d1;
477  int mp, cc, p1, pe;
478  mp = f.level()+1;
479  bool fc_isUnivariate=f.isUnivariate();
480  bool gc_isUnivariate=g.isUnivariate();
481  bool fc_and_gc_Univariate=fc_isUnivariate && gc_isUnivariate;
482#if 1
483  if (( getCharacteristic() == 0 )
484  && (f.level() >4)
485  && (g.level() >4)
486  && isOn( SW_USE_CHINREM_GCD)
487  && (!fc_and_gc_Univariate)
488  && (isPurePoly_m(f))
489  && (isPurePoly_m(g))
490  )
491  {
492      return chinrem_gcd( f, g );
493  }
494#endif
495  cf_prepgcd( f, g, cc, p1, pe);
496  if ( cc != 0 )
497  {
498    if ( cc > 0 )
499    {
500      fc = replacevar( f, Variable(cc), Variable(mp) );
501      gc = g;
502    }
503    else
504    {
505      fc = replacevar( g, Variable(-cc), Variable(mp) );
506      gc = f;
507    }
508    return cf_content( fc, gc );
509  }
510// now each appearing variable is in f and g
511  fc = f;
512  gc = g;
513  if( gcd_avoid_mtaildegree ( fc, gc, d1 ) )
514      return d1;
515  if ( getCharacteristic() != 0 )
516  {
517    if (isOn(SW_USE_fieldGCD)
518    && (!fc_and_gc_Univariate)
519    && (getCharacteristic() >100))
520    {
521      return fieldGCD(f,g);
522    }
523    else if (isOn( SW_USE_EZGCD_P ) && (!fc_and_gc_Univariate))
524    {
525      if ( pe == 1 )
526        fc = fin_ezgcd( fc, gc );
527      else if ( pe > 0 )// no variable at position 1
528      {
529        fc = replacevar( fc, Variable(pe), Variable(1) );
530        gc = replacevar( gc, Variable(pe), Variable(1) );
531        fc = replacevar( fin_ezgcd( fc, gc ), Variable(1), Variable(pe) );
532      }
533      else
534      {
535        pe = -pe;
536        fc = swapvar( fc, Variable(pe), Variable(1) );
537        gc = swapvar( gc, Variable(pe), Variable(1) );
538        fc = swapvar( fin_ezgcd( fc, gc ), Variable(1), Variable(pe) );
539      }
540    }
541    else if (isOn(SW_USE_GCD_P))
542    {
543      fc=newGCD(fc,gc);
544    }
545    else if ( p1 == fc.level() )
546      fc = gcd_poly_p( fc, gc );
547    else
548    {
549      fc = replacevar( fc, Variable(p1), Variable(mp) );
550      gc = replacevar( gc, Variable(p1), Variable(mp) );
551      fc = replacevar( gcd_poly_p( fc, gc ), Variable(mp), Variable(p1) );
552    }
553  }
554  else if (!fc_and_gc_Univariate)
555  {
556    if (
557    isOn(SW_USE_CHINREM_GCD)
558    && (gc.level() >5)
559    && (fc.level() >5)
560    && (isPurePoly_m(fc)) && (isPurePoly_m(gc))
561    )
562    {
563    #if 0
564      if ( p1 == fc.level() )
565        fc = chinrem_gcd( fc, gc );
566      else
567      {
568        fc = replacevar( fc, Variable(p1), Variable(mp) );
569        gc = replacevar( gc, Variable(p1), Variable(mp) );
570        fc = replacevar( chinrem_gcd( fc, gc ), Variable(mp), Variable(p1) );
571      }
572    #else
573      fc = chinrem_gcd( fc, gc);
574    #endif
575    }
576    if ( isOn( SW_USE_EZGCD ) )
577    {
578      if ( pe == 1 )
579        fc = ezgcd( fc, gc );
580      else if ( pe > 0 )// no variable at position 1
581      {
582        fc = replacevar( fc, Variable(pe), Variable(1) );
583        gc = replacevar( gc, Variable(pe), Variable(1) );
584        fc = replacevar( ezgcd( fc, gc ), Variable(1), Variable(pe) );
585      }
586      else
587      {
588        pe = -pe;
589        fc = swapvar( fc, Variable(pe), Variable(1) );
590        gc = swapvar( gc, Variable(pe), Variable(1) );
591        fc = swapvar( ezgcd( fc, gc ), Variable(1), Variable(pe) );
592      }
593    }
594    else if (
595    isOn(SW_USE_CHINREM_GCD)
596    && (isPurePoly_m(fc)) && (isPurePoly_m(gc))
597    )
598    {
599    #if 0
600      if ( p1 == fc.level() )
601        fc = chinrem_gcd( fc, gc );
602      else
603      {
604        fc = replacevar( fc, Variable(p1), Variable(mp) );
605        gc = replacevar( gc, Variable(p1), Variable(mp) );
606        fc = replacevar( chinrem_gcd( fc, gc ), Variable(mp), Variable(p1) );
607      }
608    #else
609      fc = chinrem_gcd( fc, gc);
610    #endif
611    }
612    else
613    {
614       fc = gcd_poly_0( fc, gc );
615    }
616  }
617  else
618  {
619    fc = gcd_poly_0( fc, gc );
620  }
621  if ( d1.degree() > 0 )
622    fc *= d1;
623  return fc;
624}
625//}}}
626
627//{{{ static CanonicalForm cf_content ( const CanonicalForm & f, const CanonicalForm & g )
628//{{{ docu
629//
630// cf_content() - return gcd(g, content(f)).
631//
632// content(f) is calculated with respect to f's main variable.
633//
634// Used by gcd(), content(), content( CF, Variable ).
635//
636//}}}
637static CanonicalForm
638cf_content ( const CanonicalForm & f, const CanonicalForm & g )
639{
640    if ( f.inPolyDomain() || ( f.inExtension() && ! getReduce( f.mvar() ) ) )
641    {
642        CFIterator i = f;
643        CanonicalForm result = g;
644        while ( i.hasTerms() && ! result.isOne() )
645        {
646            result = gcd( i.coeff(), result );
647            i++;
648        }
649        return result;
650    }
651    else
652        return abs( f );
653}
654//}}}
655
656//{{{ CanonicalForm content ( const CanonicalForm & f )
657//{{{ docu
658//
659// content() - return content(f) with respect to main variable.
660//
661// Normalizes result.
662//
663//}}}
664CanonicalForm
665content ( const CanonicalForm & f )
666{
667    if ( f.inPolyDomain() || ( f.inExtension() && ! getReduce( f.mvar() ) ) )
668    {
669        CFIterator i = f;
670        CanonicalForm result = abs( i.coeff() );
671        i++;
672        while ( i.hasTerms() && ! result.isOne() )
673        {
674            result = gcd( i.coeff(), result );
675            i++;
676        }
677        return result;
678    }
679    else
680        return abs( f );
681}
682//}}}
683
684//{{{ CanonicalForm content ( const CanonicalForm & f, const Variable & x )
685//{{{ docu
686//
687// content() - return content(f) with respect to x.
688//
689// x should be a polynomial variable.
690//
691//}}}
692CanonicalForm
693content ( const CanonicalForm & f, const Variable & x )
694{
695    ASSERT( x.level() > 0, "cannot calculate content with respect to algebraic variable" );
696    Variable y = f.mvar();
697
698    if ( y == x )
699        return cf_content( f, 0 );
700    else  if ( y < x )
701        return f;
702    else
703        return swapvar( content( swapvar( f, y, x ), y ), y, x );
704}
705//}}}
706
707//{{{ CanonicalForm vcontent ( const CanonicalForm & f, const Variable & x )
708//{{{ docu
709//
710// vcontent() - return content of f with repect to variables >= x.
711//
712// The content is recursively calculated over all coefficients in
713// f having level less than x.  x should be a polynomial
714// variable.
715//
716//}}}
717CanonicalForm
718vcontent ( const CanonicalForm & f, const Variable & x )
719{
720    ASSERT( x.level() > 0, "cannot calculate vcontent with respect to algebraic variable" );
721
722    if ( f.mvar() <= x )
723        return content( f, x );
724    else {
725        CFIterator i;
726        CanonicalForm d = 0;
727        for ( i = f; i.hasTerms() && ! d.isOne(); i++ )
728            d = gcd( d, vcontent( i.coeff(), x ) );
729        return d;
730    }
731}
732//}}}
733
734//{{{ CanonicalForm pp ( const CanonicalForm & f )
735//{{{ docu
736//
737// pp() - return primitive part of f.
738//
739// Returns zero if f equals zero, otherwise f / content(f).
740//
741//}}}
742CanonicalForm
743pp ( const CanonicalForm & f )
744{
745    if ( f.isZero() )
746        return f;
747    else
748        return f / content( f );
749}
750//}}}
751
752CanonicalForm
753gcd ( const CanonicalForm & f, const CanonicalForm & g )
754{
755    bool b = f.isZero();
756    if ( b || g.isZero() )
757    {
758        if ( b )
759            return abs( g );
760        else
761            return abs( f );
762    }
763    if ( f.inPolyDomain() || g.inPolyDomain() )
764    {
765        if ( f.mvar() != g.mvar() )
766        {
767            if ( f.mvar() > g.mvar() )
768                return cf_content( f, g );
769            else
770                return cf_content( g, f );
771        }
772        if (isOn(SW_USE_QGCD))
773        {
774          Variable m;
775          if ((hasFirstAlgVar(f,m) || hasFirstAlgVar(g,m))
776          //&& f.isUnivariate()
777          //&& g.isUnivariate()
778          && (getCharacteristic() == 0)
779          )
780          {
781            //if ((f.level()==g.level()) && f.isUnivariate() && g.isUnivariate())
782            //  return univarQGCD(f,g);
783            //else
784              return QGCD(f,g);
785          }
786        }
787           
788        if ( f.inExtension() && getReduce( f.mvar() ) )
789            return CanonicalForm(1);
790        else
791        {
792            if ( fdivides( f, g ) )
793                return abs( f );
794            else  if ( fdivides( g, f ) )
795                return abs( g );
796            if ( !( getCharacteristic() == 0 && isOn( SW_RATIONAL ) ) )
797            {
798                CanonicalForm d;
799#if 1
800                do{ d = gcd_poly( f, g ); }
801                while ((!fdivides(d,f)) || (!fdivides(d,g)));
802#else
803                while(1)
804                {
805                  d = gcd_poly( f, g );
806                  if ((fdivides(d,f)) && (fdivides(d,g))) break;
807                  printf("g"); fflush(stdout);
808                }
809#endif
810                return abs( d );
811            }
812            else
813            {
814                CanonicalForm cdF = bCommonDen( f );
815                CanonicalForm cdG = bCommonDen( g );
816                Off( SW_RATIONAL );
817                CanonicalForm l = lcm( cdF, cdG );
818                On( SW_RATIONAL );
819                CanonicalForm F = f * l, G = g * l;
820                Off( SW_RATIONAL );
821                do { l = gcd_poly( F, G ); }
822                while ((!fdivides(l,F)) || (!fdivides(l,G)));
823                On( SW_RATIONAL );
824                return abs( l );
825            }
826        }
827    }
828    if ( f.inBaseDomain() && g.inBaseDomain() )
829        return bgcd( f, g );
830    else
831        return 1;
832}
833
834//{{{ CanonicalForm lcm ( const CanonicalForm & f, const CanonicalForm & g )
835//{{{ docu
836//
837// lcm() - return least common multiple of f and g.
838//
839// The lcm is calculated using the formula lcm(f, g) = f * g / gcd(f, g).
840//
841// Returns zero if one of f or g equals zero.
842//
843//}}}
844CanonicalForm
845lcm ( const CanonicalForm & f, const CanonicalForm & g )
846{
847    if ( f.isZero() || g.isZero() )
848        return 0;
849    else
850        return ( f / gcd( f, g ) ) * g;
851}
852//}}}
853
854#ifdef HAVE_NTL
855
856static CanonicalForm
857gcd_univar_ntl0( const CanonicalForm & F, const CanonicalForm & G )
858{
859    ZZX F1=convertFacCF2NTLZZX(F);
860    ZZX G1=convertFacCF2NTLZZX(G);
861    ZZX R=GCD(F1,G1);
862    return convertNTLZZX2CF(R,F.mvar());
863}
864
865static CanonicalForm
866gcd_univar_ntlp( const CanonicalForm & F, const CanonicalForm & G )
867{
868  if (fac_NTL_char!=getCharacteristic())
869  {
870    fac_NTL_char=getCharacteristic();
871    #ifdef NTL_ZZ
872    ZZ r;
873    r=getCharacteristic();
874    ZZ_pContext ccc(r);
875    #else
876    zz_pContext ccc(getCharacteristic());
877    #endif
878    ccc.restore();
879    #ifdef NTL_ZZ
880    ZZ_p::init(r);
881    #else
882    zz_p::init(getCharacteristic());
883    #endif
884  }
885  #ifdef NTL_ZZ
886  ZZ_pX F1=convertFacCF2NTLZZpX(F);
887  ZZ_pX G1=convertFacCF2NTLZZpX(G);
888  ZZ_pX R=GCD(F1,G1);
889  return  convertNTLZZpX2CF(R,F.mvar());
890  #else
891  zz_pX F1=convertFacCF2NTLzzpX(F);
892  zz_pX G1=convertFacCF2NTLzzpX(G);
893  zz_pX R=GCD(F1,G1);
894  return  convertNTLzzpX2CF(R,F.mvar());
895  #endif
896}
897
898#endif
899
900static bool
901gcd_avoid_mtaildegree ( CanonicalForm & f1, CanonicalForm & g1, CanonicalForm & d1 )
902{
903    bool rdy = true;
904    int df = f1.taildegree();
905    int dg = g1.taildegree();
906
907    d1 = d1.genOne();
908    if ( dg == 0 )
909    {
910        if ( df == 0 )
911            return false;
912        else
913        {
914            if ( f1.degree() == df )
915                d1 = cf_content( g1, LC( f1 ) );
916            else
917            {
918                f1 /= power( f1.mvar(), df );
919                rdy = false;
920            }
921        }
922    }
923    else
924    {
925        if ( df == 0)
926        {
927            if ( g1.degree() == dg )
928                d1 = cf_content( f1, LC( g1 ) );
929            else
930            {
931                g1 /= power( g1.mvar(), dg );
932                rdy = false;
933            }
934        }
935        else
936        {
937            if ( df > dg )
938                d1 = power( f1.mvar(), dg );
939            else
940                d1 = power( f1.mvar(), df );
941            if ( f1.degree() == df )
942            {
943                if (g1.degree() == dg)
944                    d1 *= gcd( LC( f1 ), LC( g1 ) );
945                else
946                {
947                    g1 /= power( g1.mvar(), dg);
948                    d1 *= cf_content( g1, LC( f1 ) );
949                }
950            }
951            else
952            {
953                f1 /= power( f1.mvar(), df );
954                if ( g1.degree() == dg )
955                    d1 *= cf_content( f1, LC( g1 ) );
956                else
957                {
958                    g1 /= power( g1.mvar(), dg );
959                    rdy = false;
960                }
961            }
962        }
963    }
964    return rdy;
965}
966
967/*
968*  compute positions p1 and pe of optimal variables:
969*    pe is used in "ezgcd" and
970*    p1 in "gcd_poly1"
971*/
972static
973void optvalues ( const int * df, const int * dg, const int n, int & p1, int &pe )
974{
975    int i, o1, oe;
976    if ( df[n] > dg[n] )
977    {
978        o1 = df[n]; oe = dg[n];
979    }
980    else
981    {
982        o1 = dg[n]; oe = df[n];
983    }
984    i = n-1;
985    while ( i > 0 )
986    {
987        if ( df[i] != 0 )
988        {
989            if ( df[i] > dg[i] )
990            {
991                if ( o1 > df[i]) { o1 = df[i]; p1 = i; }
992                if ( oe <= dg[i]) { oe = dg[i]; pe = i; }
993            }
994            else
995            {
996                if ( o1 > dg[i]) { o1 = dg[i]; p1 = i; }
997                if ( oe <= df[i]) { oe = df[i]; pe = i; }
998            }
999        }
1000        i--;
1001    }
1002}
1003
1004/*
1005*  make some changes of variables, see optvalues
1006*/
1007static void
1008cf_prepgcd( const CanonicalForm & f, const CanonicalForm & g, int & cc, int & p1, int &pe )
1009{
1010    int i, k, n;
1011    n = f.level();
1012    cc = 0;
1013    p1 = pe = n;
1014    if ( n == 1 )
1015        return;
1016    int * degsf = new int[n+1];
1017    int * degsg = new int[n+1];
1018    for ( i = n; i > 0; i-- )
1019    {
1020        degsf[i] = degsg[i] = 0;
1021    }
1022    degsf = degrees( f, degsf );
1023    degsg = degrees( g, degsg );
1024
1025    k = 0;
1026    for ( i = n-1; i > 0; i-- )
1027    {
1028        if ( degsf[i] == 0 )
1029        {
1030            if ( degsg[i] != 0 )
1031            {
1032                cc = -i;
1033                break;
1034            }
1035        }
1036        else
1037        {
1038            if ( degsg[i] == 0 )
1039            {
1040                cc = i;
1041                break;
1042            }
1043            else k++;
1044        }
1045    }
1046
1047    if ( ( cc == 0 ) && ( k != 0 ) )
1048        optvalues( degsf, degsg, n, p1, pe );
1049    if ( ( pe != 1 ) && ( degsf[1] != 0 ) )
1050        pe = -pe;
1051
1052    delete [] degsf;
1053    delete [] degsg;
1054}
1055
1056
1057static CanonicalForm
1058balance_p ( const CanonicalForm & f, const CanonicalForm & q )
1059{
1060    Variable x = f.mvar();
1061    CanonicalForm result = 0, qh = q / 2;
1062    CanonicalForm c;
1063    CFIterator i;
1064    for ( i = f; i.hasTerms(); i++ )
1065    {
1066        c = i.coeff();
1067        if ( c.inCoeffDomain())
1068        {
1069          if ( c > qh )
1070            result += power( x, i.exp() ) * (c - q);
1071          else
1072            result += power( x, i.exp() ) * c;
1073        }
1074        else
1075          result += power( x, i.exp() ) * balance_p(c,q);
1076    }
1077    return result;
1078}
1079
1080CanonicalForm chinrem_gcd ( const CanonicalForm & FF, const CanonicalForm & GG )
1081{
1082  CanonicalForm f, g, cg, cl, q, Dp, newD, D, newq;
1083  int p, i, dp_deg, d_deg;;
1084
1085  CanonicalForm cd = bCommonDen( FF );
1086  f=cd*FF;
1087  f /=vcontent(f,Variable(1));
1088  //cd = bCommonDen( f ); f *=cd;
1089  //f /=vcontent(f,Variable(1));
1090
1091  cd = bCommonDen( GG );
1092  g=cd*GG;
1093  g /=vcontent(g,Variable(1));
1094  //cd = bCommonDen( g ); g *=cd;
1095  //g /=vcontent(g,Variable(1));
1096
1097  q = 0;
1098  i = cf_getNumBigPrimes() - 1;
1099  cl =  f.lc()* g.lc();
1100
1101  while ( true )
1102  {
1103    p = cf_getBigPrime( i );
1104    i--;
1105    while ( i >= 0 && mod( cl, p ) == 0 )
1106    {
1107      p = cf_getBigPrime( i );
1108      i--;
1109    }
1110    //printf("try p=%d\n",p);
1111    setCharacteristic( p );
1112    Dp = gcd_poly( mapinto( f ), mapinto( g ) );
1113    Dp /=Dp.lc();
1114    setCharacteristic( 0 );
1115    dp_deg=totaldegree(Dp);
1116    if ( dp_deg == 0 )
1117    {
1118      //printf(" -> 1\n");
1119      return CanonicalForm(1);
1120    }
1121    if ( q.isZero() )
1122    {
1123      D = mapinto( Dp );
1124      d_deg=dp_deg;
1125      q = p;
1126    }
1127    else
1128    {
1129      if ( dp_deg == d_deg )
1130      {
1131        chineseRemainder( D, q, mapinto( Dp ), p, newD, newq );
1132        q = newq;
1133        D = newD;
1134      }
1135      else if ( dp_deg < d_deg )
1136      {
1137        //printf(" were all bad, try more\n");
1138        // all previous p's are bad primes
1139        q = p;
1140        D = mapinto( Dp );
1141        d_deg=dp_deg;
1142      }
1143      else
1144      {
1145        //printf(" was bad, try more\n");
1146      }
1147      //else dp_deg > d_deg: bad prime
1148    }
1149    if ( i >= 0 )
1150    {
1151      CanonicalForm Dn= Farey(D,q);
1152      int is_rat=isOn(SW_RATIONAL);
1153      On(SW_RATIONAL);
1154      CanonicalForm cd = bCommonDen( Dn ); // we need On(SW_RATIONAL)
1155      if (!is_rat) Off(SW_RATIONAL);
1156      Dn *=cd;
1157      //Dn /=vcontent(Dn,Variable(1));
1158      if ( fdivides( Dn, f ) && fdivides( Dn, g ) )
1159      {
1160        //printf(" -> success\n");
1161        return Dn;
1162      }
1163      //else: try more primes
1164    }
1165    else
1166    { // try other method
1167      //printf("try other gcd\n");
1168      Off(SW_USE_CHINREM_GCD);
1169      D=gcd_poly( f, g );
1170      On(SW_USE_CHINREM_GCD);
1171      return D;
1172    }
1173  }
1174}
1175#include "algext.cc"
Note: See TracBrowser for help on using the repository browser.