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
|
newPackage(
"Polymake",
Version => "0.1",
Date => "March 21, 2008",
Authors => {{Name => "Josephine Yu",
Email => "jyu@math.mit.edu",
HomePage => "http://www.math.mit.edu/~jyu/"}},
Headline => "interfacing with polymake",
Keywords => {"Interfaces"},
DebuggingMode => false
)
export {"PolymakeObject", "polymakeObject",
"removeComments",
"getPropertyNames",
"getProperty", "getMatrixProperty", "getListProperty",
"getVectorProperty", "makevec", "makemat",
"toPolymakeFormat", "runPolymake", "NewtonPolytope"}
PolymakeObject = new Type of MutableHashTable
isBlankLine = (s) -> (
#s == 0 or
match("^[[:space:]]*$",s) or
match("^[[:space:]]*#",s)
)
makevec = (str) -> (
t := separateRegexp(" +", removeComment str);
t = apply(t,value);
select(t, x-> x =!= null)
)
makemat = (L) -> (
if #L == 0 then map(ZZ^0,ZZ^0,0)
else matrix(L/makevec)
)
makelist = (L) -> (
if #L == 0 then {}
else apply(removeComments(L), t-> value replace(" ", ",", t))
)
removeComments = (F) -> (
-- F is a list of lines
-- removes comments beginning with #
-- also removes trailing blank spaces
if F === null then return null
else
apply(F, s-> replace("[[:space:]]*#.*|[[:space:]]+$","" ,s))
)
removeComment = (str) -> (
-- F is a list of lines
-- removes comments beginning with #
-- also removes trailing blank spaces
replace("[[:space:]]*#.*|[[:space:]]+$","" ,str)
)
getPropertyNames = method()
getPropertyNames(List) := (F) -> (
-- F is a list of lines, as from a polymake file
-- returns a list of strings, names of known properties
FF := removeComments(F);
PP := select(FF, s-> match("^[[:space:]]*[[:alpha:]]", s));
apply(PP, s -> toString s)
)
getProperty = method()
getProperty(List, String) := (F, section) -> (
-- F is a list of lines, as from a polymake file
n := position(F, s -> match("^"|section|///[:space:]*$///,s) or
match("^"|section|///[:space:]*#///,s));
if(n =!= null) then
(
p := n+1;
if isBlankLine F_p then {}
else
while p < #F and not isBlankLine F_p list (p=p+1; F_(p-1))
)
else null
)
getProperty(String,String) := (filename, propertyname) -> (
F := lines get filename;
getProperty(F, propertyname)
)
getMatrixProperty = method()
getMatrixProperty(List, String) := (F, section) -> (
L := getProperty(F,section);
if L === null then null
else makemat L
)
getMatrixProperty(String,String) := (filename, propertyname) -> (
F := lines get filename;
getMatrixProperty(F, propertyname)
)
getListProperty = method()
getListProperty(List, String) := (F, section) -> (
L := getProperty(F,section);
if L === null then null
else makelist L
)
getListProperty(String,String) := (filename, propertyname) -> (
F := lines get filename;
getListProperty(F, propertyname)
)
getVectorProperty = method()
getVectorProperty(List, String) := (F, section) -> (
L := getProperty(F,section);
if L === null then null
else if #L > 1 then error "property is not a vector"
else if #L == 0 then {}
else makevec L_0
)
getVectorProperty(String,String) := (filename, propertyname) -> (
F := lines get filename;
getVectorProperty(F, propertyname)
)
polymakeObject = method()
polymakeObject(String, List) := (filename, properties) -> (
F := removeComments lines get filename;
new PolymakeObject from apply(properties, p -> p => getMatrixProperty(F,p))
)
polymakeObject(String) := (filename) -> (
F := removeComments lines get filename;
properties := getPropertyNames(F);
new PolymakeObject from apply(properties, p -> p => getMatrixProperty(F,p))
)
toPolymakeFormat = method()
toPolymakeFormat(String, Matrix) := (propertyname, M) -> (
if M === null then ""
else(
S := propertyname|"\n";
if numRows M > 0 then
S = S|replace("\\|", "", toString net M);
S
)
)
toPolymakeFormat(String,Vector) := (propertyname,V) -> (
if V === null then ""
else(
L := entries V;
S := propertyname|"\n";
if length L > 0 then
S = S|replace("\\|", "", toString net matrix{L});
S
)
)
toPolymakeFormat(PolymakeObject) := (P) -> (
concatenate apply(pairs P, a -> toPolymakeFormat(a)|"\n\n")
)
runPolymake = method()
runPolymake(String,String) := (filename, propertyname)->(
ex := "polymake "|filename|" "|propertyname;
run ex
)
runPolymake(PolymakeObject, String) := (P, propertyname) -> (
filename := temporaryFileName();
<< "using temporary file " << filename << endl;
filename << toPolymakeFormat(P) << endl << close;
runPolymake(filename, propertyname)
)
NewtonPolytope = method()
NewtonPolytope(RingElement) := (f) -> (
new PolymakeObject from {"POINTS" => matrix apply(listForm f, m -> {1}|m#0)}
)
-----------------------DOCUMENTATION----------------------
beginDocumentation()
document {
Key => Polymake,
Headline => "a package for interfacing with polymake",
EM "Polymake", " is a package for interfacing with polymake, and
for reading and writing files in polymake's format.",
PARA{},
"Warning: this package is not complete, and is mostly undocumented, but it is used in ", TO "gfanInterface::gfanInterface", ".
It is expected that the interface will change in the (near) future."
}
end
document {
Key => {(firstFunction,ZZ),firstFunction},
Headline => "a silly first function",
Usage => "firstFunction n",
Inputs => { "n" },
Outputs => {{ "a silly string, depending on the value of ", TT "n" }},
SourceCode => {(firstFunction,ZZ)},
EXAMPLE lines ///
firstFunction 1
firstFunction 0
///
}
document {
Key => {(firstFunction,ZZ),firstFunction},
Headline => "a silly first function",
Usage => "firstFunction n",
Inputs => { "n" },
Outputs => {{ "a silly string, depending on the value of ", TT "n" }},
SourceCode => {(firstFunction,ZZ)},
EXAMPLE lines ///
firstFunction 1
firstFunction 0
///
}
document {
Key => {(firstFunction,ZZ),firstFunction},
Headline => "a silly first function",
Usage => "firstFunction n",
Inputs => { "n" },
Outputs => {{ "a silly string, depending on the value of ", TT "n" }},
SourceCode => {(firstFunction,ZZ)},
EXAMPLE lines ///
firstFunction 1
firstFunction 0
///
}
------------------------- TEST ---------------------------
TEST ///
assert ( firstFunction 2 == "D'oh!" )
///
readMat = method(TypicalValue => Matrix)
readMat(String,Ring) := (filename,R) -> (
ss := select(lines get filename, s -> length s > 0);
matrix(R, apply(ss, s -> (t := separateRegexp(" +", s);
t = apply(t,value);
select(t, x -> class x =!= Nothing))))
)
firstFunction = method(TypicalValue => String)
firstFunction ZZ := String => n -> if n == 1 then "Hello World!" else "D'oh!"
restart
loadPackage "Polymake"
filename = "/Users/bb/Desktop/3cube.poly";
F = lines get "/Users/bb/Desktop/3cube.poly";
L := getPropertyNames(F)
class L_0
P = polymakeObject(filename);
peek P
getProperty(F, "AMBIENT_DIM")
getProperty(filename, "VERTICES")
getProperty(F, "FACETS")
getProperty(F, "AFFINE_HULL")
getMatrixProperty(F, "VERTICES")
facets = getMatrixProperty(F, "FACETS")
AH = getMatrixProperty(F, "AFFINE_HULL")
fv = getVectorProperty(F, "F_VECTOR")
P = polymakeObject("/Users/bb/Desktop/3cube.poly", {"VERTICES","DIM","FACETS"})
P = polymakeObject(filename, {"VERTICES","DIM","FACETS"})
P = polymakeObject("/Users/bb/Desktop/3cube.poly", {"AMBIENT_DIM"})
peek oo
peek P
toPolymakeFormat(P)
"/Users/bb/Desktop/temp" << toPolymake("FACETS", facets) <<endl<< close;
runPolymake("/Users/bb/Desktop/3cube.poly", "FACETS")
runPolymake(P, "F_VECTOR");
FF = lines get "/tmp/M2-799-1";
getVectorProperty(FF,"F_VECTOR")
viewHelp listForm
ZZ[x,y,z]
f = (1-x)*(1-y)*(1-z)
P = NewtonPolytope f
peek P
runPolymake(P,"FACETS");
getMatrixProperty("/tmp/M2-825-7","VERTICES")
------------------ TO DO ---------------------
get other properties such as lists, graphs, simplicial complexes
if can't process, just read as list of strings
|