From 63ae171839f7c3aa61d79b87f15a01662edf4e88 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Sun, 1 Sep 2024 17:24:00 +0200 Subject: [PATCH] more careful conversion --- ext/IntervalArithmeticsIntervalSetsExt.jl | 26 +++++++++++------------ test/interval_tests/construction.jl | 23 ++++++++++++-------- test/runtests.jl | 1 + 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/ext/IntervalArithmeticsIntervalSetsExt.jl b/ext/IntervalArithmeticsIntervalSetsExt.jl index 299f1227..d5b89713 100644 --- a/ext/IntervalArithmeticsIntervalSetsExt.jl +++ b/ext/IntervalArithmeticsIntervalSetsExt.jl @@ -3,23 +3,21 @@ module IntervalArithmeticsIntervalSetsExt import IntervalSets as IS import IntervalArithmetic as IA - -# Interval <- IS.Interval -function IA.interval(i::IS.Interval) - x = IA.interval(IS.endpoints(i)...) - return IA._unsafe_interval(IA.bareinterval(x), IA.decoration(x), false) -end -function IA.interval(::Type{T}, i::IS.Interval) where {T<:IA.NumTypes} +IA.interval(i::IS.Interval{L,R,T}) where {L,R,T} = IA.interval(IA.promote_numtype(T, T), i) +function IA.interval(::Type{T}, i::IS.Interval{L,R}) where {T<:IA.NumTypes,L,R} + # infinite endpoints are always open in IA, finite always closed: + isinf(IS.leftendpoint(i)) != IS.isleftopen(i) && return IA.nai(T) + isinf(IS.rightendpoint(i)) != IS.isrightopen(i) && return IA.nai(T) x = IA.interval(T, IS.endpoints(i)...) return IA._unsafe_interval(IA.bareinterval(x), IA.decoration(x), false) end -Base.convert(::Type{IA.Interval}, i::IS.Interval) = IA.interval(i) -Base.convert(::Type{IA.Interval{T}}, i::IS.Interval) where {T<:IA.NumTypes} = IA.interval(T, i) -# Interval -> IS.Interval -IS.Interval(i::IA.Interval) = IS.Interval(IA.inf(i), IA.sup(i)) -IS.Interval{T,S,R}(i::IA.Interval) where {T,S,R} = IS.Interval{T,S,R}(IA.inf(i), IA.sup(i)) -Base.convert(::Type{IS.Interval}, i::IA.Interval) = IS.Interval(i) -Base.convert(::Type{T}, i::IA.Interval) where {T<:IS.Interval} = T(i) +function IS.Interval(i::IA.Interval) + lo, hi = IA.bounds(i) + # infinite endpoints are always open in IA, finite always closed: + L = ifelse(isinf(lo), :open, :closed) + R = ifelse(isinf(hi), :open, :closed) + return IS.Interval{L,R}(lo, hi) +end end diff --git a/test/interval_tests/construction.jl b/test/interval_tests/construction.jl index c50f8ce0..81e88230 100644 --- a/test/interval_tests/construction.jl +++ b/test/interval_tests/construction.jl @@ -158,19 +158,24 @@ end end @testset "Interval types conversion" begin - import IntervalSets as IS - - i = convert(Interval, IS.Interval(1, 2)) + i = interval(IS.Interval(1, 2)) @test isequal_interval(i, interval(1., 2.)) && !isguaranteed(i) - i = convert(Interval, IS.Interval(0.1, 2)) + i = interval(IS.Interval(0.1, 2)) @test isequal_interval(i, interval(0.1, 2.)) && !isguaranteed(i) - @test interval(IS.Interval(0.1, 2)) === i @test interval(Float64, IS.Interval(0.1, 2)) === i - - @test convert(IS.Interval, interval(1, 2)) === IS.Interval(1., 2.) - @test convert(IS.Interval, interval(0.1, 2)) === IS.Interval(0.1, 2.) + + i = interval(IS.iv"[0.1, Inf)") + @test isequal_interval(i, interval(0.1, Inf)) && !isguaranteed(i) + @test interval(IS.iv"[0.1, Inf]") === nai(Float64) + @test interval(IS.iv"(0.1, Inf]") === nai(Float64) + @test interval(IS.iv"(0.1, Inf)") === nai(Float64) + @test interval(IS.iv"(0.1, 1)") === nai(Float64) + @test interval(IS.iv"(0.1, 1]") === nai(Float64) + + @test IS.Interval(interval(1, 2)) === IS.Interval(1., 2.) @test IS.Interval(interval(0.1, 2)) === IS.Interval(0.1, 2.) - @test IS.ClosedInterval{Float64}(interval(0.1, 2)) === IS.Interval(0.1, 2.) + @test IS.Interval(interval(0.1, Inf)) === IS.iv"[0.1, Inf)" + @test IS.Interval(interval(-Inf, Inf)) === IS.iv"(-Inf, Inf)" end @testset "Propagation of `isguaranteed`" begin diff --git a/test/runtests.jl b/test/runtests.jl index 1c5cf2d5..cebbfb63 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,6 +3,7 @@ using Test using ForwardDiff using IntervalArithmetic using InteractiveUtils +import IntervalSets as IS include("generate_ITF1788.jl")