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