Skip to content

Commit 24c5ad2

Browse files
authored
Merge pull request #316 from JuliaReach/mforets/300
#300 - Correct usage of IntervalArithmetic
2 parents 84e80fb + a352a9f commit 24c5ad2

File tree

6 files changed

+34
-36
lines changed

6 files changed

+34
-36
lines changed

docs/src/lib/representations.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ low(::Hyperrectangle{Float64})
135135

136136
```@docs
137137
Interval
138-
IA
139138
dim(::Interval)
140139
σ(::AbstractVector{Float64}, ::Interval{Float64, IntervalArithmetic.AbstractInterval{Float64}})
141140
center(::Interval)

src/Interval.jl

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
using IntervalArithmetic
1+
import IntervalArithmetic
2+
import IntervalArithmetic: AbstractInterval
23

3-
"""
4-
IA = IntervalArithmetic
5-
6-
Name alias for the interval arithmetic library.
7-
"""
8-
const IA = IntervalArithmetic
9-
10-
export Interval, IA,
4+
export Interval,
115
dim, σ, center, +, -, *, , ,
126
low, high, vertices_list
137

148
"""
15-
Interval{N<:Real, IN <: IA.AbstractInterval{N}} <: AbstractPointSymmetricPolytope{N}
9+
Interval{N<:Real, IN <: AbstractInterval{N}} <: AbstractPointSymmetricPolytope{N}
1610
1711
Type representing an interval on the real line. Mathematically, it is of the
1812
form
1913
2014
```math
21-
[a, b] := \{ a \le x \le b \} \subseteq \mathbb{R}.
15+
[a, b] := \\{ a x b \\} ⊆ \\mathbb{R}.
2216
```
2317
2418
### Fields
@@ -72,20 +66,27 @@ julia> center(x)
7266
This type is such that the usual pairwise arithmetic operators `+`, `-`, `*` trigger
7367
the corresponding interval arithmetic backend method, and return a new
7468
`Interval` object. For the symbolic Minkowksi sum, use `MinkowskiSum` or `⊕`.
69+
70+
Interval of other numeric types can be created as well, eg. a rational interval:
71+
72+
```jldoctest interval_constructor
73+
julia> Interval(0//1, 2//1)
74+
LazySets.Interval{Rational{Int64},IntervalArithmetic.Interval{Rational{Int64}}}([0//1, 2//1])
75+
```
7576
"""
76-
struct Interval{N<:Real, IN <: IA.AbstractInterval{N}} <: AbstractPointSymmetricPolytope{N}
77+
struct Interval{N<:Real, IN <: AbstractInterval{N}} <: AbstractPointSymmetricPolytope{N}
7778
dat::IN
7879
end
7980
# type-less convenience constructor
80-
Interval(interval::IN) where {N, IN <: IA.AbstractInterval{N}} = Interval{N, IN}(interval)
81+
Interval(interval::IN) where {N, IN <: AbstractInterval{N}} = Interval{N, IN}(interval)
8182

8283
# TODO: adapt show method
8384

8485
# constructor that takes two numbers
85-
Interval(lo::N, hi::N) where {N} = Interval(IA.Interval(lo, hi))
86+
Interval(lo::N, hi::N) where {N} = Interval(IntervalArithmetic.Interval(lo, hi))
8687

8788
# constructor from a vector
88-
Interval(x::AbstractVector{N}) where {N} = Interval(IA.Interval(x[1], x[2]))
89+
Interval(x::AbstractVector{N}) where {N} = Interval(IntervalArithmetic.Interval(x[1], x[2]))
8990

9091
"""
9192
dim(x::Interval)::Int
@@ -103,7 +104,7 @@ The integer 1.
103104
dim(x::Interval)::Int = 1
104105

105106
"""
106-
σ(d::V, x::Interval{N, IN})::V where {N, IN <: IA.AbstractInterval{N}, V<:AbstractVector{N}}
107+
σ(d::V, x::Interval{N, IN})::V where {N, IN <: AbstractInterval{N}, V<:AbstractVector{N}}
107108
108109
Return the support vector of an ellipsoid in a given direction.
109110
@@ -116,7 +117,7 @@ Return the support vector of an ellipsoid in a given direction.
116117
117118
Support vector in the given direction.
118119
"""
119-
function σ(d::V, x::Interval{N, IN})::V where {N, IN <: IA.AbstractInterval{N}, V<:AbstractVector{N}}
120+
function σ(d::V, x::Interval{N, IN})::V where {N, IN <: AbstractInterval{N}, V<:AbstractVector{N}}
120121
@assert length(d) == dim(x)
121122
return d[1] > 0 ? [x.dat.hi] : [x.dat.lo]
122123
end
@@ -134,7 +135,7 @@ Return the interval's center.
134135
135136
The center, or midpoint, of ``x``.
136137
"""
137-
center(x::Interval) = [IA.mid(x.dat)]
138+
center(x::Interval) = [IntervalArithmetic.mid(x.dat)]
138139

139140
import Base:+, -, *, ,
140141

src/convert.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ function convert(::Type{Zonotope}, H::AbstractHyperrectangle{N}) where {N}
8484
return Zonotope{N}(center(H), diagm(radius_hyperrectangle(H)))
8585
end
8686

87-
@require IntervalArithmetic begin
87+
import IntervalArithmetic.AbstractInterval
8888

8989
"""
90-
convert(::Type{Hyperrectangle}, x::LazySets.Interval{N, IN}) where {N, IN <: IA.AbstractInterval{N}}
90+
convert(::Type{Hyperrectangle}, x::LazySets.Interval{N, IN}) where {N, IN <: AbstractInterval{N}}
9191
9292
Converts a unidimensional interval into a hyperrectangular set.
9393
@@ -107,7 +107,6 @@ julia> convert(Hyperrectangle, Interval(0.0, 1.0))
107107
LazySets.Hyperrectangle{Float64}([0.5], [0.5])
108108
```
109109
"""
110-
function convert(::Type{Hyperrectangle}, x::LazySets.Interval{N, IN}) where {N, IN <: IA.AbstractInterval{N}}
110+
function convert(::Type{Hyperrectangle}, x::LazySets.Interval{N, IN}) where {N, IN <: AbstractInterval{N}}
111111
return Hyperrectangle(low=[low(x)], high=[high(x)])
112112
end
113-
end

test/unit_Interval.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using IntervalArithmetic
1+
import IntervalArithmetic
22

33
# default constructor
4-
x = LazySets.Interval{Float64, IntervalArithmetic.Interval{Float64}}(IntervalArithmetic.Interval(0.0, 1.0))
4+
x = Interval{Float64, IntervalArithmetic.Interval{Float64}}(IntervalArithmetic.Interval(0.0, 1.0))
55

66
# type-less constructor
7-
x = LazySets.Interval(0.0, 1.0)
7+
x = Interval(0.0, 1.0)
88

99
# constructor with two numbers
10-
x = LazySets.Interval(0.0, 1.0)
10+
x = Interval(0.0, 1.0)
1111

1212
@test dim(x) == 1
1313
@test center(x) == [0.5]
@@ -18,12 +18,12 @@ v = vertices_list(x)
1818
@test an_element(x) x
1919
# test containment
2020
@test (x x) && !(x 0.2 * x) && (x 2. * x)
21-
@test issubset(x, LazySets.Interval(0.0, 2.0))
22-
@test !issubset(x, LazySets.Interval(-1.0, 0.5))
21+
@test issubset(x, Interval(0.0, 2.0))
22+
@test !issubset(x, Interval(-1.0, 0.5))
2323

2424

2525
# + operator (= concrete Minkowski sum of intervals)
26-
y = LazySets.Interval(-2.0, 0.5)
26+
y = Interval(-2.0, 0.5)
2727
m = x + y
2828
@test dim(m) == 1
2929
@test σ([1.0], m) == [1.5]

test/unit_decompose.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# using IntervalArithmetic
21
import LazySets.Approximations.decompose
32

43
for N in [Float64, Float32] # TODO Rational{Int}
@@ -40,8 +39,9 @@ for N in [Float64, Float32] # TODO Rational{Int}
4039
@test d.array[1] isa Hyperrectangle && test_directions(d.array[1])
4140
d = decompose(b, set_type=HPolygon, ɛ=to_N(N, 1e-2))
4241
@test d.array[1] isa HPolygon && test_directions(d.array[1])
43-
d = decompose(b, set_type=LazySets.Interval, blocks=ones(Int, 6))
44-
@test d.array[1] isa LazySets.Interval &&
42+
43+
d = decompose(b, set_type=Interval, blocks=ones(Int, 6))
44+
@test d.array[1] isa Interval &&
4545
σ(N[1], d.array[1])[1] == one(N) && σ(N[-1], d.array[1])[1] == -one(N)
4646

4747
# ===================
@@ -69,8 +69,8 @@ for N in [Float64, Float32] # TODO Rational{Int}
6969
@test ai isa Hyperrectangle
7070
end
7171
# 1D intervals
72-
d = decompose(b, set_type=LazySets.Interval)
72+
d = decompose(b, set_type=Interval)
7373
for ai in array(d)
74-
@test ai isa LazySets.Interval
74+
@test ai isa Interval
7575
end
7676
end

test/unit_overapproximate.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using IntervalArithmetic
21
import LazySets.Approximations.overapproximate
32

43
for N in [Float64, Float32] # TODO Rational{Int}

0 commit comments

Comments
 (0)