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

Issues 5 to 8 #10

Merged
merged 5 commits into from
Jun 11, 2021
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ julia> y = interval(-1,1);
julia> x / y # Standard interval arithmetic
[-∞, ∞]

julia> x1 = intervalU(x); # Convert to interval union
julia> y1 = intervalU(y);
julia> x1 = intervalUnion(x); # Convert to interval union
julia> y1 = intervalUnion(y);
julia> x1 / y1 # Does x1 / y1 for y1\{0} if 0 ∈ y1
[-∞, -2] ∪ [2, ∞]

Expand Down
6 changes: 2 additions & 4 deletions src/IntervalUnionArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ using Reexport

import Base: getindex, ∪, intersect, \, in, isequal
import Base: +, -, *, /, min, max, ^, log, <, >, exp, sin, cos, tan, sqrt
import IntervalArithmetic: hull, bisect, intersect, ⊆, ⊂

abstract type IntervalUnion{T} <: AbstractInterval{T} end
import IntervalArithmetic: union, ∪, hull, bisect, intersect, ⊆, ⊂

export
IntervalUnion, IntervalU, intervalU, remove_empties, condense, closeGaps!,
IntervalUnion, intervalUnion, remove_empties, condense, closeGaps!,
iscondensed, condense_weak, iscondensed_weak, env, hull, complement, left, right, bisect,
intersect, setdiff, \, ⊆, ⊂, isequal, in

Expand Down
26 changes: 13 additions & 13 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@
# Special case for /. Probably better with promotion rules and convert
for op in (:+, :-, :/, :*, :min, :max, :^, :log, :<, :>)

if op != :/; @eval ($op)( x::IntervalU, y::IntervalU) = intervalU([$op(xv, yv) for xv in x.v, yv in y.v][:]); end
if op != :/; @eval ($op)( x::IntervalUnion, y::IntervalUnion) = intervalUnion([$op(xv, yv) for xv in x.v, yv in y.v][:]); end

@eval ($op)( x::IntervalU, y::Interval) = intervalU(broadcast($op, x.v, y))
if op != :/; @eval ($op)( x::Interval, y::IntervalU) = intervalU(broadcast($op, x, y.v)); end
@eval ($op)( x::IntervalUnion, y::Interval) = intervalUnion(broadcast($op, x.v, y))
if op != :/; @eval ($op)( x::Interval, y::IntervalUnion) = intervalUnion(broadcast($op, x, y.v)); end

@eval ($op)( x::IntervalUnion, n::Real) = intervalUnion(broadcast($op, x.v, n))
@eval ($op)( n::Real, x::IntervalUnion) = intervalUnion(broadcast($op, n, x.v))

@eval ($op)( x::IntervalU, n::Real) = intervalU(broadcast($op, x.v, n))
@eval ($op)( n::Real, x::IntervalU) = intervalU(broadcast($op, n, x.v))

end

for op in (:-, :sin, :cos, :tan, :exp, :log)
@eval ($op)( x::IntervalU) = intervalU(broadcast($op, x.v))
@eval ($op)( x::IntervalUnion) = intervalUnion(broadcast($op, x.v))
end


# Does x/y for y\{0} if 0 ∈ y
function /(x :: IntervalU, y :: IntervalU)
function /(x :: IntervalUnion, y :: IntervalUnion)
yNew = bisect(y, 0)
return intervalU(broadcast(/, x.v, yNew.v))
return intervalUnion(broadcast(/, x.v, yNew.v))
end

function /(x :: Interval, y :: IntervalU)
function /(x :: Interval, y :: IntervalUnion)
yNew = bisect(y, 0)
return intervalU(broadcast(/, x, yNew.v))
return intervalUnion(broadcast(/, x, yNew.v))
end

function sqrt( x:: IntervalU)
function sqrt( x:: IntervalUnion)
us = sqrt.(x.v)
return intervalU([us; -us])
return intervalUnion([us; -us])
end
54 changes: 27 additions & 27 deletions src/interval_unions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,55 @@
###
# IntervalUnion constructor. Consists of a vector of intervals
###
struct IntervalU{T<:Real} <: IntervalUnion{T}
struct IntervalUnion{T<:Real} <: AbstractInterval{T}
v :: Vector{Interval{T}}
end


###
# Outer constructors
###
function intervalU(x)
x = IntervalU(x)
function intervalUnion(x)
x = IntervalUnion(x)
sort!(x.v)
x = remove_empties(x)
x = condense(x)
closeGaps!(x)
return x
end

intervalU(num :: Real) = IntervalU([interval(num)])
intervalUnion(num :: Real) = IntervalUnion([interval(num)])

intervalU(lo :: Real, hi :: Real) = IntervalU([interval(lo,hi)])
IntervalU(lo :: Real, hi :: Real) = IntervalU([interval(lo,hi)])
intervalUnion(lo :: Real, hi :: Real) = IntervalUnion([interval(lo,hi)])
IntervalUnion(lo :: Real, hi :: Real) = IntervalUnion([interval(lo,hi)])

