source: git/factory/fac_ezgcd.cc @ c244d04

spielwiese
Last change on this file since c244d04 was c244d04, checked in by Martin Lee <martinlee84@…>, 11 years ago
chg: deleted unused old functions + editing chg: added description
  • Property mode set to 100644
File size: 13.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#include "config.h"
16
17#include "cf_assert.h"
18#include "debug.h"
19
20#include "cf_defs.h"
21#include "canonicalform.h"
22#include "fac_util.h" // HEADER for this file
23#include "cf_algorithm.h"
24#include "cf_reval.h"
25#include "cf_random.h"
26#include "cf_primes.h"
27#include "fac_distrib.h"
28#include "templates/ftmpl_functions.h"
29#include "cf_map.h"
30#include "facHensel.h"
31
32static
33int compress4EZGCD (const CanonicalForm& F, const CanonicalForm& G, CFMap & M,
34                    CFMap & N, int& both_non_zero)
35{
36  int n= tmax (F.level(), G.level());
37  int * degsf= new int [n + 1];
38  int * degsg= new int [n + 1];
39
40  for (int i = 0; i <= n; i++)
41    degsf[i]= degsg[i]= 0;
42
43  degsf= degrees (F, degsf);
44  degsg= degrees (G, degsg);
45
46  both_non_zero= 0;
47  int f_zero= 0;
48  int g_zero= 0;
49
50  for (int i= 1; i <= n; i++)
51  {
52    if (degsf[i] != 0 && degsg[i] != 0)
53    {
54      both_non_zero++;
55      continue;
56    }
57    if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level())
58    {
59      f_zero++;
60      continue;
61    }
62    if (degsg[i] == 0 && degsf[i] && i <= F.level())
63    {
64      g_zero++;
65      continue;
66    }
67  }
68
69  if (both_non_zero == 0)
70  {
71    delete [] degsf;
72    delete [] degsg;
73    return 0;
74  }
75
76  // map Variables which do not occur in both polynomials to higher levels
77  int k= 1;
78  int l= 1;
79  for (int i= 1; i <= n; i++)
80  {
81    if (degsf[i] != 0 && degsg[i] == 0 && i <= F.level())
82    {
83      if (k + both_non_zero != i)
84      {
85        M.newpair (Variable (i), Variable (k + both_non_zero));
86        N.newpair (Variable (k + both_non_zero), Variable (i));
87      }
88      k++;
89    }
90    if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level())
91    {
92      if (l + g_zero + both_non_zero != i)
93      {
94        M.newpair (Variable (i), Variable (l + g_zero + both_non_zero));
95        N.newpair (Variable (l + g_zero + both_non_zero), Variable (i));
96      }
97      l++;
98    }
99  }
100
101  // sort Variables x_{i} in decreasing order of
102  // min(deg_{x_{i}}(f),deg_{x_{i}}(g))
103  int m= tmin (F.level(), G.level());
104  int max_min_deg;
105  k= both_non_zero;
106  l= 0;
107  int i= 1;
108  while (k > 0)
109  {
110    max_min_deg= tmin (degsf[i], degsg[i]);
111    while (max_min_deg == 0)
112    {
113      i++;
114      max_min_deg= tmin (degsf[i], degsg[i]);
115    }
116    for (int j= i + 1; j <= m; j++)
117    {
118      if ((tmin (degsf[j],degsg[j]) < max_min_deg) &&
119          (tmin (degsf[j], degsg[j]) != 0))
120      {
121        max_min_deg= tmin (degsf[j], degsg[j]);
122        l= j;
123      }
124    }
125
126    if (l != 0)
127    {
128      if (l != k)
129      {
130        M.newpair (Variable (l), Variable(k));
131        N.newpair (Variable (k), Variable(l));
132        degsf[l]= 0;
133        degsg[l]= 0;
134        l= 0;
135      }
136      else
137      {
138        degsf[l]= 0;
139        degsg[l]= 0;
140        l= 0;
141      }
142    }
143    else if (l == 0)
144    {
145      if (i != k)
146      {
147        M.newpair (Variable (i), Variable (k));
148        N.newpair (Variable (k), Variable (i));
149        degsf[i]= 0;
150        degsg[i]= 0;
151      }
152      else
153      {
154        degsf[i]= 0;
155        degsg[i]= 0;
156      }
157      i++;
158    }
159    k--;
160  }
161
162  delete [] degsf;
163  delete [] degsg;
164
165  return both_non_zero;
166}
167
168static inline
169CanonicalForm myShift2Zero (const CanonicalForm& F, CFList& Feval,
170                            const CFList& evaluation)
171{
172  CanonicalForm A= F;
173  int k= 2;
174  for (CFListIterator i= evaluation; i.hasItem(); i++, k++)
175    A= A (Variable (k) + i.getItem(), k);
176
177  CanonicalForm buf= A;
178  Feval= CFList();
179  Feval.append (buf);
180  for (k= evaluation.length() + 1; k > 2; k--)
181  {
182    buf= mod (buf, Variable (k));
183    Feval.insert (buf);
184  }
185  return A;
186}
187
188static inline
189CanonicalForm myReverseShift (const CanonicalForm& F, const CFList& evaluation)
190{
191  int l= evaluation.length() + 1;
192  CanonicalForm result= F;
193  CFListIterator j= evaluation;
194  for (int i= 2; i < l + 1; i++, j++)
195  {
196    if (F.level() < i)
197      continue;
198    result= result (Variable (i) - j.getItem(), i);
199  }
200  return result;
201}
202
203static inline
204Evaluation optimize4Lift (const CanonicalForm& F, CFMap & M,
205                    CFMap & N, const Evaluation& A)
206{
207  int n= F.level();
208  int * degsf= new int [n + 1];
209
210  for (int i = 0; i <= n; i++)
211    degsf[i]= 0;
212
213  degsf= degrees (F, degsf);
214
215  Evaluation result= Evaluation (A.min(), A.max());
216  ASSERT (A.min() == 2, "expected A.min() == 2");
217  ASSERT (A.max() == n, "expected A.max() == n");
218  int max_deg;
219  int k= n;
220  int l= 1;
221  int i= 2;
222  int pos= 2;
223  while (k > 1)
224  {
225    max_deg= degsf [i];
226    while (max_deg == 0)
227    {
228      i++;
229      max_deg= degsf [i];
230    }
231    l= i;
232    for (int j= i + 1; j <=  n; j++)
233    {
234      if (degsf[j] > max_deg)
235      {
236        max_deg= degsf[j];
237        l= j;
238      }
239    }
240
241    if (l <= n)
242    {
243      if (l != pos)
244      {
245        result.setValue (pos, A [l]);
246        M.newpair (Variable (l), Variable (pos));
247        N.newpair (Variable (pos), Variable (l));
248        degsf[l]= 0;
249        l= 2;
250        if (k == 2 && n == 3)
251        {
252          result.setValue (l, A [pos]);
253          M.newpair (Variable (pos), Variable (l));
254          N.newpair (Variable (l), Variable (pos));
255          degsf[pos]= 0;
256        }
257      }
258      else
259      {
260        result.setValue (l, A [l]);
261        degsf [l]= 0;
262      }
263    }
264    pos++;
265    k--;
266    l= 2;
267  }
268
269  delete [] degsf;
270
271  return result;
272}
273
274static inline
275int Hensel (const CanonicalForm & UU, CFArray & G, const Evaluation & AA,
276            const CFArray& LeadCoeffs )
277{
278  CFList factors;
279  factors.append (G[1]);
280  factors.append (G[2]);
281
282  CFMap NN, MM;
283  Evaluation A= optimize4Lift (UU, MM, NN, AA);
284
285  CanonicalForm U= MM (UU);
286  CFArray LCs= CFArray (1,2);
287  LCs [1]= MM (LeadCoeffs [1]);
288  LCs [2]= MM (LeadCoeffs [2]);
289
290  CFList evaluation;
291  for (int i= A.min(); i <= A.max(); i++)
292    evaluation.append (A [i]);
293  CFList UEval;
294  CanonicalForm shiftedU= myShift2Zero (U, UEval, evaluation);
295
296  if (size (shiftedU)/getNumVars (U) > 500)
297    return -1;
298
299  CFArray shiftedLCs= CFArray (2);
300  CFList shiftedLCsEval1, shiftedLCsEval2;
301  shiftedLCs[0]= myShift2Zero (LCs[1], shiftedLCsEval1, evaluation);
302  shiftedLCs[1]= myShift2Zero (LCs[2], shiftedLCsEval2, evaluation);
303  factors.insert (1);
304  int liftBound= degree (UEval.getLast(), 2) + 1;
305  CFArray Pi;
306  CFMatrix M= CFMatrix (liftBound, factors.length() - 1);
307  CFList diophant;
308  CFArray lcs= CFArray (2);
309  lcs [0]= shiftedLCsEval1.getFirst();
310  lcs [1]= shiftedLCsEval2.getFirst();
311  nonMonicHenselLift12 (UEval.getFirst(), factors, liftBound, Pi, diophant, M,
312                        lcs, false);
313
314  for (CFListIterator i= factors; i.hasItem(); i++)
315  {
316    if (!fdivides (i.getItem(), UEval.getFirst()))
317      return 0;
318  }
319
320  int * liftBounds;
321  bool noOneToOne= false;
322  if (U.level() > 2)
323  {
324    liftBounds= new int [U.level() - 1]; /* index: 0.. U.level()-2 */
325    liftBounds[0]= liftBound;
326    for (int i= 1; i < U.level() - 1; i++)
327      liftBounds[i]= degree (shiftedU, Variable (i + 2)) + 1;
328    factors= nonMonicHenselLift2 (UEval, factors, liftBounds, U.level() - 1,
329                                  false, shiftedLCsEval1, shiftedLCsEval2, Pi,
330                                  diophant, noOneToOne);
331    delete [] liftBounds;
332    if (noOneToOne)
333      return 0;
334  }
335  G[1]= factors.getFirst();
336  G[2]= factors.getLast();
337  G[1]= myReverseShift (G[1], evaluation);
338  G[2]= myReverseShift (G[2], evaluation);
339  G[1]= NN (G[1]);
340  G[2]= NN (G[2]);
341  return 1;
342}
343
344static CanonicalForm
345ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG, REvaluation & b,
346        bool internal )
347{
348  bool isRat= isOn (SW_RATIONAL);
349  if (!isRat)
350    On (SW_RATIONAL);
351  CanonicalForm F, G, f, g, d, Fb, Gb, Db, Fbt, Gbt, Dbt, B0, B, D0, lcF, lcG,
352                lcD, cand, result;
353  CFArray DD( 1, 2 ), lcDD( 1, 2 );
354  int degF, degG, delta, t;
355  REvaluation bt;
356  bool gcdfound = false;
357  Variable x = Variable(1);
358  modpk bound;
359
360  F= FF;
361  G= GG;
362
363  CFMap M,N;
364  int smallestDegLev;
365  int best_level= compress4EZGCD (F, G, M, N, smallestDegLev);
366
367  if (best_level == 0)
368  {
369    if (!isRat)
370      Off (SW_RATIONAL);
371    return G.genOne();
372  }
373
374  F= M (F);
375  G= M (G);
376
377
378  DEBINCLEVEL( cerr, "ezgcd" );
379  DEBOUTLN( cerr, "FF = " << FF );
380  DEBOUTLN( cerr, "GG = " << GG );
381  f = content( F, x ); g = content( G, x ); d = gcd( f, g );
382  DEBOUTLN( cerr, "f = " << f );
383  DEBOUTLN( cerr, "g = " << g );
384  F /= f; G /= g;
385  if ( F.isUnivariate() && G.isUnivariate() )
386  {
387    DEBDECLEVEL( cerr, "ezgcd" );
388    if (!isRat)
389      Off (SW_RATIONAL);
390    if(F.mvar()==G.mvar())
391      d*=gcd(F,G);
392    return N (d);
393  }
394  else  if ( gcd_test_one( F, G, false ) )
395  {
396    DEBDECLEVEL( cerr, "ezgcd" );
397    if (!isRat)
398      Off (SW_RATIONAL);
399    return N (d);
400  }
401  lcF = LC( F, x ); lcG = LC( G, x );
402  lcD = gcd( lcF, lcG );
403  delta = 0;
404  degF = degree( F, x ); degG = degree( G, x );
405  t = tmax( F.level(), G.level() );
406  //bound = findBound( F, G, lcF, lcG, degF, degG );
407  if ( ! internal )
408    b = REvaluation( 2, t, IntRandom( 25 ) );
409  while ( ! gcdfound )
410  {
411    /// ---> A2
412    DEBOUTLN( cerr, "search for evaluation, delta = " << delta );
413    DEBOUTLN( cerr, "F = " << F );
414    DEBOUTLN( cerr, "G = " << G );
415    findeval( F, G, Fb, Gb, Db, b, delta, degF, degG );
416    DEBOUTLN( cerr, "found evaluation b = " << b );
417    DEBOUTLN( cerr, "F(b) = " << Fb );
418    DEBOUTLN( cerr, "G(b) = " << Gb );
419    DEBOUTLN( cerr, "D(b) = " << Db );
420    delta = degree( Db );
421    /// ---> A3
422    if ( delta == 0 )
423    {
424      DEBDECLEVEL( cerr, "ezgcd" );
425      if (!isRat)
426        Off (SW_RATIONAL);
427      return N (d);
428    }
429    /// ---> A4
430    //deltaold = delta;
431    while ( 1 ) 
432    {
433      bt = b;
434      findeval( F, G, Fbt, Gbt, Dbt, bt, delta + 1, degF, degG );
435      int dd=degree( Dbt );
436      if ( dd /*degree( Dbt )*/ == 0 )
437      {
438        DEBDECLEVEL( cerr, "ezgcd" );
439        if (!isRat)
440          Off (SW_RATIONAL);
441        return N (d);
442      }
443      if ( dd /*degree( Dbt )*/ == delta )
444        break;
445      else  if ( dd /*degree( Dbt )*/ < delta )
446      {
447        delta = dd /*degree( Dbt )*/;
448        b = bt;
449        Db = Dbt; Fb = Fbt; Gb = Gbt;
450      }
451    }
452    DEBOUTLN( cerr, "now after A4, delta = " << delta );
453    /// ---> A5
454    if (delta == degF)
455    {
456      if (degF <= degG  && fdivides (F, G))
457      {
458        DEBDECLEVEL( cerr, "ezgcd" );
459        if (!isRat)
460          Off (SW_RATIONAL);
461        return N (d*F);
462      }
463      else
464        delta--;
465    }
466    else if (delta == degG)
467    {
468      if (degG <= degF && fdivides( G, F ))
469      {
470        DEBDECLEVEL( cerr, "ezgcd" );
471        if (!isRat)
472          Off (SW_RATIONAL);
473        return N (d*G);
474      }
475      else
476        delta--;
477    }
478    if ( delta != degF && delta != degG )
479    {
480      /// ---> A6
481      bool B_is_F;
482      CanonicalForm xxx1, xxx2;
483      CanonicalForm buf;
484      DD[1] = Fb / Db;
485      buf= Gb/Db;
486      xxx1 = gcd( DD[1], Db );
487      xxx2 = gcd( buf, Db );
488      if (((xxx1.inCoeffDomain() && xxx2.inCoeffDomain()) &&
489          (size (F) <= size (G)))
490            || (xxx1.inCoeffDomain() && !xxx2.inCoeffDomain()))
491      {
492        B = F;
493        DD[2] = Db;
494        lcDD[1] = lcF;
495        lcDD[2] = lcD;
496        B_is_F = true;
497      }
498      else if (((xxx1.inCoeffDomain() && xxx2.inCoeffDomain()) &&
499                (size (G) < size (F)))
500                || (!xxx1.inCoeffDomain() && xxx2.inCoeffDomain()))
501      {
502        DD[1] = buf;
503        B = G;
504        DD[2] = Db;
505        lcDD[1] = lcG;
506        lcDD[2] = lcD;
507        B_is_F = false;
508      }
509      else
510      {
511        //special case
512        Off(SW_USE_EZGCD);
513        result=gcd( F, G );
514        On(SW_USE_EZGCD);
515        if (!isRat)
516          Off (SW_RATIONAL);
517        return N (d*result);
518      }
519      /// ---> A7
520      DD[2] = DD[2] * ( b( lcDD[2] ) / lc( DD[2] ) );
521      DD[1] = DD[1] * ( b( lcDD[1] ) / lc( DD[1] ) );
522      DEBOUTLN( cerr, "(hensel) B    = " << B );
523      DEBOUTLN( cerr, "(hensel) lcB  = " << LC( B, Variable(1) ) );
524      DEBOUTLN( cerr, "(hensel) b(B) = " << b(B) );
525      DEBOUTLN( cerr, "(hensel) DD   = " << DD );
526      DEBOUTLN( cerr, "(hensel) lcDD = " << lcDD );
527      gcdfound= Hensel (B*lcD, DD, b, lcDD);
528      DEBOUTLN( cerr, "(hensel finished) DD   = " << DD );
529
530      if (gcdfound == -1)
531      {
532        Off (SW_USE_EZGCD);
533        result= gcd (F,G);
534        On (SW_USE_EZGCD);
535        if (!isRat)
536          Off (SW_RATIONAL);
537        return N (d*result);
538      }
539
540      if (gcdfound)
541      {
542        cand = DD[2] / content( DD[2], Variable(1) );
543        gcdfound = fdivides( cand, G ) && fdivides ( cand, F );
544      }
545      /// ---> A8 (gcdfound)
546    }
547    delta--;
548  }
549  /// ---> A9
550  DEBDECLEVEL( cerr, "ezgcd" );
551  if (!isRat)
552    Off (SW_RATIONAL);
553  return N (d*cand);
554}
555
556CanonicalForm
557ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG )
558{
559  REvaluation b;
560  return ezgcd( FF, GG, b, false );
561}
562
Note: See TracBrowser for help on using the repository browser.