-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintervals.jl
292 lines (219 loc) · 5.7 KB
/
intervals.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
284
285
286
287
288
289
290
291
292
using IntervalSets
const LEFT=1
const RIGHT=2
function inv_closedness(i,end_)
return closedendpoints(i)[end_] ? :open : :closed
end
function closedness(i,end_)
return closedendpoints(i)[end_] ? :closed : :open
end
function invert(a::Interval)
left = nothing
right = nothing
if a.left == -Inf && a.right == Inf
return nothing
elseif a.left == -Inf
return Interval{inv_closedness(a,RIGHT),:open}(a.right,+Inf)
elseif a.right == +Inf
return Interval{:open,inv_closedness(a,LEFT)}(-Inf,a.left)
else
b = Interval{:open,inv_closedness(a,LEFT)}(-Inf,a.left)
c = Interval{inv_closedness(a,RIGHT),:open}(a.right,+Inf)
return (b,c)
end
end
function intersect(a::Interval, b::Interval)
left, right = nothing, nothing
left_open, right_open = nothing, nothing
if a.left ∈ b
left = a.left
left_open = closedness(a,LEFT)
end
if a.right ∈ b
right = a.right
right_open = closedness(a,RIGHT)
end
if b.left ∈ a
left = b.left
left_open = closedness(b,LEFT)
end
if b.right ∈ a
right = b.right
right_open = closedness(b,RIGHT)
end
if left != nothing && right != nothing
return Interval{left_open, right_open}(left, right)
else
throw( ArgumentError("Cannot intersect disjoint intervals $(a) $(b)"))
end
end
function covers_helper(a::Interval, b::Interval)
res = false
x = leftendpoint(a)
xc = closedness(a,LEFT) == :closed
#println("left: $(x) $(xc) $(closedendpoints(a)) $(closedness(a,LEFT))")
if x == leftendpoint(b)
return true
elseif x == rightendpoint(b)
return xc && closedness(b,RIGHT) == :closed
else
res = res || x ∈ b
end
x = rightendpoint(a)
xc = closedness(a,RIGHT) == :closed
#println("right: $(x) $(xc)")
if x == leftendpoint(b)
return xc && closedness(b,LEFT) == :closed
elseif x == rightendpoint(b)
return true
else
res = res || x ∈ b
end
return res
end
function covers(a::Interval, b::Interval)
# true if a covers partially or completely b
return covers_helper(a,b) || covers_helper(b,a)
end
function iunion( list::Array{T,1}, itv::T) where {T <: Interval}
#println("0:")
is_valid(list)
for a in list
if covers(a,itv)
itv = union(a,itv)
end
end
#println("0:")
new_list = Array{Interval,1}()
i = 1
while i <= length(list) && leftendpoint(list[i]) < leftendpoint(itv)
#println("1: $(i)")
if !covers(list[i],itv)
push!(new_list,list[i])
end
i = i + 1
end
push!(new_list, itv)
while i <= length(list)
#println("2: $(i)")
if !covers(list[i],itv)
push!(new_list,list[i])
end
i = i + 1
end
return new_list
end
function is_valid(list::Array{T,1}) where {T <: Interval}
if length(list) > 0
for i in 2:length(list)
@assert leftendpoint(list[i-1]) <= leftendpoint(list[i]) "$(list)"
@assert !covers(list[i-1],list[i]) "$(list)"
end
end
end
function intersect( list::Array{T,1}, a::Interval) where {T <: Interval}
nu = Array{Interval,1}()
for i in list
if covers(a,i)
push!(nu, intersect(a,i))
end
end
return nu
end
function linvert( list::Array{T,1}) where {T <: Interval}
is_valid(list)
if length(list) == 0
return [OpenInterval(-Inf,+Inf)]
end
# Assuming list is sorted according to leftendpoint(itv)
# Assuming intervals are all disjoint
all = Array{Interval,1}()
left = -Inf
left_open = :open
for i in list
right = leftendpoint(i)
right_open = inv_closedness(i,LEFT)
if left != right
push!(all, Interval{left_open,right_open}(left,right))
end
left = rightendpoint(i)
left_open = inv_closedness(i,RIGHT)
end
right = +Inf
right_open = :open
if left != right
push!(all, Interval{left_open,right_open}(left,right))
end
return all
end
function test()
a = ClosedInterval(1,5)
inv_a1, inv_a2 = invert(a)
@assert inv_a1.left == -Inf
@assert inv_a1.right == 1
@assert inv_a2.left == 5
@assert inv_a2.right == +Inf
r = linvert(Array{Interval,1}())
@assert length(r) == 1 && r[1].left == -Inf && r[1].right == + Inf
r = linvert([OpenInterval(-Inf,+Inf)])
@assert length(r) == 0 "$(r)"
r = linvert([OpenInterval(0,1)])
@assert length(r) == 2 "$(r)"
r = linvert([ClosedInterval(0,1),ClosedInterval(2,3)])
@assert length(r) == 3 "$(r)"
r = linvert(linvert([ClosedInterval(0,1),ClosedInterval(2,3)]))
@show r
@assert length(r) == 2 "$(r)"
# Unions
b = ClosedInterval(1,10)
@show iunion([b], ClosedInterval(-10,1))
@show iunion([b], ClosedInterval(-10,5))
@show iunion([b], ClosedInterval(-10,10))
@show iunion([b], ClosedInterval(-10,12))
b = ClosedInterval(1,2)
c = ClosedInterval(3,4)
d = ClosedInterval(5,6)
@show iunion(Array{Interval,1}(), a)
@show iunion([b; c; d], a)
@show iunion([c; d], b)
@show iunion([b; c], d)
@show iunion([b; d], c)
b = ClosedInterval(1,1)
c = ClosedInterval(2,2)
@show iunion([b], c)
@show iunion([c], b)
# Covering
b = ClosedInterval(1,2)
c = ClosedInterval(3,4)
@assert !covers(b,c)
@assert !covers(c,b)
c = OpenInterval(1,2)
@assert covers(b,c)
@assert covers(c,b)
b = ClosedInterval(1,2)
c = ClosedInterval(2,3)
@assert covers(b,c)
@assert covers(c,b)
b = OpenInterval(1,2)
c = ClosedInterval(2,3)
@assert !covers(b,c)
@assert !covers(c,b)
b = OpenInterval(1,2)
c = OpenInterval(2,3)
@assert !covers(b,c)
@assert !covers(c,b)
b = ClosedInterval(1,2)
c = OpenInterval(2,3)
@assert !covers(b,c)
@assert !covers(c,b)
# partly covers
b = ClosedInterval(1,2)
c = OpenInterval(1.5,3)
@assert covers(b,c)
@assert covers(c,b)
# completely covers
b = ClosedInterval(0,100)
c = OpenInterval(1.5,3)
@assert covers(b,c)
@assert covers(c,b)
end