Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions ext/IntervalArithmeticsIntervalSetsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module IntervalArithmeticsIntervalSetsExt

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this line, it seems that IS.Interval can have bounds that are of different real types (eg Rational, or even maybe Int)?
In our case, IA.Interval can have bounds that are either Rational or AbstractFloat.
Hence, if you would like to preserve as much as possible the same bound type as the initial interval, I suggest the following:

Base.convert(::Type{IA.Interval}, I::IS.Interval{<:Any,<:Any,T}) where {T} = convert(IA.Interval{IA.promote_numtype(T, T)}, i)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally, makes sense! Didn't know this promote function.

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
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"
22 changes: 22 additions & 0 deletions test/interval_tests/construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down