Skip to content

Commit

Permalink
Merge pull request #316 from JuliaReach/mforets/300
Browse files Browse the repository at this point in the history
#300 - Correct usage of IntervalArithmetic
  • Loading branch information
mforets authored Apr 3, 2018
2 parents 84e80fb + a352a9f commit 24c5ad2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
1 change: 0 additions & 1 deletion docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ low(::Hyperrectangle{Float64})

```@docs
Interval
IA
dim(::Interval)
σ(::AbstractVector{Float64}, ::Interval{Float64, IntervalArithmetic.AbstractInterval{Float64}})
center(::Interval)
Expand Down
37 changes: 19 additions & 18 deletions src/Interval.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
using IntervalArithmetic
import IntervalArithmetic
import IntervalArithmetic: AbstractInterval

"""
IA = IntervalArithmetic
Name alias for the interval arithmetic library.
"""
const IA = IntervalArithmetic

export Interval, IA,
export Interval,
dim, σ, center, +, -, *, , ,
low, high, vertices_list

"""
Interval{N<:Real, IN <: IA.AbstractInterval{N}} <: AbstractPointSymmetricPolytope{N}
Interval{N<:Real, IN <: AbstractInterval{N}} <: AbstractPointSymmetricPolytope{N}
Type representing an interval on the real line. Mathematically, it is of the
form
```math
[a, b] := \{ a \le x \le b \} \subseteq \mathbb{R}.
[a, b] := \\{ a x b \\} ⊆ \\mathbb{R}.
```
### Fields
Expand Down Expand Up @@ -72,20 +66,27 @@ julia> center(x)
This type is such that the usual pairwise arithmetic operators `+`, `-`, `*` trigger
the corresponding interval arithmetic backend method, and return a new
`Interval` object. For the symbolic Minkowksi sum, use `MinkowskiSum` or `⊕`.
Interval of other numeric types can be created as well, eg. a rational interval:
```jldoctest interval_constructor
julia> Interval(0//1, 2//1)
LazySets.Interval{Rational{Int64},IntervalArithmetic.Interval{Rational{Int64}}}([0//1, 2//1])
```
"""
struct Interval{N<:Real, IN <: IA.AbstractInterval{N}} <: AbstractPointSymmetricPolytope{N}
struct Interval{N<:Real, IN <: AbstractInterval{N}} <: AbstractPointSymmetricPolytope{N}
dat::IN
end
# type-less convenience constructor
Interval(interval::IN) where {N, IN <: IA.AbstractInterval{N}} = Interval{N, IN}(interval)
Interval(interval::IN) where {N, IN <: AbstractInterval{N}} = Interval{N, IN}(interval)

# TODO: adapt show method

# constructor that takes two numbers
Interval(lo::N, hi::N) where {N} = Interval(IA.Interval(lo, hi))
Interval(lo::N, hi::N) where {N} = Interval(IntervalArithmetic.Interval(lo, hi))

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

"""
dim(x::Interval)::Int
Expand All @@ -103,7 +104,7 @@ The integer 1.
dim(x::Interval)::Int = 1

"""
σ(d::V, x::Interval{N, IN})::V where {N, IN <: IA.AbstractInterval{N}, V<:AbstractVector{N}}
σ(d::V, x::Interval{N, IN})::V where {N, IN <: AbstractInterval{N}, V<:AbstractVector{N}}
Return the support vector of an ellipsoid in a given direction.
Expand All @@ -116,7 +117,7 @@ Return the support vector of an ellipsoid in a given direction.
Support vector in the given direction.
"""
function σ(d::V, x::Interval{N, IN})::V where {N, IN <: IA.AbstractInterval{N}, V<:AbstractVector{N}}
function σ(d::V, x::Interval{N, IN})::V where {N, IN <: AbstractInterval{N}, V<:AbstractVector{N}}
@assert length(d) == dim(x)
return d[1] > 0 ? [x.dat.hi] : [x.dat.lo]
end
Expand All @@ -134,7 +135,7 @@ Return the interval's center.
The center, or midpoint, of ``x``.
"""
center(x::Interval) = [IA.mid(x.dat)]
center(x::Interval) = [IntervalArithmetic.mid(x.dat)]

import Base:+, -, *, ,

Expand Down
7 changes: 3 additions & 4 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ function convert(::Type{Zonotope}, H::AbstractHyperrectangle{N}) where {N}
return Zonotope{N}(center(H), diagm(radius_hyperrectangle(H)))
end

@require IntervalArithmetic begin
import IntervalArithmetic.AbstractInterval

"""
convert(::Type{Hyperrectangle}, x::LazySets.Interval{N, IN}) where {N, IN <: IA.AbstractInterval{N}}
convert(::Type{Hyperrectangle}, x::LazySets.Interval{N, IN}) where {N, IN <: AbstractInterval{N}}
Converts a unidimensional interval into a hyperrectangular set.
Expand All @@ -107,7 +107,6 @@ julia> convert(Hyperrectangle, Interval(0.0, 1.0))
LazySets.Hyperrectangle{Float64}([0.5], [0.5])
```
"""
function convert(::Type{Hyperrectangle}, x::LazySets.Interval{N, IN}) where {N, IN <: IA.AbstractInterval{N}}
function convert(::Type{Hyperrectangle}, x::LazySets.Interval{N, IN}) where {N, IN <: AbstractInterval{N}}
return Hyperrectangle(low=[low(x)], high=[high(x)])
end
end
14 changes: 7 additions & 7 deletions test/unit_Interval.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using IntervalArithmetic
import IntervalArithmetic

# default constructor
x = LazySets.Interval{Float64, IntervalArithmetic.Interval{Float64}}(IntervalArithmetic.Interval(0.0, 1.0))
x = Interval{Float64, IntervalArithmetic.Interval{Float64}}(IntervalArithmetic.Interval(0.0, 1.0))

# type-less constructor
x = LazySets.Interval(0.0, 1.0)
x = Interval(0.0, 1.0)

# constructor with two numbers
x = LazySets.Interval(0.0, 1.0)
x = Interval(0.0, 1.0)

@test dim(x) == 1
@test center(x) == [0.5]
Expand All @@ -18,12 +18,12 @@ v = vertices_list(x)
@test an_element(x) x
# test containment
@test (x x) && !(x 0.2 * x) && (x 2. * x)
@test issubset(x, LazySets.Interval(0.0, 2.0))
@test !issubset(x, LazySets.Interval(-1.0, 0.5))
@test issubset(x, Interval(0.0, 2.0))
@test !issubset(x, Interval(-1.0, 0.5))


# + operator (= concrete Minkowski sum of intervals)
y = LazySets.Interval(-2.0, 0.5)
y = Interval(-2.0, 0.5)
m = x + y
@test dim(m) == 1
@test σ([1.0], m) == [1.5]
Expand Down
10 changes: 5 additions & 5 deletions test/unit_decompose.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# using IntervalArithmetic
import LazySets.Approximations.decompose

for N in [Float64, Float32] # TODO Rational{Int}
Expand Down Expand Up @@ -40,8 +39,9 @@ for N in [Float64, Float32] # TODO Rational{Int}
@test d.array[1] isa Hyperrectangle && test_directions(d.array[1])
d = decompose(b, set_type=HPolygon, ɛ=to_N(N, 1e-2))
@test d.array[1] isa HPolygon && test_directions(d.array[1])
d = decompose(b, set_type=LazySets.Interval, blocks=ones(Int, 6))
@test d.array[1] isa LazySets.Interval &&

d = decompose(b, set_type=Interval, blocks=ones(Int, 6))
@test d.array[1] isa Interval &&
σ(N[1], d.array[1])[1] == one(N) && σ(N[-1], d.array[1])[1] == -one(N)

# ===================
Expand Down Expand Up @@ -69,8 +69,8 @@ for N in [Float64, Float32] # TODO Rational{Int}
@test ai isa Hyperrectangle
end
# 1D intervals
d = decompose(b, set_type=LazySets.Interval)
d = decompose(b, set_type=Interval)
for ai in array(d)
@test ai isa LazySets.Interval
@test ai isa Interval
end
end
1 change: 0 additions & 1 deletion test/unit_overapproximate.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using IntervalArithmetic
import LazySets.Approximations.overapproximate

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

0 comments on commit 24c5ad2

Please sign in to comment.