Skip to content

Commit

Permalink
add IntervalSets conversions (#677)
Browse files Browse the repository at this point in the history
* add IntervalSets conversions

* upd Project

* fixes

* upd

* more careful conversion
  • Loading branch information
aplavin authored Sep 3, 2024
1 parent d3f91fa commit bcf688e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions ext/IntervalArithmeticsIntervalSetsExt.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 21 additions & 0 deletions test/interval_tests/construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using Test
using ForwardDiff
using IntervalArithmetic
using InteractiveUtils
import IntervalSets as IS

include("generate_ITF1788.jl")

Expand Down

0 comments on commit bcf688e

Please sign in to comment.