source: git/factory/fac_ezgcd.cc @ 16f511

spielwiese
Last change on this file since 16f511 was 16f511, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Fixed the usage of "config.h" (if defined HAVE_CONFIG_H)
  • Property mode set to 100644
File size: 17.0 KB
Line 
1/*****************************************************************************\
2 * Computer Algebra System SINGULAR
3\*****************************************************************************/
4/** @file fac_ezgcd.cc
5 *
6 * This file implements the GCD of two multivariate polynomials over Q using
7 * EZ-GCD as described in "Algorithms for Computer Algebra" by Geddes, Czapor,
8 * Labahnn
9 *
10 * @author Martin Lee
11 *
12 **/
13/*****************************************************************************/
14
15#ifdef HAVE_CONFIG_H
16#include "config.h"
17#endif /* HAVE_CONFIG_H */
18
19#include "timing.h"
20#include "cf_assert.h"
21#include "debug.h"
22
23#include "cf_defs.h"
24#include "canonicalform.h"
25#include "fac_util.h" // HEADER for this file
26#include "cf_algorithm.h"
27#include "cf_reval.h"
28#include "cf_random.h"
29#include "cf_primes.h"
30#include "templates/ftmpl_functions.h"
31#include "cf_map.h"
32#include "facHensel.h"
33
34#ifdef HAVE_NTL
35
36TIMING_DEFINE_PRINT(ez_eval)
37TIMING_DEFINE_PRINT(ez_compress)
38TIMING_DEFINE_PRINT(ez_hensel_lift)
39TIMING_DEFINE_PRINT(ez_content)
40TIMING_DEFINE_PRINT(ez_termination)
41
42static
43int compress4EZGCD (const CanonicalForm& F, const CanonicalForm& G, CFMap & M,
44                    CFMap & N, int& both_non_zero)
45{
46  int n= tmax (F.level(), G.level());
47  int * degsf= new int [n + 1];
48  int * degsg= new int [n + 1];
49
50  for (int i = 0; i <= n; i++)
51    degsf[i]= degsg[i]= 0;
52
53  degsf= degrees (F, degsf);
54  degsg= degrees (G, degsg);
55
56  both_non_zero= 0;
57  int f_zero= 0;
58  int g_zero= 0;
59
60  for (int i= 1; i <= n; i++)
61  {
62    if (degsf[i] != 0 && degsg[i] != 0)
63    {
64      both_non_zero++;
65      continue;
66    }
67    if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level())
68    {
69      f_zero++;
70      continue;
71    }
72    if (degsg[i] == 0 && degsf[i] && i <= F.level())
73    {
74      g_zero++;
75      continue;
76    }
77  }
78
79  if (both_non_zero == 0)
80  {
81    delete [] degsf;
82    delete [] degsg;
83    return 0;
84  }
85
86  // map Variables which do not occur in both polynomials to higher levels
87  int k= 1;
88  int l= 1;
89  for (int i= 1; i <= n; i++)
90  {
91    if (degsf[i] != 0 && degsg[i] == 0 && i <= F.level())
92    {
93      if (k + both_non_zero != i)
94      {
95        M.newpair (Variable (i), Variable (k + both_non_zero));
96        N.newpair (Variable (k + both_non_zero), Variable (i));
97      }
98      k++;
99    }
100    if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level())
101    {
102      if (l + g_zero + both_non_zero != i)
103      {
104        M.newpair (Variable (i), Variable (l + g_zero + both_non_zero));
105        N.newpair (Variable (l + g_zero + both_non_zero), Variable (i));
106      }
107      l++;
108    }
109  }
110
111  // sort Variables x_{i} in decreasing order of
112  // min(deg_{x_{i}}(f),deg_{x_{i}}(g))
113  int m= tmin (F.level(), G.level());
114  int max_min_deg;
115  k= both_non_zero;
116  l= 0;
117  int i= 1;
118  while (k > 0)
119  {
120    max_min_deg= tmin (degsf[i], degsg[i]);
121    while (max_min_deg == 0)
122    {
123      i++;
124      max_min_deg= tmin (degsf[i], degsg[i]);
125    }
126    for (int j= i + 1; j <= m; j++)
127    {
128      if ((tmin (degsf[j],degsg[j]) < max_min_deg) &&
129          (tmin (degsf[j], degsg[j]) != 0))
130      {
131        max_min_deg= tmin (degsf[j], degsg[j]);
132        l= j;
133      }
134    }
135
136    if (l != 0)
137    {
138      if (l != k)
139      {
140        M.newpair (Variable (l), Variable(k));
141        N.newpair (Variable (k), Variable(l));
142        degsf[l]= 0;
143        degsg[l]= 0;
144        l= 0;
145      }
146      else
147      {
148        degsf[l]= 0;
149        degsg[l]= 0;
150        l= 0;
151      }
152    }
153    else if (l == 0)
154    {
155      if (i != k)
156      {
157        M.newpair (Variable (i), Variable (k));
158        N.newpair (Variable (k), Variable (i));
159        degsf[i]= 0;
160        degsg[i]= 0;
161      }
162      else
163      {
164        degsf[i]= 0;
165        degsg[i]= 0;
166      }
167      i++;
168    }
169    k--;
170  }
171
172  delete [] degsf;
173  delete [] degsg;
174
175  return both_non_zero;
176}
177
178static inline
179CanonicalForm myShift2Zero (const CanonicalForm& F, CFList& Feval,
180                            const CFList& evaluation)
181{
182  CanonicalForm A= F;
183  int k= 2;
184  for (CFListIterator i= evaluation; i.hasItem(); i++, k++)
185    A= A (Variable (k) + i.getItem(), k);
186
187  CanonicalForm buf= A;
188  Feval= CFList();
189  Feval.append (buf);
190  for (k= evaluation.length() + 1; k > 2; k--)
191  {
192    buf= mod (buf, Variable (k));
193    Feval.insert (buf);
194  }
195  return A;
196}
197
198static inline
199CanonicalForm myReverseShift (const CanonicalForm& F, const CFList& evaluation)
200{
201  int l= evaluation.length() + 1;
202  CanonicalForm result= F;
203  CFListIterator j= evaluation;
204  for (int i= 2; i < l + 1; i++, j++)
205  {
206    if (F.level() < i)
207      continue;
208    result= result (Variable (i) - j.getItem(), i);
209  }
210  return result;
211}
212
213static inline
214Evaluation optimize4Lift (const CanonicalForm& F, CFMap & M,
215                    CFMap & N, const Evaluation& A)
216{
217  int n= F.level();
218  int * degsf= new int [n + 1];
219
220  for (int i = 0; i <= n; i++)
221    degsf[i]= 0;
222
223  degsf= degrees (F, degsf);
224
225  Evaluation result= Evaluation (A.min(), A.max());
226  ASSERT (A.min() == 2, "expected A.min() == 2");
227  int max_deg;
228  int k= n;
229  int l= 1;
230  int i= 2;
231  int pos= 2;
232  while (k > 1)
233  {
234    max_deg= degsf [i];
235    while (max_deg == 0)
236    {
237      i++;
238      max_deg= degsf [i];
239    }
240    l= i;
241    for (int j= i + 1; j <=  n; j++)
242    {
243      if (degsf[j] > max_deg)
244      {
245        max_deg= degsf[j];
246        l= j;
247      }
248    }
249
250    if (l <= n)
251    {
252      if (l != pos)
253      {
254        result.setValue (pos, A [l]);
255        M.newpair (Variable (l), Variable (pos));
256        N.newpair (Variable (pos), Variable (l));
257        degsf[l]= 0;
258        l= 2;
259        if (k == 2 && n == 3)
260        {
261          result.setValue (l, A [pos]);
262          M.newpair (Variable (pos), Variable (l));
263          N.newpair (Variable (l), Variable (pos));
264          degsf[pos]= 0;
265        }
266      }
267      else
268      {
269        result.setValue (l, A [l]);
270        degsf [l]= 0;
271      }
272    }
273    pos++;
274    k--;
275    l= 2;
276  }
277
278  delete [] degsf;
279
280  return result;
281}
282
283static inline
284int Hensel (const CanonicalForm & UU, CFArray & G, const Evaluation & AA,
285            const CFArray& LeadCoeffs )
286{
287  CFList factors;
288  factors.append (G[1]);
289  factors.append (G[2]);
290
291  CFMap NN, MM;
292  Evaluation A= optimize4Lift (UU, MM, NN, AA);
293
294  CanonicalForm U= MM (UU);
295  CFArray LCs= CFArray (1,2);
296  LCs [1]= MM (LeadCoeffs [1]);
297  LCs [2]= MM (LeadCoeffs [2]);
298
299  CFList evaluation;
300  long termEstimate= size (U);
301  for (int i= A.min(); i <= A.max(); i++)
302  {
303    if (!A[i].isZero())
304    {
305      termEstimate *= degree (U,i)*2;
306      termEstimate /= 3;
307    }
308    evaluation.append (A [i]);
309  }
310  if (termEstimate/getNumVars(U) > 500)
311    return -1;
312  CFList UEval;
313  CanonicalForm shiftedU= myShift2Zero (U, UEval, evaluation);
314
315  if (size (shiftedU)/getNumVars (U) > 500)
316    return -1;
317
318  CFArray shiftedLCs= CFArray (2);
319  CFList shiftedLCsEval1, shiftedLCsEval2;
320  shiftedLCs[0]= myShift2Zero (LCs[1], shiftedLCsEval1, evaluation);
321  shiftedLCs[1]= myShift2Zero (LCs[2], shiftedLCsEval2, evaluation);
322  factors.insert (1);
323  int liftBound= degree (UEval.getLast(), 2) + 1;
324  CFArray Pi;
325  CFMatrix M= CFMatrix (liftBound, factors.length() - 1);
326  CFList diophant;
327  CFArray lcs= CFArray (2);
328  lcs [0]= shiftedLCsEval1.getFirst();
329  lcs [1]= shiftedLCsEval2.getFirst();
330  nonMonicHenselLift12 (UEval.getFirst(), factors, liftBound, Pi, diophant, M,
331                        lcs, false);
332
333  for (CFListIterator i= factors; i.hasItem(); i++)
334  {
335    if (!fdivides (i.getItem(), UEval.getFirst()))
336      return 0;
337  }
338
339  int * liftBounds;
340  bool noOneToOne= false;
341  if (U.level() > 2)
342  {
343    liftBounds= new int [U.level() - 1]; /* index: 0.. U.level()-2 */
344    liftBounds[0]= liftBound;
345    for (int i= 1; i < U.level() - 1; i++)
346      liftBounds[i]= degree (shiftedU, Variable (i + 2)) + 1;
347    factors= nonMonicHenselLift2 (UEval, factors, liftBounds, U.level() - 1,
348                                  false, shiftedLCsEval1, shiftedLCsEval2, Pi,
349                                  diophant, noOneToOne);
350    delete [] liftBounds;
351    if (noOneToOne)
352      return 0;
353  }
354  G[1]= factors.getFirst();
355  G[2]= factors.getLast();
356  G[1]= myReverseShift (G[1], evaluation);
357  G[2]= myReverseShift (G[2], evaluation);
358  G[1]= NN (G[1]);
359  G[2]= NN (G[2]);
360  return 1;
361}
362
363static
364bool findeval (const CanonicalForm & F, const CanonicalForm & G,
365               CanonicalForm & Fb, CanonicalForm & Gb, CanonicalForm & Db,
366               REvaluation & b, int delta, int degF, int degG, int maxeval,
367               int & count, int& k, int bound, int& l)
368{
369  if( count == 0 && delta != 0)
370  {
371    if( count++ > maxeval )
372      return false;
373  }
374  if (count > 0)
375  {
376    b.nextpoint(k);
377    if (k == 0)
378      k++;
379    l++;
380    if (l > bound)
381    {
382      l= 1;
383      k++;
384      if (k > tmax (F.level(), G.level()) - 1)
385        return false;
386      b.nextpoint (k);
387    }
388    if (count++ > maxeval)
389      return false;
390  }
391  while( true )
392  {
393    Fb = b( F );
394    if( degree( Fb, 1 ) == degF )
395    {
396      Gb = b( G );
397      if( degree( Gb, 1 ) == degG )
398      {
399        Db = gcd( Fb, Gb );
400        if( delta > 0 )
401        {
402          if( degree( Db, 1 ) <= delta )
403            return true;
404        }
405        else
406        {
407          k++;
408          return true;
409        }
410      }
411    }
412    if (k == 0)
413      k++;
414    b.nextpoint(k);
415    l++;
416    if (l > bound)
417    {
418      l= 1;
419      k++;
420      if (k > tmax (F.level(), G.level()) - 1)
421        return false;
422      b.nextpoint (k);
423    }
424    if( count++ > maxeval )
425      return false;
426  }
427}
428
429static CanonicalForm
430ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG, REvaluation & b,
431        bool internal )
432{
433  bool isRat= isOn (SW_RATIONAL);
434  CanonicalForm F, G, f, g, d, Fb, Gb, Db, Fbt, Gbt, Dbt, B0, B, D0, lcF, lcG,
435                lcD, cand, contcand, result;
436  CFArray DD( 1, 2 ), lcDD( 1, 2 );
437  int degF, degG, delta, t, count, maxeval;
438  REvaluation bt;
439  int gcdfound = 0;
440  Variable x = Variable(1);
441  count= 0;
442  maxeval= 200;
443  int o, l;
444  o= 0;
445  l= 1;
446
447  if (!isRat)
448    On (SW_RATIONAL);
449  F= FF*bCommonDen (FF);
450  G= GG*bCommonDen (GG);
451  if (!isRat)
452    Off (SW_RATIONAL);
453
454  TIMING_START (ez_compress)
455  CFMap M,N;
456  int smallestDegLev;
457  int best_level= compress4EZGCD (F, G, M, N, smallestDegLev);
458
459  if (best_level == 0)
460  {
461    DEBDECLEVEL( cerr, "ezgcd" );
462    return G.genOne();
463  }
464
465  F= M (F);
466  G= M (G);
467  TIMING_END_AND_PRINT (ez_compress, "time for compression in EZ: ")
468
469  DEBINCLEVEL( cerr, "ezgcd" );
470  DEBOUTLN( cerr, "FF = " << FF );
471  DEBOUTLN( cerr, "GG = " << GG );
472  TIMING_START (ez_content)
473  f = content( F, x ); g = content( G, x ); d = gcd( f, g );
474  d /= icontent (d);
475  DEBOUTLN( cerr, "f = " << f );
476  DEBOUTLN( cerr, "g = " << g );
477  F /= f; G /= g;
478  TIMING_END_AND_PRINT (ez_content, "time to extract content in EZ: ")
479  if ( F.isUnivariate() && G.isUnivariate() )
480  {
481    DEBDECLEVEL( cerr, "ezgcd" );
482    if(F.mvar()==G.mvar())
483      d*=gcd(F,G);
484    else
485      return N (d);
486    return N (d);
487  }
488  if ( F.isUnivariate())
489  {
490    g= content (G,G.mvar());
491    return N(d*gcd(F,g));
492  }
493  if ( G.isUnivariate())
494  {
495    f= content (F,F.mvar());
496    return N(d*gcd(G,f));
497  }
498
499  int maxNumVars= tmax (getNumVars (F), getNumVars (G));
500  int sizeF= size (F);
501  int sizeG= size (G);
502
503
504  if (!isRat)
505    On (SW_RATIONAL);
506  if (sizeF/maxNumVars > 500 && sizeG/maxNumVars > 500)
507  {
508    Off(SW_USE_EZGCD);
509    result=gcd( F, G );
510    On(SW_USE_EZGCD);
511    if (!isRat)
512      Off (SW_RATIONAL);
513    result /= icontent (result);
514    DEBDECLEVEL( cerr, "ezgcd" );
515    return N (d*result);
516  }
517
518  int dummy= 0;
519  if ( gcd_test_one( F, G, false, dummy ) )
520  {
521    DEBDECLEVEL( cerr, "ezgcd" );
522    if (!isRat)
523      Off (SW_RATIONAL);
524    return N (d);
525  }
526  lcF = LC( F, x ); lcG = LC( G, x );
527  lcD = gcd( lcF, lcG );
528  delta = 0;
529  degF = degree( F, x ); degG = degree( G, x );
530  t = tmax( F.level(), G.level() );
531  if ( ! internal )
532    b = REvaluation( 2, t, IntRandom( 25 ) );
533  while ( ! gcdfound )
534  {
535    /// ---> A2
536    DEBOUTLN( cerr, "search for evaluation, delta = " << delta );
537    DEBOUTLN( cerr, "F = " << F );
538    DEBOUTLN( cerr, "G = " << G );
539    TIMING_START (ez_eval)
540    if (!findeval( F, G, Fb, Gb, Db, b, delta, degF, degG, maxeval, count,
541                   o, 25, l))
542    {
543      Off(SW_USE_EZGCD);
544      result=gcd( F, G );
545      On(SW_USE_EZGCD);
546      if (!isRat)
547        Off (SW_RATIONAL);
548      DEBDECLEVEL( cerr, "ezgcd" );
549      result /= icontent (result);
550      return N (d*result);
551    }
552    TIMING_END_AND_PRINT (ez_eval, "time to find eval point in EZ1: ")
553    DEBOUTLN( cerr, "found evaluation b = " << b );
554    DEBOUTLN( cerr, "F(b) = " << Fb );
555    DEBOUTLN( cerr, "G(b) = " << Gb );
556    DEBOUTLN( cerr, "D(b) = " << Db );
557    delta = degree( Db );
558    /// ---> A3
559    if ( delta == 0 )
560    {
561      DEBDECLEVEL( cerr, "ezgcd" );
562      if (!isRat)
563        Off (SW_RATIONAL);
564      return N (d);
565    }
566    /// ---> A4
567    //deltaold = delta;
568    while ( 1 ) 
569    {
570      bt = b;
571      TIMING_START (ez_eval)
572      if (!findeval( F, G, Fbt, Gbt, Dbt, bt, delta, degF, degG, maxeval, count,
573                     o, 25,l ))
574      {
575        Off(SW_USE_EZGCD);
576        result=gcd( F, G );
577        On(SW_USE_EZGCD);
578        if (!isRat)
579          Off (SW_RATIONAL);
580        DEBDECLEVEL( cerr, "ezgcd" );
581        result /= icontent (result);
582        return N (d*result);
583      }
584      TIMING_END_AND_PRINT (ez_eval, "time to find eval point in EZ2: ")
585      int dd=degree( Dbt );
586      if ( dd /*degree( Dbt )*/ == 0 )
587      {
588        DEBDECLEVEL( cerr, "ezgcd" );
589        if (!isRat)
590          Off (SW_RATIONAL);
591        return N (d);
592      }
593      if ( dd /*degree( Dbt )*/ == delta )
594        break;
595      else  if ( dd /*degree( Dbt )*/ < delta )
596      {
597        delta = dd /*degree( Dbt )*/;
598        b = bt;
599        Db = Dbt; Fb = Fbt; Gb = Gbt;
600      }
601      DEBOUTLN( cerr, "now after A4, delta = " << delta );
602      /// ---> A5
603      if (delta == degF)
604      {
605        if (degF <= degG  && fdivides (F, G))
606        {
607          DEBDECLEVEL( cerr, "ezgcd" );
608          if (!isRat)
609            Off (SW_RATIONAL);
610          return N (d*F);
611        }
612        else
613          delta--;
614      }
615      else if (delta == degG)
616      {
617        if (degG <= degF && fdivides( G, F ))
618        {
619          DEBDECLEVEL( cerr, "ezgcd" );
620          if (!isRat)
621            Off (SW_RATIONAL);
622          return N (d*G);
623        }
624        else
625          delta--;
626      }
627    }
628    if ( delta != degF && delta != degG )
629    {
630      /// ---> A6
631      bool B_is_F;
632      CanonicalForm xxx1, xxx2;
633      CanonicalForm buf;
634      DD[1] = Fb / Db;
635      buf= Gb/Db;
636      xxx1 = gcd( DD[1], Db );
637      xxx2 = gcd( buf, Db );
638      if (((xxx1.inCoeffDomain() && xxx2.inCoeffDomain()) &&
639          (size (F) <= size (G)))
640            || (xxx1.inCoeffDomain() && !xxx2.inCoeffDomain()))
641      {
642        B = F;
643        DD[2] = Db;
644        lcDD[1] = lcF;
645        lcDD[2] = lcD;
646        B_is_F = true;
647      }
648      else if (((xxx1.inCoeffDomain() && xxx2.inCoeffDomain()) &&
649                (size (G) < size (F)))
650                || (!xxx1.inCoeffDomain() && xxx2.inCoeffDomain()))
651      {
652        DD[1] = buf;
653        B = G;
654        DD[2] = Db;
655        lcDD[1] = lcG;
656        lcDD[2] = lcD;
657        B_is_F = false;
658      }
659      else
660      {
661        //special case
662        Off(SW_USE_EZGCD);
663        result=gcd( F, G );
664        On(SW_USE_EZGCD);
665        if (!isRat)
666          Off (SW_RATIONAL);
667        DEBDECLEVEL( cerr, "ezgcd" );
668        result /= icontent (result);
669        return N (d*result);
670      }
671      /// ---> A7
672      DD[2] = DD[2] * ( b( lcDD[2] ) / lc( DD[2] ) );
673      DD[1] = DD[1] * ( b( lcDD[1] ) / lc( DD[1] ) );
674      DEBOUTLN( cerr, "(hensel) B    = " << B );
675      DEBOUTLN( cerr, "(hensel) lcB  = " << LC( B, Variable(1) ) );
676      DEBOUTLN( cerr, "(hensel) b(B) = " << b(B) );
677      DEBOUTLN( cerr, "(hensel) DD   = " << DD );
678      DEBOUTLN( cerr, "(hensel) lcDD = " << lcDD );
679      TIMING_START (ez_hensel_lift)
680      gcdfound= Hensel (B*lcD, DD, b, lcDD);
681      TIMING_END_AND_PRINT (ez_hensel_lift, "time to hensel lift in EZ: ")
682      DEBOUTLN( cerr, "(hensel finished) DD   = " << DD );
683
684      if (gcdfound == -1)
685      {
686        Off (SW_USE_EZGCD);
687        result= gcd (F,G);
688        On (SW_USE_EZGCD);
689        if (!isRat)
690          Off (SW_RATIONAL);
691        DEBDECLEVEL( cerr, "ezgcd" );
692        result /= icontent (result);
693        return N (d*result);
694      }
695
696      if (gcdfound)
697      {
698        TIMING_START (ez_termination)
699        contcand= content (DD[2], Variable (1));
700        cand = DD[2] / contcand;
701        if (B_is_F)
702          gcdfound = fdivides( cand, G ) && cand*(DD[1]/(lcD/contcand)) == F;
703        else
704          gcdfound = fdivides( cand, F ) && cand*(DD[1]/(lcD/contcand)) == G;
705        TIMING_END_AND_PRINT (ez_termination,
706                              "time for termination test in EZ: ")
707      }
708      /// ---> A8 (gcdfound)
709    }
710    delta--;
711  }
712  /// ---> A9
713  DEBDECLEVEL( cerr, "ezgcd" );
714  cand *= bCommonDen (cand);
715  if (!isRat)
716    Off (SW_RATIONAL);
717  cand /= icontent (cand);
718  return N (d*cand);
719}
720#endif
721
722CanonicalForm
723ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG )
724{
725#ifdef HAVE_NTL
726  REvaluation b;
727  return ezgcd( FF, GG, b, false );
728#else
729  Off (SW_USE_EZGCD);
730  return gcd (FF, GG);
731  On (SW_USE_EZGCD);
732#endif
733}
734
Note: See TracBrowser for help on using the repository browser.