intervalU(x :: Interval) = IntervalU([x])
∪(x :: Interval) = intervalU(x)
intervalUnion(x :: Interval) = IntervalUnion([x])
∪(x :: Interval) = intervalUnion(x)

∪(x :: Interval, y :: Interval) = intervalU([x; y])
∪(x :: Array{Interval{T}}) where T <:Real = intervalU(x)
∪(x :: Interval, y :: Interval) = intervalUnion([x; y])
∪(x :: Array{Interval{T}}) where T <:Real = intervalUnion(x)

intervalU(x :: Interval, y :: IntervalU) = intervalU([x; y.v])
∪(x :: Interval, y :: IntervalU) = intervalU(x,y)
intervalUnion(x :: Interval, y :: IntervalUnion) = intervalUnion([x; y.v])
∪(x :: Interval, y :: IntervalUnion) = intervalUnion(x,y)

intervalU(x :: IntervalU, y :: Interval) = intervalU([x.v; y])
∪(x :: IntervalU, y :: Interval) = intervalU(x,y)
intervalUnion(x :: IntervalUnion, y :: Interval) = intervalUnion([x.v; y])
∪(x :: IntervalUnion, y :: Interval) = intervalUnion(x,y)

intervalM(x :: IntervalU, y :: IntervalU) = intervalU([x.v; y.v])
∪(x :: IntervalU, y :: IntervalU) = intervalU(x,y)
intervalUnion(x :: IntervalUnion, y :: IntervalUnion) = intervalUnion([x.v; y.v])
∪(x :: IntervalUnion, y :: IntervalUnion) = intervalUnion(x,y)

# MultiInterval can act like a vector
getindex(x :: IntervalU, ind :: Integer) = getindex(x.v,ind)
getindex(x :: IntervalU, ind :: Array{ <: Integer}) = getindex(x.v,ind)
getindex(x :: IntervalUnion, ind :: Integer) = getindex(x.v,ind)
getindex(x :: IntervalUnion, ind :: Array{ <: Integer}) = getindex(x.v,ind)

# Remove ∅ from IntervalUnion
function remove_empties(x :: IntervalU)
function remove_empties(x :: IntervalUnion)
v = x.v
Vnew = v[v .!= ∅]
return IntervalU(Vnew)
return IntervalUnion(Vnew)
end

function closeGaps!(x :: IntervalU, maxInts = MAXINTS[1])
function closeGaps!(x :: IntervalUnion, maxInts = MAXINTS[1])

while length(x.v) > maxInts # Global

Expand All @@ -97,7 +97,7 @@ function closeGaps!(x :: IntervalU, maxInts = MAXINTS[1])
end

# Recursively envolpe intervals which intersect.
function condense(x :: IntervalU)
function condense(x :: IntervalUnion)

if iscondensed(x); return x; end

Expand All @@ -112,10 +112,10 @@ function condense(x :: IntervalU)

push!(Vnew, hull(v[these]))
end
return condense( intervalU(Vnew) )
return condense( intervalUnion(Vnew) )
end

function iscondensed(x :: IntervalU)
function iscondensed(x :: IntervalUnion)
v = sort(x.v)
for i=1:length(v)
intersects = findall( intersect.(v[i],v[1:end .!= i]) .!= ∅)
Expand All @@ -125,7 +125,7 @@ function iscondensed(x :: IntervalU)
end

# Recursively envolpe intervals which intersect, except those which touch
function condense_weak(x :: IntervalU)
function condense_weak(x :: IntervalUnion)

if iscondensed(x); return x; end

Expand All @@ -145,10 +145,10 @@ function condense_weak(x :: IntervalU)

push!(Vnew, hull(v[them]))
end
return condense( intervalU(Vnew) )
return condense( intervalUnion(Vnew) )
end

function iscondensed_weak(x :: IntervalU)
function iscondensed_weak(x :: IntervalUnion)
v = sort(x.v)
for i=1:length(v)
intersects = intersect.(v[i],v[1:end .!= i])
Expand Down
52 changes: 26 additions & 26 deletions src/set_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
###

# Envolpe/hull. Keeps it as a IntervalUnion
env(x :: IntervalU) = IntervalU([hull(x.v)])
hull(x :: IntervalU) = IntervalU([hull(x.v)])
env(x :: IntervalUnion) = IntervalUnion([hull(x.v)])
hull(x :: IntervalUnion) = IntervalUnion([hull(x.v)])


# Computes the complement of a IntervalUnion
function complement(x :: IntervalU)
function complement(x :: IntervalUnion)

v = sort(x.v)
vLo = left.(v)
Expand All @@ -20,14 +20,14 @@ function complement(x :: IntervalU)

complements = interval.(vHi,vLo)

return intervalU(complements)
return intervalUnion(complements)
end

complement(x :: Interval) = complement(intervalU(x))
complement(x :: Interval) = complement(intervalUnion(x))


# bisect x at α
function bisect( x :: IntervalU, α = 0)
function bisect( x :: IntervalUnion, α = 0)

v = deepcopy(x.v)

Expand All @@ -42,69 +42,69 @@ function bisect( x :: IntervalU, α = 0)

bs = bisect(this, β)

new = IntervalU(v ∪ bs[1] ∪ bs[2])
new = IntervalUnion(v ∪ bs[1] ∪ bs[2])
new = remove_empties(new)
sort!(new.v)
return new

end


function intersect(x :: IntervalU, y :: IntervalU)
function intersect(x :: IntervalUnion, y :: IntervalUnion)
intersects = [intersect(xv, yv) for xv in x.v, yv in y.v]
if all(intersects .== ∅); return interval(∅); end
return intervalU(intersects[:])
if all(intersects .== ∅); return ; end
return intervalUnion(intersects[:])
end

function intersect(x :: IntervalU, y :: Interval)
function intersect(x :: IntervalUnion, y :: Interval)
intersects = [intersect(xv, y) for xv in x.v]
if all(intersects .== ∅); return interval(∅); end
return intervalU(intersects[:])
if all(intersects .== ∅); return ; end
return intervalUnion(intersects[:])
end

function intersect(x :: Interval, y :: IntervalU)
function intersect(x :: Interval, y :: IntervalUnion)
intersects = [intersect(x, yv) for yv in y.v]
if all(intersects .== ∅); return interval(∅); end
return intervalU(intersects[:])
if all(intersects .== ∅); return ; end
return intervalUnion(intersects[:])
end


function setdiff(x :: IntervalU, y :: IntervalU)
function setdiff(x :: IntervalUnion, y :: IntervalUnion)
yc = complement(y)
return intersect(x, yc)
end

\(x :: IntervalU, y :: IntervalU) = setdiff(x, y)
\(x :: Interval, y :: IntervalU) = setdiff(intervalU(x), y)
\(x :: IntervalU, y :: Interval) = setdiff(x, intervalU(y))
\(x :: IntervalUnion, y :: IntervalUnion) = setdiff(x, y)
\(x :: Interval, y :: IntervalUnion) = setdiff(intervalUnion(x), y)
\(x :: IntervalUnion, y :: IntervalUnion) = setdiff(x, intervalUnion(y))

function ⊂(x :: IntervalU, y :: IntervalU)
function ⊂(x :: IntervalUnion, y :: IntervalUnion)
issubs = [xv .⊂ y.v for xv in x.v]
return all(any.(issubs))
end

function ⊂(x :: Interval, y :: IntervalU)
function ⊂(x :: Interval, y :: IntervalUnion)
if length(y.v) == 1; return x ⊂ y; end
issubs = x .⊆ y.v
return any(issubs)
end

function ⊂( x::IntervalU, y :: Interval)
function ⊂( x::IntervalUnion, y :: Interval)
issubs = x.v .⊂ y
return all(issubs)
end

function ⊆(x :: IntervalU, y :: IntervalU)
function ⊆(x :: IntervalUnion, y :: IntervalUnion)
issubs = [xv .⊆ y.v for xv in x.v]
return all(any.(issubs))
end

function ⊆(x :: Interval, y :: IntervalU)
function ⊆(x :: Interval, y :: IntervalUnion)
issubs = x .⊆ y.v
return any(issubs)
end

function ⊆( x::IntervalU, y :: Interval)
function ⊆( x::IntervalUnion, y :: Interval)
issubs = x.v .⊆ y
return all(issubs)
end
20 changes: 12 additions & 8 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@
left(x :: Interval) = x.lo
right(x :: Interval) = x.hi

left(x :: IntervalU) = left.(x.v)
right(x :: IntervalU) = right.(x.v)
left(x :: IntervalUnion) = left.(x.v)
right(x :: IntervalUnion) = right.(x.v)

isequal(x :: IntervalU, y ::IntervalU) = all(isequal.(sort(x.v),sort(y.v)))
isequal(x :: IntervalUnion, y ::IntervalUnion) = all(isequal.(sort(x.v),sort(y.v)))

function in( x:: IntervalU, y :: Vector{IntervalU})
function in( x:: IntervalUnion, y :: Vector{IntervalUnion})
for i =1:length(y)
if x == y[i]
return true
end
end
return false
end
end


function Base.show(io::IO, this::IntervalU)
v = sort(this.v)
print(io, join(v, " ∪ "));
function Base.show(io::IO, this::IntervalUnion)
if length(this.v) == 1;
print(io, "$(this.v[1])ᵤ");
else
v = sort(this.v)
print(io, join(v, " ∪ "));
end
end