Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit eecfab5

Browse files
dpsanderslbenet
authored andcommitted
More efficient intersection and union for boxes (#196)
* More efficient intersection and union for boxes * Improve subset for boxes * Add a couple of tests for intersection and union * Make union(a,b) and hull(a,b) more specific to only work with intervals * Fix union and hull * Import union from Base!
1 parent f38c8b3 commit eecfab5

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/ValidatedNumerics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Base:
2020
union, intersect, isempty,
2121
convert, promote_rule, eltype,
2222
BigFloat, float, widen, big,
23-
, , eps,
23+
, , , eps,
2424
floor, ceil, trunc, sign, round,
2525
expm1, log1p,
2626
precision,

src/intervals/set_operations.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ all of `a` and `b`.
6969
"""
7070
hull{T}(a::Interval{T}, b::Interval{T}) = Interval{T}(min(a.lo, b.lo), max(a.hi, b.hi))
7171

72-
hull(a, b) = hull(promote(a, b)...)
72+
hull{T,S}(a::Interval{T}, b::Interval{S}) = hull(promote(a, b)...)
7373

7474
"""
7575
union(a, b)
@@ -80,7 +80,7 @@ to `hull(a,b)`.
8080
"""
8181
union{T}(a::Interval{T}, b::Interval{T}) = hull(a, b)
8282

83-
union(a, b) = union(promote(a, b)...)
83+
union{T,S}(a::Interval{T}, b::Interval{S}) = union(promote(a, b)...)
8484

8585

8686
doc"""

src/multidim/intervalbox.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,25 @@ mid(X::IntervalBox) = [mid(x) for x in X]
1919

2020
## set operations
2121

22-
(X::IntervalBox, Y::IntervalBox) = all([x y for (x,y) in zip(X, Y)])
22+
# TODO: Update to use generator
23+
{N,T}(X::IntervalBox{N,T}, Y::IntervalBox{N,T}) = all(i->(X[i] Y[i]), 1:N)
24+
# all(X[i] ⊆ Y[i] for i in 1:N) # on Julia 0.6
25+
26+
{N,T}(X::IntervalBox{N,T}, Y::IntervalBox{N,T}) = IntervalBox(ntuple(i -> X[i] Y[i], Val{N}))
27+
{N,T}(X::IntervalBox{N,T}, Y::IntervalBox{N,T}) = IntervalBox(ntuple(i -> X[i] Y[i], Val{N}))
28+
29+
#=
30+
On Julia 0.6 can now write
31+
∩{N,T}(X::IntervalBox{N,T}, Y::IntervalBox{N,T}) = IntervalBox(NTuple{N, Interval{Float64}}( (X[i] ∩ Y[i]) for i in 1:N))
32+
=#
2333

24-
(X::IntervalBox, Y::IntervalBox) = IntervalBox([x y for (x,y) in zip(X, Y)]...)
2534

2635
isempty(X::IntervalBox) = any(isempty, X)
2736

37+
# TODO: Replace with generator in 0.5:
2838
diam(X::IntervalBox) = maximum([diam(x) for x in X])
2939

30-
emptyinterval(X::IntervalBox) = IntervalBox(map(emptyinterval, X))
40+
emptyinterval{N,T}(X::IntervalBox{N,T}) = IntervalBox(ntuple(i->emptyinterval(T), Val{N}))
3141

3242

3343
import Base

test/multidim_tests/multidim.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ facts("Operations on boxes") do
1010
@fact dot(A, B) --> @interval(9, 28)
1111

1212
@fact A B --> true
13+
@fact A B --> A
14+
@fact A B --> B
1315

1416
X = IntervalBox(1..2, 3..4)
1517
Y = IntervalBox(3..4, 3..4)
1618

1719
@fact isempty(X Y) --> true
20+
@fact X Y --> IntervalBox(1..4, 3..4)
21+
22+
X = IntervalBox(2..4, 3..5)
23+
Y = IntervalBox(3..5, 4..17)
24+
@fact X Y --> IntervalBox(3..4, 4..5)
1825

1926
v = [@interval(i, i+1) for i in 1:10]
2027
V = IntervalBox(v...)

0 commit comments

Comments
 (0)