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

Add conversion to and from IntervalBox #1215

Merged
merged 6 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions docs/src/lib/conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ convert(::Type{VPolygon}, ::AbstractPolytope)
convert(::Type{VPolytope}, ::AbstractPolytope)
convert(::Type{VPolytope}, ::HPolytope)
convert(::Type{Zonotope}, ::AbstractHyperrectangle)
convert(::Type{IntervalBox}, ::AbstractHyperrectangle)
convert(::Type{Hyperrectangle}, ::IntervalBox)
```
40 changes: 40 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Base.convert

using IntervalArithmetic: IntervalBox, inf, sup

#= conversion between set types =#

"""
Expand Down Expand Up @@ -497,3 +499,41 @@ function convert(X::Type{HPOLYGON},
"hyperrectangle into a two-dimensional polygon"
return HPOLYGON(constraints_list(H))
end

"""
convert(::Type{IntervalBox}, H::AbstractHyperrectangle)

Converts a hyperrectangular set to an `IntervalBox` from `IntervalArithmetic`.

### Input

- `IntervalBox` -- type used for dispatch
- `H` -- hyperrectangular set

### Output

An `IntervalBox`.
"""
function convert(::Type{IntervalBox}, H::AbstractHyperrectangle)
return IntervalBox(low(H), high(H))
end

"""
convert(::Type{Hyperrectangle}, IB::IntervalBox)

Converts an `IntervalBox` from `IntervalArithmetic` to an hyperrectangular set.
mforets marked this conversation as resolved.
Show resolved Hide resolved

### Input

- `Hyperrectangle` -- type used for dispatch
- `IB` -- interval box

### Output

A `Hyperrectangle`.
"""
function convert(::Type{Hyperrectangle}, IB::IntervalBox)
low_IB = Vector(inf.(IB)) # TODO: temprary conversion, see #1214
high_IB = Vector(sup.(IB)) # TODO: temprary conversion, see #1214
return Hyperrectangle(low=low_IB, high=high_IB)
end
7 changes: 7 additions & 0 deletions test/unit_Hyperrectangle.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using IntervalArithmetic: IntervalBox
mforets marked this conversation as resolved.
Show resolved Hide resolved

for N in [Float64, Rational{Int}, Float32]
# random hyperrectangle
rand(Hyperrectangle)
Expand Down Expand Up @@ -157,4 +159,9 @@ for N in [Float64, Rational{Int}, Float32]
@test ispermutation(clist,
[HalfSpace(N[1, 0], N(3)), HalfSpace(N[0, 1], N(3)),
HalfSpace(N[-1, 0], N(1)), HalfSpace(N[0, -1], N(1))])

# conversion to and from IntervalArithmetic's IntervalBox type
B = IntervalBox(0..1, 0..1)
H = convert(Hyperrectangle, B)
@test convert(IntervalBox, H) == B
schillic marked this conversation as resolved.
Show resolved Hide resolved
end