Skip to content

Commit 59ef14d

Browse files
eeshan9815dpsanders
authored andcommitted
Add interoperability between IntervalBox and SVector{Interval} (#166)
* Add interoperability between IntervalBox and SVector{Interval} * Add interop for SVector{Real} and IntervalBox * Simplify methods and add constructor * Add tests and more constructors * Add constructor for IntervalBox(X::IntervalBox, n)
1 parent e6e0d0d commit 59ef14d

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/intervals/intervals.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function interval(a::Real, b::Real)
107107
end
108108

109109
interval(a::Real) = interval(a, a)
110+
interval(a::Interval) = a
110111

111112

112113
## Include files

src/multidim/arithmetic.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ wrap(v) = v
2424
Base.broadcast(f, X::IntervalBox) = wrap(f.(X.v))
2525
Base.broadcast(f, X::IntervalBox, Y::IntervalBox) = wrap(f.(X.v, Y.v))
2626
Base.broadcast(f, X::IntervalBox, y) = wrap(f.(X.v, y))
27+
28+
for op in (:+, :-, :, :, :, :isinterior, :dot, :setdiff, :×)
29+
@eval $(op)(a::SVector, b::IntervalBox) = $(op)(IntervalBox(a), b)
30+
end
31+
32+
for op in (:+, :-, :, :, :, :isinterior, :dot, :setdiff, :×)
33+
@eval $(op)(a::IntervalBox, b::SVector) = $(op)(a, IntervalBox(b))
34+
end

src/multidim/intervalbox.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ end
1010
# IntervalBox(x::Interval) = IntervalBox( SVector(x) ) # single interval treated as tuple with one element
1111

1212
IntervalBox(x::Interval...) = IntervalBox(SVector(x))
13-
IntervalBox(x::Tuple{T}) where {T<:Interval} = IntervalBox(SVector(x))
13+
IntervalBox(x::SVector) = IntervalBox(interval.(x))
14+
IntervalBox(x::Tuple) = IntervalBox(SVector(x))
15+
IntervalBox(x::Real) = IntervalBox(interval.(x))
16+
IntervalBox(x...) = IntervalBox(x)
17+
IntervalBox(x) = IntervalBox(x...)
18+
IntervalBox(X::IntervalBox, n) = foldl(×, Iterators.repeated(X, n))
1419

1520
Base.@propagate_inbounds Base.getindex(X::IntervalBox, i) = X.v[i]
1621

test/multidim_tests/multidim.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@ end
1212
@testset "Operations on boxes" begin
1313
A = IntervalBox(1..2, 3..4)
1414
B = IntervalBox(0..2, 3..6)
15+
s = @SVector [1, 2]
1516

1617
@test 2*A == A*2 == IntervalBox(2..4, 6..8)
1718
@test typeof(2*A) == IntervalBox{2, Float64}
1819
@test A + B == IntervalBox(1..4, 6..10)
20+
@test A + B.v == IntervalBox(1..4, 6..10)
21+
@test A.v + B == IntervalBox(1..4, 6..10)
22+
@test A + s == IntervalBox(2..3, 5..6)
23+
@test A - B == IntervalBox(-1..2, -3..1)
24+
@test A.v - B == IntervalBox(-1..2, -3..1)
25+
@test A - B.v == IntervalBox(-1..2, -3..1)
26+
@test A - s == IntervalBox(0..1, 1..2)
1927
@test 2 + A == IntervalBox(3..4,5..6)
2028
@test A + 2 == IntervalBox(3..4,5..6)
2129
@test -A == IntervalBox((-2)..(-1), (-4)..(-3))
2230
@test 2 - A == IntervalBox(0..1, (-2)..(-1))
2331
@test B - 2 == IntervalBox((-2)..0, 1..4)
2432
@test dot(A, B) == @interval(9, 28)
33+
@test dot(A, B.v) == @interval(9, 28)
34+
@test dot(A.v, B) == @interval(9, 28)
2535
@test A .* B == IntervalBox(0..4, 9..24)
2636
@test A ./ A == IntervalBox((0.5)..2, (0.75)..(4/3))
2737
@test 1 ./ B == IntervalBox((0.5)..Inf, (1/6)..(1/3))
@@ -30,8 +40,16 @@ end
3040
@test B .^ 0.5 == IntervalBox(@interval(0,sqrt(2)), @interval(sqrt(3),sqrt(6)))
3141

3242
@test A B
43+
@test A.v B
44+
@test A B.v
45+
3346
@test A B == A
47+
@test A.v B == A
48+
@test A B.v == A
49+
3450
@test A B == B
51+
@test A.v B == B
52+
@test A B.v == B
3553

3654
X = IntervalBox(1..2, 3..4)
3755
Y = IntervalBox(3..4, 3..4)
@@ -88,6 +106,11 @@ end
88106
@test setdiff(X, Y) == [ IntervalBox(3..4, 3..4),
89107
IntervalBox(2..3, 3..5) ]
90108

109+
@test setdiff(X.v, Y) == [ IntervalBox(3..4, 3..4),
110+
IntervalBox(2..3, 3..5) ]
111+
112+
@test setdiff(X, Y.v) == [ IntervalBox(3..4, 3..4),
113+
IntervalBox(2..3, 3..5) ]
91114

92115
X = IntervalBox(2..5, 3..6)
93116
Y = IntervalBox(-10..10, 4..5)
@@ -146,6 +169,24 @@ end
146169
@test Y == IntervalBox(Interval(0, 2), Interval(3, 5), Interval(4, 8))
147170
@test diam(Y) == 4
148171

172+
Z = X × Y
173+
@test isa(Z, IntervalBox)
174+
@test length(Z) == 5
175+
@test Z == IntervalBox(Interval(0, 2), Interval(3, 5), Interval(0, 2), Interval(3, 5), Interval(4, 8))
176+
@test diam(Z) == 4
177+
178+
Z = X × Y.v
179+
@test isa(Z, IntervalBox)
180+
@test length(Z) == 5
181+
@test Z == IntervalBox(Interval(0, 2), Interval(3, 5), Interval(0, 2), Interval(3, 5), Interval(4, 8))
182+
@test diam(Z) == 4
183+
184+
Z = X.v × Y
185+
@test isa(Z, IntervalBox)
186+
@test length(Z) == 5
187+
@test Z == IntervalBox(Interval(0, 2), Interval(3, 5), Interval(0, 2), Interval(3, 5), Interval(4, 8))
188+
@test diam(Z) == 4
189+
149190
@test mid(IntervalBox(0..1, 3), 0.75) == [0.75, 0.75, 0.75]
150191
end
151192

@@ -155,6 +196,16 @@ end
155196
@test IntervalBox(1..2, Val{5}) == (1..2) × (1..2) × (1..2) × (1..2) × (1..2)
156197

157198
@test IntervalBox(1..2, 3) == IntervalBox(1..2, Val{3})
199+
@test IntervalBox((1..2, 2..3)) == IntervalBox(1..2, 2..3)
200+
@test IntervalBox((1, 2)) == IntervalBox(1..1, 2..2)
201+
@test IntervalBox( (1, 2, 3) ) == IntervalBox(1..1, 2..2, 3..3)
202+
@test IntervalBox( (1, 2, 3.1) ) == IntervalBox(1..1, 2..2, interval(3.1))
203+
@test IntervalBox( SVector(1, 2, 3.1) ) == IntervalBox(1..1, 2..2, interval(3.1))
204+
@test IntervalBox( interval.(SVector(1, 2, 3.1)) ) == IntervalBox(1..1, 2..2, interval(3.1))
205+
@test IntervalBox(3) == IntervalBox(3..3)
206+
@test IntervalBox(1:5) == IntervalBox(1..1, 2..2, 3..3, 4..4, 5..5)
207+
@test IntervalBox([1:5...]) == IntervalBox(1..1, 2..2, 3..3, 4..4, 5..5)
208+
@test IntervalBox((1..2) × (2..3), 2) == IntervalBox((1..2) × (2..3) × (1..2) × (2..3))
158209

159210
end
160211

0 commit comments

Comments
 (0)