source: git/factory/algext.cc @ 0a7d0ca

spielwiese
Last change on this file since 0a7d0ca was 0a7d0ca, checked in by Martin Lee <martinlee84@…>, 11 years ago
fix: assert violations related to minpoly handling
  • Property mode set to 100644
File size: 27.7 KB
Line 
1#include <factory/factoryconf.h>
2
3#ifndef NOSTREAMIO
4#ifdef HAVE_CSTDIO
5#include <cstdio>
6#else
7#include <stdio.h>
8#endif
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
194void tryInvert( const CanonicalForm & F, const CanonicalForm & M, CanonicalForm & inv, bool & fail )
195{ // F, M are required to be "univariate" polynomials in an algebraic variable
196  // we try to invert F modulo M
197  if(F.inBaseDomain())
198  {
199    if(F.isZero())
200    {
201      fail = true;
202      return;
203    }
204    inv = 1/F;
205    return;
206  }
207  CanonicalForm b;
208  Variable a = M.mvar();
209  Variable x = Variable(1);
210  if(!extgcd( replacevar( F, a, x ), replacevar( M, a, x ), inv, b ).isOne())
211    fail = true;
212  else
213    inv = replacevar( inv, x, a ); // change back to alg var
214}
215
216void tryDivrem (const CanonicalForm& F, const CanonicalForm& G, CanonicalForm& Q,
217                CanonicalForm& R, CanonicalForm& inv, const CanonicalForm& mipo,
218                bool& fail)
219{
220  if (F.inCoeffDomain())
221  {
222    Q= 0;
223    R= F;
224    return;
225  }
226
227  CanonicalForm A, B;
228  Variable x= F.mvar();
229  A= F;
230  B= G;
231  int degA= degree (A, x);
232  int degB= degree (B, x);
233
234  if (degA < degB)
235  {
236    R= A;
237    Q= 0;
238    return;
239  }
240
241  tryInvert (Lc (B), mipo, inv, fail);
242  if (fail)
243    return;
244
245  R= A;
246  Q= 0;
247  CanonicalForm Qi;
248  for (int i= degA -degB; i >= 0; i--)
249  {
250    if (degree (R, x) == i + degB)
251    {
252      Qi= Lc (R)*inv*power (x, i);
253      Qi= reduce (Qi, mipo);
254      R -= Qi*B;
255      R= reduce (R, mipo);
256      Q += Qi;
257    }
258  }
259}
260
261void tryEuclid( const CanonicalForm & A, const CanonicalForm & B, const CanonicalForm & M, CanonicalForm & result, bool & fail )
262{
263  CanonicalForm P;
264  if(A.inCoeffDomain())
265  {
266    tryInvert( A, M, P, fail );
267    if(fail)
268      return;
269    result = 1;
270    return;
271  }
272  if(B.inCoeffDomain())
273  {
274    tryInvert( B, M, P, fail );
275    if(fail)
276      return;
277    result = 1;
278    return;
279  }
280  // here: both not inCoeffDomain
281  if( A.degree() > B.degree() )
282  {
283    P = A; result = B;
284  }
285  else
286  {
287    P = B; result = A;
288  }
289  CanonicalForm inv;
290  if( result.isZero() )
291  {
292    tryInvert( Lc(P), M, inv, fail );
293    if(fail)
294      return;
295    result = inv*P; // monify result (not reduced, yet)
296    result= reduce (result, M);
297    return;
298  }
299  Variable x = P.mvar();
300  CanonicalForm rem, Q;
301  // here: degree(P) >= degree(result)
302  while(true)
303  {
304    tryDivrem (P, result, Q, rem, inv, M, fail);
305    if (fail)
306      return;
307    if( rem.isZero() )
308    {
309      result *= inv;
310      result= reduce (result, M);
311      return;
312    }
313    if(result.degree(x) >= rem.degree(x))
314    {
315      P = result;
316      result = rem;
317    }
318    else
319      P = rem;
320  }
321}
322
323bool hasFirstAlgVar( const CanonicalForm & f, Variable & a )
324{
325  if( f.inBaseDomain() ) // f has NO alg. variable
326    return false;
327  if( f.level()<0 ) // f has only alg. vars, so take the first one
328  {
329    a = f.mvar();
330    return true;
331  }
332  for(CFIterator i=f; i.hasTerms(); i++)
333    if( hasFirstAlgVar( i.coeff(), a ))
334      return true; // 'a' is already set
335  return false;
336}
337
338CanonicalForm QGCD( const CanonicalForm & F, const CanonicalForm & G );
339int * leadDeg(const CanonicalForm & f, int *degs);
340bool isLess(int *a, int *b, int lower, int upper);
341bool isEqual(int *a, int *b, int lower, int upper);
342CanonicalForm firstLC(const CanonicalForm & f);
343static CanonicalForm trycontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail );
344static CanonicalForm tryvcontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail );
345static CanonicalForm trycf_content ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, bool & fail );
346static void tryDivide( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, CanonicalForm & result, bool & divides, bool & fail );
347
348static inline CanonicalForm
349tryNewtonInterp (const CanonicalForm alpha, const CanonicalForm u,
350              const CanonicalForm newtonPoly, const CanonicalForm oldInterPoly,
351              const Variable & x, const CanonicalForm& M, bool& fail)
352{
353  CanonicalForm interPoly;
354
355  CanonicalForm inv;
356  tryInvert (newtonPoly (alpha, x), M, inv, fail);
357  if (fail)
358    return 0;
359
360  interPoly= oldInterPoly+reduce ((u - oldInterPoly (alpha, x))*inv*newtonPoly, M);
361  return interPoly;
362}
363
364void tryBrownGCD( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, bool & fail, bool topLevel )
365{ // assume F,G are multivariate polys over Z/p(a) for big prime p, M "univariate" polynomial in an algebraic variable
366  // M is assumed to be monic
367  if(F.isZero())
368  {
369    if(G.isZero())
370    {
371      result = G; // G is zero
372      return;
373    }
374    if(G.inCoeffDomain())
375    {
376      tryInvert(G,M,result,fail);
377      if(fail)
378        return;
379      result = 1;
380      return;
381    }
382    // try to make G monic modulo M
383    CanonicalForm inv;
384    tryInvert(Lc(G),M,inv,fail);
385    if(fail)
386      return;
387    result = inv*G;
388    result= reduce (result, M);
389    return;
390  }
391  if(G.isZero()) // F is non-zero
392  {
393    if(F.inCoeffDomain())
394    {
395      tryInvert(F,M,result,fail);
396      if(fail)
397        return;
398      result = 1;
399      return;
400    }
401    // try to make F monic modulo M
402    CanonicalForm inv;
403    tryInvert(Lc(F),M,inv,fail);
404    if(fail)
405      return;
406    result = inv*F;
407    result= reduce (result, M);
408    return;
409  }
410  // here: F,G both nonzero
411  if(F.inCoeffDomain())
412  {
413    tryInvert(F,M,result,fail);
414    if(fail)
415      return;
416    result = 1;
417    return;
418  }
419  if(G.inCoeffDomain())
420  {
421    tryInvert(G,M,result,fail);
422    if(fail)
423      return;
424    result = 1;
425    return;
426  }
427  CFMap MM,NN;
428  int lev= myCompress (F, G, MM, NN, topLevel);
429  if (lev == 0)
430  {
431    result= 1;
432    return;
433  }
434  CanonicalForm f=MM(F);
435  CanonicalForm g=MM(G);
436  // here: f,g are compressed
437  // compute largest variable in f or g (least one is Variable(1))
438  int mv = f.level();
439  if(g.level() > mv)
440    mv = g.level();
441  // here: mv is level of the largest variable in f, g
442  if(mv == 1) // f,g univariate
443  {
444    tryEuclid(f,g,M,result,fail);
445    if(fail)
446      return;
447    result= NN (reduce (result, M)); // do not forget to map back
448    return;
449  }
450  // here: mv > 1
451  CanonicalForm cf = tryvcontent(f, Variable(2), M, fail); // cf is univariate poly in var(1)
452  if(fail)
453    return;
454  CanonicalForm cg = tryvcontent(g, Variable(2), M, fail);
455  if(fail)
456    return;
457  CanonicalForm c;
458  tryEuclid(cf,cg,M,c,fail);
459  if(fail)
460    return;
461  // f /= cf
462  f.tryDiv (cf, M, fail);
463  if(fail)
464    return;
465  // g /= cg
466  g.tryDiv (cg, M, fail);
467  if(fail)
468    return;
469  if(f.inCoeffDomain())
470  {
471    tryInvert(f,M,result,fail);
472    if(fail)
473      return;
474    result = NN(c);
475    return;
476  }
477  if(g.inCoeffDomain())
478  {
479    tryInvert(g,M,result,fail);
480    if(fail)
481      return;
482    result = NN(c);
483    return;
484  }
485  int *L = new int[mv+1]; // L is addressed by i from 2 to mv
486  int *N = new int[mv+1];
487  for(int i=2; i<=mv; i++)
488    L[i] = N[i] = 0;
489  L = leadDeg(f, L);
490  N = leadDeg(g, N);
491  CanonicalForm gamma;
492  tryEuclid( firstLC(f), firstLC(g), M, gamma, fail );
493  if(fail)
494    return;
495  for(int i=2; i<=mv; i++) // entries at i=0,1 not visited
496    if(N[i] < L[i])
497      L[i] = N[i];
498  // L is now upper bound for degrees of gcd
499  int *dg_im = new int[mv+1]; // for the degree vector of the image we don't need any entry at i=1
500  for(int i=2; i<=mv; i++)
501    dg_im[i] = 0; // initialize
502  CanonicalForm gamma_image, m=1;
503  CanonicalForm gm=0;
504  CanonicalForm g_image, alpha, gnew;
505  FFGenerator gen = FFGenerator();
506  Variable x= Variable (1);
507  bool divides= true;
508  for(FFGenerator gen = FFGenerator(); gen.hasItems(); gen.next())
509  {
510    alpha = gen.item();
511    gamma_image = reduce(gamma(alpha, x),M); // plug in alpha for var(1)
512    if(gamma_image.isZero()) // skip lc-bad points var(1)-alpha
513      continue;
514    tryBrownGCD( f(alpha, x), g(alpha, x), M, g_image, fail, false ); // recursive call with one var less
515    if(fail)
516      return;
517    g_image = reduce(g_image, M);
518    if(g_image.inCoeffDomain()) // early termination
519    {
520      tryInvert(g_image,M,result,fail);
521      if(fail)
522        return;
523      result = NN(c);
524      return;
525    }
526    for(int i=2; i<=mv; i++)
527      dg_im[i] = 0; // reset (this is necessary, because some entries may not be updated by call to leadDeg)
528    dg_im = leadDeg(g_image, dg_im); // dg_im cannot be NIL-pointer
529    if(isEqual(dg_im, L, 2, mv))
530    {
531      CanonicalForm inv;
532      tryInvert (firstLC (g_image), M, inv, fail);
533      if (fail)
534        return;
535      g_image *= inv;
536      g_image *= gamma_image; // multiply by multiple of image lc(gcd)
537      g_image= reduce (g_image, M);
538      gnew= tryNewtonInterp (alpha, g_image, m, gm, x, M, fail);
539      // gnew = gm mod m
540      // gnew = g_image mod var(1)-alpha
541      // mnew = m * (var(1)-alpha)
542      if(fail)
543        return;
544      m *= (x - alpha);
545      if(gnew == gm) // gnew did not change
546      {
547        cf = tryvcontent(gm, Variable(2), M, fail);
548        if(fail)
549          return;
550        divides = true;
551        g_image= gm;
552        g_image.tryDiv (cf, M, fail);
553        if(fail)
554          return;
555        divides= tryFdivides (g_image,f, M, fail); // trial division (f)
556        if(fail)
557          return;
558        if(divides)
559        {
560          bool divides2= tryFdivides (g_image,g, M, fail); // trial division (g)
561          if(fail)
562            return;
563          if(divides2)
564          {
565            result = NN(reduce (c*g_image, M));
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    if( fail ) // mipo splits in char p
675      continue;
676    if( Dp.inCoeffDomain() ) // early termination
677    {
678      tryInvert(Dp,mipo,tmp,fail); // check if zero divisor
679      if(fail)
680        continue;
681      setReduce(a,true);
682      if (off_rational) Off(SW_RATIONAL); else On(SW_RATIONAL);
683      setCharacteristic(0);
684      return CanonicalForm(1);
685    }
686    setCharacteristic(0);
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
783void tryExtgcd( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, CanonicalForm & s, CanonicalForm & t, bool & fail )
784{ // F, G are univariate polynomials (i.e. they have exactly one polynomial variable)
785  // F and G must have the same level AND level > 0
786  // we try to calculate gcd(F,G) = s*F + t*G
787  // if a zero divisor is encontered, 'fail' is set to one
788  // M is assumed to be monic
789  CanonicalForm P;
790  if(F.inCoeffDomain())
791  {
792    tryInvert( F, M, P, fail );
793    if(fail)
794      return;
795    result = 1;
796    s = P; t = 0;
797    return;
798  }
799  if(G.inCoeffDomain())
800  {
801    tryInvert( G, M, P, fail );
802    if(fail)
803      return;
804    result = 1;
805    s = 0; t = P;
806    return;
807  }
808  // here: both not inCoeffDomain
809  CanonicalForm inv, rem, tmp, u, v, q, sum=0;
810  if( F.degree() > G.degree() )
811  {
812    P = F; result = G;  s=v=0; t=u=1;
813  }
814  else
815  {
816    P = G; result = F; s=v=1; t=u=0;
817  }
818  Variable x = P.mvar();
819  // here: degree(P) >= degree(result)
820  while(true)
821  {
822    tryDivrem (P, result, q, rem, inv, M, fail);
823    if(fail)
824      return;
825    if( rem.isZero() )
826    {
827      s*=inv;
828      s= reduce (s, M);
829      t*=inv;
830      t= reduce (t, M);
831      result *= inv; // monify result
832      result= reduce (result, M);
833      return;
834    }
835    sum += q;
836    if(result.degree(x) >= rem.degree(x))
837    {
838      P=result;
839      result=rem;
840      tmp=u-sum*s;
841      u=s;
842      s=tmp;
843      tmp=v-sum*t;
844      v=t;
845      t=tmp;
846      sum = 0; // reset
847    }
848    else
849      P = rem;
850  }
851}
852
853
854static CanonicalForm trycontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail )
855{ // as 'content', but takes care of zero divisors
856  ASSERT( x.level() > 0, "cannot calculate content with respect to algebraic variable" );
857  Variable y = f.mvar();
858  if ( y == x )
859    return trycf_content( f, 0, M, fail );
860  if ( y < x )
861     return f;
862  return swapvar( trycontent( swapvar( f, y, x ), y, M, fail ), y, x );
863}
864
865
866static CanonicalForm tryvcontent ( const CanonicalForm & f, const Variable & x, const CanonicalForm & M, bool & fail )
867{ // as vcontent, but takes care of zero divisors
868  ASSERT( x.level() > 0, "cannot calculate vcontent with respect to algebraic variable" );
869  if ( f.mvar() <= x )
870    return trycontent( f, x, M, fail );
871  CFIterator i;
872  CanonicalForm d = 0, e, ret;
873  for ( i = f; i.hasTerms() && ! d.isOne() && ! fail; i++ )
874  {
875    e = tryvcontent( i.coeff(), x, M, fail );
876    if(fail)
877      break;
878    tryBrownGCD( d, e, M, ret, fail );
879    d = ret;
880  }
881  return d;
882}
883
884
885static CanonicalForm trycf_content ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, bool & fail )
886{ // as cf_content, but takes care of zero divisors
887  if ( f.inPolyDomain() || ( f.inExtension() && ! getReduce( f.mvar() ) ) )
888  {
889    CFIterator i = f;
890    CanonicalForm tmp = g, result;
891    while ( i.hasTerms() && ! tmp.isOne() && ! fail )
892    {
893      tryBrownGCD( i.coeff(), tmp, M, result, fail );
894      tmp = result;
895      i++;
896    }
897    return result;
898  }
899  return abs( f );
900}
901
902
903static void tryDivide( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & M, CanonicalForm & result, bool & divides, bool & fail )
904{ // M "univariate" monic polynomial
905  // f, g polynomials with coeffs modulo M.
906  // if f is divisible by g, 'divides' is set to 1 and 'result' == f/g mod M coefficientwise.
907  // 'fail' is set to 1, iff a zero divisor is encountered.
908  // divides==1 implies fail==0
909  // required: getReduce(M.mvar())==0
910  if(g.inBaseDomain())
911  {
912    result = f/g;
913    divides = true;
914    return;
915  }
916  if(g.inCoeffDomain())
917  {
918    tryInvert(g,M,result,fail);
919    if(fail)
920      return;
921    result = reduce(f*result, M);
922    divides = true;
923    return;
924  }
925  // here: g NOT inCoeffDomain
926  Variable x = g.mvar();
927  if(f.degree(x) < g.degree(x))
928  {
929    divides = false;
930    return;
931  }
932  // here: f.degree(x) > 0 and f.degree(x) >= g.degree(x)
933  CanonicalForm F = f;
934  CanonicalForm q, leadG = LC(g);
935  result = 0;
936  while(!F.isZero())
937  {
938    tryDivide(F.LC(x),leadG,M,q,divides,fail);
939    if(fail || !divides)
940      return;
941    if(F.degree(x)<g.degree(x))
942    {
943      divides = false;
944      return;
945    }
946    q *= power(x,F.degree(x)-g.degree(x));
947    result += q;
948    F = reduce(F-q*g, M);
949  }
950  result = reduce(result, M);
951  divides = true;
952}
953
954void tryExtgcd( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & result, CanonicalForm & s, CanonicalForm & t, bool & fail )
955{
956  // F, G are univariate polynomials (i.e. they have exactly one polynomial variable)
957  // F and G must have the same level AND level > 0
958  // we try to calculate gcd(f,g) = s*F + t*G
959  // if a zero divisor is encontered, 'fail' is set to one
960  Variable a, b;
961  if( !hasFirstAlgVar(F,a) && !hasFirstAlgVar(G,b) ) // note lazy evaluation
962  {
963    result = extgcd( F, G, s, t ); // no zero divisors possible
964    return;
965  }
966  if( b.level() > a.level() )
967    a = b;
968  // here: a is the biggest alg. var in F and G
969  CanonicalForm M = getMipo(a);
970  CanonicalForm P;
971  if( degree(F) > degree(G) )
972  {
973    P=F; result=G; s=0; t=1;
974  }
975  else
976  {
977    P=G; result=F; s=1; t=0;
978  }
979  CanonicalForm inv, rem, q, u, v;
980  // here: degree(P) >= degree(result)
981  while(true)
982  {
983    tryInvert( Lc(result), M, inv, fail );
984    if(fail)
985      return;
986    // here: Lc(result) is invertible modulo M
987    q = Lc(P)*inv * power( P.mvar(), degree(P)-degree(result) );
988    rem = P - q*result;
989    // here: s*F + t*G = result
990    if( rem.isZero() )
991    {
992      s*=inv;
993      t*=inv;
994      result *= inv; // monify result
995      return;
996    }
997    P=result;
998    result=rem;
999    rem=u-q*s;
1000    u=s;
1001    s=rem;
1002    rem=v-q*t;
1003    v=t;
1004    t=rem;
1005  }
1006}
1007
1008void tryCRA( const CanonicalForm & x1, const CanonicalForm & q1, const CanonicalForm & x2, const CanonicalForm & q2, CanonicalForm & xnew, CanonicalForm & qnew, bool & fail )
1009{ // polys of level <= 1 are considered coefficients. q1,q2 are assumed to be coprime
1010  // xnew = x1 mod q1 (coefficientwise in the above sense)
1011  // xnew = x2 mod q2
1012  // qnew = q1*q2
1013  CanonicalForm tmp;
1014  if(x1.level() <= 1 && x2.level() <= 1) // base case
1015  {
1016    tryExtgcd(q1,q2,tmp,xnew,qnew,fail);
1017    if(fail)
1018      return;
1019    xnew = x1 + (x2-x1) * xnew * q1;
1020    qnew = q1*q2;
1021    xnew = mod(xnew,qnew);
1022    return;
1023  }
1024  CanonicalForm tmp2;
1025  xnew = 0;
1026  qnew = q1 * q2;
1027  // here: x1.level() > 1 || x2.level() > 1
1028  if(x1.level() > x2.level())
1029  {
1030    for(CFIterator i=x1; i.hasTerms(); i++)
1031    {
1032      if(i.exp() == 0) // const. term
1033      {
1034        tryCRA(i.coeff(),q1,x2,q2,tmp,tmp2,fail);
1035        if(fail)
1036          return;
1037        xnew += tmp;
1038      }
1039      else
1040      {
1041        tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail);
1042        if(fail)
1043          return;
1044        xnew += tmp * power(x1.mvar(),i.exp());
1045      }
1046    }
1047    return;
1048  }
1049  // here: x1.level() <= x2.level() && ( x1.level() > 1 || x2.level() > 1 )
1050  if(x2.level() > x1.level())
1051  {
1052    for(CFIterator j=x2; j.hasTerms(); j++)
1053    {
1054      if(j.exp() == 0) // const. term
1055      {
1056        tryCRA(x1,q1,j.coeff(),q2,tmp,tmp2,fail);
1057        if(fail)
1058          return;
1059        xnew += tmp;
1060      }
1061      else
1062      {
1063        tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail);
1064        if(fail)
1065          return;
1066        xnew += tmp * power(x2.mvar(),j.exp());
1067      }
1068    }
1069    return;
1070  }
1071  // here: x1.level() == x2.level() && x1.level() > 1 && x2.level() > 1
1072  CFIterator i = x1;
1073  CFIterator j = x2;
1074  while(i.hasTerms() || j.hasTerms())
1075  {
1076    if(i.hasTerms())
1077    {
1078      if(j.hasTerms())
1079      {
1080        if(i.exp() == j.exp())
1081        {
1082          tryCRA(i.coeff(),q1,j.coeff(),q2,tmp,tmp2,fail);
1083          if(fail)
1084            return;
1085          xnew += tmp * power(x1.mvar(),i.exp());
1086          i++; j++;
1087        }
1088        else
1089        {
1090          if(i.exp() < j.exp())
1091          {
1092            tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail);
1093            if(fail)
1094              return;
1095            xnew += tmp * power(x1.mvar(),i.exp());
1096            i++;
1097          }
1098          else // i.exp() > j.exp()
1099          {
1100            tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail);
1101            if(fail)
1102              return;
1103            xnew += tmp * power(x1.mvar(),j.exp());
1104            j++;
1105          }
1106        }
1107      }
1108      else // j is out of terms
1109      {
1110        tryCRA(i.coeff(),q1,0,q2,tmp,tmp2,fail);
1111        if(fail)
1112          return;
1113        xnew += tmp * power(x1.mvar(),i.exp());
1114        i++;
1115      }
1116    }
1117    else // i is out of terms
1118    {
1119      tryCRA(0,q1,j.coeff(),q2,tmp,tmp2,fail);
1120      if(fail)
1121        return;
1122      xnew += tmp * power(x1.mvar(),j.exp());
1123      j++;
1124    }
1125  }
1126}
1127
Note: See TracBrowser for help on using the repository browser.