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

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