-
Notifications
You must be signed in to change notification settings - Fork 17
/
perlobj.jl
283 lines (224 loc) · 11.8 KB
/
perlobj.jl
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
@testset "bigobject" begin
points_int = [ 1 0 0 ; 1 3 0 ; 1 0 3 ; 1 3 3 ]
points_rat = Rational{Int}[ 1 0 0 ; 1 3 0 ; 1 0 3 ; 1 3 3 ]
points_unbounded = [1 0 0; 0 1 1]
@testset "constructors" begin
@test Polymake.bigobject("polytope::Polytope", POINTS=points_int ) isa Polymake.BigObject
@test Polymake.bigobject("polytope::Polytope", POINTS=points_rat ) isa Polymake.BigObject
# macro literals
@test (@pm polytope.Polytope(POINTS=[ 1 0 0 ; 1 3 0 ; 1 0 3 ; 1 3 3 ])) isa Polymake.BigObject
# Make sure that we can also handle different matrix types, e.g. adjoint
@test (@pm polytope.Polytope(POINTS=points_int')) isa Polymake.BigObject
pm1 = Polymake.Integer(1)
pm2 = Polymake.Integer(2)
@test (@pm polytope.Polytope(POINTS=[pm1 pm2])) isa Polymake.BigObject
@test (@pm polytope.Polytope(POINTS=[pm1//pm2 pm2//pm2])) isa Polymake.BigObject
@test (@pm polytope.Polytope(POINTS=[1//2 1//2])) isa Polymake.BigObject
@test Polymake.bigobject("fulton::CyclicQuotient", N=7, Q=2) isa Polymake.BigObject
G = Polymake.graph.complete(4).ADJACENCY
@test G isa Polymake.GraphAllocated{Polymake.Undirected}
@test Polymake.bigobject("graph::Graph", ADJACENCY=G) isa Polymake.BigObject
@test polytope.cube(3, 1//4, -1//4) isa Polymake.BigObject
function test_pm_macro()
P = @pm polytope.cube(3)
Pfl = @pm common.convert_to{Float}(P)
d = polytope.dim(Pfl)::Int
return d+1
end
@test test_pm_macro() == 4
@test polytope.cube(Polymake.PropertyValue, 3) isa Polymake.PropertyValue
c = polytope.cube(Polymake.PropertyValue, 3);
@test polytope.spherize(c) isa Polymake.BigObject
@testset "giving polytope a name" begin
p = polytope.rand_sphere(3,20);
@test polytope.Polytope("my cuttie", INEQUALITIES=p.POINTS) isa Polymake.BigObject
P = polytope.Polytope("my cuttie", INEQUALITIES=p.POINTS)
@test occursin("my cuttie", String(Polymake.properties(P)))
end
@testset "copy" begin
p = polytope.cube(3)
@test Polymake.exists(p,"F_VECTOR") == false
pc = copy(p)
@test pc.F_VECTOR isa Polymake.Vector
pdc = deepcopy(p)
@test pdc.F_VECTOR isa Polymake.Vector
@test Polymake.exists(p,"F_VECTOR") == false
@test Polymake.exists(pc,"F_VECTOR") == true
@test Polymake.exists(pdc,"F_VECTOR") == true
end
@testset "conversions" begin
p = polytope.rand_sphere(3,20);
@test polytope.Cone(p) isa Polymake.BigObject
# copy
@test polytope.Polytope(p) isa Polymake.BigObject
c = polytope.Cone(p)
@test Polymake.type_name(c) == "Cone<Rational>"
@test Polymake.bigobject_type(c) isa Polymake.BigObjectType
conetype = Polymake.bigobject_type(c)
@test Polymake.type_name(c) == Polymake.type_name(conetype)
# a polytope is still a cone
@test Polymake._isa(p,conetype)
@test Polymake.cast!(p,conetype) isa Polymake.BigObject
@test Polymake.BigObjectType("polytope::Polytope") isa Polymake.BigObjectType
@test Polymake.type_name(p) == "Cone<Rational>"
@test polytope.Polytope(c) isa Polymake.BigObject
@test_throws ErrorException Polymake.cast!(c,Polymake.BigObjectType("fan::PolyhedralFan"))
@test_throws ArgumentError fan.PolyhedralFan(c)
end
end
@testset "template parameters" begin
@test (@pm polytope.Polytope(POINTS=points_int)) isa Polymake.BigObject
@test (@pm polytope.Polytope{Rational}(POINTS=points_int)) isa Polymake.BigObject
@test (@pm polytope.Polytope{QuadraticExtension}(POINTS=points_int)) isa Polymake.BigObject
@test (@pm polytope.Polytope{QuadraticExtension{Rational}}(POINTS=points_int)) isa Polymake.BigObject
@test (@pm polytope.Polytope(POINTS=points_rat)) isa Polymake.BigObject
@test (@pm tropical.Polytope{Max}(POINTS=points_int)) isa Polymake.BigObject
@test (@pm tropical.Polytope{Max}(POINTS=points_int)) isa Polymake.BigObject
@test (@pm tropical.Polytope{Max, Rational}(POINTS=points_int)) isa Polymake.BigObject
@test (@pm tropical.Polytope{Max, QuadraticExtension}(POINTS=points_int)) isa Polymake.BigObject
@test (@pm tropical.Hypersurface{Min}(
MONOMIALS=[1 0 0; 0 1 0; 0 0 1],
COEFFICIENTS=[0, 0, 0])) isa Polymake.BigObject
# note: You need to input COEFFICIENTS as Polymake.Vector, otherwise it will be converted to Polymake.Matrix which polymake doesn't like.
P = @pm polytope.Polytope{Float}(POINTS=[1 1//2 0; 1 0 1])
@test P.VERTICES isa Polymake.Matrix{Float64}
P = @pm polytope.Polytope{Float}(POINTS=[1 0.5 0; 1 0 1])
@test P.VERTICES isa Polymake.Matrix{Float64}
P = @pm polytope.Polytope(POINTS=[1 0.5 0; 1 0 1])
@test P.VERTICES isa Polymake.Matrix{Polymake.Rational}
P = @pm polytope.Polytope{Rational}(POINTS=[1 0.5 0; 1 0 1])
@test P.VERTICES isa Polymake.Matrix{Polymake.Rational}
end
@testset "PolymakeException" begin
test_polytope = @pm polytope.Polytope(POINTS=points_int)
@test !(:STH in Base.propertynames(test_polytope))
@test_throws Polymake.PolymakeError test_polytope.STH
end
@testset "properties" begin
test_polytope = @pm polytope.Polytope(POINTS=points_int)
@test test_polytope.F_VECTOR == [ 4, 4 ]
@test test_polytope.INTERIOR_LATTICE_POINTS ==
[ 1 1 1 ; 1 1 2 ; 1 2 1 ; 1 2 2 ]
@test test_polytope.GRAPH isa Polymake.BigObject
test_graph = test_polytope.GRAPH
@test :ADJACENCY in Base.propertynames(test_graph)
@test test_polytope.LATTICE_POINTS_GENERATORS isa Polymake.Array
test_polytope = @pm polytope.Polytope(POINTS=points_unbounded)
@test test_polytope.FAR_FACE == Polymake.Set([1])
c = polytope.cube(3, 1//4, -1//4)
@test c.VERTICES[1,2] == -1//4
i = Polymake.polytope.icosahedron()
@test i.VERTICES[1, :] == [1, 0, Polymake.QuadraticExtension{Polymake.Rational}(1//4, 1//4, 5), 1//2]
@test i.VOLUME == Polymake.QuadraticExtension{Polymake.Rational}(5//4, 5//12, 5)
end
@testset "attachments" begin
test_polytope = polytope.Polytope(POINTS=points_int)
att = Polymake.Matrix{Polymake.Rational}(3,3)
@test Polymake.attach(test_polytope,"ATT",att) === nothing
@test Polymake.get_attachment(test_polytope,"ATT") isa Polymake.Matrix
@test Polymake.get_attachment(Polymake.PropertyValue,test_polytope,"ATT") isa Polymake.PropertyValue
@test Polymake.remove_attachment(test_polytope,"ATT") === nothing
@test Polymake.get_attachment(test_polytope,"ATT") === nothing
stra = Polymake.Array{String}(["hello", "world"])
@test Polymake.attach(test_polytope, "STRA", stra) === nothing
# @test Polymake.get_attachment(test_polytope) .== ["hello", "world"]
end
@testset "tab-completion" begin
test_polytope = @pm polytope.Polytope(POINTS=points_int)
@test Base.propertynames(test_polytope) isa Base.Vector{Symbol}
names = Base.propertynames(test_polytope)
@test :VERTICES in names
@test :FAR_FACE in names
@test :GRAPH in names
@test test_polytope.GRAPH isa Polymake.BigObject
@test allunique(Base.propertynames(test_polytope))
g = test_polytope.GRAPH
@test Base.propertynames(g) isa Base.Vector{Symbol}
end
@testset "polymake tutorials" begin
p = @pm polytope.Polytope(POINTS=polytope.cube(4).VERTICES)
@test p isa Polymake.BigObject
lp = @pm polytope.LinearProgram(LINEAR_OBJECTIVE=[0,1,1,1,1])
@test lp isa Polymake.BigObject
@test (p.LP = lp) isa Polymake.BigObject
@test p.LP.MAXIMAL_VALUE == 4
matrix = Base.Rational{Int64}[
1//1 0//1 0//1 0//1;
1//1 1//16 1//4 1//16;
1//1 3//8 1//4 1//32;
1//1 1//4 3//8 1//32;
1//1 1//16 1//16 1//4;
1//1 1//32 3//8 1//4;
1//1 1//4 1//16 1//16;
1//1 1//32 1//4 3//8;
1//1 3//8 1//32 1//4;
1//1 1//4 1//32 3//8]
special_points = Polymake.Rational[
1 1//16 1//4 1//16;
1 1//16 1//16 1//4;
1 1//4 1//16 1//16]
p = @pm polytope.Polytope(POINTS=matrix)
@test polytope.dim(p) == 3
@test p.VERTEX_SIZES == [9, 3, 4, 4, 3, 4, 3, 4, 4, 4]
s = Polymake.Set(i for (i, vsize) in enumerate(p.VERTEX_SIZES)
if vsize == polytope.dim(p))
s = Polymake.Set(i for (i, vsize) in enumerate(p.VERTEX_SIZES)
if vsize == polytope.dim(p))
@test Polymake.Set([2,5,7]) == s == s
@test p.VERTICES[collect(s), :] isa Polymake.Matrix{Polymake.Rational}
@test p.VERTICES[collect(s), :] == special_points
end
@testset "polymake MILP" begin
p = @pm polytope.Polytope( INEQUALITIES=[1 1 -1; -1 0 1; 7 -1 -1] )
intvar = Polymake.Set([0,1,2])
@test Polymake.convert(Polymake.PolymakeType, intvar) isa Polymake.Set{Polymake.to_cxx_type(Int64)}
obj = [0,-1,-1]
@test (@pm polytope.MixedIntegerLinearProgram( LINEAR_OBJECTIVE = obj, INTEGER_VARIABLES = intvar)) isa Polymake.BigObject
pmintvar = Polymake.Set(intvar)
@test (@pm polytope.MixedIntegerLinearProgram( LINEAR_OBJECTIVE = obj, INTEGER_VARIABLES = pmintvar)) isa Polymake.BigObject
p.MILP = @pm polytope.MixedIntegerLinearProgram( LINEAR_OBJECTIVE = obj, INTEGER_VARIABLES = intvar)
@test p.MILP.MINIMAL_VALUE == -7
end
@testset "poly2lp2poly" begin
p = polytope.Polytope( INEQUALITIES=[1 1 -1; -1 0 1; 7 -1 -1] )
lp = polytope.LinearProgram(LINEAR_OBJECTIVE=[0,1,1])
p.LP = lp
x = mktemp() do path, _
@test Polymake.polytope.poly2lp(p,lp, false, path) == 1
return Polymake.polytope.lp2poly(path)
end
@test x isa Polymake.BigObject
@test p.LP.LINEAR_OBJECTIVE == x.LP.LINEAR_OBJECTIVE
end
@testset "multiple subobjects" begin
p = polytope.Polytope( INEQUALITIES=[1 1 -1; -1 0 1; 7 -1 -1] )
lp1 = polytope.LinearProgram(LINEAR_OBJECTIVE=[0,1,0])
lp2 = polytope.LinearProgram(LINEAR_OBJECTIVE=[0,0,1])
lp3 = polytope.LinearProgram(LINEAR_OBJECTIVE=[0,2,1])
p.LP = lp1
Polymake.add(p,"LP",lp2)
Polymake.add(p,"LP","third",lp3)
@test Polymake._lookup_multi(p,"LP") isa Polymake.Array{Polymake.BigObject}
@test length(Polymake._lookup_multi(p,"LP")) == 3
@test Polymake._lookup_multi(p,"LP","third").LINEAR_OBJECTIVE == lp3.LINEAR_OBJECTIVE
@test Polymake._lookup_multi(p,"LP",1).LINEAR_OBJECTIVE == lp2.LINEAR_OBJECTIVE
@test p.LP.LINEAR_OBJECTIVE == lp1.LINEAR_OBJECTIVE
@test_throws ErrorException Polymake._lookup_multi(p,"LP",3)
@test_throws ErrorException Polymake._lookup_multi(p,"LP","nonexisting")
end
@testset "bigobject array" begin
c = polytope.cube(3)
c_type = Polymake.bigobject_type(c)
@test Polymake.Array{Polymake.BigObject}(c_type,2) isa Polymake.Array{Polymake.BigObject}
arr = Polymake.Array{Polymake.BigObject}(c_type,2)
@test length(arr) == 2
arr[1] = c
arr[2] = polytope.simplex(2)
@test arr[1] isa Polymake.BigObject
@test arr[2] isa Polymake.BigObject
@test arr[1].N_VERTICES == 8
end
@testset "toplevel visual" begin
@test visual(Polymake.Visual, polytope.cube(3)) isa Polymake.Visual
end
end