source: git/factory/fac_ezgcd.cc @ ba5e9e

spielwiese
Last change on this file since ba5e9e was e380c09, checked in by Martin Lee <martinlee84@…>, 11 years ago
chg: earlier switching to dense gcd
  • Property mode set to 100644
File size: 17.4 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
435  int maxNumVars= tmax (getNumVars (FF), getNumVars (GG));
436  int sizeF= size (FF);
437  int sizeG= size (GG);
438
439
440  if (!isRat)
441    On (SW_RATIONAL);
442  if (sizeF/maxNumVars > 500 && sizeG/maxNumVars > 500)
443  {
444    Off(SW_USE_EZGCD);
445    CanonicalForm result=gcd( FF, GG );
446    On(SW_USE_EZGCD);
447    if (!isRat)
448      Off (SW_RATIONAL);
449    result /= icontent (result);
450    DEBDECLEVEL( cerr, "ezgcd" );
451    return result;
452  }
453
454
455  CanonicalForm F, G, f, g, d, Fb, Gb, Db, Fbt, Gbt, Dbt, B0, B, D0, lcF, lcG,
456                lcD, cand, contcand, result;
457  CFArray DD( 1, 2 ), lcDD( 1, 2 );
458  int degF, degG, delta, t, count, maxeval;
459  REvaluation bt;
460  int gcdfound = 0;
461  Variable x = Variable(1);
462  count= 0;
463  maxeval= 200;
464  int o, l;
465  o= 0;
466  l= 1;
467
468  if (!isRat)
469    On (SW_RATIONAL);
470  F= FF*bCommonDen (FF);
471  G= GG*bCommonDen (GG);
472  if (!isRat)
473    Off (SW_RATIONAL);
474
475  TIMING_START (ez_compress)
476  CFMap M,N;
477  int smallestDegLev;
478  int best_level= compress4EZGCD (F, G, M, N, smallestDegLev);
479
480  if (best_level == 0)
481  {
482    DEBDECLEVEL( cerr, "ezgcd" );
483    return G.genOne();
484  }
485
486  F= M (F);
487  G= M (G);
488  TIMING_END_AND_PRINT (ez_compress, "time for compression in EZ: ")
489
490  DEBINCLEVEL( cerr, "ezgcd" );
491  DEBOUTLN( cerr, "FF = " << FF );
492  DEBOUTLN( cerr, "GG = " << GG );
493  TIMING_START (ez_content)
494  f = content( F, x ); g = content( G, x ); d = gcd( f, g );
495  d /= icontent (d);
496  DEBOUTLN( cerr, "f = " << f );
497  DEBOUTLN( cerr, "g = " << g );
498  F /= f; G /= g;
499  TIMING_END_AND_PRINT (ez_content, "time to extract content in EZ: ")
500  if ( F.isUnivariate() && G.isUnivariate() )
501  {
502    DEBDECLEVEL( cerr, "ezgcd" );
503    if(F.mvar()==G.mvar())
504      d*=gcd(F,G);
505    else
506      return N (d);
507    return N (d);
508  }
509  if ( F.isUnivariate())
510  {
511    g= content (G,G.mvar());
512    return N(d*gcd(F,g));
513  }
514  if ( G.isUnivariate())
515  {
516    f= content (F,F.mvar());
517    return N(d*gcd(G,f));
518  }
519
520  maxNumVars= tmax (getNumVars (F), getNumVars (G));
521  sizeF= size (F);
522  sizeG= size (G);
523
524  if (!isRat)
525    On (SW_RATIONAL);
526  if (sizeF/maxNumVars > 500 && sizeG/maxNumVars > 500)
527  {
528    Off(SW_USE_EZGCD);
529    result=gcd( F, G );
530    On(SW_USE_EZGCD);
531    if (!isRat)
532      Off (SW_RATIONAL);
533    result /= icontent (result);
534    DEBDECLEVEL( cerr, "ezgcd" );
535    return N (d*result);
536  }
537
538  int dummy= 0;
539  if ( gcd_test_one( F, G, false, dummy ) )
540  {
541    DEBDECLEVEL( cerr, "ezgcd" );
542    if (!isRat)
543      Off (SW_RATIONAL);
544    return N (d);
545  }
546  lcF = LC( F, x ); lcG = LC( G, x );
547  lcD = gcd( lcF, lcG );
548  delta = 0;
549  degF = degree( F, x ); degG = degree( G, x );
550  t = tmax( F.level(), G.level() );
551  if ( ! internal )
552    b = REvaluation( 2, t, IntRandom( 25 ) );
553  while ( ! gcdfound )
554  {
555    /// ---> A2
556    DEBOUTLN( cerr, "search for evaluation, delta = " << delta );
557    DEBOUTLN( cerr, "F = " << F );
558    DEBOUTLN( cerr, "G = " << G );
559    TIMING_START (ez_eval)
560    if (!findeval( F, G, Fb, Gb, Db, b, delta, degF, degG, maxeval, count,
561                   o, 25, l))
562    {
563      Off(SW_USE_EZGCD);
564      result=gcd( F, G );
565      On(SW_USE_EZGCD);
566      if (!isRat)
567        Off (SW_RATIONAL);
568      DEBDECLEVEL( cerr, "ezgcd" );
569      result /= icontent (result);
570      return N (d*result);
571    }
572    TIMING_END_AND_PRINT (ez_eval, "time to find eval point in EZ1: ")
573    DEBOUTLN( cerr, "found evaluation b = " << b );
574    DEBOUTLN( cerr, "F(b) = " << Fb );
575    DEBOUTLN( cerr, "G(b) = " << Gb );
576    DEBOUTLN( cerr, "D(b) = " << Db );
577    delta = degree( Db );
578    /// ---> A3
579    if ( delta == 0 )
580    {
581      DEBDECLEVEL( cerr, "ezgcd" );
582      if (!isRat)
583        Off (SW_RATIONAL);
584      return N (d);
585    }
586    /// ---> A4
587    //deltaold = delta;
588    while ( 1 ) 
589    {
590      bt = b;
591      TIMING_START (ez_eval)
592      if (!findeval( F, G, Fbt, Gbt, Dbt, bt, delta, degF, degG, maxeval, count,
593                     o, 25,l ))
594      {
595        Off(SW_USE_EZGCD);
596        result=gcd( F, G );
597        On(SW_USE_EZGCD);
598        if (!isRat)
599          Off (SW_RATIONAL);
600        DEBDECLEVEL( cerr, "ezgcd" );
601        result /= icontent (result);
602        return N (d*result);
603      }
604      TIMING_END_AND_PRINT (ez_eval, "time to find eval point in EZ2: ")
605      int dd=degree( Dbt );
606      if ( dd /*degree( Dbt )*/ == 0 )
607      {
608        DEBDECLEVEL( cerr, "ezgcd" );
609        if (!isRat)
610          Off (SW_RATIONAL);
611        return N (d);
612      }
613      if ( dd /*degree( Dbt )*/ == delta )
614        break;
615      else  if ( dd /*degree( Dbt )*/ < delta )
616      {
617        delta = dd /*degree( Dbt )*/;
618        b = bt;
619        Db = Dbt; Fb = Fbt; Gb = Gbt;
620      }
621      DEBOUTLN( cerr, "now after A4, delta = " << delta );
622      /// ---> A5
623      if (delta == degF)
624      {
625        if (degF <= degG  && fdivides (F, G))
626        {
627          DEBDECLEVEL( cerr, "ezgcd" );
628          if (!isRat)
629            Off (SW_RATIONAL);
630          return N (d*F);
631        }
632        else
633          delta--;
634      }
635      else if (delta == degG)
636      {
637        if (degG <= degF && fdivides( G, F ))
638        {
639          DEBDECLEVEL( cerr, "ezgcd" );
640          if (!isRat)
641            Off (SW_RATIONAL);
642          return N (d*G);
643        }
644        else
645          delta--;
646      }
647    }
648    if ( delta != degF && delta != degG )
649    {
650      /// ---> A6
651      bool B_is_F;
652      CanonicalForm xxx1, xxx2;
653      CanonicalForm buf;
654      DD[1] = Fb / Db;
655      buf= Gb/Db;
656      xxx1 = gcd( DD[1], Db );
657      xxx2 = gcd( buf, Db );
658      if (((xxx1.inCoeffDomain() && xxx2.inCoeffDomain()) &&
659          (size (F) <= size (G)))
660            || (xxx1.inCoeffDomain() && !xxx2.inCoeffDomain()))
661      {
662        B = F;
663        DD[2] = Db;
664        lcDD[1] = lcF;
665        lcDD[2] = lcD;
666        B_is_F = true;
667      }
668      else if (((xxx1.inCoeffDomain() && xxx2.inCoeffDomain()) &&
669                (size (G) < size (F)))
670                || (!xxx1.inCoeffDomain() && xxx2.inCoeffDomain()))
671      {
672        DD[1] = buf;
673        B = G;
674        DD[2] = Db;
675        lcDD[1] = lcG;
676        lcDD[2] = lcD;
677        B_is_F = false;
678      }
679      else
680      {
681        //special case
682        Off(SW_USE_EZGCD);
683        result=gcd( F, G );
684        On(SW_USE_EZGCD);
685        if (!isRat)
686          Off (SW_RATIONAL);
687        DEBDECLEVEL( cerr, "ezgcd" );
688        result /= icontent (result);
689        return N (d*result);
690      }
691      /// ---> A7
692      DD[2] = DD[2] * ( b( lcDD[2] ) / lc( DD[2] ) );
693      DD[1] = DD[1] * ( b( lcDD[1] ) / lc( DD[1] ) );
694      DEBOUTLN( cerr, "(hensel) B    = " << B );
695      DEBOUTLN( cerr, "(hensel) lcB  = " << LC( B, Variable(1) ) );
696      DEBOUTLN( cerr, "(hensel) b(B) = " << b(B) );
697      DEBOUTLN( cerr, "(hensel) DD   = " << DD );
698      DEBOUTLN( cerr, "(hensel) lcDD = " << lcDD );
699      TIMING_START (ez_hensel_lift)
700      gcdfound= Hensel (B*lcD, DD, b, lcDD);
701      TIMING_END_AND_PRINT (ez_hensel_lift, "time to hensel lift in EZ: ")
702      DEBOUTLN( cerr, "(hensel finished) DD   = " << DD );
703
704      if (gcdfound == -1)
705      {
706        Off (SW_USE_EZGCD);
707        result= gcd (F,G);
708        On (SW_USE_EZGCD);
709        if (!isRat)
710          Off (SW_RATIONAL);
711        DEBDECLEVEL( cerr, "ezgcd" );
712        result /= icontent (result);
713        return N (d*result);
714      }
715
716      if (gcdfound)
717      {
718        TIMING_START (ez_termination)
719        contcand= content (DD[2], Variable (1));
720        cand = DD[2] / contcand;
721        if (B_is_F)
722          gcdfound = fdivides( cand, G ) && cand*(DD[1]/(lcD/contcand)) == F;
723        else
724          gcdfound = fdivides( cand, F ) && cand*(DD[1]/(lcD/contcand)) == G;
725        TIMING_END_AND_PRINT (ez_termination,
726                              "time for termination test in EZ: ")
727      }
728      /// ---> A8 (gcdfound)
729    }
730    delta--;
731  }
732  /// ---> A9
733  DEBDECLEVEL( cerr, "ezgcd" );
734  cand *= bCommonDen (cand);
735  if (!isRat)
736    Off (SW_RATIONAL);
737  cand /= icontent (cand);
738  return N (d*cand);
739}
740#endif
741
742CanonicalForm
743ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG )
744{
745#ifdef HAVE_NTL
746  REvaluation b;
747  return ezgcd( FF, GG, b, false );
748#else
749  Off (SW_USE_EZGCD);
750  return gcd (FF, GG);
751  On (SW_USE_EZGCD);
752#endif
753}
754
Note: See TracBrowser for help on using the repository browser.