Skip to content

Commit 74d271e

Browse files
dpsanderslbenet
authored andcommitted
Add IntervalBox constructor from vectors giving opposite corners (#238)
* Add IntervalBox constructor from corners of box * Force the interval even if the bounds are the wrong way round * Add test * Allow arbitrary AbstractVectors for construction of IntervalBox from corners * (Re-)add fast version for creating IntervalBox from two equal length SVectors * Move force_interval to intervals.jl and add tests * Fix extra end
1 parent 1b9b29f commit 74d271e

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

src/intervals/intervals.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ end
109109
interval(a::Real) = interval(a, a)
110110
interval(a::Interval) = a
111111

112+
"Make an interval even if a > b"
113+
function force_interval(a, b)
114+
a > b && return interval(b, a)
115+
return interval(a, b)
116+
end
117+
112118

113119
## Include files
114120
include("special.jl")

src/multidim/intervalbox.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ IntervalBox(x...) = IntervalBox(x)
1717
IntervalBox(x) = IntervalBox(x...)
1818
IntervalBox(X::IntervalBox, n) = foldl(×, Iterators.repeated(X, n))
1919

20+
# construct from two vectors giving bottom and top corners:
21+
IntervalBox(lo::AbstractVector, hi::AbstractVector) where {N,T} = IntervalBox(force_interval.(lo, hi))
22+
23+
IntervalBox(lo::SVector{N,T}, hi::SVector{N,T}) where {N,T} = IntervalBox(force_interval.(lo, hi))
24+
25+
2026
Base.@propagate_inbounds Base.getindex(X::IntervalBox, i) = X.v[i]
2127

2228
setindex(X::IntervalBox, y, i) = IntervalBox( setindex(X.v, y, i) )

test/interval_tests/construction.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,12 @@ end
331331
@test !isequal(hash(x), hash(y))
332332

333333
end
334+
335+
import IntervalArithmetic: force_interval
336+
@testset "force_interval" begin
337+
@test force_interval(4, 3) == Interval(3, 4)
338+
@test force_interval(4, Inf) == Interval(4, Inf)
339+
@test force_interval(Inf, 4) == Interval(4, Inf)
340+
@test force_interval(Inf, -Inf) == Interval(-Inf, Inf)
341+
@test_throws ArgumentError force_interval(NaN, 3)
342+
end

test/multidim_tests/multidim.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ end
191191
@test IntervalBox([1:5...]) == IntervalBox(1..1, 2..2, 3..3, 4..4, 5..5)
192192
@test IntervalBox((1..2) × (2..3), 2) == (1..2) × (2..3) × (1..2) × (2..3)
193193

194+
# construct from corners:
195+
@test IntervalBox(SVector(1, 2), SVector(3, 4)) == (1..3) × (2..4)
196+
@test IntervalBox(SVector(3, 4), SVector(1, 2)) == (1..3) × (2..4)
197+
194198
end
195199

196200
@testset "getindex and setindex" begin

0 commit comments

Comments
 (0)