source: git/factory/cf_gcd.cc @ f4b180

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