-
Notifications
You must be signed in to change notification settings - Fork 27
/
interval.jl
209 lines (184 loc) · 6.34 KB
/
interval.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
function cartesian_product_test()
p1 = polyhedron(hrep([HalfSpace([1.0], 1.0), HalfSpace([-1.0], 0.0)]))
p2 = polyhedron(hrep([HalfSpace([1.0], 1.0), HalfSpace([-1.0], 0.0)]))
p = Polyhedra.hcartesianproduct(p1, p2)
@test p isa DefaultPolyhedron{Float64,
Polyhedra.Intersection{Float64,StaticArrays.SArray{Tuple{2},Float64,1,2},StaticArrays.Size{(2,)}},
Polyhedra.Hull{Float64,StaticArrays.SArray{Tuple{2},Float64,1,2},StaticArrays.Size{(2,)}}}
expected = HalfSpace((@SVector [1.0, 0.0]), 1.0) ∩ HalfSpace((@SVector [-1.0, 0.0]), 0.0) ∩ HalfSpace((@SVector [0.0, 1.0]), 1.0) ∩ HalfSpace((@SVector [0.0, -1.0]), 0.0)
inequality_fulltest(p, expected)
end
@testset "Interval tests" begin
# Closed interval
h = HalfSpace([-1], 3.) ∩ HalfSpace([1.], 8)
v = convexhull([-3.], [8.])
v0 = [[1.], [8], [-2], [4], [-1], [-3], [5]]
pp = polyhedron(vrep(v0), Polyhedra.DefaultLibrary{Float64}())
@test !hrepiscomputed(pp)
@test vrepiscomputed(pp)
d = StaticArrays.Size{(1,)}()
for p in (Interval{Float64, SVector{1, Float64}, typeof(d)}(pp),
Interval{Float64, SVector{1, Float64}, typeof(d)}(d, Polyhedra.vreps(pp)...))
@test similar_library(pp, 1) == IntervalLibrary{Float64}()
@test similar_library(pp, StaticArrays.Size((1,))) == IntervalLibrary{Float64}()
@test library(p) == IntervalLibrary{Float64}()
@test similar_library(p, 2) == Polyhedra.DefaultLibrary{Float64}()
@test similar_library(p, StaticArrays.Size((2,))) == Polyhedra.DefaultLibrary{Float64}()
@test hrepiscomputed(p)
@test vrepiscomputed(p)
@test p isa Interval{Float64}
@test !isempty(p)
@test volume(p) == 11
@test center_of_mass(p) ≈ [2.5]
@test dim(p) == 1
inequality_fulltest(p, h)
generator_fulltest(p, v)
end
pp = polyhedron(h, Polyhedra.DefaultLibrary{Float64}())
@test hrepiscomputed(pp)
@test !vrepiscomputed(pp)
for p in (Interval{Float64, SVector{1, Float64}, typeof(d)}(pp),
Interval{Float64, SVector{1, Float64}, typeof(d)}(d, Polyhedra.hreps(pp)...))
@test hrepiscomputed(p)
@test vrepiscomputed(p)
@test p isa Interval{Float64}
@test !isempty(p)
@test iszero(surface(p))
@test volume(p) == 11
@test center_of_mass(p) ≈ [2.5]
@test dim(p) == 1
generator_fulltest(p, v)
inequality_fulltest(p, h)
end
# Singleton
h = intersect(HyperPlane([1.], 2))
v = convexhull([2.])
v0 = reshape([2., 2., 2.], 3, 1)
p = polyhedron(vrep(v0))
@test p isa Interval{Float64}
@test !isempty(p)
@test iszero(surface(p))
@test iszero(volume(p))
@test dim(p) == 0
inequality_fulltest(p, h)
generator_fulltest(p, v)
p = polyhedron(h)
@test p isa Interval{Float64}
@test !isempty(p)
@test iszero(surface(p))
@test iszero(volume(p))
@test dim(p) == 0
generator_fulltest(p, v)
inequality_fulltest(p, h)
# Left-open interval
h = intersect(HalfSpace([1.], 2))
v = v + conichull([-1.])
r0 = reshape([-1., -1], 2, 1)
p = polyhedron(vrep(v0, r0))
@test p isa Interval{Float64}
@test !isempty(p)
@test iszero(surface(p))
@test dim(p) == 1
inequality_fulltest(p, h)
generator_fulltest(p, v)
p = polyhedron(HalfSpace([1.], 3) ∩ HalfSpace([5.], 10) ∩ HalfSpace([2.], 6))
@test p isa Interval{Float64}
@test !isempty(p)
@test iszero(surface(p))
@test dim(p) == 1
generator_fulltest(p, v)
inequality_fulltest(p, h)
# Right-open interval
h = intersect(HalfSpace([-1.], -2))
v = convexhull([2.]) + conichull([1.])
p = polyhedron(convexhull([2.], [3.]) + conichull([1.], [3.]))
@test p isa Interval{Float64}
@test !isempty(p)
@test iszero(surface(p))
@test dim(p) == 1
inequality_fulltest(p, h)
generator_fulltest(p, v)
p = polyhedron(HalfSpace([-3.], -6) ∩ HalfSpace([-2.], -2))
# TODO test value of p
p = polyhedron(h)
@test p isa Interval{Float64}
@test !isempty(p)
@test iszero(surface(p))
@test dim(p) == 1
generator_fulltest(p, v)
inequality_fulltest(p, h)
# Empty
h = HyperPlane([0], 1) ∩ HyperPlane([1], 0)
v = vrep(Line{Int, StaticArrays.SVector{1, Int}}[])
p = polyhedron(v)
@test p isa Interval{Int}
@test isempty(p)
@test iszero(surface(p))
@test iszero(volume(p))
@test dim(p) == -1
inequality_fulltest(p, h)
generator_fulltest(p, v)
p = polyhedron(HalfSpace([1], 1) ∩ HalfSpace([-1], -2))
@test p isa Interval{Int}
@test isempty(p)
@test iszero(surface(p))
@test iszero(volume(p))
@test dim(p) == -1
generator_fulltest(p, v)
inequality_fulltest(p, h)
p = polyhedron(intersect(HyperPlane([0], 1)))
@test p isa Interval{Int}
@test isempty(p)
@test iszero(surface(p))
@test iszero(volume(p))
@test dim(p) == -1
generator_fulltest(p, v)
inequality_fulltest(p, h)
p = polyhedron(intersect(HalfSpace([0], -1)))
@test p isa Interval{Int}
@test isempty(p)
@test iszero(surface(p))
@test iszero(volume(p))
@test dim(p) == -1
generator_fulltest(p, v)
inequality_fulltest(p, h)
# Line
h = hrep(HyperPlane{Int, SVector{1, Int}}[])
v = conichull(Line([1]))
p = polyhedron(v)
@test p isa Interval{Int}
@test !isempty(p)
@test iszero(surface(p))
@test dim(p) == 1
inequality_fulltest(p, h)
generator_fulltest(p, v)
p = polyhedron(h)
@test p isa Interval{Int}
@test !isempty(p)
@test iszero(surface(p))
@test dim(p) == 1
generator_fulltest(p, v)
inequality_fulltest(p, h)
# Symmetric interval
h = HalfSpace([1], 1) ∩ HalfSpace([-1], 1)
v = convexhull([1], [-1])
p = polyhedron(convexhull([-1], [1], [0]))
@test p isa Interval{Int}
@test !isempty(p)
@test iszero(surface(p))
@test volume(p) == 2
@test dim(p) == 1
inequality_fulltest(p, h)
generator_fulltest(p, v)
p = polyhedron(h)
@test p isa Interval{Int}
@test !isempty(p)
@test iszero(surface(p))
@test volume(p) == 2
@test dim(p) == 1
generator_fulltest(p, v)
inequality_fulltest(p, h)
@testset "Cartesian product (#132)" begin
cartesian_product_test()
end
end