Skip to content

Commit

Permalink
Empty intervalbox (#145)
Browse files Browse the repository at this point in the history
* Add emptyinterval(x::IntervalBox)

* Rewrite a couple of IntervalBox functions; better structure for intervalbox.jl

* Add test for emptyinterval(IntervalBox)
  • Loading branch information
dpsanders committed Jun 4, 2016
1 parent 67f3b9a commit 005b38c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/multidim/intervalbox.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
# Multidimensional intervals, called IntervalBox
# This file is part of the ValidatedNumerics.jl package; MIT licensed

doc"""An `IntervalBox` is an $N$-dimensional rectangular box, given
by a Cartesian product of $N$ `Interval`s.
"""

doc"""An `IntervalBox` is a Cartesian product of an arbitrary number of `Interval`s,
representing an $N$-dimensional rectangular IntervalBox."""

immutable IntervalBox{N, T} <: FixedVector{N, Interval{T}} # uses FixedSizeArrays package
immutable IntervalBox{N, T} <: FixedVector{N, Interval{T}} # uses FixedSizeArrays.jl package
_ :: NTuple{N, Interval{T}}
end

IntervalBox(x::Interval) = IntervalBox( (x,) ) # single interval treated as tuple with one element


## arithmetic operations
# Note that standard arithmetic operations are implemented automatically by FixedSizeArrays.jl

mid(X::IntervalBox) = [mid(x) for x in X]


## set operations

(X::IntervalBox, Y::IntervalBox) = all([x y for (x,y) in zip(X, Y)])

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

isempty(X::IntervalBox) = any([isempty(x) for x in X])
isempty(X::IntervalBox) = any(isempty, X)

diam(X::IntervalBox) = maximum([diam(x) for x in X])

emptyinterval(X::IntervalBox) = IntervalBox(map(emptyinterval, X))


## printing

function show(io::IO, X::IntervalBox)
for (i, x) in enumerate(X)
Expand Down
3 changes: 3 additions & 0 deletions test/multidim_tests/multidim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ facts("@intervalbox tests") do

@intervalbox g(x, y) = x - y
@fact isa(g(X), IntervalBox) --> true

@fact emptyinterval(X) --> IntervalBox(∅, ∅)

end

facts("setdiff for IntervalBox") do
Expand Down

0 comments on commit 005b38c

Please sign in to comment.