source: git/factory/algext.cc @ 6f08f3

spielwiese
Last change on this file since 6f08f3 was 6f08f3, checked in by Martin Lee <martinlee84@…>, 12 years ago
create Variable(1) just once git-svn-id: file:///usr/local/Singular/svn/trunk@14315 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 30.7 KB
Line 
1#include "factoryconf.h"
2
3#ifdef HAVE_CSTDIO
4#include <cstdio>
5#else
6#include <stdio.h>
7#endif
8#ifndef NOSTREAMIO
9#ifdef HAVE_IOSTREAM_H
10#include <iostream.h>
11#elif defined(HAVE_IOSTREAM)
12#include <iostream>
13#endif
14#endif
15
16#include "templates/ftmpl_functions.h"
17#include "cf_defs.h"
18#include "canonicalform.h"
19#include "cf_iter.h"
20#include "cf_primes.h"
21#include "cf_algorithm.h"
22#include "algext.h"
23#include "fieldGCD.h"
24#include "cf_map.h"
25#include "cf_generator.h"
26
27/// compressing two polynomials F and G, M is used for compressing,
28/// N to reverse the compression
29static
30int myCompress (const CanonicalForm& F, const CanonicalForm& G, CFMap & M,
31                CFMap & N, bool topLevel)
32{
33  int n= tmax (F.level(), G.level());
34  int * degsf= new int [n + 1];
35  int * degsg= new int [n + 1];
36
37  for (int i = 0; i <= n; i++)
38    degsf[i]= degsg[i]= 0;
39
40  degsf= degrees (F, degsf);
41  degsg= degrees (G, degsg);
42
43  int both_non_zero= 0;
44  int f_zero= 0;
45  int g_zero= 0;
46  int both_zero= 0;
47
48  if (topLevel)
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 increasing order of
102    // min(deg_{x_{i}}(f),deg_{x_{i}}(g))
103    int m= tmax (F.level(), G.level());
104    int min_max_deg;
105    k= both_non_zero;
106    l= 0;
107    int i= 1;
108    while (k > 0)
109    {
110      if (degsf [i] != 0 && degsg [i] != 0)
111        min_max_deg= tmax (degsf[i], degsg[i]);
112      else
113        min_max_deg= 0;
114      while (min_max_deg == 0)
115      {
116        i++;
117        min_max_deg= tmax (degsf[i], degsg[i]);
118        if (degsf [i] != 0 && degsg [i] != 0)
119          min_max_deg= tmax (degsf[i], degsg[i]);
120        else
121          min_max_deg= 0;
122      }
123      for (int j= i + 1; j <=  m; j++)
124      {
125        if (tmax (degsf[j],degsg[j]) <= min_max_deg && degsf[j] != 0 && degsg [j] != 0)
126        {
127          min_max_deg= tmax (degsf[j], degsg[j]);
128          l= j;
129        }
130      }
131      if (l != 0)
132      {
133        if (l != k)
134        {
135          M.newpair (Variable (l), Variable(k));
136          N.newpair (Variable (k), Variable(l));
137          degsf[l]= 0;
138          degsg[l]= 0;
139          l= 0;
140        }
141        else
142        {
143          degsf[l]= 0;
144          degsg[l]= 0;
145          l= 0;
146        }
147      }
148      else if (l == 0)
149      {
150        if (i != k)
151        {
152          M.newpair (Variable (i), Variable (k));
153          N.newpair (Variable (k), Variable (i));
154          degsf[i]= 0;
155          degsg[i]= 0;
156        }
157        else
158        {
159          degsf[i]= 0;
160          degsg[i]= 0;
161        }
162        i++;
163      }
164      k--;
165    }
166  }
167  else
168  {
169    //arrange Variables such that no gaps occur
170    for (int i= 1; i <= n; i++)
171    {
172      if (degsf[i] == 0 && degsg[i] == 0)
173      {
174        both_zero++;
175        continue;
176      }
177      else
178      {
179        if (both_zero != 0)
180        {
181          M.newpair (Variable (i), Variable (i - both_zero));
182          N.newpair (Variable (i - both_zero), Variable (i));
183        }
184      }
185    }
186  }
187
188  delete [] degsf;
189  delete [] degsg;
190
191  return 1;
192}
193
194CanonicalForm reduce(const CanonicalForm & f, const CanonicalForm & M)
195{ // polynomials in M.mvar() are considered coefficients
196  // M univariate monic polynomial
197  // the coefficients of f are reduced modulo M
198  if(f.inBaseDomain() || f.level() < M.level())
199    return f;
200  if(f.level() == M.level())
201  {
202    if(f.degree() < M.degree())
203      return f;
204    CanonicalForm tmp = mod (f, M);
205    return tmp;
206  }
207  // here: f.level() > M.level()
208  CanonicalForm result = 0;
209  for(CFIterator i=f; i.hasTerms(); i++)
210    result += reduce(i.coeff(),M) * power(f.mvar(),i.exp());
211  return result;
212}
213
214void tryInvert( const CanonicalForm & F, const CanonicalForm & M, CanonicalForm & inv, bool & fail )
215{ // F, M are required to be "univariate" polynomials in an algebraic variable
216  // we try to invert F modulo M
217  if(F.inBaseDomain())
218  {
219    if(F.isZero())
220    {
221      fail = true;
222      return;
223    }
224    inv = 1/F;
225    return;
226  }
227  CanonicalForm b;
228  Variable a = M.mvar();
229  Variable x = Variable(1);
230  if(!extgcd( replacevar( F, a, x ), replacevar( M, a, x ), inv, b ).isOne())
231    fail = true;
232  else
233    inv = replacevar( inv, x, a ); // change back to alg var
234}
235
236void tryDivrem (const CanonicalForm& F, const CanonicalForm& G, CanonicalForm& Q,
237                CanonicalForm& R, CanonicalForm& inv, const CanonicalForm& mipo,
238                bool& fail)
239{
240  if (F.inCoeffDomain())
241  {
242    Q= 0;
243    R= F;
244    return;
245  }
246
247  CanonicalForm A, B;
248  Variable x= F.mvar();
249  A= F;
250  B= G;
251  int degA= degree (A, x);
252  int degB= degree (B, x);
253
254  if (degA < degB)
255  {
256    R= A;
257    Q= 0;
258    return;
259  }
260
261  tryInvert (Lc (B), mipo, inv, fail);
262  if (fail)
263    return;
264
265  R= A;
266  Q= 0;
267  CanonicalForm Qi;
268  for (int i= degA -degB; i >= 0; i--)
269  {
270    if (degree (R, x) == i + degB)
271    {
272      Qi= Lc (R)*inv*power (x, i);
273      Qi= reduce (Qi, mipo);
274      R -= Qi*B;
275      R= reduce (R, mipo);
276      Q += Qi;
277    }
278  }
279}
280
281void tryEuclid( const CanonicalForm & A, const CanonicalForm & B, const CanonicalForm & M, CanonicalForm & result, bool & fail )
282{
283  CanonicalForm P;
284  if(A.inCoeffDomain())
285  {
286    tryInvert( A, M, P, fail );
287    if(fail)
288      return;
289    result = 1;
290    return;
291  }
292  if(B.inCoeffDomain())
293  {
294    tryInvert( B, M, P, fail );
295    if(fail)
296      return;
297    result = 1;
298    return;
299  }
300  // here: both not inCoeffDomain
301  if( A.degree() > B.degree() )
302  {
303    P = A; result = B;
304  }
305  else
306  {
307    P = B; result = A;
308  }
309  CanonicalForm inv;
310  if( result.isZero() )
311  {
312    tryInvert( Lc(P), M, inv, fail );
313    if(fail)
314      return;
315    result = inv*P; // monify result (not reduced, yet)
316    return;
317  }
318  Variable x = P.mvar();
319  CanonicalForm rem, Q;
320  // here: degree(P) >= degree(result)
321  while(true)
322  {
323    tryDivrem (P, result, Q, rem, inv, M, fail);
324    if (fail)
325      return;
326    if( rem.isZero() )
327    {
328      result *= inv;
329      return;
330    }
331    if(result.degree(x) >= rem.degree(x))
332    {
333      P = result;
334      result = rem;
335    }
336    else
337      P = rem;
338  }
339}
340
341bool hasFirstAlgVar( const CanonicalForm & f, Variable & a )
342{
343  if( f.inBaseDomain() ) // f has NO alg. variable
344    return false;
345  if( f.level()<0 ) // f has only alg. vars, so take the first one
346  {
347    a = f.mvar();
348    return true;
349  }
350  for(CFIterator i=f; i.hasTerms(); i++)
351    if( hasFirstAlgVar( i.coeff(), a ))
352      return true; // 'a' is already set
353  return false;
354}
355
356CanonicalForm QGCD( const CanonicalForm & F, const CanonicalForm & G );
357int * leadDeg(const CanonicalForm & f, int *degs);
358bool isLess(int *a, int *b, int lower, int upper);
359bool isEqual(int *a, int *b, int lower, int upper);
360CanonicalForm firstLC(const CanonicalForm & f);
361void tryCRA( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, const CanonicalForm & M, CanonicalForm & xnew, CanonicalForm & qnew, bool & fail );
362void tryExtgcd( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, CanonicalForm & s, CanonicalForm & t, bool & fail );
363static CanonicalForm trycontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail );
364static CanonicalForm tryvcontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail );
365static CanonicalForm trycf_content ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, bool & fail );
366static void tryDivide( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, CanonicalForm & result, bool & divides, bool & fail );
367
368
369void tryBrownGCD( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, bool & fail, bool topLevel )
370{ // assume F,G are multivariate polys over Z/p(a) for big prime p, M "univariate" polynomial in an algebraic variable
371  // M is assumed to be monic
372  if(F.isZero())
373  {
374    if(G.isZero())
375    {
376      result = G; // G is zero
377      return;
378    }
379    if(G.inCoeffDomain())
380    {
381      tryInvert(G,M,result,fail);
382      if(fail)
383        return;
384      result = 1;
385      return;
386    }
387    // try to make G monic modulo M
388    CanonicalForm inv;
389    tryInvert(Lc(G),M,inv,fail);
390    if(fail)
391      return;
392    result = inv*G;
393    return;
394  }
395  if(G.isZero()) // F is non-zero
396  {
397    if(F.inCoeffDomain())
398    {
399      tryInvert(F,M,result,fail);
400      if(fail)
401        return;
402      result = 1;
403      return;
404    }
405    // try to make F monic modulo M
406    CanonicalForm inv;
407    tryInvert(Lc(F),M,inv,fail);
408    if(fail)
409      return;
410    result = inv*F;
411    return;
412  }
413  // here: F,G both nonzero
414  if(F.inCoeffDomain())
415  {
416    tryInvert(F,M,result,fail);
417    if(fail)
418      return;
419    result = 1;
420    return;
421  }
422  if(G.inCoeffDomain())
423  {
424    tryInvert(G,M,result,fail);
425    if(fail)
426      return;
427    result = 1;
428    return;
429  }
430  CFMap MM,NN;
431  int lev= myCompress (F, G, MM, NN, topLevel);
432  if (lev == 0)
433  {
434    result= 1;
435    return;
436  }
437  CanonicalForm f=MM(F);
438  CanonicalForm g=MM(G);
439  // here: f,g are compressed
440  // compute largest variable in f or g (least one is Variable(1))
441  int mv = f.level();
442  if(g.level() > mv)
443    mv = g.level();
444  // here: mv is level of the largest variable in f, g
445  if(mv == 1) // f,g univariate
446  {
447    tryEuclid(f,g,M,result,fail);
448    if(fail)
449      return;
450    result = NN(result); // do not forget to map back
451    return;
452  }
453  // here: mv > 1
454  CanonicalForm cf = tryvcontent(f, Variable(2), M, fail); // cf is univariate poly in var(1)
455  if(fail)
456    return;
457  CanonicalForm cg = tryvcontent(g, Variable(2), M, fail);
458  if(fail)
459    return;
460  CanonicalForm c;
461  tryEuclid(cf,cg,M,c,fail);
462  if(fail)
463    return;
464  bool divides = true;
465  CanonicalForm tmp;
466  // f /= cf
467  tryDivide(f,cf,M,tmp,divides,fail); // 'divides' is not checked
468  if(fail)
469    return;
470  f = tmp;
471  // g /= cg
472  tryDivide(g,cg,M,tmp,divides,fail); // 'divides' is not checked
473  if(fail)
474    return;
475  g = tmp;
476  if(f.inCoeffDomain())
477  {
478    tryInvert(f,M,result,fail);
479    if(fail)
480      return;
481    result = NN(c);
482    return;
483  }
484  if(g.inCoeffDomain())
485  {
486    tryInvert(g,M,result,fail);
487    if(fail)
488      return;
489    result = NN(c);
490    return;
491  }
492  int *L = new int[mv+1]; // L is addressed by i from 2 to mv
493  int *N = new int[mv+1];
494  for(int i=2; i<=mv; i++)
495    L[i] = N[i] = 0;
496  L = leadDeg(f, L);
497  N = leadDeg(g, N);
498  CanonicalForm gamma;
499  tryEuclid( firstLC(f), firstLC(g), M, gamma, fail );
500  if(fail)
501    return;
502  for(int i=2; i<=mv; i++) // entries at i=0,1 not visited
503    if(N[i] < L[i])
504      L[i] = N[i];
505  // L is now upper bound for degrees of gcd
506  int *dg_im = new int[mv+1]; // for the degree vector of the image we don't need any entry at i=1
507  for(int i=2; i<=mv; i++)
508    dg_im[i] = 0; // initialize
509  CanonicalForm gamma_image, m=1;
510  CanonicalForm gm=0;
511  CanonicalForm g_image, alpha, gnew, mnew;
512  FFGenerator gen = FFGenerator();
513  Variable x= Variable (1);
514  for(FFGenerator gen = FFGenerator(); gen.hasItems(); gen.next())
515  {
516    alpha = gen.item();
517    gamma_image = reduce(gamma(alpha, x),M); // plug in alpha for var(1)
518    if(gamma_image.isZero()) // skip lc-bad points var(1)-alpha
519      continue;
520    tryBrownGCD( f(alpha, x), g(alpha, x), M, g_image, fail, false ); // recursive call with one var less
521    if(fail)
522      return;
523    g_image = reduce(g_image, M);
524    if(g_image.inCoeffDomain()) // early termination
525    {
526      tryInvert(g_image,M,result,fail);
527      if(fail)
528        return;
529      result = NN(c);
530      return;
531    }
532    for(int i=2; i<=mv; i++)
533      dg_im[i] = 0; // reset (this is necessary, because some entries may not be updated by call to leadDeg)
534    dg_im = leadDeg(g_image, dg_im); // dg_im cannot be NIL-pointer
535    if(isEqual(dg_im, L, 2, mv))
536    {
537      g_image /= lc(g_image); // make g_image monic over Z/p
538      g_image *= gamma_image; // multiply by multiple of image lc(gcd)
539      tryCRA( g_image, x-alpha, gm, m, M, gnew, mnew, fail );
540      // gnew = gm mod m
541      // gnew = g_image mod var(1)-alpha
542      // mnew = m * (var(1)-alpha)
543      if(fail)
544        return;
545      m = mnew;
546      if(gnew == gm) // gnew did not change
547      {
548        cf = tryvcontent(gm, Variable(2), M, fail);
549        if(fail)
550          return;
551        divides = true;
552        tryDivide(gm,cf,M,g_image,divides,fail); // 'divides' is ignored
553        if(fail)
554          return;
555        tryDivide(f,g_image,M,tmp,divides,fail); // trial division (f)
556        if(fail)
557          return;
558        if(divides)
559        {
560          tryDivide(g,g_image,M,tmp,divides,fail); // trial division (g)
561          if(fail)
562            return;
563          if(divides)
564          {
565            result = NN(c*g_image);
566            return;
567          }
568        }
569      }
570      gm = gnew;
571      continue;
572    }
573
574    if(isLess(L, dg_im, 2, mv)) // dg_im > L --> current point unlucky
575      continue;
576
577    // here: isLess(dg_im, L, 2, mv) --> all previous points were unlucky
578    m = CanonicalForm(1); // reset
579    gm = 0; // reset
580    for(int i=2; i<=mv; i++) // tighten bound
581      L[i] = dg_im[i];
582  }
583  // we are out of evaluation points
584  fail = true;
585}
586
587CanonicalForm QGCD( const CanonicalForm & F, const CanonicalForm & G )
588{ // f,g in Q(a)[x1,...,xn]
589  if(F.isZero())
590  {
591    if(G.isZero())
592      return G; // G is zero
593    if(G.inCoeffDomain())
594      return CanonicalForm(1);
595    return G/Lc(G); // return monic G
596  }
597  if(G.isZero()) // F is non-zero
598  {
599    if(F.inCoeffDomain())
600      return CanonicalForm(1);
601    return F/Lc(F); // return monic F
602  }
603  if(F.inCoeffDomain() || G.inCoeffDomain())
604    return CanonicalForm(1);
605  // here: both NOT inCoeffDomain
606  CanonicalForm f, g, tmp, M, q, D, Dp, cl, newq, mipo;
607  int p, i;
608  int *bound, *other; // degree vectors
609  bool fail;
610  bool off_rational=!isOn(SW_RATIONAL);
611  On( SW_RATIONAL ); // needed by bCommonDen
612  f = F * bCommonDen(F);
613  g = G * bCommonDen(G);
614  Variable a, b;
615  if(hasFirstAlgVar(f,a))
616  {
617    if(hasFirstAlgVar(g,b))
618    {
619      if(b.level() > a.level())
620        a = b;
621    }
622  }
623  else
624  {
625    if(!hasFirstAlgVar(g,a))// both not in extension
626    {
627      Off( SW_RATIONAL );
628      Off( SW_USE_QGCD );
629      tmp = gcd( F, G );
630      On( SW_USE_QGCD );
631      if (off_rational) Off(SW_RATIONAL);
632      return tmp;
633    }
634  }
635  // here: a is the biggest alg. var in f and g AND some of f,g is in extension
636  // (in the sequel b is used to swap alg/poly vars)
637  setReduce(a,false); // do not reduce expressions modulo mipo
638  tmp = getMipo(a);
639  M = tmp * bCommonDen(tmp);
640  // here: f, g in Z[a][x1,...,xn], M in Z[a] not necessarily monic
641  Off( SW_RATIONAL ); // needed by mod
642  // calculate upper bound for degree vector of gcd
643  int mv = f.level(); i = g.level();
644  if(i > mv)
645    mv = i;
646  // here: mv is level of the largest variable in f, g
647  b = Variable(mv+1);
648  bound = new int[mv+1]; // 'bound' could be indexed from 0 to mv, but we will only use from 1 to mv
649  other = new int[mv+1];
650  for(int i=1; i<=mv; i++) // initialize 'bound', 'other' with zeros
651    bound[i] = other[i] = 0;
652  bound = leadDeg(f,bound); // 'bound' is set the leading degree vector of f
653  other = leadDeg(g,other);
654  for(int i=1; i<=mv; i++) // entry at i=0 not visited
655    if(other[i] < bound[i])
656      bound[i] = other[i];
657  // now 'bound' is the smaller vector
658  cl = lc(M) * lc(f) * lc(g);
659  q = 1;
660  D = 0;
661  CanonicalForm test= 0;
662  bool equal= false;
663  for( i=cf_getNumBigPrimes()-1; i>-1; i-- )
664  {
665    p = cf_getBigPrime(i);
666    if( mod( cl, p ).isZero() ) // skip lc-bad primes
667      continue;
668    fail = false;
669    setCharacteristic(p);
670    mipo = mapinto(M);
671    mipo /= mipo.lc();
672    // here: mipo is monic
673    tryBrownGCD( mapinto(f), mapinto(g), mipo, Dp, fail );
674    Dp = reduce( Dp, mipo );
675    setCharacteristic(0);
676    if( fail ) // mipo splits in char p
677      continue;
678    if( Dp.inCoeffDomain() ) // early termination
679    {
680      tryInvert(Dp,mipo,tmp,fail); // check if zero divisor
681      if(fail)
682        continue;
683      setReduce(a,true);
684      if (off_rational) Off(SW_RATIONAL); else On(SW_RATIONAL);
685      return CanonicalForm(1);
686    }
687    // here: Dp NOT inCoeffDomain
688    for(int i=1; i<=mv; i++)
689      other[i] = 0; // reset (this is necessary, because some entries may not be updated by call to leadDeg)
690    other = leadDeg(Dp,other);
691
692    if(isEqual(bound, other, 1, mv)) // equal
693    {
694      chineseRemainder( D, q, replacevar( mapinto(Dp), a, b ), p, tmp, newq );
695      // tmp = Dp mod p
696      // tmp = D mod q
697      // newq = p*q
698      q = newq;
699      if( D != tmp )
700        D = tmp;
701      On( SW_RATIONAL );
702      tmp = replacevar( Farey( D, q ), b, a ); // Farey and switch back to alg var
703      setReduce(a,true); // reduce expressions modulo mipo
704      On( SW_RATIONAL ); // needed by fdivides
705      if (test != tmp)
706        test= tmp;
707      else
708        equal= true; // modular image did not add any new information
709      if(equal && fdivides( tmp, f ) && fdivides( tmp, g )) // trial division
710      {
711        Off( SW_RATIONAL );
712        setReduce(a,true);
713        if (off_rational) Off(SW_RATIONAL); else On(SW_RATIONAL);
714        return tmp;
715      }
716      Off( SW_RATIONAL );
717      setReduce(a,false); // do not reduce expressions modulo mipo
718      continue;
719    }
720    if( isLess(bound, other, 1, mv) ) // current prime unlucky
721      continue;
722    // here: isLess(other, bound, 1, mv) ) ==> all previous primes unlucky
723    q = p;
724    D = replacevar( mapinto(Dp), a, b ); // shortcut CRA // shortcut CRA
725    for(int i=1; i<=mv; i++) // tighten bound
726      bound[i] = other[i];
727  }
728  // hopefully, we never reach this point
729  setReduce(a,true);
730  Off( SW_USE_QGCD );
731  D = gcd( f, g );
732  On( SW_USE_QGCD );
733  if (off_rational) Off(SW_RATIONAL); else On(SW_RATIONAL);
734  return D;
735}
736
737
738int * leadDeg(const CanonicalForm & f, int *degs)
739{ // leading degree vector w.r.t. lex. monomial order x(i+1) > x(i)
740  // if f is in a coeff domain, the zero pointer is returned
741  // 'a' should point to an array of sufficient size level(f)+1
742  if(f.inCoeffDomain())
743    return 0;
744  CanonicalForm tmp = f;
745  do
746  {
747    degs[tmp.level()] = tmp.degree();
748    tmp = LC(tmp);
749  }
750  while(!tmp.inCoeffDomain());
751  return degs;
752}
753
754
755bool isLess(int *a, int *b, int lower, int upper)
756{ // compares the degree vectors a,b on the specified part. Note: x(i+1) > x(i)
757  for(int i=upper; i>=lower; i--)
758    if(a[i] == b[i])
759      continue;
760    else
761      return a[i] < b[i];
762  return true;
763}
764
765
766bool isEqual(int *a, int *b, int lower, int upper)
767{ // compares the degree vectors a,b on the specified part. Note: x(i+1) > x(i)
768  for(int i=lower; i<=upper; i++)
769    if(a[i] != b[i])
770      return false;
771  return true;
772}
773
774
775CanonicalForm firstLC(const CanonicalForm & f)
776{ // returns the leading coefficient (LC) of level <= 1
777  CanonicalForm ret = f;
778  while(ret.level() > 1)
779    ret = LC(ret);
780  return ret;
781}
782
783
784void tryCRA( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, const CanonicalForm & M, CanonicalForm & xnew, CanonicalForm & qnew, bool & fail )
785{ // as CRA, but takes care of zero divisors
786  CanonicalForm tmp;
787  if(x1.level() <= 1 && x2.level() <= 1) // base case
788  {
789    tryExtgcd(q1,q2,M,tmp,xnew,qnew,fail);
790    if(fail)
791      return;
792    xnew = x1 + (x2-x1) * xnew * q1;
793    qnew = q1*q2;
794    xnew = mod(xnew,qnew);
795    return;
796  }
797  CanonicalForm tmp2;
798  xnew = 0;
799  qnew = q1 * q2;
800  // here: x1.level() > 1 || x2.level() > 1
801  if(x1.level() > x2.level())
802  {
803    for(CFIterator i=x1; i.hasTerms(); i++)
804    {
805      if(i.exp() == 0) // const. term
806      {
807        tryCRA(i.coeff(),q1,x2,q2,M,tmp,tmp2,fail);
808        if(fail)
809          return;
810        xnew += tmp;
811      }
812      else
813      {
814        tryCRA(i.coeff(),q1,0,q2,M,tmp,tmp2,fail);
815        if(fail)
816          return;
817        xnew += tmp * power(x1.mvar(),i.exp());
818      }
819    }
820    return;
821  }
822  // here: x1.level() <= x2.level() && ( x1.level() > 1 || x2.level() > 1 )
823  if(x2.level() > x1.level())
824  {
825    for(CFIterator j=x2; j.hasTerms(); j++)
826    {
827      if(j.exp() == 0) // const. term
828      {
829        tryCRA(x1,q1,j.coeff(),q2,M,tmp,tmp2,fail);
830        if(fail)
831          return;
832        xnew += tmp;
833      }
834      else
835      {
836        tryCRA(0,q1,j.coeff(),q2,M,tmp,tmp2,fail);
837        if(fail)
838          return;
839        xnew += tmp * power(x2.mvar(),j.exp());
840      }
841    }
842    return;
843  }
844  // here: x1.level() == x2.level() && x1.level() > 1 && x2.level() > 1
845  CFIterator i = x1;
846  CFIterator j = x2;
847  while(i.hasTerms() || j.hasTerms())
848  {
849    if(i.hasTerms())
850    {
851      if(j.hasTerms())
852      {
853        if(i.exp() == j.exp())
854        {
855          tryCRA(i.coeff(),q1,j.coeff(),q2,M,tmp,tmp2,fail);
856          if(fail)
857            return;
858          xnew += tmp * power(x1.mvar(),i.exp());
859          i++; j++;
860        }
861        else
862        {
863          if(i.exp() < j.exp())
864          {
865            tryCRA(i.coeff(),q1,0,q2,M,tmp,tmp2,fail);
866            if(fail)
867              return;
868            xnew += tmp * power(x1.mvar(),i.exp());
869            i++;
870          }
871          else // i.exp() > j.exp()
872          {
873            tryCRA(0,q1,j.coeff(),q2,M,tmp,tmp2,fail);
874            if(fail)
875              return;
876            xnew += tmp * power(x1.mvar(),j.exp());
877            j++;
878          }
879        }
880      }
881      else // j is out of terms
882      {
883        tryCRA(i.coeff(),q1,0,q2,M,tmp,tmp2,fail);
884        if(fail)
885          return;
886        xnew += tmp * power(x1.mvar(),i.exp());
887        i++;
888      }
889    }
890    else // i is out of terms
891    {
892      tryCRA(0,q1,j.coeff(),q2,M,tmp,tmp2,fail);
893      if(fail)
894        return;
895      xnew += tmp * power(x1.mvar(),j.exp());
896      j++;
897    }
898  }
899}
900
901
902void tryExtgcd( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, CanonicalForm & s, CanonicalForm & t, bool & fail )
903{ // F, G are univariate polynomials (i.e. they have exactly one polynomial variable)
904  // F and G must have the same level AND level > 0
905  // we try to calculate gcd(F,G) = s*F + t*G
906  // if a zero divisor is encontered, 'fail' is set to one
907  // M is assumed to be monic
908  CanonicalForm P;
909  if(F.inCoeffDomain())
910  {
911    tryInvert( F, M, P, fail );
912    if(fail)
913      return;
914    result = 1;
915    s = P; t = 0;
916    return;
917  }
918  if(G.inCoeffDomain())
919  {
920    tryInvert( G, M, P, fail );
921    if(fail)
922      return;
923    result = 1;
924    s = 0; t = P;
925    return;
926  }
927  // here: both not inCoeffDomain
928  CanonicalForm inv, rem, tmp, u, v, q, sum=0;
929  if( F.degree() > G.degree() )
930  {
931    P = F; result = G;  s=v=0; t=u=1;
932  }
933  else
934  {
935    P = G; result = F; s=v=1; t=u=0;
936  }
937  Variable x = P.mvar();
938  // here: degree(P) >= degree(result)
939  while(true)
940  {
941    tryDivrem (P, result, q, rem, inv, M, fail);
942    if(fail)
943      return;
944    if( rem.isZero() )
945    {
946      s*=inv;
947      t*=inv;
948      result *= inv; // monify result
949      return;
950    }
951    sum += q;
952    if(result.degree(x) >= rem.degree(x))
953    {
954      P=result;
955      result=rem;
956      tmp=u-sum*s;
957      u=s;
958      s=tmp;
959      tmp=v-sum*t;
960      v=t;
961      t=tmp;
962      sum = 0; // reset
963    }
964    else
965      P = rem;
966  }
967}
968
969
970static CanonicalForm trycontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail )
971{ // as 'content', but takes care of zero divisors
972  ASSERT( x.level() > 0, "cannot calculate content with respect to algebraic variable" );
973  Variable y = f.mvar();
974  if ( y == x )
975    return trycf_content( f, 0, M, fail );
976  if ( y < x )
977     return f;
978  return swapvar( trycontent( swapvar( f, y, x ), y, M, fail ), y, x );
979}
980
981
982static CanonicalForm tryvcontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail )
983{ // as vcontent, but takes care of zero divisors
984  ASSERT( x.level() > 0, "cannot calculate vcontent with respect to algebraic variable" );
985  if ( f.mvar() <= x )
986    return trycontent( f, x, M, fail );
987  CFIterator i;
988  CanonicalForm d = 0, e, ret;
989  for ( i = f; i.hasTerms() && ! d.isOne() && ! fail; i++ )
990  {
991    e = tryvcontent( i.coeff(), x, M, fail );
992    if(fail)
993      break;
994    tryBrownGCD( d, e, M, ret, fail );
995    d = ret;
996  }
997  return d;
998}
999
1000
1001static CanonicalForm trycf_content ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, bool & fail )
1002{ // as cf_content, but takes care of zero divisors
1003  if ( f.inPolyDomain() || ( f.inExtension() && ! getReduce( f.mvar() ) ) )
1004  {
1005    CFIterator i = f;
1006    CanonicalForm tmp = g, result;
1007    while ( i.hasTerms() && ! tmp.isOne() && ! fail )
1008    {
1009      tryBrownGCD( i.coeff(), tmp, M, result, fail );
1010      tmp = result;
1011      i++;
1012    }
1013    return result;
1014  }
1015  return abs( f );
1016}
1017
1018
1019static void tryDivide( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, CanonicalForm & result, bool & divides, bool & fail )
1020{ // M "univariate" monic polynomial
1021  // f, g polynomials with coeffs modulo M.
1022  // if f is divisible by g, 'divides' is set to 1 and 'result' == f/g mod M coefficientwise.
1023  // 'fail' is set to 1, iff a zero divisor is encountered.
1024  // divides==1 implies fail==0
1025  // required: getReduce(M.mvar())==0
1026  if(g.inBaseDomain())
1027  {
1028    result = f/g;
1029    divides = true;
1030    return;
1031  }
1032  if(g.inCoeffDomain())
1033  {
1034    tryInvert(g,M,result,fail);
1035    if(fail)
1036      return;
1037    result = reduce(f*result, M);
1038    divides = true;
1039    return;
1040  }
1041  // here: g NOT inCoeffDomain
1042  Variable x = g.mvar();
1043  if(f.degree(x) < g.degree(x))
1044  {
1045    divides = false;
1046    return;
1047  }
1048  // here: f.degree(x) > 0 and f.degree(x) >= g.degree(x)
1049  CanonicalForm F = f;
1050  CanonicalForm q, leadG = LC(g);
1051  result = 0;
1052  while(!F.isZero())
1053  {
1054    tryDivide(F.LC(x),leadG,M,q,divides,fail);
1055    if(fail || !divides)
1056      return;
1057    if(F.degree(x)<g.degree(x))
1058    {
1059      divides = false;
1060      return;
1061    }
1062    q *= power(x,F.degree(x)-g.degree(x));
1063    result += q;
1064    F = reduce(F-q*g, M);
1065  }
1066  result = reduce(result, M);
1067  divides = true;
1068}
1069
1070
1071
1072void tryCRA( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, CanonicalForm & xnew, CanonicalForm & qnew, bool & fail )
1073{ // polys of level <= 1 are considered coefficients. q1,q2 are assumed to be coprime
1074  // xnew = x1 mod q1 (coefficientwise in the above sense)
1075  // xnew = x2 mod q2
1076  // qnew = q1*q2
1077  CanonicalForm tmp;
1078  if(x1.level() <= 1 && x2.level() <= 1) // base case
1079  {
1080    tryExtgcd(q1,q2,tmp,xnew,qnew,fail);
1081    if(fail)
1082      return;
1083    xnew = x1 + (x2-x1) * xnew * q1;
1084    qnew = q1*q2;
1085    xnew = mod(xnew,qnew);
1086    return;
1087  }
1088  CanonicalForm tmp2;
1089  xnew = 0;
1090  qnew = q1 * q2;
1091  // here: x1.level() > 1 || x2.level() > 1
1092  if(x1.level() > x2.level())
1093  {
1094    for(CFIterator i=x1; i.hasTerms(); i++)
1095    {
1096      if(i.exp() == 0) // const. term
1097      {
1098        tryCRA(i.coeff(),q1,x2,q2,tmp,tmp2,fail);
1099        if(fail)
1100          return;
1101        xnew += tmp;
1102      }
1103      else
1104      {
1105        tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail);
1106        if(fail)
1107          return;
1108        xnew += tmp * power(x1.mvar(),i.exp());
1109      }
1110    }
1111    return;
1112  }
1113  // here: x1.level() <= x2.level() && ( x1.level() > 1 || x2.level() > 1 )
1114  if(x2.level() > x1.level())
1115  {
1116    for(CFIterator j=x2; j.hasTerms(); j++)
1117    {
1118      if(j.exp() == 0) // const. term
1119      {
1120        tryCRA(x1,q1,j.coeff(),q2,tmp,tmp2,fail);
1121        if(fail)
1122          return;
1123        xnew += tmp;
1124      }
1125      else
1126      {
1127        tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail);
1128        if(fail)
1129          return;
1130        xnew += tmp * power(x2.mvar(),j.exp());
1131      }
1132    }
1133    return;
1134  }
1135  // here: x1.level() == x2.level() && x1.level() > 1 && x2.level() > 1
1136  CFIterator i = x1;
1137  CFIterator j = x2;
1138  while(i.hasTerms() || j.hasTerms())
1139  {
1140    if(i.hasTerms())
1141    {
1142      if(j.hasTerms())
1143      {
1144        if(i.exp() == j.exp())
1145        {
1146          tryCRA(i.coeff(),q1,j.coeff(),q2,tmp,tmp2,fail);
1147          if(fail)
1148            return;
1149          xnew += tmp * power(x1.mvar(),i.exp());
1150          i++; j++;
1151        }
1152        else
1153        {
1154          if(i.exp() < j.exp())
1155          {
1156            tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail);
1157            if(fail)
1158              return;
1159            xnew += tmp * power(x1.mvar(),i.exp());
1160            i++;
1161          }
1162          else // i.exp() > j.exp()
1163          {
1164            tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail);
1165            if(fail)
1166              return;
1167            xnew += tmp * power(x1.mvar(),j.exp());
1168            j++;
1169          }
1170        }
1171      }
1172      else // j is out of terms
1173      {
1174        tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail);
1175        if(fail)
1176          return;
1177        xnew += tmp * power(x1.mvar(),i.exp());
1178        i++;
1179      }
1180    }
1181    else // i is out of terms
1182    {
1183      tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail);
1184      if(fail)
1185        return;
1186      xnew += tmp * power(x1.mvar(),j.exp());
1187      j++;
1188    }
1189  }
1190}
1191
1192
1193void tryExtgcd( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & result, CanonicalForm & s, CanonicalForm & t, bool & fail )
1194{
1195  // F, G are univariate polynomials (i.e. they have exactly one polynomial variable)
1196  // F and G must have the same level AND level > 0
1197  // we try to calculate gcd(f,g) = s*F + t*G
1198  // if a zero divisor is encontered, 'fail' is set to one
1199  Variable a, b;
1200  if( !hasFirstAlgVar(F,a) && !hasFirstAlgVar(G,b) ) // note lazy evaluation
1201  {
1202    result = extgcd( F, G, s, t ); // no zero divisors possible
1203    return;
1204  }
1205  if( b.level() > a.level() )
1206    a = b;
1207  // here: a is the biggest alg. var in F and G
1208  CanonicalForm M = getMipo(a);
1209  CanonicalForm P;
1210  if( degree(F) > degree(G) )
1211  {
1212    P=F; result=G; s=0; t=1;
1213  }
1214  else
1215  {
1216    P=G; result=F; s=1; t=0;
1217  }
1218  CanonicalForm inv, rem, q, u, v;
1219  // here: degree(P) >= degree(result)
1220  while(true)
1221  {
1222    tryInvert( Lc(result), M, inv, fail );
1223    if(fail)
1224      return;
1225    // here: Lc(result) is invertible modulo M
1226    q = Lc(P)*inv * power( P.mvar(), degree(P)-degree(result) );
1227    rem = P - q*result;
1228    // here: s*F + t*G = result
1229    if( rem.isZero() )
1230    {
1231      s*=inv;
1232      t*=inv;
1233      result *= inv; // monify result
1234      return;
1235    }
1236    P=result;
1237    result=rem;
1238    rem=u-q*s;
1239    u=s;
1240    s=rem;
1241    rem=v-q*t;
1242    v=t;
1243    t=rem;
1244  }
1245}
Note: See TracBrowser for help on using the repository browser.