From bd6ad12be497b5da05a82d7bac44fcbe72a003a5 Mon Sep 17 00:00:00 2001 From: schillic Date: Tue, 28 May 2024 11:33:06 +0200 Subject: [PATCH] revise and test convert to Interval --- src/convert.jl | 17 ++++++----------- test/Sets/Interval.jl | 8 +++++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/convert.jl b/src/convert.jl index 7b9448da96..f182726e71 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -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 """ diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index 757e7917f6..01392b5bff 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -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))