1 | /////////////////////////////////////////////////////////////////// |
---|
2 | version="$Id: sing4ti2.lib,v 1.2 2009-04-07 16:18:06 seelisch Exp $"; |
---|
3 | category="Commutative Algebra"; |
---|
4 | info=" |
---|
5 | LIBRARY: sing4ti2.lib Communication Interface to 4ti2 |
---|
6 | |
---|
7 | AUTHORS: Thomas Kahle , kahle@mis.mpg.de |
---|
8 | @* Anne Fruehbis-Krueger, anne@math.uni-hannover.de |
---|
9 | |
---|
10 | NOTE: This library uses the external program 4ti2 for calculations |
---|
11 | @* and the standard unix tools sed and awk for conversion of |
---|
12 | @* the returned result |
---|
13 | |
---|
14 | REQUIRES: External programs 4ti2, sed and awk to be installed |
---|
15 | |
---|
16 | PROCEDURES: |
---|
17 | markov4ti2(A[,i]) compute Markov basis of given lattice |
---|
18 | hilbert4ti2(A[,i]) compute Hilbert basis of given lattice |
---|
19 | graver4ti2(A[,i]) compute Graver basis of given lattice |
---|
20 | "; |
---|
21 | |
---|
22 | |
---|
23 | proc markov4ti2(matrix A, list #) |
---|
24 | "USAGE: markov4ti2(A[,i]); |
---|
25 | @* A=intmat |
---|
26 | @* i=int |
---|
27 | ASSUME: - A is a matrix with integer entries which describes the lattice |
---|
28 | @* as ker(A), if second argument is not present, |
---|
29 | @* as left image Im(A) = {zA, z \in ZZ^k}(!), if second argument is a positive integer |
---|
30 | @* - number of variables of basering equals number of columns of A |
---|
31 | @* (for ker(A)) resp. of rows of A (for Im(A)) |
---|
32 | CREATE: files sing4ti2.mat, sing4ti2.lat, sing4ti2.mar in the current |
---|
33 | @* directory (I/O files for communication with 4ti2) |
---|
34 | NOTE: input rules for 4ti2 also apply to input to this procedure |
---|
35 | @* hence ker(A)={x|Ax=0} and Im(A)={xA} |
---|
36 | RETURN: toric ideal specified by Markov basis thereof |
---|
37 | EXAMPLE: example markov4ti2; shows an example |
---|
38 | " |
---|
39 | { |
---|
40 | //-------------------------------------------------------------------------- |
---|
41 | // Initialization and Sanity Checks |
---|
42 | //-------------------------------------------------------------------------- |
---|
43 | int i,j; |
---|
44 | int nr=nrows(A); |
---|
45 | int nc=ncols(A); |
---|
46 | string fileending="mat"; |
---|
47 | if (size(#)!=0) |
---|
48 | { |
---|
49 | //--- default behaviour: use ker(A) as lattice |
---|
50 | //--- if #[1]!=0 use Im(A) as lattice |
---|
51 | if(typeof(#[1])!="int") |
---|
52 | { |
---|
53 | ERROR("optional parameter needs to be integer value");\ |
---|
54 | } |
---|
55 | if(#[1]!=0) |
---|
56 | { |
---|
57 | fileending="lat"; |
---|
58 | } |
---|
59 | } |
---|
60 | //--- we should also be checking whether all entries are indeed integers |
---|
61 | //--- or whether there are fractions, but in this case the error message |
---|
62 | //--- of 4ti2 is printed directly |
---|
63 | if(nvars(basering)!=ncols(A)) |
---|
64 | { |
---|
65 | ERROR("number of columns needs to match number of variables"); |
---|
66 | } |
---|
67 | //-------------------------------------------------------------------------- |
---|
68 | // preparing input file for 4ti2 |
---|
69 | //-------------------------------------------------------------------------- |
---|
70 | link eing=":w sing4ti2."+fileending; |
---|
71 | string eingstring=string(nr)+" "+string(nc); |
---|
72 | write(eing,eingstring); |
---|
73 | for(i=1;i<=nr;i++) |
---|
74 | { |
---|
75 | kill eingstring; |
---|
76 | string eingstring; |
---|
77 | for(j=1;j<=nc;j++) |
---|
78 | { |
---|
79 | if((deg(A[i,j])>0)||(char(basering)!=0)||(npars(basering)>0)) |
---|
80 | { |
---|
81 | ERROR("Input to markov4ti2 needs to be a matrix with integer entries"); |
---|
82 | } |
---|
83 | eingstring=eingstring+string(A[i,j])+" "; |
---|
84 | } |
---|
85 | write(eing, eingstring); |
---|
86 | } |
---|
87 | close(eing); |
---|
88 | |
---|
89 | //---------------------------------------------------------------------- |
---|
90 | // calling 4ti2 and converting output |
---|
91 | // Singular's string is too clumsy for this, hence we first prepare |
---|
92 | // using standard unix commands |
---|
93 | //---------------------------------------------------------------------- |
---|
94 | j=system("sh","markov sing4ti2"); |
---|
95 | j=system("sh","awk \'BEGIN{ORS=\",\";}{print $0;}\' sing4ti2.mar | sed s/[\\\ \\\t\\\v\\\f]/,/g | sed s/,+/,/g|sed s/,,/,/g|sed s/,,/,/g > sing4ti2.converted"); |
---|
96 | if(!defined(keepfiles)) |
---|
97 | { |
---|
98 | j=system("sh",("rm -f sing4ti2.mar sing4ti2."+fileending)); |
---|
99 | } |
---|
100 | //---------------------------------------------------------------------- |
---|
101 | // reading output of 4ti2 |
---|
102 | //---------------------------------------------------------------------- |
---|
103 | link ausg=":r sing4ti2.converted"; |
---|
104 | //--- last entry ideal(0) is used to tie the list to the basering |
---|
105 | //--- it will not be processed any further |
---|
106 | string ergstr="list erglist="+read(ausg)+ string(ideal(0))+";"; |
---|
107 | execute(ergstr); |
---|
108 | ideal toric; |
---|
109 | poly temppol1,temppol2; |
---|
110 | for(i=1;i<=erglist[1];i++) |
---|
111 | { |
---|
112 | temppol1=1; |
---|
113 | temppol2=1; |
---|
114 | for(j=1;j<=erglist[2];j++) |
---|
115 | { |
---|
116 | if(erglist[2+(i-1)*erglist[2]+j]>=0) |
---|
117 | { |
---|
118 | //--- positive exponents |
---|
119 | temppol1=temppol1*(var(j)^erglist[2+(i-1)*erglist[2]+j]); |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | //--- negative exponents |
---|
124 | temppol2=temppol2*(var(j)^(-erglist[2+(i-1)*erglist[2]+j])); |
---|
125 | } |
---|
126 | } |
---|
127 | toric=toric,temppol1-temppol2; |
---|
128 | } |
---|
129 | //--- get rid of leading entry 0; |
---|
130 | toric=toric[2..ncols(toric)]; |
---|
131 | return(toric); |
---|
132 | } |
---|
133 | example |
---|
134 | {"EXAMPLE:"; |
---|
135 | echo=2; |
---|
136 | ring r=0,(x,y,z),dp; |
---|
137 | matrix M[2][3]=0,1,2,2,1,0; |
---|
138 | markov4ti2(M); |
---|
139 | matrix N[1][3]=1,2,1; |
---|
140 | markov4ti2(N,1); |
---|
141 | } |
---|
142 | |
---|
143 | /////////////////////////////////////////////////////////////////////////////// |
---|
144 | |
---|
145 | proc graver4ti2(matrix A, list #) |
---|
146 | "USAGE: graver4ti2(A[,i]); |
---|
147 | @* A=intmat |
---|
148 | @* i=int |
---|
149 | ASSUME: - A is a matrix with integer entries which describes the lattice |
---|
150 | @* as ker(A), if second argument is not present, |
---|
151 | @* as the left image Im(A) = {zA : z \in ZZ^k}, if second argument is a positive integer |
---|
152 | @* - number of variables of basering equals number of columns of A |
---|
153 | @* (for ker(A)) resp. of rows of A (for Im(A)) |
---|
154 | CREATE: temporary files sing4ti2.mat, sing4ti2.lat, sing4ti2.gra |
---|
155 | @* in the current directory (I/O files for communication with 4ti2) |
---|
156 | NOTE: input rules for 4ti2 also apply to input to this procedure |
---|
157 | @* hence ker(A)={x|Ax=0} and Im(A)={xA} |
---|
158 | RETURN: toric ideal specified by Graver basis thereof |
---|
159 | EXAMPLE: example graver4ti2; shows an example |
---|
160 | " |
---|
161 | { |
---|
162 | //-------------------------------------------------------------------------- |
---|
163 | // Initialization and Sanity Checks |
---|
164 | //-------------------------------------------------------------------------- |
---|
165 | int i,j; |
---|
166 | int nr=nrows(A); |
---|
167 | int nc=ncols(A); |
---|
168 | string fileending="mat"; |
---|
169 | if (size(#)!=0) |
---|
170 | { |
---|
171 | //--- default behaviour: use ker(A) as lattice |
---|
172 | //--- if #[1]!=0 use Im(A) as lattice |
---|
173 | if(typeof(#[1])!="int") |
---|
174 | { |
---|
175 | ERROR("optional parameter needs to be integer value");\ |
---|
176 | } |
---|
177 | if(#[1]!=0) |
---|
178 | { |
---|
179 | fileending="lat"; |
---|
180 | } |
---|
181 | } |
---|
182 | //--- we should also be checking whether all entries are indeed integers |
---|
183 | //--- or whether there are fractions, but in this case the error message |
---|
184 | //--- of 4ti2 is printed directly |
---|
185 | if(nvars(basering)!=ncols(A)) |
---|
186 | { |
---|
187 | ERROR("number of columns needs to match number of variables"); |
---|
188 | } |
---|
189 | //-------------------------------------------------------------------------- |
---|
190 | // preparing input file for 4ti2 |
---|
191 | //-------------------------------------------------------------------------- |
---|
192 | link eing=":w sing4ti2."+fileending; |
---|
193 | string eingstring=string(nr)+" "+string(nc); |
---|
194 | write(eing,eingstring); |
---|
195 | for(i=1;i<=nr;i++) |
---|
196 | { |
---|
197 | kill eingstring; |
---|
198 | string eingstring; |
---|
199 | for(j=1;j<=nc;j++) |
---|
200 | { |
---|
201 | if((deg(A[i,j])>0)||(char(basering)!=0)||(npars(basering)>0)) |
---|
202 | { |
---|
203 | ERROR("Input to graver4ti2 needs to be a matrix with integer entries"); |
---|
204 | } |
---|
205 | eingstring=eingstring+string(A[i,j])+" "; |
---|
206 | } |
---|
207 | write(eing, eingstring); |
---|
208 | } |
---|
209 | close(eing); |
---|
210 | |
---|
211 | //---------------------------------------------------------------------- |
---|
212 | // calling 4ti2 and converting output |
---|
213 | // Singular's string is too clumsy for this, hence we first prepare |
---|
214 | // using standard unix commands |
---|
215 | //---------------------------------------------------------------------- |
---|
216 | j=system("sh","graver sing4ti2"); |
---|
217 | j=system("sh","awk \'BEGIN{ORS=\",\";}{print $0;}\' sing4ti2.gra | sed s/[\\\ \\\t\\\v\\\f]/,/g | sed s/,+/,/g |sed s/,,/,/g|sed s/,,/,/g > sing4ti2.converted"); |
---|
218 | if(!defined(keepfiles)) |
---|
219 | { |
---|
220 | j=system("sh",("rm -f sing4ti2.gra sing4ti2."+fileending)); |
---|
221 | } |
---|
222 | //---------------------------------------------------------------------- |
---|
223 | // reading output of 4ti2 |
---|
224 | //---------------------------------------------------------------------- |
---|
225 | link ausg=":r sing4ti2.converted"; |
---|
226 | //--- last entry ideal(0) is used to tie the list to the basering |
---|
227 | //--- it will not be processed any further |
---|
228 | string ergstr="list erglist="+read(ausg)+ string(ideal(0))+";"; |
---|
229 | execute(ergstr); |
---|
230 | ideal toric; |
---|
231 | poly temppol1,temppol2; |
---|
232 | for(i=1;i<=erglist[1];i++) |
---|
233 | { |
---|
234 | temppol1=1; |
---|
235 | temppol2=1; |
---|
236 | for(j=1;j<=erglist[2];j++) |
---|
237 | { |
---|
238 | if(erglist[2+(i-1)*erglist[2]+j]>=0) |
---|
239 | { |
---|
240 | //--- positive exponents |
---|
241 | temppol1=temppol1*(var(j)^erglist[2+(i-1)*erglist[2]+j]); |
---|
242 | } |
---|
243 | else |
---|
244 | { |
---|
245 | //--- negative exponents |
---|
246 | temppol2=temppol2*(var(j)^(-erglist[2+(i-1)*erglist[2]+j])); |
---|
247 | } |
---|
248 | } |
---|
249 | toric=toric,temppol1-temppol2; |
---|
250 | } |
---|
251 | //--- get rid of leading entry 0; |
---|
252 | toric=toric[2..ncols(toric)]; |
---|
253 | return(toric); |
---|
254 | } |
---|
255 | example |
---|
256 | {"EXAMPLE:"; |
---|
257 | echo=2; |
---|
258 | ring r=0,(x,y,z,w),dp; |
---|
259 | matrix M[2][4]=0,1,2,3,3,2,1,0; |
---|
260 | graver4ti2(M); |
---|
261 | } |
---|
262 | |
---|
263 | /////////////////////////////////////////////////////////////////////////////// |
---|
264 | |
---|
265 | proc hilbert4ti2(matrix A, list #) |
---|
266 | "USAGE: hilbert4ti2(A[,i]); |
---|
267 | @* A=intmat |
---|
268 | @* i=int |
---|
269 | ASSUME: - A is a matrix with integer entries which describes the lattice |
---|
270 | @* as ker(A), if second argument is not present, |
---|
271 | @* as the left image Im(A) = {zA : z \in ZZ^k}, if second argument is a positive integer |
---|
272 | @* - number of variables of basering equals number of columns of A |
---|
273 | @* (for ker(A)) resp. of rows of A (for Im(A)) |
---|
274 | CREATE: temporary files sing4ti2.mat, sing4ti2.lat, sing4ti2.mar |
---|
275 | @* in the current directory (I/O files for communication with 4ti2) |
---|
276 | NOTE: input rules for 4ti2 also apply to input to this procedure |
---|
277 | @* hence ker(A)={x|Ax=0} and Im(A)={xA} |
---|
278 | RETURN: toric ideal specified by Hilbert basis thereof |
---|
279 | EXAMPLE: example graver4ti2; shows an example |
---|
280 | " |
---|
281 | { |
---|
282 | //-------------------------------------------------------------------------- |
---|
283 | // Initialization and Sanity Checks |
---|
284 | //-------------------------------------------------------------------------- |
---|
285 | int i,j; |
---|
286 | int nr=nrows(A); |
---|
287 | int nc=ncols(A); |
---|
288 | string fileending="mat"; |
---|
289 | if (size(#)!=0) |
---|
290 | { |
---|
291 | //--- default behaviour: use ker(A) as lattice |
---|
292 | //--- if #[1]!=0 use Im(A) as lattice |
---|
293 | if(typeof(#[1])!="int") |
---|
294 | { |
---|
295 | ERROR("optional parameter needs to be integer value");\ |
---|
296 | } |
---|
297 | if(#[1]!=0) |
---|
298 | { |
---|
299 | fileending="lat"; |
---|
300 | } |
---|
301 | } |
---|
302 | //--- we should also be checking whether all entries are indeed integers |
---|
303 | //--- or whether there are fractions, but in this case the error message |
---|
304 | //--- of 4ti2 is printed directly |
---|
305 | if(nvars(basering)!=ncols(A)) |
---|
306 | { |
---|
307 | ERROR("number of columns needs to match number of variables"); |
---|
308 | } |
---|
309 | //-------------------------------------------------------------------------- |
---|
310 | // preparing input file for 4ti2 |
---|
311 | //-------------------------------------------------------------------------- |
---|
312 | link eing=":w sing4ti2."+fileending; |
---|
313 | string eingstring=string(nr)+" "+string(nc); |
---|
314 | write(eing,eingstring); |
---|
315 | for(i=1;i<=nr;i++) |
---|
316 | { |
---|
317 | kill eingstring; |
---|
318 | string eingstring; |
---|
319 | for(j=1;j<=nc;j++) |
---|
320 | { |
---|
321 | if((deg(A[i,j])>0)||(char(basering)!=0)||(npars(basering)>0)) |
---|
322 | { |
---|
323 | ERROR("Input to hilbert4ti2 needs to be a matrix with integer entries"); |
---|
324 | } |
---|
325 | eingstring=eingstring+string(A[i,j])+" "; |
---|
326 | } |
---|
327 | write(eing, eingstring); |
---|
328 | } |
---|
329 | close(eing); |
---|
330 | |
---|
331 | //---------------------------------------------------------------------- |
---|
332 | // calling 4ti2 and converting output |
---|
333 | // Singular's string is too clumsy for this, hence we first prepare |
---|
334 | // using standard unix commands |
---|
335 | //---------------------------------------------------------------------- |
---|
336 | j=system("sh","hilbert sing4ti2"); |
---|
337 | j=system("sh","awk \'BEGIN{ORS=\",\";}{print $0;}\' sing4ti2.hil | sed s/[\\\ \\\t\\\v\\\f]/,/g | sed s/,+/,/g |sed s/,,/,/g|sed s/,,/,/g > sing4ti2.converted"); |
---|
338 | if(!defined(keepfiles)) |
---|
339 | { |
---|
340 | j=system("sh",("rm -f sing4ti2.hil sing4ti2."+fileending)); |
---|
341 | } |
---|
342 | //---------------------------------------------------------------------- |
---|
343 | // reading output of 4ti2 |
---|
344 | //---------------------------------------------------------------------- |
---|
345 | link ausg=":r sing4ti2.converted"; |
---|
346 | //--- last entry ideal(0) is used to tie the list to the basering |
---|
347 | //--- it will not be processed any further |
---|
348 | string ergstr="list erglist="+read(ausg)+ string(ideal(0))+";"; |
---|
349 | execute(ergstr); |
---|
350 | ideal toric; |
---|
351 | poly temppol1,temppol2; |
---|
352 | for(i=1;i<=erglist[1];i++) |
---|
353 | { |
---|
354 | temppol1=1; |
---|
355 | temppol2=1; |
---|
356 | for(j=1;j<=erglist[2];j++) |
---|
357 | { |
---|
358 | if(erglist[2+(i-1)*erglist[2]+j]>=0) |
---|
359 | { |
---|
360 | //--- positive exponents |
---|
361 | temppol1=temppol1*(var(j)^erglist[2+(i-1)*erglist[2]+j]); |
---|
362 | } |
---|
363 | else |
---|
364 | { |
---|
365 | //--- negative exponents |
---|
366 | temppol2=temppol2*(var(j)^(-erglist[2+(i-1)*erglist[2]+j])); |
---|
367 | } |
---|
368 | } |
---|
369 | toric=toric,temppol1-temppol2; |
---|
370 | } |
---|
371 | //--- get rid of leading entry 0; |
---|
372 | toric=toric[2..ncols(toric)]; |
---|
373 | return(toric); |
---|
374 | } |
---|
375 | // A nice example here is the 3x3 Magic Squares |
---|
376 | example |
---|
377 | {"EXAMPLE:"; |
---|
378 | echo=2; |
---|
379 | ring r=0,(x1,x2,x3,x4,x5,x6,x7,x8,x9),dp; |
---|
380 | matrix M[7][9]=1,1,1,-1,-1,-1,0,0,0,1,1,1,0,0,0,-1,-1,-1,0,1,1,-1,0,0,-1,0,0,1,0,1,0,-1,0,0,-1,0,1,1,0,0,0,-1,0,0,-1,0,1,1,0,-1,0,0,0,-1,1,1,0,0,-1,0,-1,0,0; |
---|
381 | hilbert4ti2(M); |
---|
382 | } |
---|
383 | |
---|
384 | ///////////////////////////////////////////////////////////////////////////// |
---|
385 | |
---|