From 6f3dc531fda566eea18f6767656940b88184ca8d Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Fri, 23 Aug 2024 13:56:51 -0400 Subject: [PATCH 1/5] add IntervalSets conversions --- ext/IntervalArithmeticsIntervalSetsExt.jl | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 ext/IntervalArithmeticsIntervalSetsExt.jl diff --git a/ext/IntervalArithmeticsIntervalSetsExt.jl b/ext/IntervalArithmeticsIntervalSetsExt.jl new file mode 100644 index 000000000..f1a6c018e --- /dev/null +++ b/ext/IntervalArithmeticsIntervalSetsExt.jl @@ -0,0 +1,9 @@ +module IntervalArithmeticsIntervalSetsExt + +import IntervalSets as IS +import IntervalArithmetic as IA + +Base.convert(T::Type{<:IS.Interval}, i::IA.Interval) = T(IA.inf(i), IA.sup(i)) +Base.convert(T::Type{<:IA.Interval}, i::IS.Interval) = T(IS.infinum(i), IS.supremum(i)) + +end From cd38097b07e433e5cfadc0cb2857786351227733 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Fri, 23 Aug 2024 13:58:36 -0400 Subject: [PATCH 2/5] upd Project --- Project.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Project.toml b/Project.toml index 057ee785a..570a25c05 100644 --- a/Project.toml +++ b/Project.toml @@ -11,17 +11,20 @@ RoundingEmulator = "5eaf0fd0-dfba-4ccb-bf02-d820a40db705" [weakdeps] DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" [extensions] IntervalArithmeticDiffRulesExt = "DiffRules" IntervalArithmeticForwardDiffExt = "ForwardDiff" +IntervalArithmeticsIntervalSetsExt = "IntervalSets" IntervalArithmeticRecipesBaseExt = "RecipesBase" [compat] CRlibm_jll = "1" DiffRules = "1" ForwardDiff = "0.10" +IntervalSets = "0.7" MacroTools = "0.5" RecipesBase = "1" RoundingEmulator = "0.2" From 5dc5d654bd4a7fb567577d07cbf5ef016acb50ca Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Sat, 24 Aug 2024 16:52:35 -0400 Subject: [PATCH 3/5] fixes --- ext/IntervalArithmeticsIntervalSetsExt.jl | 22 ++++++++++++++++++++-- test/Project.toml | 1 + test/interval_tests/construction.jl | 22 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ext/IntervalArithmeticsIntervalSetsExt.jl b/ext/IntervalArithmeticsIntervalSetsExt.jl index f1a6c018e..7cd7e4866 100644 --- a/ext/IntervalArithmeticsIntervalSetsExt.jl +++ b/ext/IntervalArithmeticsIntervalSetsExt.jl @@ -3,7 +3,25 @@ module IntervalArithmeticsIntervalSetsExt import IntervalSets as IS import IntervalArithmetic as IA -Base.convert(T::Type{<:IS.Interval}, i::IA.Interval) = T(IA.inf(i), IA.sup(i)) -Base.convert(T::Type{<:IA.Interval}, i::IS.Interval) = T(IS.infinum(i), IS.supremum(i)) + +# BareInterval <- IS.Interval +Base.convert(::Type{IA.BareInterval}, i::IS.Interval) = IA.bareinterval(IS.endpoints(i)...) +IA.bareinterval(i::IS.Interval) = IA.bareinterval(IS.endpoints(i)...) + +# BareInterval -> IS.Interval +Base.convert(::Type{IS.Interval}, i::IA.BareInterval) = IS.Interval(IA.inf(i), IA.sup(i)) +IS.Interval(i::IA.BareInterval) = IS.Interval(IA.inf(i), IA.sup(i)) + +# Interval <- IS.Interval +Base.convert(::Type{IA.Interval}, i::IS.Interval{<:Any,<:Any,T}) where {T} = convert(IA.Interval{float(T)}, i) +function Base.convert(::Type{IA.Interval{T}}, i::IS.Interval) where {T} + bi = IA.bareinterval(i) + return IA._unsafe_interval(bi, IA.decoration(bi), false) +end +IA.interval(i::IS.Interval) = IA.interval(IS.endpoints(i)...) + +# Interval -> IS.Interval +Base.convert(::Type{IS.Interval}, i::IA.Interval) = IS.Interval(IA.inf(i), IA.sup(i)) +IS.Interval(i::IA.Interval) = IS.Interval(IA.inf(i), IA.sup(i)) end diff --git a/test/Project.toml b/test/Project.toml index a7e449099..70dd3a83f 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,4 +1,5 @@ [deps] ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/interval_tests/construction.jl b/test/interval_tests/construction.jl index 4115a9e01..77f22ab52 100644 --- a/test/interval_tests/construction.jl +++ b/test/interval_tests/construction.jl @@ -157,6 +157,28 @@ end @test_throws DomainError convert(Interval{Float64}, interval(1+im)) end +@testset "Interval types conversion" begin + import IntervalSets as IS + + @test convert(BareInterval, IS.Interval(1, 2)) === bareinterval(1., 2.) + @test convert(BareInterval, IS.Interval(0.1, 2.)) === bareinterval(0.1, 2.) + @test bareinterval(IS.Interval(1., 2.)) === bareinterval(1., 2.) + + @test convert(IS.Interval, bareinterval(1, 2)) === IS.Interval(1., 2.) + @test convert(IS.Interval, bareinterval(0.1, 2.)) === IS.Interval(0.1, 2.) + @test IS.Interval(bareinterval(1., 2.)) === IS.Interval(1., 2.) + + i = convert(Interval, IS.Interval(1, 2)) + @test isequal_interval(i, interval(1., 2.)) && !isguaranteed(i) + i = convert(Interval, IS.Interval(0.1, 2)) + @test isequal_interval(i, interval(0.1, 2.)) && !isguaranteed(i) + @test interval(IS.Interval(0.1, 2)) === interval(0.1, 2.) + + @test convert(IS.Interval, interval(1, 2)) === IS.Interval(1., 2.) + @test convert(IS.Interval, interval(0.1, 2)) === IS.Interval(0.1, 2.) + @test IS.Interval(interval(0.1, 2)) === IS.Interval(0.1, 2.) +end + @testset "Propagation of `isguaranteed`" begin @test !isguaranteed(interval(convert(Interval{Float64}, 0), interval(convert(Interval{Float64}, 1)))) @test !isguaranteed(interval(0, convert(Interval{Float64}, 1))) From 045acc21d48bc31c9f9b6d2559ae15531156f900 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Sun, 25 Aug 2024 19:32:04 -0400 Subject: [PATCH 4/5] upd --- ext/IntervalArithmeticsIntervalSetsExt.jl | 26 +++++++++++------------ test/interval_tests/construction.jl | 12 +++-------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/ext/IntervalArithmeticsIntervalSetsExt.jl b/ext/IntervalArithmeticsIntervalSetsExt.jl index 7cd7e4866..299f12273 100644 --- a/ext/IntervalArithmeticsIntervalSetsExt.jl +++ b/ext/IntervalArithmeticsIntervalSetsExt.jl @@ -4,24 +4,22 @@ import IntervalSets as IS import IntervalArithmetic as IA -# BareInterval <- IS.Interval -Base.convert(::Type{IA.BareInterval}, i::IS.Interval) = IA.bareinterval(IS.endpoints(i)...) -IA.bareinterval(i::IS.Interval) = IA.bareinterval(IS.endpoints(i)...) - -# BareInterval -> IS.Interval -Base.convert(::Type{IS.Interval}, i::IA.BareInterval) = IS.Interval(IA.inf(i), IA.sup(i)) -IS.Interval(i::IA.BareInterval) = IS.Interval(IA.inf(i), IA.sup(i)) - # Interval <- IS.Interval -Base.convert(::Type{IA.Interval}, i::IS.Interval{<:Any,<:Any,T}) where {T} = convert(IA.Interval{float(T)}, i) -function Base.convert(::Type{IA.Interval{T}}, i::IS.Interval) where {T} - bi = IA.bareinterval(i) - return IA._unsafe_interval(bi, IA.decoration(bi), false) +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} + x = IA.interval(T, IS.endpoints(i)...) + return IA._unsafe_interval(IA.bareinterval(x), IA.decoration(x), false) end -IA.interval(i::IS.Interval) = IA.interval(IS.endpoints(i)...) +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 -Base.convert(::Type{IS.Interval}, i::IA.Interval) = IS.Interval(IA.inf(i), IA.sup(i)) 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) end diff --git a/test/interval_tests/construction.jl b/test/interval_tests/construction.jl index 77f22ab52..c50f8ce02 100644 --- a/test/interval_tests/construction.jl +++ b/test/interval_tests/construction.jl @@ -160,23 +160,17 @@ end @testset "Interval types conversion" begin import IntervalSets as IS - @test convert(BareInterval, IS.Interval(1, 2)) === bareinterval(1., 2.) - @test convert(BareInterval, IS.Interval(0.1, 2.)) === bareinterval(0.1, 2.) - @test bareinterval(IS.Interval(1., 2.)) === bareinterval(1., 2.) - - @test convert(IS.Interval, bareinterval(1, 2)) === IS.Interval(1., 2.) - @test convert(IS.Interval, bareinterval(0.1, 2.)) === IS.Interval(0.1, 2.) - @test IS.Interval(bareinterval(1., 2.)) === IS.Interval(1., 2.) - i = convert(Interval, IS.Interval(1, 2)) @test isequal_interval(i, interval(1., 2.)) && !isguaranteed(i) i = convert(Interval, IS.Interval(0.1, 2)) @test isequal_interval(i, interval(0.1, 2.)) && !isguaranteed(i) - @test interval(IS.Interval(0.1, 2)) === interval(0.1, 2.) + @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.) @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.) end @testset "Propagation of `isguaranteed`" begin From 63ae171839f7c3aa61d79b87f15a01662edf4e88 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Sun, 1 Sep 2024 17:24:00 +0200 Subject: [PATCH 5/5] 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 299f12273..d5b89713e 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 c50f8ce02..81e882302 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 1c5cf2d50..cebbfb63a 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")