source: git/Singular/LIB/modstd.lib @ e3c414

spielwiese
Last change on this file since e3c414 was e3c414, checked in by Hans Schönemann <hannes@…>, 17 years ago
*hannes: fixed syntax (liftPoly) git-svn-id: file:///usr/local/Singular/svn/trunk@10140 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.3 KB
Line 
1//GP, last modified 23.10.06
2///////////////////////////////////////////////////////////////////////////////
3version="$Id: modstd.lib,v 1.11 2007-06-22 15:27:33 Singular Exp $";
4category="Commutative Algebra";
5info="
6LIBRARY: modstd.lib  Grobner basis of ideals
7AUTHORS: A. Hashemi,     Amir.Hashemi@lip6.fr
8@*       G. Pfister      pfister@mathematik.uni-kl.de
9@*       H. Schoenemann  hannes@mathematik.uni-kl.de
10
11NOTE:
12 A library for computing the Grobner basis of an ideal in the polynomial
13 ring over the rational numbers using modular methods.The procedures are
14 inspired by the following paper:
15 Elizabeth A. Arnold:
16 Modular Algorithms for Computing Groebner Bases , Journal of Symbolic
17 Computation , April 2003, Volume 35, (4), p. 403-419.
18
19
20
21PROCEDURES:
22modStd(I);     compute a standard basis of I using modular methods
23modS(I,L);     liftings to Q of standard bases of I mod p for p in L
24primeList(n);  intvec of n primes  <= 2134567879 in decreasing order
25";
26
27LIB "poly.lib";
28LIB "crypto.lib";
29///////////////////////////////////////////////////////////////////////////////
30proc modStd(ideal I)
31"USAGE:  modStd(I);
32RETURN:  a standard basis of I if no warning appears;
33NOTE:    the procedure computes a standard basis of I (over the
34         rational numbers) by using  modular methods. If a
35         warning appears then the result is a standard basis with no defined
36         relation to I; this is a sign that not enough prime numbers have
37         been used. For further experiments see procedure modS.
38EXAMPLE: example modStd; shows an example
39"
40{
41  def R0=basering;
42  list rl=ringlist(R0);
43  if((npars(R0)>0)||(rl[1]>0))
44  {
45     ERROR("characteristic of basering should be zero");
46  }
47  int l,j,k,q;
48  int en=2134567879;
49  int an=1000000000;
50  intvec hi,hl,hc,hpl,hpc;
51  list T,TT;
52  intvec L=primeList(5);
53  L[6]=prime(random(an,en));
54  ideal J,cT,lT,K;
55  ideal I0=I;
56  int h=homog(I);
57  if((!h)&&(ord_test(R0)==0))
58  {
59     ERROR("input is not homogeneous and ordering is not local");
60  }
61  if(h)
62  {
63     execute("ring gn="+string(L[6])+",x(1.."+string(nvars(R0))+"),dp;");
64     ideal I=fetch(R0,I);
65     ideal J=std(I);
66     hi=hilb(J,1);
67     setring R0;
68  }
69  for (j=1;j<=size(L);j++)
70  {
71    rl[1]=L[j];
72    def oro=ring(rl);
73    setring oro;
74    ideal I=fetch(R0,I);
75    option(redSB);
76    if(h)
77    {
78       ideal I1=std(I,hi);
79    }
80    else
81    {
82      if(ord_test(R0)==-1)
83      {
84         ideal I1=std(I);
85      }
86      else
87      {
88         matrix M;
89         ideal I1=liftstd(I,M);
90      }
91    }
92    setring R0;
93    T[j]=fetch(oro,I1);
94    kill oro;
95  }
96  //================= delete unlucky primes ====================
97  // unlucky iff the leading ideal is wrong
98  list LL=deleteUnluckyPrimes(T,L);
99  T=LL[1];
100  L=LL[2];
101  lT=LL[3];
102  //============ now all leading ideals are the same ============
103  for(j=1;j<=ncols(T[1]);j++)
104  {
105    for(k=1;k<=size(L);k++)
106    {
107      TT[k]=T[k][j];
108    }
109    J[j]=liftPoly(TT,L);
110  }
111  //=========== chooses more primes up to the moment the result becomes stable
112  while(1)
113  {
114     k=0;
115     q=prime(random(an,en));
116     while(k<size(L))
117     {
118        k++;
119        if(L[k]==q)
120        {
121           k=0;
122           q=prime(random(an,en));
123        }
124     }
125     L[size(L)+1]=q;
126     rl[1]=L[size(L)];
127     def @r=ring(rl);
128     setring @r;
129     ideal i=fetch(R0,I);
130     option(redSB);
131     if(h)
132     {
133       i=std(i,hi);
134     }
135     else
136     {
137       if(ord_test(R0)==-1)
138       {
139          i=std(i);
140       }
141       else
142       {
143          matrix M;
144          i=liftstd(i,M);
145       }
146     }
147     setring R0;
148     T[size(T)+1]=fetch(@r,i);
149     kill @r;
150     cT=lead(T[size(T)]);
151     attrib(cT,"isSB",1);
152     if((size(reduce(cT,lT))!=0)||(size(reduce(lT,cT))!=0))
153     {
154        T=delete(T,size(T));
155        L=L[1..size(L)-1];
156        k=0;
157     }
158     else
159     {
160        for(j=1;j<=ncols(T[1]);j++)
161        {
162           for(k=1;k<=size(L);k++)
163           {
164              TT[k]=T[k][j];
165           }
166           K[j]=liftPoly(TT,L);
167        }
168        k=1;
169        for(j=1;j<=size(K);j++)
170        {
171           if(K[j]-J[j]!=0)
172           {
173              k=0;
174              J=K;
175              break;
176           }
177        }
178     }
179     if(k){break;}
180  }
181  //============  test for standard basis and I=J =======
182  J=std(J);
183  I0=reduce(I0,J);
184  if(size(I0)>0)
185  {
186     "WARNING: The input ideal is not contained
187                        in the ideal generated by the standardbasis";
188     "list of primes used:";
189     L;
190  }
191  attrib(J,"isSB",1);
192  return(J);
193}
194example
195{ "EXAMPLE:"; echo = 2;
196   ring r=0,(x,y,z),dp;
197   ideal I=3x3+x2+1,11y5+y3+2,5z4+z2+4;
198   ideal J=modStd(I);
199   J;
200}
201///////////////////////////////////////////////////////////////////////////////
202proc modS(ideal I, intvec L, list #)
203"USAGE:  modS(I,L); I ideal, L intvec of primes
204         if size(#)>0 std is used instead of groebner
205RETURN:  an ideal which is with high probability a standard basis
206NOTE:    This procedure is designed for fast experiments.
207         It is not tested whether the result is a standard basis.
208         It is not tested whether the result generates I.
209EXAMPLE: example modS; shows an example
210"
211{
212  int j,k;
213  list T,TT;
214  def R0=basering;
215  ideal J,cT,lT,K;
216  ideal I0=I;
217  list rl=ringlist(R0);
218  if((npars(R0)>0)||(rl[1]>0))
219  {
220     ERROR("characteristic of basering should be zero");
221  }
222  for (j=1;j<=size(L);j++)
223  {
224    rl[1]=L[j];
225    def @r=ring(rl);
226    setring @r;
227    ideal i=fetch(R0,I);
228    option(redSB);
229    if(size(#)>0)
230    {
231       i=std(i);
232    }
233    else
234    {
235       i=groebner(i);
236    }
237    setring R0;
238    T[j]=fetch(@r,i);
239    kill @r;
240  }
241  //================= delete unlucky primes ====================
242  // unlucky iff the leading ideal is wrong
243  list LL=deleteUnluckyPrimes(T,L);
244  T=LL[1];
245  L=LL[2];
246  //============ now all leading ideals are the same ============
247  for(j=1;j<=ncols(T[1]);j++)
248  {
249    for(k=1;k<=size(L);k++)
250    {
251      TT[k]=T[k][j];
252    }
253    J[j]=liftPoly(TT,L);
254  }
255  attrib(J,"isSB",1);
256  return(J);
257}
258example
259{ "EXAMPLE:"; echo = 2;
260   intvec L=3,5,11,13,181;
261   ring r=0,(x,y,z),dp;
262   ideal I=3x3+x2+1,11y5+y3+2,5z4+z2+4;
263   ideal J=modS(I,L);
264   J;
265}
266///////////////////////////////////////////////////////////////////////////////
267proc deleteUnluckyPrimes(list T,intvec L)
268"USAGE:  deleteUnluckyPrimes(T,L);T list of polys, L intvec of primes
269RETURN:  list L,T with T list of polys, L intvec of primes
270EXAMPLE: example deleteUnluckyPrimes; shows an example
271NOTE:    works only for homogeneous ideals with global orderings or
272         for ideals with local orderings
273"
274{
275  int j,k;
276  intvec hl,hc;
277  ideal cT,lT;
278
279  lT=lead(T[size(T)]);
280  attrib(lT,"isSB",1);
281  hl=hilb(lT,1);
282  for (j=1;j<size(T);j++)
283  {
284     cT=lead(T[j]);
285     attrib(cT,"isSB",1);
286     hc=hilb(cT,1);
287     if(hl==hc)
288     {
289        for(k=1;k<=size(lT);k++)
290        {
291           if(lT[k]<cT[k]){lT=cT;break;}
292           if(lT[k]>cT[k]){break;}
293        }
294     }
295     else
296     {
297        if(hc<hl){lT=cT;hl=hilb(lT,1);}
298     }
299  }
300  j=1;
301  attrib(lT,"isSB",1);
302  while(j<=size(T))
303  {
304     cT=lead(T[j]);
305     attrib(cT,"isSB",1);
306     if((size(reduce(cT,lT))!=0)||(size(reduce(lT,cT))!=0))
307     {
308        T=delete(T,j);
309        if(j==1) { L=L[2..size(L)]; }
310        else
311        {
312          if (j==size(L)) { L=L[1..size(L)-1]; }
313          else { L=L[1..j-1],L[j+1..size(L)]; }
314        }
315        j--;
316     }
317     j++;
318  }
319  return(list(T,L,lT));
320}
321example
322{ "EXAMPLE:"; echo = 2;
323   list L=2,3,5,7,11;
324   ring r=0,(y,x),Dp;
325   ideal I1=y2x,y6;
326   ideal I2=yx2,y3x,x5,y6;
327   ideal I3=y2x,x3y,x5,y6;
328   ideal I4=y2x,x3y,x5;
329   ideal I5=y2x,yx3,x5,y6;
330   list T=I1,I2,I3,I4,I5;
331   list TT=deleteUnluckyPrimes(T,L);
332   TT;
333}
334///////////////////////////////////////////////////////////////////////////////
335proc liftPoly(list T, intvec L)
336"USAGE:  liftPoly(T,L); T list of polys, L intvec of primes
337RETURN:  poly p in Q[x] such that p mod L[i]=T[i]
338EXAMPLE: example liftPoly; shows an example
339"
340{
341   int i;
342   list TT;
343   for(i=size(T);i>0;i--)
344   { TT[i]=ideal(T[i]);
345   ideal hh=chinrem(T,L);
346   poly h=hh[1];
347   poly p=lead(h);
348   poly result;
349   number n;
350   number N=L[1];
351   for(i=size(L);i>1;i--)
352   {
353      N=N*L[i];
354   }
355   while(h!=0)
356   {
357     n=Farey(N,leadcoef(h));
358     result=result+n*p;
359     h=h-lead(h);
360     p=leadmonom(h);
361   }
362   return(result);
363}
364example
365{ "EXAMPLE:"; echo = 2;
366   ring R = 0,(x,y),dp;
367   intvec L=32003,181,241,499;
368   list T=ideal(x2+7000x+13000),ideal(x2+100x+147y+40),ideal(x2+120x+191y+10),ideal(x2+x+67y+100);
369   liftPoly(T,L);
370}
371///////////////////////////////////////////////////////////////////////////
372proc liftPoly1(list T, intvec L)
373"USAGE:  liftPoly1(T,L); T list of polys, L intvec of primes
374RETURN:  poly p in Q[x] such that p mod L[i]=T[i]
375EXAMPLE: example liftPoly; shows an example
376"
377{
378   poly result;
379   int i;
380   poly p;
381   list TT;
382   number n;
383
384   number N=L[1];
385   for(i=2;i<=size(L);i++)
386   {
387      N=N*L[i];
388   }
389   while(1)
390   {
391     p=leadmonom(T[1]);
392     for(i=2;i<=size(T);i++)
393     {
394        if(leadmonom(T[i])>p)
395        {
396          p=leadmonom(T[i]);
397        }
398     }
399     if (p==0) {return(result);}
400     for(i=1;i<=size(T);i++)
401     {
402       if(p==leadmonom(T[i]))
403       {
404          TT[i]=leadcoef(T[i]);
405          T[i]=T[i]-lead(T[i]);
406       }
407       else
408       {
409          TT[i]=0;
410       }
411     }
412     n=chineseR(TT,L,N);
413     n=Farey(N,n);
414     result=result+n*p;
415   }
416}
417example
418{ "EXAMPLE:"; echo = 2;
419   ring R = 0,(x,y),dp;
420   intvec L=32003,181,241,499;
421   list T=x2+7000x+13000,x2+100x+147y+40,x2+120x+191y+10,x2+x+67y+100;
422   liftPoly1(T,L);
423}
424 ///////////////////////////////////////////////////////////////////////////////
425proc fareyIdeal(ideal I,intvec L)
426{
427   poly result,p;
428   int i,j;
429   number n;
430   number N=L[1];
431   for(i=2;i<=size(L);i++)
432   {
433      N=N*L[i];
434   }
435
436   for(i=1;i<=size(I);i++)
437   {
438     p=I[i];
439     result=lead(p);
440     while(1)
441     {
442        if (p==0) {break;}
443        p=p-lead(p);
444        n=Farey(N,leadcoef(p));
445        result=result+n*leadmonom(p);
446     }
447     I[i]=result;
448   }
449   return(I);
450}
451///////////////////////////////////////////////////////////////////////////////
452proc Farey (number P, number N)
453"USAGE:  Farey (P,N); P, N number;
454RETURN:  a rational number a/b such that a/b=N mod P
455         and |a|,|b|<(P/2)^{1/2}
456"
457{
458   if (P<0){P=-P;}
459   if (N<0){N=N+P;}
460   number A,B,C,D,E;
461   E=P;
462   B=1;
463   while (N!=0)
464   {
465        if (2*N^2<P)
466        {
467           return(N/B);
468        }
469        D=E mod N;
470        C=A-(E-E mod N)/N*B;
471        E=N;
472        N=D;
473        A=B;
474        B=C;
475   }
476   return(0);
477}
478example
479{ "EXAMPLE:"; echo = 2;
480   ring R = 0,x,dp;
481   Farey(32003,12345);
482}
483 ///////////////////////////////////////////////////////////////////////////////
484proc chineseR(list T,intvec L,number N)
485"USAGE:  chineseR(T,L,N);
486RETURN: x such that x = T[i] mod L[i], N=product(L[i])
487NOTE:   chinese remainder theorem
488EXAMPLE:example chineseR; shows an example
489"
490{
491   number x;
492   if(size(L)==1)
493   {
494      x=T[1] mod L[1];
495      return(x);
496   }
497   int i;
498   int n=size(L);
499   list M;
500   for(i=1;i<=n;i++)
501   {
502      M[i]=N/L[i];
503   }
504   list S=eexgcdN(M);
505   for(i=1;i<=n;i++)
506   {
507      x=x+S[i]*M[i]*T[i];
508   }
509   x=x mod N;
510   return(x);
511}
512example
513{ "EXAMPLE:"; echo = 2;
514   ring R = 0,x,dp;
515   chineseR(list(24,15,7),intvec(2,3,5),30);
516}
517
518///////////////////////////////////////////////////////////////////////////////
519proc primeList(int n)
520"USAGE:  primeList(n);
521RETURN: the intvec of n greatest primes  <= 2134567879
522EXAMPLE:example primList; shows an example
523"
524{
525  intvec L=0:n;
526  int i;
527  int p=2134567879;
528  for(i=1;i<=n;i++)
529  {
530    L[i]=p;
531    p=prime(p-1);
532  }
533  return(L);
534}
535example
536{ "EXAMPLE:"; echo = 2;
537   intvec L=primeList(10);
538   size(L);
539   L[size(L)];
540}
541///////////////////////////////////////////////////////////////////////////////
542/*
543ring r=0,(x,y,z),lp;
544poly s1 = 5x3y2z+3y3x2z+7xy2z2;
545poly s2 = 3xy2z2+x5+11y2z2;
546poly s3 = 4xyz+7x3+12y3+1;
547poly s4 = 3x3-4y3+yz2;
548ideal i =  s1, s2, s3, s4;
549
550ring r=0,(x,y,z),lp;
551poly s1 = 2xy4z2+x3y2z-x2y3z+2xyz2+7y3+7;
552poly s2 = 2x2y4z+x2yz2-xy2z2+2x2yz-12x+12y;
553poly s3 = 2y5z+x2y2z-xy3z-xy3+y4+2y2z;
554poly s4 = 3xy4z3+x2y2z-xy3z+4y3z2+3xyz3+4z2-x+y;
555ideal i =  s1, s2, s3, s4;
556
557ring r=0,(x,y,z),lp;
558poly s1 = 8x2y2 + 5xy3 + 3x3z + x2yz;
559poly s2 = x5 + 2y3z2 + 13y2z3 + 5yz4;
560poly s3 = 8x3 + 12y3 + xz2 + 3;
561poly s4 = 7x2y4 + 18xy3z2 +  y3z3;
562ideal i =  s1, s2, s3, s4;
563
564int n = 6;
565ring r = 0,(x(1..n)),lp;
566ideal i = cyclic(n);
567ring s=0,(x(1..n),t),lp;
568ideal i=imap(r,i);
569i=homog(i,t);
570
571ring r=0,(x(1..4),s),(dp(4),dp);
572poly s1 =1 + s^2*x(1)*x(3) + s^8*x(2)*x(3) + s^19*x(1)*x(2)*x(4);
573poly s2 = x(1) + s^8 *x(1)* x(2)* x(3) + s^19* x(2)* x(4);
574poly s3 = x(2) + s^10*x(3)*x(4) + s^11*x(1)*x(4);
575poly s4 = x(3) + s^4*x(1)*x(2) + s^19*x(1)*x(3)*x(4) +s^24*x(2)*x(3)*x(4);
576poly s5 = x(4) + s^31* x(1)* x(2)* x(3)* x(4);
577ideal i =  s1, s2, s3, s4, s5;
578
579ring r=0,(x,y,z),ds;
580int a =16;
581int b =15;
582int c =4;
583int t =1;
584poly f =x^a+y^b+z^(3*c)+x^(c+2)*y^(c-1)+x^(c-1)*y^(c-1)*z3+x^(c-2)*y^c*(y2+t*x)^2;
585ideal i= jacob(f);
586
587ring r=0,(x,y,z),ds;
588int a =25;
589int b =25;
590int c =5;
591int t =1;
592poly f =x^a+y^b+z^(3*c)+x^(c+2)*y^(c-1)+x^(c-1)*y^(c-1)*z3+x^(c-2)*y^c*(y2+t*x)^2;
593ideal i= jacob(f),f;
594
595ring r=0,(x,y,z),ds;
596int a=10;
597poly f =xyz*(x+y+z)^2 +(x+y+z)^3 +x^a+y^a+z^a;
598ideal i= jacob(f);
599
600ring r=0,(x,y,z),ds;
601int a =6;
602int b =8;
603int c =10;
604int alpha =5;
605int beta= 5;
606int t= 1;
607poly f =x^a+y^b+z^c+x^alpha*y^(beta-5)+x^(alpha-2)*y^(beta-3)+x^(alpha-3)*y^(beta-4)*z^2+x^(alpha-4)*y^(beta-4)*(y^2+t*x)^2;
608ideal i= jacob(f);
609
610*/
Note: See TracBrowser for help on using the repository browser.