Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete conversion to/from IA.Interval #3024

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/lib/conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ convert(::Type{CartesianProduct{N, Interval{N}, Interval{N}}}, ::AbstractHyperre
convert(::Type{CartesianProductArray{N, Interval{N}}}, ::AbstractHyperrectangle{N}) where {N<:Real}
convert(::Type{Hyperrectangle}, ::Rectification{N, AH}) where {N<:Real, AH<:AbstractHyperrectangle{N}}
convert(::Type{Interval}, ::Rectification{N, IN}) where {N<:Real, IN<:Interval{N}}
convert(::Type{IntervalArithmetic.Interval}, ::Interval)
convert(::Type{IntervalArithmetic.Interval}, ::ConvexSet)
convert(::Type{Interval}, ::IntervalArithmetic.Interval)
convert(::Type{VPolytope}, ::ConvexHullArray{N, Singleton{N, VT}}) where {N, VT}
convert(::Type{VPolygon}, ::ConvexSet)
Expand Down
18 changes: 13 additions & 5 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -855,21 +855,22 @@ function convert(X::Type{HPOLYGON},
end

"""
convert(::Type{IntervalArithmetic.Interval}, x::Interval)
convert(::Type{IntervalArithmetic.Interval}, X::ConvexSet)

Converts a `LazySets` interval to an `Interval` from `IntervalArithmetic`.
Converts a convex set to an `Interval` from `IntervalArithmetic`.

### Input

- `Interval` -- type used for dispatch, from `IntervalArithmetic`
- `x` -- interval (`LazySets.Interval`)
- `X` -- convex set

### Output

An `IntervalArithmetic.Interval`.
"""
function convert(::Type{IntervalArithmetic.Interval}, x::Interval)
return IntervalArithmetic.interval(min(x), max(x))
function convert(::Type{IntervalArithmetic.Interval}, X::ConvexSet)
@assert dim(X) == 1 "cannot convert a $(dim(X))-dimensional set to an interval"
return convert(Interval, X).dat
end

"""
Expand Down Expand Up @@ -934,6 +935,13 @@ function convert(::Type{Hyperrectangle}, IB::IntervalArithmetic.IntervalBox)
return Hyperrectangle(low=low_IB, high=high_IB)
end

# method for Interval
function convert(::Type{Hyperrectangle}, I::IntervalArithmetic.Interval)
low_I = [IntervalArithmetic.inf(I)]
high_I = [IntervalArithmetic.sup(I)]
return Hyperrectangle(low=low_I, high=high_I)
end

"""
convert(::Type{Hyperrectangle}, r::Rectification{N, AH})
where {N<:Real, AH<:AbstractHyperrectangle{N}}
Expand Down
5 changes: 5 additions & 0 deletions test/Sets/Hyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ for N in [Float64, Rational{Int}, Float32]
H = convert(Hyperrectangle, B)
@test convert(IntervalBox, H) == B

# conversion to and from IntervalArithmetic's Interval type
I = IntervalArithmetic.Interval(0, 1)
H = convert(Hyperrectangle, I)
@test convert(IntervalArithmetic.Interval, H) == I

# conversion from other hyperrectangular sets
H = Hyperrectangle(N[1], N[1])
@test convert(Hyperrectangle, BallInf(N[1], N(1))) == H
Expand Down
5 changes: 4 additions & 1 deletion test/Sets/Interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,13 @@ for N in [Float64, Float32, Rational{Int}]
A = convert(Interval, H)
@test A isa Interval && low(A) == [N(-1/2)] && high(A) == [N(1/2)]

# conversion from a lazyset to an interval
# conversion from a LazySet to an interval
M = hcat(N[2])
B = convert(Interval, M*H)
@test B isa Interval && low(B) == [N(-1)] && high(B) == [N(1)]
# conversion to an IntervalArithmetic.Interval
B2 = convert(IntervalArithmetic.Interval, M*H)
@test B2 == B.dat

# set difference
A = Interval(N(5), N(8))
Expand Down