From bcf688eddf9cb3fc0e62489dea6c2e86b13d59f8 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Tue, 3 Sep 2024 06:38:59 +0200 Subject: [PATCH] add IntervalSets conversions (#677) * add IntervalSets conversions * upd Project * fixes * upd * more careful conversion --- Project.toml | 3 +++ ext/IntervalArithmeticsIntervalSetsExt.jl | 23 +++++++++++++++++++++++ test/Project.toml | 1 + test/interval_tests/construction.jl | 21 +++++++++++++++++++++ test/runtests.jl | 1 + 5 files changed, 49 insertions(+) create mode 100644 ext/IntervalArithmeticsIntervalSetsExt.jl 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" diff --git a/ext/IntervalArithmeticsIntervalSetsExt.jl b/ext/IntervalArithmeticsIntervalSetsExt.jl new file mode 100644 index 000000000..d5b89713e --- /dev/null +++ b/ext/IntervalArithmeticsIntervalSetsExt.jl @@ -0,0 +1,23 @@ +module IntervalArithmeticsIntervalSetsExt + +import IntervalSets as IS +import IntervalArithmetic as IA + +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 + +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/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..81e882302 100644 --- a/test/interval_tests/construction.jl +++ b/test/interval_tests/construction.jl @@ -157,6 +157,27 @@ end @test_throws DomainError convert(Interval{Float64}, interval(1+im)) end +@testset "Interval types conversion" begin + i = interval(IS.Interval(1, 2)) + @test isequal_interval(i, interval(1., 2.)) && !isguaranteed(i) + i = interval(IS.Interval(0.1, 2)) + @test isequal_interval(i, interval(0.1, 2.)) && !isguaranteed(i) + @test interval(Float64, IS.Interval(0.1, 2)) === i + + 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.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 @test !isguaranteed(interval(convert(Interval{Float64}, 0), interval(convert(Interval{Float64}, 1)))) @test !isguaranteed(interval(0, convert(Interval{Float64}, 1))) 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")