1 | //GP, last modified 23.10.06 |
---|
2 | /////////////////////////////////////////////////////////////////////////////// |
---|
3 | version="$Id: modstd.lib,v 1.1 2007-01-09 10:31:49 Singular Exp $"; |
---|
4 | category="Commutative Algebra"; |
---|
5 | info=" |
---|
6 | LIBRARY: modstd.lib Grobner basis of ideals |
---|
7 | AUTHORS: A. Hashemi, Amir.Hashemi@lip6.fr |
---|
8 | @* G. Pfister pfister@mathematik.uni-kl.de |
---|
9 | @* H. Schoenemann hannes@mathematik.uni-kl.de |
---|
10 | |
---|
11 | NOTE: |
---|
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 | PROCEDURES: |
---|
21 | modStd(I,1); compute the reduced Groebner basis of I using modular methods |
---|
22 | modS(I,L); liftings to Q of Groebner bases of I mod p for p in L |
---|
23 | primeList(n); list of n primes <= 2134567879 in decreasing order |
---|
24 | "; |
---|
25 | |
---|
26 | LIB "crypto.lib"; |
---|
27 | /////////////////////////////////////////////////////////////////////////////// |
---|
28 | proc modStd(ideal I,list #) |
---|
29 | "USAGE: modStd(I,[k]); I ideal (an optional integer k) |
---|
30 | RETURN: if # is empty: |
---|
31 | an ideal which is with high probability a reduced Groebner basis of I; |
---|
32 | it is not tested whether the result is a Groebner basis and |
---|
33 | it is not tested whether the result contains I. |
---|
34 | if #[1]=1: |
---|
35 | a Groebner basis which contains I if no warning appears; |
---|
36 | if I is homogeneous it is a Groebner basis of I |
---|
37 | NOTE: the procedure computes the reduced Groebner basis of I (over the |
---|
38 | rational numbers) by using modular methods. If #[1]=1 and a |
---|
39 | warning appears then the result is a Groebner basis with no defined |
---|
40 | relation to I; this is a sign that not enough prime numbers have |
---|
41 | been used. For further experiments see procedure modS. |
---|
42 | EXAMPLE: example modStd; shows an example |
---|
43 | " |
---|
44 | { |
---|
45 | def R0=basering; |
---|
46 | list rl=ringlist(R0); |
---|
47 | if((npars(R0)>0)||(rl[1]>0)) |
---|
48 | { |
---|
49 | ERROR("characteristic of basering should be zero"); |
---|
50 | } |
---|
51 | int l,j,k,q; |
---|
52 | list T,TT; |
---|
53 | list L=primeList(5); |
---|
54 | L[6]=prime(random(1000000000,2000000000)); |
---|
55 | ideal J,cT,lT,K; |
---|
56 | ideal I0=I; |
---|
57 | |
---|
58 | for (j=1;j<=size(L);j++) |
---|
59 | { |
---|
60 | rl[1]=L[j]; |
---|
61 | def oro=ring(rl); |
---|
62 | setring oro; |
---|
63 | ideal I=fetch(R0,I); |
---|
64 | option(redSB); |
---|
65 | ideal I1=groebner(I); |
---|
66 | setring R0; |
---|
67 | T[j]=fetch(oro,I1); |
---|
68 | kill oro; |
---|
69 | } |
---|
70 | //================= delete unlucky primes ==================== |
---|
71 | // unlucky iff the leading ideal is wrong |
---|
72 | lT=lead(T[size(T)]); |
---|
73 | for (j=1;j<size(T);j++) |
---|
74 | { |
---|
75 | cT=lead(T[j]); |
---|
76 | for(k=1;k<=size(lT);k++) |
---|
77 | { |
---|
78 | if(lT[k]<cT[k]){lT=cT;break;} |
---|
79 | } |
---|
80 | if(size(lT)<size(cT)){lT=cT;} |
---|
81 | } |
---|
82 | j=1; |
---|
83 | attrib(lT,"isSB",1); |
---|
84 | while(j<=size(T)) |
---|
85 | { |
---|
86 | cT=lead(T[j]); |
---|
87 | attrib(cT,"isSB",1); |
---|
88 | if((size(reduce(cT,lT))!=0)||(size(reduce(lT,cT))!=0)) |
---|
89 | { |
---|
90 | T=delete(T,j); |
---|
91 | L=delete(L,j); |
---|
92 | j--; |
---|
93 | } |
---|
94 | j++; |
---|
95 | } |
---|
96 | //============ now all leading ideals are the same ============ |
---|
97 | for(j=1;j<=ncols(T[1]);j++) |
---|
98 | { |
---|
99 | for(k=1;k<=size(L);k++) |
---|
100 | { |
---|
101 | TT[k]=T[k][j]; |
---|
102 | } |
---|
103 | J[j]=liftPoly(TT,L); |
---|
104 | } |
---|
105 | //=========== chooses more primes up to the moment the result becomes stable |
---|
106 | while(1) |
---|
107 | { |
---|
108 | k=0; |
---|
109 | q=prime(random(2000000011,2100000000)); |
---|
110 | while(k<size(L)) |
---|
111 | { |
---|
112 | k++; |
---|
113 | if(L[k]==q) |
---|
114 | { |
---|
115 | k=0; |
---|
116 | q=prime(random(1000000,2100000000)); |
---|
117 | } |
---|
118 | } |
---|
119 | L[size(L)+1]=q; |
---|
120 | rl[1]=L[size(L)]; |
---|
121 | def @r=ring(rl); |
---|
122 | setring @r; |
---|
123 | ideal i=fetch(R0,I); |
---|
124 | option(redSB); |
---|
125 | i=groebner(i); |
---|
126 | setring R0; |
---|
127 | T[size(T)+1]=fetch(@r,i); |
---|
128 | kill @r; |
---|
129 | cT=lead(T[size(T)]); |
---|
130 | attrib(cT,"isSB",1); |
---|
131 | if((size(reduce(cT,lT))!=0)||(size(reduce(lT,cT))!=0)) |
---|
132 | { |
---|
133 | T=delete(T,size(T)); |
---|
134 | L=delete(L,size(L)); |
---|
135 | k=0; |
---|
136 | } |
---|
137 | else |
---|
138 | { |
---|
139 | for(j=1;j<=ncols(T[1]);j++) |
---|
140 | { |
---|
141 | for(k=1;k<=size(L);k++) |
---|
142 | { |
---|
143 | TT[k]=T[k][j]; |
---|
144 | } |
---|
145 | K[j]=liftPoly(TT,L); |
---|
146 | } |
---|
147 | k=1; |
---|
148 | for(j=1;j<=size(K);j++){if(K[j]-J[j]!=0){k=0;break;}} |
---|
149 | } |
---|
150 | if(k){break;} |
---|
151 | } |
---|
152 | //============ optional test for standard basis and I=J ======= |
---|
153 | if(size(#)>0) |
---|
154 | { |
---|
155 | J=std(J); |
---|
156 | I0=reduce(I0,J); |
---|
157 | if(size(I0)>0){"WARNING: The input ideal is not contained |
---|
158 | in the ideal generated by the standardbasis";} |
---|
159 | } |
---|
160 | attrib(J,"isSB",1); |
---|
161 | return(J); |
---|
162 | } |
---|
163 | example |
---|
164 | { "EXAMPLE:"; echo = 2; |
---|
165 | ring r=0,(x,y,z),dp; |
---|
166 | ideal I=3x3+x2+1,11y5+y3+2,5z4+z2+4; |
---|
167 | ideal J=modStd(I); |
---|
168 | J; |
---|
169 | } |
---|
170 | /////////////////////////////////////////////////////////////////////////////// |
---|
171 | proc modS(ideal I, list L) |
---|
172 | "USAGE: modS(I,L); I ideal, L list of primes |
---|
173 | RETURN: an ideal which is with high probability a standard basis |
---|
174 | NOTE: This procedure is designed for fast experiments. |
---|
175 | It is not tested whether the result is a standard basis. |
---|
176 | It is not tested whether the result generates I. |
---|
177 | EXAMPLE: example modS; shows an example |
---|
178 | " |
---|
179 | { |
---|
180 | int j,k; |
---|
181 | list T,TT; |
---|
182 | def R0=basering; |
---|
183 | ideal J,cT,lT,K; |
---|
184 | ideal I0=I; |
---|
185 | list rl=ringlist(R0); |
---|
186 | if((npars(R0)>0)||(rl[1]>0)) |
---|
187 | { |
---|
188 | ERROR("characteristic of basering should be zero"); |
---|
189 | } |
---|
190 | for (j=1;j<=size(L);j++) |
---|
191 | { |
---|
192 | rl[1]=L[j]; |
---|
193 | def @r=ring(rl); |
---|
194 | setring @r; |
---|
195 | ideal i=fetch(R0,I); |
---|
196 | option(redSB); |
---|
197 | i=groebner(i); |
---|
198 | setring R0; |
---|
199 | T[j]=fetch(@r,i); |
---|
200 | kill @r; |
---|
201 | } |
---|
202 | //================= delete unlucky primes ==================== |
---|
203 | // unlucky iff the leading ideal is wrong |
---|
204 | lT=lead(T[1]); |
---|
205 | for (j=2;j<=size(T);j++) |
---|
206 | { |
---|
207 | cT=lead(T[j]); |
---|
208 | for(k=1;k<=size(lT);k++) |
---|
209 | { |
---|
210 | if(lT[k]<cT[k]){lT=cT;break;} |
---|
211 | } |
---|
212 | if(size(lT)<size(cT)){lT=cT;} |
---|
213 | } |
---|
214 | j=1; |
---|
215 | attrib(lT,"isSB",1); |
---|
216 | while(j<=size(T)) |
---|
217 | { |
---|
218 | cT=lead(T[j]); |
---|
219 | attrib(cT,"isSB",1); |
---|
220 | if((size(reduce(cT,lT))!=0)||(size(reduce(lT,cT))!=0)) |
---|
221 | { |
---|
222 | T=delete(T,j); |
---|
223 | L=delete(L,j); |
---|
224 | j--; |
---|
225 | } |
---|
226 | j++; |
---|
227 | } |
---|
228 | //============ now all leading ideals are the same ============ |
---|
229 | for(j=1;j<=ncols(T[1]);j++) |
---|
230 | { |
---|
231 | for(k=1;k<=size(L);k++) |
---|
232 | { |
---|
233 | TT[k]=T[k][j]; |
---|
234 | } |
---|
235 | J[j]=liftPoly(TT,L); |
---|
236 | |
---|
237 | } |
---|
238 | attrib(J,"isSB",1); |
---|
239 | return(J); |
---|
240 | } |
---|
241 | example |
---|
242 | { "EXAMPLE:"; echo = 2; |
---|
243 | list L=3,5,11,13,181; |
---|
244 | ring r=0,(x,y,z),dp; |
---|
245 | ideal I=3x3+x2+1,11y5+y3+2,5z4+z2+4; |
---|
246 | ideal J=modS(I,L); |
---|
247 | J; |
---|
248 | } |
---|
249 | /////////////////////////////////////////////////////////////////////////////// |
---|
250 | proc liftPoly(list T, list L) |
---|
251 | "USAGE: liftPoly(T,L); T list of polys, L list of primes |
---|
252 | RETURN: poly p in Q[x] such that p mod L[i]=T[i] |
---|
253 | EXAMPLE: example liftPoly; shows an example |
---|
254 | " |
---|
255 | { |
---|
256 | poly result; |
---|
257 | int i; |
---|
258 | poly p; |
---|
259 | list TT; |
---|
260 | number n; |
---|
261 | |
---|
262 | number N=L[1]; |
---|
263 | for(i=2;i<=size(L);i++) |
---|
264 | { |
---|
265 | N=N*L[i]; |
---|
266 | } |
---|
267 | while(1) |
---|
268 | { |
---|
269 | p=leadmonom(T[1]); |
---|
270 | for(i=2;i<=size(T);i++) |
---|
271 | { |
---|
272 | if(leadmonom(T[i])>p) |
---|
273 | { |
---|
274 | p=leadmonom(T[i]); |
---|
275 | } |
---|
276 | } |
---|
277 | if (p==0) {return(result);} |
---|
278 | for(i=1;i<=size(T);i++) |
---|
279 | { |
---|
280 | if(p==leadmonom(T[i])) |
---|
281 | { |
---|
282 | TT[i]=leadcoef(T[i]); |
---|
283 | T[i]=T[i]-lead(T[i]); |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | TT[i]=0; |
---|
288 | } |
---|
289 | } |
---|
290 | n=chineseR(TT,L); |
---|
291 | n=Farey(N,n); |
---|
292 | result=result+n*p; |
---|
293 | } |
---|
294 | } |
---|
295 | example |
---|
296 | { "EXAMPLE:"; echo = 2; |
---|
297 | ring R = 0,(x,y),dp; |
---|
298 | list L=32003,181,241,499; |
---|
299 | list T=x2+7000x+13000,x2+100x+147y+40,x2+120x+191y+10,x2+x+67y+100; |
---|
300 | liftPoly(T,L); |
---|
301 | } |
---|
302 | /////////////////////////////////////////////////////////////////////////////// |
---|
303 | proc Farey (number P, number N) |
---|
304 | "USAGE: Farey (P,N); P, N number; |
---|
305 | RETURN: a rational number a/b such that a/b=N mod P |
---|
306 | and |a|,|b|<(P/2)^{1/2} |
---|
307 | " |
---|
308 | { |
---|
309 | if (P<0){P=-P;} |
---|
310 | if (N<0){N=N+P;} |
---|
311 | number A,B,C,D,E; |
---|
312 | E=P; |
---|
313 | B=1; |
---|
314 | while (N!=0) |
---|
315 | { |
---|
316 | if (2*N^2<P){return(N/B);} |
---|
317 | D=E mod N; |
---|
318 | C=A-(E-E mod N)/N*B; |
---|
319 | E=N; |
---|
320 | N=D; |
---|
321 | A=B; |
---|
322 | B=C; |
---|
323 | } |
---|
324 | return(0); |
---|
325 | } |
---|
326 | example |
---|
327 | { "EXAMPLE:"; echo = 2; |
---|
328 | ring R = 0,x,dp; |
---|
329 | Farey(32003,12345); |
---|
330 | } |
---|
331 | /////////////////////////////////////////////////////////////////////////////// |
---|
332 | proc chineseR(list T,list L) |
---|
333 | "USAGE: chineseRem(T,L); |
---|
334 | RETURN: x such that x = T[i] mod L[i] |
---|
335 | NOTE: chinese remainder theorem |
---|
336 | EXAMPLE:example chineseRem; shows an example |
---|
337 | " |
---|
338 | { |
---|
339 | number x; |
---|
340 | if(size(L)==1) |
---|
341 | { |
---|
342 | x=T[1] mod L[1]; |
---|
343 | if(x>L[1]/2){x=x-L[1];} |
---|
344 | return(x); |
---|
345 | } |
---|
346 | int i; |
---|
347 | int n=size(L); |
---|
348 | number N=1; |
---|
349 | for(i=1;i<=n;i++) |
---|
350 | { |
---|
351 | N=N*L[i]; |
---|
352 | } |
---|
353 | list M; |
---|
354 | for(i=1;i<=n;i++) |
---|
355 | { |
---|
356 | M[i]=N/L[i]; |
---|
357 | } |
---|
358 | list S=eexgcdN(M); |
---|
359 | for(i=1;i<=n;i++) |
---|
360 | { |
---|
361 | x=x+S[i]*M[i]*T[i]; |
---|
362 | } |
---|
363 | x=x mod N; |
---|
364 | if (x>N/2) { x=x-N; } |
---|
365 | return(x); |
---|
366 | } |
---|
367 | example |
---|
368 | { "EXAMPLE:"; echo = 2; |
---|
369 | ring R = 0,x,dp; |
---|
370 | chineseRem(list(24,15,7),list(2,3,5)); |
---|
371 | } |
---|
372 | /////////////////////////////////////////////////////////////////////////////// |
---|
373 | proc primeList(int n) |
---|
374 | "USAGE: primeList(n); |
---|
375 | RETURN: the list of n greatest primes <= 2134567879 |
---|
376 | EXAMPLE:example primList; shows an example |
---|
377 | " |
---|
378 | { |
---|
379 | list L; |
---|
380 | int i; |
---|
381 | int p=2134567879; |
---|
382 | for(i=1;i<=n;i++) |
---|
383 | { |
---|
384 | L[i]=p; |
---|
385 | p=prime(p-1); |
---|
386 | } |
---|
387 | return(L); |
---|
388 | } |
---|
389 | example |
---|
390 | { "EXAMPLE:"; echo = 2; |
---|
391 | list L=primeList(10); |
---|
392 | size(L); |
---|
393 | L[size(L)]; |
---|
394 | } |
---|
395 | /////////////////////////////////////////////////////////////////////////////// |
---|
396 | /* |
---|
397 | ring r=0,(x,y,z),lp; |
---|
398 | poly s1 = 5x3y2z+3y3x2z+7xy2z2; |
---|
399 | poly s2 = 3xy2z2+x5+11y2z2; |
---|
400 | poly s3 = 4xyz+7x3+12y3+1; |
---|
401 | poly s4 = 3x3-4y3+yz2; |
---|
402 | ideal i = s1, s2, s3, s4; |
---|
403 | |
---|
404 | ring r=0,(x,y,z),lp; |
---|
405 | poly s1 = 2xy4z2+x3y2z-x2y3z+2xyz2+7y3+7; |
---|
406 | poly s2 = 2x2y4z+x2yz2-xy2z2+2x2yz-12x+12y; |
---|
407 | poly s3 = 2y5z+x2y2z-xy3z-xy3+y4+2y2z; |
---|
408 | poly s4 = 3xy4z3+x2y2z-xy3z+4y3z2+3xyz3+4z2-x+y; |
---|
409 | ideal i = s1, s2, s3, s4; |
---|
410 | |
---|
411 | ring r=0,(x,y,z),lp; |
---|
412 | poly s1 = 8x2y2 + 5xy3 + 3x3z + x2yz; |
---|
413 | poly s2 = x5 + 2y3z2 + 13y2z3 + 5yz4; |
---|
414 | poly s3 = 8x3 + 12y3 + xz2 + 3; |
---|
415 | poly s4 = 7x2y4 + 18xy3z2 + y3z3; |
---|
416 | ideal i = s1, s2, s3, s4; |
---|
417 | |
---|
418 | ring r=0,x(1..4),lp; |
---|
419 | ideal i=cyclic(4); |
---|
420 | |
---|
421 | ring r=0,(x(1..4),s),(dp(4),dp); |
---|
422 | poly s1 =1 + s^2*x(1)*x(3) + s^8*x(2)*x(3) + s^19*x(1)*x(2)*x(4); |
---|
423 | poly s2 = x(1) + s^8 *x(1)* x(2)* x(3) + s^19* x(2)* x(4); |
---|
424 | poly s3 = x(2) + s^10*x(3)*x(4) + s^11*x(1)*x(4); |
---|
425 | poly 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); |
---|
426 | poly s5 = x(4) + s^31* x(1)* x(2)* x(3)* x(4); |
---|
427 | ideal i = s1, s2, s3, s4, s5; |
---|
428 | */ |
---|