diff --git a/docs/src/lib/representations.md b/docs/src/lib/representations.md index 5afe2b5519..7ee779274e 100644 --- a/docs/src/lib/representations.md +++ b/docs/src/lib/representations.md @@ -135,7 +135,6 @@ low(::Hyperrectangle{Float64}) ```@docs Interval -IA dim(::Interval) σ(::AbstractVector{Float64}, ::Interval{Float64, IntervalArithmetic.AbstractInterval{Float64}}) center(::Interval) diff --git a/src/Interval.jl b/src/Interval.jl index 9a129ce1f1..82b58641e0 100644 --- a/src/Interval.jl +++ b/src/Interval.jl @@ -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 @@ -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 @@ -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. @@ -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 @@ -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:+, -, *, ∈, ⊆ diff --git a/src/convert.jl b/src/convert.jl index 3bf1f13546..036a62af4f 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -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. @@ -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 diff --git a/test/unit_Interval.jl b/test/unit_Interval.jl index 6be1591c07..980a38412f 100644 --- a/test/unit_Interval.jl +++ b/test/unit_Interval.jl @@ -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] @@ -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] diff --git a/test/unit_decompose.jl b/test/unit_decompose.jl index 9fd860511d..2c0f5bbe1f 100644 --- a/test/unit_decompose.jl +++ b/test/unit_decompose.jl @@ -1,4 +1,3 @@ -# using IntervalArithmetic import LazySets.Approximations.decompose for N in [Float64, Float32] # TODO Rational{Int} @@ -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) # =================== @@ -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 diff --git a/test/unit_overapproximate.jl b/test/unit_overapproximate.jl index 7b269fe12b..7af56b6aad 100644 --- a/test/unit_overapproximate.jl +++ b/test/unit_overapproximate.jl @@ -1,4 +1,3 @@ -using IntervalArithmetic import LazySets.Approximations.overapproximate for N in [Float64, Float32] # TODO Rational{Int}