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