Skip to content

Commit

Permalink
more careful conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Sep 1, 2024
1 parent 045acc2 commit 63ae171
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
26 changes: 12 additions & 14 deletions ext/IntervalArithmeticsIntervalSetsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 14 additions & 9 deletions test/interval_tests/construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 63ae171

Please sign in to comment.