-
Notifications
You must be signed in to change notification settings - Fork 32
/
Interval.jl
767 lines (534 loc) · 15.5 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
import Base: +, -, *, min, max
export Interval,
center,
isflat
"""
Interval{N} <: AbstractHyperrectangle{N}
Type representing an interval on the real line.
Mathematically, it is of the form
```math
[a, b] := \\{ a ≤ x ≤ b \\} ⊆ ℝ.
```
### Fields
- `dat` -- data container for the given interval
### Notes
This type relies on the
[IntervalArithmetic.jl](https://juliaintervals.github.io/IntervalArithmetic.jl/stable/)
library for representation of intervals and arithmetic operations.
### Examples
Unidimensional intervals are symbolic representations of a real closed interval.
We can create intervals in different ways. The simplest way is to pass a pair
of numbers:
```jldoctest interval_constructor
julia> x = Interval(0.0, 1.0)
Interval{Float64}([0, 1])
```
A 2-vector is also possible:
```jldoctest interval_constructor
julia> x = Interval([0.0, 1.0])
Interval{Float64}([0, 1])
```
An interval can also be constructed from an `IntervalArithmetic.Interval`.
Note that if the package `IntervalArithmetic` is loaded in the current scope,
you have to prepend the package names to `Interval`s since there is a name
conflict otherwise.
```jldoctest interval_constructor
julia> using IntervalArithmetic
WARNING: using IntervalArithmetic.Interval in module Main conflicts with an existing identifier.
julia> x = LazySets.Interval(IntervalArithmetic.Interval(0.0, 1.0))
Interval{Float64}([0, 1])
julia> dim(x)
1
julia> center(x)
1-element Vector{Float64}:
0.5
```
The usual pairwise arithmetic operators `-` and `*` are interpreted in the
standard way known in interval arithmetic, so the results are `Interval`s.
Note that `+` is generally used for the lazy Minkowksi sum in this library.
Intervals of other numeric types can be created as well, e.g., a rational
interval:
```jldoctest interval_constructor
julia> Interval(0//1, 2//1)
Interval{Rational{Int64}}([0//1, 2//1])
```
"""
struct Interval{N} <: AbstractHyperrectangle{N}
dat::IA.Interval{N}
function Interval(dat::IA.Interval{N}) where {N}
@assert isfinite(dat.lo) && isfinite(dat.hi) "intervals must be bounded"
return new{N}(dat)
end
end
# constructor from two numbers with type promotion
function Interval(lo::N1, hi::N2) where {N1,N2}
N = promote_type(N1, N2)
return Interval(IA.interval(N(lo), N(hi)))
end
# constructor from a vector
function Interval(x::AbstractVector)
@assert length(x) == 2 "cannot construct an Interval from a " *
"$(length(x))-dimensional vector"
return @inbounds Interval(x[1], x[2])
end
# constructor from a single number
function Interval(x::Real)
return Interval(IA.interval(x))
end
isoperationtype(::Type{<:Interval}) = false
"""
dim(x::Interval)
Return the ambient dimension of an interval.
### Input
- `x` -- interval
### Output
``1``.
"""
dim(x::Interval) = 1
"""
σ(d::AbstractVector, x::Interval)
Return the support vector of an interval in a given direction.
### Input
- `d` -- direction
- `x` -- interval
### Output
The support vector in the given direction.
"""
function σ(d::AbstractVector, x::Interval)
@assert length(d) == dim(x) "a $(length(d))-dimensional vector is " *
"incompatible with an $(dim(x))-dimensional set"
N = promote_type(eltype(d), eltype(x))
return @inbounds d[1] > zero(N) ? high(x) : low(x)
end
"""
ρ(d::AbstractVector, x::Interval)
Evaluate the support function of an interval in a given direction.
### Input
- `d` -- direction
- `x` -- interval
### Output
Evaluation of the support function in the given direction.
"""
function ρ(d::AbstractVector, x::Interval)
@assert length(d) == dim(x) "a $(length(d))-dimensional vector is " *
"incompatible with an $(dim(x))-dimensional set"
N = promote_type(eltype(d), eltype(x))
return @inbounds d[1] * (d[1] > zero(N) ? max(x) : min(x))
end
"""
center(x::Interval)
Return the center of an interval.
### Input
- `x` -- interval
### Output
The center, or midpoint, of `x`.
"""
function center(x::Interval)
return [_center(x)]
end
"""
center(H::Interval, i::Int)
Return the center along a given dimension of a interval.
### Input
- `x` -- interval
- `i` -- dimension of interest
### Output
The center along a given dimension of the interval.
"""
@inline function center(x::Interval, i::Int)
@assert i == 1 "an interval has dimension 1, but the index is $i"
return _center(x)
end
# returns a number, not a vector
function _center(x::Interval)
return IA.mid(x.dat)
end
"""
-(x::Interval, y::Interval)
Return the difference of two intervals (in the interval-arithmetic sense).
### Input
- `x` -- interval
- `y` -- interval
### Output
The difference of the intervals as a new `Interval` set.
"""
function -(x::Interval, y::Interval)
return Interval(x.dat - y.dat)
end
"""
```
*(x::Interval, y::Interval)
```
Return the product of two intervals (in the interval-arithmetic sense).
### Input
- `x` -- interval
- `y` -- interval
### Output
The product of the intervals as a new `Interval` set.
"""
function *(x::Interval, y::Interval)
return Interval(x.dat * y.dat)
end
"""
∈(v::AbstractVector, x::Interval))
Check whether a given point is contained in an interval.
### Input
- `v` -- point/vector
- `x` -- interval
### Output
`true` iff ``v ∈ x``.
"""
function ∈(v::AbstractVector, x::Interval)
@assert length(v) == 1 "a $(length(v))-dimensional vector is " *
"incompatible with an interval"
return @inbounds v[1] ∈ x.dat
end
"""
∈(v::Number, x::Interval)
Check whether a number is contained in an interval.
### Input
- `v` -- scalar
- `x` -- interval
### Output
`true` iff `x` contains the singleton `[v]`.
"""
function ∈(v::Number, x::Interval)
return v ∈ x.dat
end
"""
min(x::Interval)
Return the lower component of an interval.
### Input
- `x` -- interval
### Output
The lower (`lo`) component of the interval (a number).
"""
function min(x::Interval)
return x.dat.lo
end
"""
max(x::Interval)
Return the higher or upper component of an interval.
### Input
- `x` -- interval
### Output
The higher (`hi`) component of the interval (a number).
"""
function max(x::Interval)
return x.dat.hi
end
"""
low(x::Interval)
Return the lower coordinate of an interval set.
### Input
- `x` -- interval
### Output
A vector with the lower coordinate of the interval.
"""
function low(x::Interval)
return [x.dat.lo]
end
"""
high(x::Interval)
Return the higher coordinate of an interval set.
### Input
- `x` -- interval
### Output
A vector with the higher coordinate of the interval.
"""
function high(x::Interval)
return [x.dat.hi]
end
"""
an_element(x::Interval)
Return some element of an interval.
### Input
- `x` -- interval
### Output
The left border (`low(x)`) of the interval.
"""
function an_element(x::Interval)
return low(x)
end
"""
rand(::Type{Interval}; [N]::Type{<:Real}=Float64, [dim]::Int=1,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing)
Create a random interval.
### Input
- `Interval` -- type for dispatch
- `N` -- (optional, default: `Float64`) numeric type
- `dim` -- (optional, default: 1) dimension
- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator
- `seed` -- (optional, default: `nothing`) seed for reseeding
### Output
A random interval.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
"""
function rand(::Type{Interval};
N::Type{<:Real}=Float64,
dim::Int=1,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int,Nothing}=nothing)
@assert dim == 1 "cannot create a random Interval of dimension $dim"
rng = reseed!(rng, seed)
x = randn(rng, N)
y = randn(rng, N)
return x < y ? Interval(x, y) : Interval(y, x)
end
"""
vertices_list(x::Interval; kwargs...)
Return the list of vertices of an interval.
### Input
- `x` -- interval
### Output
The list of vertices of the interval, which are two one-dimensional vectors,
or just one if the interval is degenerate (the endpoints match within the
working tolerance).
"""
function vertices_list(x::Interval; kwargs...)
a = min(x)
b = max(x)
return _isapprox(a, b) ? [[a]] : [[a], [b]]
end
"""
constraints_list(x::Interval)
Return the list of constraints of an interval.
### Input
- `x` -- interval
### Output
The list of constraints of the interval represented as two one-dimensional
half-spaces.
"""
function constraints_list(x::Interval)
N = eltype(x)
constraints = Vector{HalfSpace{N,SingleEntryVector{N}}}(undef, 2)
e₁ = SingleEntryVector(1, 1, one(N))
@inbounds constraints[1] = HalfSpace(e₁, max(x))
@inbounds constraints[2] = HalfSpace(-e₁, -min(x))
return constraints
end
"""
translate(x::Interval, v::AbstractVector)
Translate (i.e., shift) an interval by a given vector.
### Input
- `x` -- interval
- `v` -- translation vector
### Output
A translated interval.
### Algorithm
We add the vector to the left and right of the interval.
### Notes
An in-place version is not available because the `IntervalArithmetic.Interval`
type is immutable.
"""
function translate(x::Interval, v::AbstractVector)
@assert length(v) == dim(x) "cannot translate a $(dim(x))-dimensional " *
"set by a $(length(v))-dimensional vector"
return Interval(x.dat + @inbounds v[1])
end
function _radius(x::Interval{N}) where {N}
return (max(x) - min(x)) / N(2)
end
"""
radius_hyperrectangle(x::Interval{N}, i::Int) where {N}
Return the box radius of an interval in a given dimension.
### Input
- `x` -- interval
- `i` -- dimension index (must be `1`)
### Output
The box radius in the given dimension.
"""
function radius_hyperrectangle(x::Interval{N}, i::Int) where {N}
@assert i == 1 "an interval has dimension 1, but the index is $i"
return _radius(x)
end
"""
radius_hyperrectangle(x::Interval)
Return the box radius of an interval in every dimension.
### Input
- `x` -- interval
### Output
The box radius of the interval (a one-dimensional vector).
"""
function radius_hyperrectangle(x::Interval)
return [_radius(x)]
end
"""
isflat(x::Interval)
Check whether an interval is flat, i.e., whether its extreme values coincide.
### Input
- `x` -- interval
### Output
`true` if the interval is flat and `false` otherwise.
### Notes
For robustness with respect to floating-point inputs, this function relies on
the result of `isapproxzero` when applied to the diameter of the interval.
In other words, this function depends on the absolute zero tolerance `ABSZTOL`.
"""
function isflat(x::Interval)
return isapproxzero(IA.diam(x.dat))
end
"""
plot_recipe(x::Interval{N}, [ε]=zero(N)) where {N}
Convert an interval to a pair `(x, y)` of points for plotting.
### Input
- `x` -- interval
- `ε` -- (optional, default: `0`) ignored, used for dispatch
### Output
A pair `(x, y)` of two points that can be plotted.
### Notes
We consider the interval as a line segment with y coordinate equal to zero.
"""
function plot_recipe(x::Interval{N}, ε=zero(N)) where {N}
return [min(x), max(x)], zeros(N, 2)
end
"""
linear_map(M::AbstractMatrix, x::Interval)
Concrete linear map of an interval.
### Input
- `M` -- matrix
- `x` -- interval
### Output
Either an interval or a zonotope, depending on the leading dimension (i.e., the
number of rows) of `M`:
- If `size(M, 1) == 1`, the output is an `Interval` obtained by scaling `x` by
the matrix `M`.
- If `size(M, 1) ≠ 1`, the output is a `Zonotope` with center `M * center(x)`
and the single generator `M * g`, where `g = (high(x)-low(x))/2`.
"""
function linear_map(M::AbstractMatrix, x::Interval)
@assert size(M, 2) == 1 "a linear map of size $(size(M)) " *
"cannot be applied to an interval"
nout = size(M, 1)
if nout == 1
return _linear_map_interval(M, x)
else
return _linear_map_zonotope(M, x)
end
end
function _linear_map_interval(M::AbstractMatrix, x::Interval)
α = @inbounds M[1, 1]
return Interval(α * x.dat)
end
function _linear_map_zonotope(M::AbstractMatrix, x::Interval)
nout = size(M, 1)
cx = _center(x)
gx = cx - min(x)
N = promote_type(eltype(M), eltype(x))
c = Vector{N}(undef, nout)
gen = Matrix{N}(undef, nout, 1)
@inbounds for i in 1:nout
c[i] = M[i, 1] * cx
gen[i] = M[i, 1] * gx
end
return Zonotope(c, gen)
end
"""
scale(α::Real, x::Interval)
Concrete scaling of an interval.
### Input
- `α` -- scalar
- `x` -- interval
### Output
The interval obtained by scaling the given interval.
"""
function scale(α::Real, x::Interval)
return Interval(α * x.dat)
end
"""
rectify(x::Interval{N}) where {N}
Concrete rectification of an interval.
### Input
- `x` -- interval
### Output
The `Interval` that corresponds to the rectification of `x`.
### Notes
Note that the result is an `Interval` even if the set becomes a singleton (which
is the case if the original interval was nonpositive).
"""
function rectify(x::Interval{N}) where {N}
if x.dat.lo >= zero(N)
# interval is already nonnegative
return x
else
# lower end is negative
return Interval(zero(N), Base.max(x.dat.hi, zero(N)))
end
end
"""
diameter(x::Interval, [p]::Real=Inf)
Compute the diameter of an interval, defined as ``‖b - a‖`` in the
``p`-norm, where ``a`` (resp. ``b``) are the minimum (resp. maximum) value of
the interval.
### Input
- `x` -- interval
- `p` -- (optional, default: `Inf`) norm (ignored)
### Output
A real number representing the diameter.
### Notes
In one dimension all p-norms are identical.
"""
function diameter(x::Interval, p::Real=Inf)
return IA.diam(x.dat)
end
"""
split(x::Interval, k)
Partition an interval into `k` uniform sub-intervals.
### Input
- `x` -- interval
- `k` -- number of sub-intervals, possibly wrapped in a vector of length 1
### Output
A list of `k` `Interval`s.
"""
function split(x::Interval, k::AbstractVector{Int})
@assert length(k) == 1 "an interval can only be split along one dimension"
return split(x, @inbounds k[1])
end
function split(x::Interval, k::Int)
@assert k > 0 "can only split into a positive number of intervals"
return [Interval(x2) for x2 in mince(x.dat, k)]
end
"""
ngens(x::Interval)
Return the number of generators of an interval.
### Input
- `x` -- interval
### Output
The number of generators.
### Algorithm
An interval has either one generator, or zero generators if it is a degenerated
interval of diameter zero.
"""
function ngens(x::Interval)
return _isapprox(min(x), max(x)) ? 0 : 1
end
"""
chebyshev_center_radius(x::Interval; [kwargs]...)
Compute the [Chebyshev center](https://en.wikipedia.org/wiki/Chebyshev_center)
and the corresponding radius of an interval.
### Input
- `x` -- interval
- `kwargs` -- further keyword arguments (ignored)
### Output
The pair `(c, r)` where `c` is the Chebyshev center of `x` and `r` is the radius
of the largest ball with center `c` enclosed by `x`.
### Notes
The Chebyshev center of an interval is just the center of the interval.
"""
function chebyshev_center_radius(x::Interval; kwargs...)
return center(x), _radius(x)
end
"""
reflect(x::Interval)
Concrete reflection of a interval `x`, resulting in the reflected set `-x`.
### Input
- `x` -- interval
### Output
The `Interval` representing `-x`.
### Algorithm
If ``x = [a, b]``, then ``-x = [-b, -a]``.
"""
function reflect(x::Interval)
return Interval(-max(x), -min(x))
end