Skip to content

Commit

Permalink
Merge pull request #3517 from JuliaReach/schillic/convert_interval
Browse files Browse the repository at this point in the history
Revise and test `convert` to `Interval`
schillic authored Jun 1, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 6f8c656 + bd6ad12 commit 38db49f
Showing 2 changed files with 13 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/convert.jl
Original file line number Diff line number Diff line change
@@ -412,14 +412,7 @@ Interval{Float64}([0, 1])
```
"""
function convert(::Type{Interval}, X::LazySet)
@assert dim(X) == 1 "cannot convert a $(dim(X))-dimensional set to an " *
"`Interval`"
if !isconvextype(typeof(X))
error("this implementation requires a convex set")
end

l, h = extrema(X, 1)
return Interval(l, h)
return Interval(convert(IA.Interval, X))
end

"""
@@ -619,9 +612,11 @@ Convert a convex set to an `Interval` from `IntervalArithmetic`.
An `IntervalArithmetic.Interval`.
"""
function convert(::Type{IA.Interval}, X::LazySet)
@assert dim(X) == 1 "cannot convert a $(dim(X))-dimensional set to an " *
"interval"
return convert(Interval, X).dat
@assert dim(X) == 1 "cannot convert a $(dim(X))-dimensional set to an `Interval`"
@assert isconvextype(typeof(X)) "cannot convert a non-convex set to an `Interval`"

l, h = extrema(X, 1)
return IA.interval(l, h)
end

"""
8 changes: 7 additions & 1 deletion test/Sets/Interval.jl
Original file line number Diff line number Diff line change
@@ -186,9 +186,15 @@ for N in [Float64, Float32, Rational{Int}]
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
# conversion to and from IntervalArithmetic.Interval
B2 = convert(IA.Interval, M * H)
@test B2 == B.dat
B3 = convert(Interval, B2)
@test B3 == B
# conversion from a non-convex set fails
U = UnionSet(Interval(1, 2), Interval(3, 4))
@test_throws AssertionError convert(Interval, U)
@test_throws AssertionError convert(IA.Interval, U)

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

0 comments on commit 38db49f

Please sign in to comment.