-
Notifications
You must be signed in to change notification settings - Fork 71
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
Changes from 10 commits
f0bda2a
171a7a9
e0114fb
c3c6439
f4bf99c
0e4d205
fe31ea8
770c7d3
aeb6a5b
0f8cb49
d46339c
5a62f24
d6cea6e
818a95e
93bb8e0
88b2513
ce6bcec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) ) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This replaces what it was in line 11, before; do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
include("intervalbox.jl") | ||
include("setdiff.jl") | ||
include("arithmetic.jl") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat!
There was a problem hiding this comment.
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 anIntervalBox
, you do require the dot-notation. The following uses the current commit (93bb8e0) of this PR: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 returnexp.(A)
, or shall we leave it as it is now?There was a problem hiding this comment.
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 applyexp
element-wise, sinceexp
of a vector does not make sense.I don't understand the context where
exp
of anIntervalBox
makes sense. I don't actually understand yourTaylor
example.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough.