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

Rewrite IntervalBox to contain an SVector of Intervals #152

Merged
merged 17 commits into from
Jun 3, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 2 additions & 1 deletion src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import Base:
isfinite, isnan,
show, showall,
isinteger, setdiff,
parse
parse,
broadcast, dot, length

export
AbstractInterval, Interval,
Expand Down
4 changes: 2 additions & 2 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ function representation(X::IntervalBox, format=nothing)
end

if display_params.format == :full
return string("IntervalBox(", join(X, ", "), ")")
return string("IntervalBox(", join(X.v, ", "), ")")

else
return join(X, " × ")
return join(X.v, " × ")
end

end
Expand Down
18 changes: 18 additions & 0 deletions src/multidim/arithmetic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is part of the IntervalArithmetic.jl package; MIT licensed

+(a::IntervalBox, b::IntervalBox) = IntervalBox( a.v .+ b.v )
+(a::IntervalBox, b::Real) = IntervalBox( a.v .+ b )
+(a::Real, b::IntervalBox) = IntervalBox( a .+ b.v )

-(a::IntervalBox, b::IntervalBox) = IntervalBox( a.v .- b.v )
-(a::IntervalBox, b::Real) = IntervalBox( a.v .- b )
-(a::Real, b::IntervalBox) = IntervalBox( a .- b.v )
-(a::IntervalBox) = IntervalBox( .- a.v )

*(a::IntervalBox, b::Real) = IntervalBox( a.v .* b )
*(a::Real, b::IntervalBox) = IntervalBox( a .* b.v )

/(a::IntervalBox, b::Real) = IntervalBox( a.v ./ b )

Base.broadcast(f, X::IntervalBox) = IntervalBox(f.(X.v))
Base.broadcast(f, X::IntervalBox, Y::IntervalBox) = IntervalBox( f.(X.v, Y.v) )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I truly think that this broadcasting change is very nice, I just noticed that, in order to use a function (say exp) on an IntervalBox, you do require the dot-notation. The following uses the current commit (93bb8e0) of this PR:

julia> A = IntervalBox(1..2, 3..4)
[1, 2] × [3, 4]

julia> exp.(A) # this is implemented here
[2.71828, 7.38906] × [20.0855, 54.5982]

julia> exp(A) # throwing an error here is uncomfortable!
ERROR: MethodError: no method matching exp(::IntervalArithmetic.IntervalBox{2,Float64})
Closest candidates are:
  exp(::Float16) at math.jl:950
  exp(::Complex{Float16}) at math.jl:951
  exp(::BigFloat) at mpfr.jl:517
  ...

This is sort of uncomfortable, because you force the user to write whatever functions using the dot notation instead of the usual functions. This is actually not a problem, a priori, but then it should be explicitly written somewhere. My idea with #151 was to avoid this, i.e., use directly exp(A).

Should we allow exp(A) to simply return exp.(A), or shall we leave it as it is now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Julia has moved to requiring exp.(v) to make explicit that you want to apply exp element-wise, since exp of a vector does not make sense.

I don't understand the context where exp of an IntervalBox makes sense. I don't actually understand your Taylor example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Julia has moved to requiring exp.(v) to make explicit that you want to apply exp element-wise, since exp of a vector does not make sense.

Fair enough.

35 changes: 19 additions & 16 deletions src/multidim/intervalbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,56 @@
"""An `IntervalBox` is an `N`-dimensional rectangular box, given
by a Cartesian product of `N` `Interval`s.
"""
struct IntervalBox{N,T} <: StaticVector{N, Interval{T}}
data::NTuple{N,Interval{T}}
struct IntervalBox{N,T}
v::SVector{N,Interval{T}}
end

# StaticArrays.Size{N,T}(::Type{IntervalBox{N,T}}) = StaticArrays.Size(N) # @pure not needed, I think...
Base.@propagate_inbounds Base.getindex(a::IntervalBox, i::Int) = a.data[i]
# IntervalBox(x::Interval) = IntervalBox( SVector(x) ) # single interval treated as tuple with one element

IntervalBox(x::Interval...) = IntervalBox(SVector(x))
IntervalBox(x::Tuple{T}) where {T<:Interval} = IntervalBox(SVector(x))

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

Base.getindex(X::IntervalBox, i) = X.v[i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This replaces what it was in line 11, before; do we need Base.@propagate_inbounds here, or not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. To be honest I'm not sure, but it probably doesn't hurt.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure either... I've never understood why it was needed. Proceed as you prefer.


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

mid(X::IntervalBox) = mid.(X)
mid(X::IntervalBox) = mid.(X.v)


## set operations

# TODO: Update to use generator
⊆(X::IntervalBox{N,T}, Y::IntervalBox{N,T}) where {N,T} =
all(X .⊆ Y)
all(X.v .⊆ Y.v)

∩(X::IntervalBox{N,T}, Y::IntervalBox{N,T}) where {N,T} =
IntervalBox(X .∩ Y)
IntervalBox(X.v .∩ Y.v)
∪(X::IntervalBox{N,T}, Y::IntervalBox{N,T}) where {N,T} =
IntervalBox(X .∪ Y)
IntervalBox(X.v .∪ Y.v)

#=
On Julia 0.6 can now write
∩{N,T}(X::IntervalBox{N,T}, Y::IntervalBox{N,T}) = IntervalBox(NTuple{N, Interval{Float64}}( (X[i] ∩ Y[i]) for i in 1:N))
=#


isempty(X::IntervalBox) = any(isempty, X)
isempty(X::IntervalBox) = any(isempty, X.v)

diam(X::IntervalBox) = maximum(diam.(X))
diam(X::IntervalBox) = maximum(diam.(X.v))

emptyinterval(X::IntervalBox{N,T}) where {N,T} = IntervalBox(emptyinterval.(X))
emptyinterval(X::IntervalBox{N,T}) where {N,T} = IntervalBox(emptyinterval.(X.v))


import Base.×
×(a::Interval...) = IntervalBox(a...)
×(a::Interval, b::IntervalBox) = IntervalBox(a, b...)
×(a::IntervalBox, b::Interval) = IntervalBox(a..., b)
×(a::IntervalBox, b::IntervalBox) = IntervalBox(a..., b...)
×(a::Interval, b::IntervalBox) = IntervalBox(a, b.v...)
×(a::IntervalBox, b::Interval) = IntervalBox(a.v..., b)
×(a::IntervalBox, b::IntervalBox) = IntervalBox(a.v..., b.v...)

IntervalBox(x::Interval, ::Type{Val{n}}) where {n} = IntervalBox(SVector(ntuple(i->x, Val{n})))

IntervalBox(x::Interval, n::Int) = IntervalBox(x, Val{n})

dot(x::IntervalBox, y::IntervalBox) = dot(x.v, y.v)
length(x::IntervalBox) = length(x.v)
1 change: 1 addition & 0 deletions src/multidim/multidim.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include("intervalbox.jl")
include("setdiff.jl")
include("arithmetic.jl")
4 changes: 2 additions & 2 deletions src/multidim/setdiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Algorithm: Start from the total overlap (in all directions);
expand each direction in turn.
"""
function setdiff(A::IntervalBox{N,T}, B::IntervalBox{N,T}) where {N,T}
X = [labelled_setdiff(a,b) for (a, b) in zip(A, B)]
X = [labelled_setdiff(a, b) for (a, b) in zip(A.v, B.v)]
# ordered such that the first in each is the excluded interval

first = [ i[1] for i in X ]
Expand All @@ -61,7 +61,7 @@ function setdiff(A::IntervalBox{N,T}, B::IntervalBox{N,T}) where {N,T}
for which in X[dimension][2:end]
excluded[dimension] = which[1]
push!(result_list,
IntervalBox(excluded[1:dimension]..., A[dimension+1:end]...))
IntervalBox(excluded[1:dimension]..., A[dimension+1:N]...))
end
end

Expand Down
24 changes: 23 additions & 1 deletion test/multidim_tests/multidim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@ using Base.Test
A = IntervalBox(1..2, 3..4)
B = IntervalBox(0..2, 3..6)

@test 2*A == IntervalBox(2..4, 6..8)
@test 2*A == A*2 == IntervalBox(2..4, 6..8)
@test typeof(2*A) == IntervalBox{2, Float64}
@test A + B == IntervalBox(1..4, 6..10)
@test 2 + A == IntervalBox(3..4,5..6)
@test A + 2 == IntervalBox(3..4,5..6)
@test -A == IntervalBox((-2)..(-1), (-4)..(-3))
@test 2 - A == IntervalBox(0..1, (-2)..(-1))
@test B - 2 == IntervalBox((-2)..0, 1..4)
@test dot(A, B) == @interval(9, 28)
@test A .* B == IntervalBox(0..4, 9..24)
@test A ./ A == IntervalBox((0.5)..2, (0.75)..(4/3))
@test 1 ./ B == IntervalBox((0.5)..Inf, (1/6)..(1/3))
@test B ./ 1 == B
@test A .^ 2 == IntervalBox(1..4, 9..16)
@test B .^ 0.5 == IntervalBox(@interval(0,sqrt(2)), @interval(sqrt(3),sqrt(6)))

@test A ⊆ B
@test A ∩ B == A
Expand All @@ -32,7 +44,17 @@ using Base.Test
@test isa(Y, IntervalBox)
@test length(Y) == 1
@test Y == IntervalBox( (Interval(1., 2.),) )
@test typeof(Y) == IntervalBox{1, Float64}
end

@testset "Functions on boxes" begin
A = IntervalBox(1..2, 3..4)

@test exp.(A) == IntervalBox(exp(A[1]), exp(A[2]))
@test typeof(exp.(A)) == IntervalBox{2,Float64}
@test log.(A) == IntervalBox(log(A[1]), log(A[2]))
@test sqrt.(A) == IntervalBox(sqrt(A[1]), sqrt(A[2]))
@test inv.(A) == IntervalBox(inv(A[1]), inv(A[2]))
end

# @testset "@intervalbox tests" begin
Expand Down