Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working n-dimensional setdiff #144

Merged
merged 3 commits into from
May 30, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/ValidatedNumerics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Base:
precision,
isfinite, isnan,
show, showall,
isinteger
isinteger, setdiff

export
Interval, AbstractInterval,
Expand Down
3 changes: 0 additions & 3 deletions src/intervals/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ function ^(a::Interval{BigFloat}, x::Interval)
end


Base.inf(x::Rational) = 1//0 # to allow sqrt()


function sqrt{T}(a::Interval{T})
domain = Interval(zero(T), convert(T, Inf))
a = a domain
Expand Down
105 changes: 78 additions & 27 deletions src/multidim/setdiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,98 @@ function labelled_setdiff{T}(x::Interval{T}, y::Interval{T})
isempty(intersection) && return [(x, -1)]
intersection == x && return [(x, 1)]

x.lo == intersection.lo && return [(intersection,1), (Interval(intersection.hi, x.hi), 0)]
x.hi == intersection.hi && return [(Interval(x.lo, intersection.lo), 0), (intersection, 1)]
x.lo == intersection.lo && return [(intersection, 1), (Interval(intersection.hi, x.hi), 0)]
x.hi == intersection.hi && return [(intersection, 1), (Interval(x.lo, intersection.lo), 0)]

return [(Interval(x.lo, y.lo), 0),
(y, 1),
return [(y, 1),
(Interval(x.lo, y.lo), 0),
(Interval(y.hi, x.hi), 0)]

end

# function setdiff{N,T}(A::IntervalBox{N,T}, B::IntervalBox{N,T})
# X = [labelled_setdiff(a,b) for (a, b) in zip(A, B)]
# lengths = map(length, X)
# index = ones(N)
#
# # index[j] represents which set we are looking at in direction j
#``
#
# while index[1] <= N
# current_direction = 1
# current_piece = [ X[1][index[1]] ]
#
# end
# end
doc"""
setdiff(A::IntervalBox{2,T}, B::IntervalBox{2,T})
setdiff(A::IntervalBox{N,T}, B::IntervalBox{N,T})

Returns a vector of `IntervalBox`es that are in the set difference `A \ B`,
i.e. the set of `x` that are in `A` but not in `B`.

Algorithm: Start from the total overlap (in all directions);
expand each direction in turn.
"""
function setdiff{T}(A::IntervalBox{2,T}, B::IntervalBox{2,T})
X = labelled_setdiff(A[1], B[1])
Y = labelled_setdiff(A[2], B[2])

results_list = typeof(A)[]

for (x, label) in X
label == -1 && return [A] # intersection in one direction empty, so total intersection empty

if label == 0
push!(results_list, IntervalBox(x, A[2]))
continue
end
function setdiff{N,T}(A::IntervalBox{N,T}, B::IntervalBox{N,T})
X = [labelled_setdiff(a,b) for (a, b) in zip(A, B)]
# ordered such that the first in each is the excluded interval

first = [ i[1] for i in X ]
labels = [i[2] for i in first]

any(labels .== -1) && return [A] # no overlap

# label is 1 here, so there is some intersection in the x direction
for (y, label) in Y
label == -1 && return [A]
# @assert all(labels .== 1)

if label == 0
push!(results_list, IntervalBox(x, y))
continue
end
excluded = [i[1] for i in first]

# label == 1: exclude this box since all labels are 1
result_list = IntervalBox{N,T}[]

for dimension in N:-1:1
for which in X[dimension][2:end]
excluded[dimension] = which[1]
push!(result_list,
IntervalBox(excluded[1:dimension]..., A[dimension+1:end]...))
end
end

return results_list
result_list

end


# doc"""
# setdiff(A::IntervalBox{2,T}, B::IntervalBox{2,T})
#
# Returns a vector of `IntervalBox`es that are in the set difference `A \ B`,
# i.e. the set of `x` that are in `A` but not in `B`.
# """
# function setdiff{T}(A::IntervalBox{2,T}, B::IntervalBox{2,T})
# X = labelled_setdiff(A[1], B[1])
# Y = labelled_setdiff(A[2], B[2])
#
# results_list = typeof(A)[]
#
# for (x, label) in X
# label == -1 && return [A] # intersection in one direction empty, so total intersection empty
#
# if label == 0
# push!(results_list, IntervalBox(x, A[2]))
# continue
# end
#
# # label is 1 here, so there is some intersection in the x direction
# for (y, label) in Y
# label == -1 && return [A]
#
# if label == 0
# push!(results_list, IntervalBox(x, y))
# continue
# end
#
# # label == 1: exclude this box since all labels are 1
# end
# end
#
# return results_list
# end
4 changes: 0 additions & 4 deletions test/interval_tests/consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,3 @@ facts("Interval power of an interval") do
@fact b^@interval(0.3) == Interval(1.3903891703159093, 1.5157165665103982) --> true

end

facts("Rational infinity") do
@fact inf(3//4) == 1//0 --> true
end
48 changes: 48 additions & 0 deletions test/multidim_tests/multidim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,54 @@ facts("@intervalbox tests") do

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

facts("setdiff for IntervalBox") do
X = IntervalBox(2..4, 3..5)
Y = IntervalBox(3..5, 4..6)
@fact setdiff(X, Y) --> [ IntervalBox(3..4, 3..4),
IntervalBox(2..3, 3..5) ]


X = IntervalBox(2..5, 3..6)
Y = IntervalBox(-10..10, 4..5)
@fact setdiff(X, Y) --> [ IntervalBox(2..5, 3..4),
IntervalBox(2..5, 5..6) ]


X = IntervalBox(2..5, 3..6)
Y = IntervalBox(4..6, 4..5)
@fact setdiff(X, Y) --> [ IntervalBox(4..5, 3..4),
IntervalBox(4..5, 5..6),
IntervalBox(2..4, 3..6) ]


X = IntervalBox(2..5, 3..6)
Y = IntervalBox(3..4, 4..5)
@fact setdiff(X, Y) --> [ IntervalBox(3..4, 3..4),
IntervalBox(3..4, 5..6),
IntervalBox(2..3, 3..6),
IntervalBox(4..5, 3..6) ]


X = IntervalBox(2..5, 3..6)
Y = IntervalBox(2..4, 10..20)
@fact setdiff(X, Y) --> typeof(X)[X]


X = IntervalBox(2..5, 3..6)
Y = IntervalBox(-10..10, -10..10)
@fact setdiff(X, Y) --> typeof(X)[]


X = IntervalBox(1..4, 3..6, 7..10)
Y = IntervalBox(2..3, 4..5, 8..9)
@fact setdiff(X, Y) --> [ IntervalBox(2..3, 4..5, 7..8),
IntervalBox(2..3, 4..5, 9..10),
IntervalBox(2..3, 3..4, 7..10),
IntervalBox(2..3, 5..6, 7..10),
IntervalBox(1..2, 3..6, 7..10),
IntervalBox(3..4, 3..6, 7..10) ]


end