-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite IntervalBox to contain an SVector of Intervals (#152)
* Arithmetic operations and functions on IntervalBox return IntervalBox * Add some tests of functions on IntervalBoxes * More tests... * Rewrite IntervalBox to contain an SVector * Modify arithmetic operations for new IntervalBox * Fix display of IntervalBox * Add missing functions and fix tests * Rename arithmetic_functions -> arithmetic * Fix broadcasting * Fix tests. So far no iteration for IntervalBox * Add setindex * Add propagate_inbounds * Add iteration for IntervalBox * Add tests for getindex and setindex * Typo * Fix broadcasting and add tests * Fix broadcasting, take 2
- Loading branch information
Showing
7 changed files
with
121 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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 ) | ||
|
||
|
||
# broadcasting: | ||
|
||
# wrap decides whether to wrap the result in an IntervalBox or not, based on the return type | ||
wrap(v::SVector{N,T} where {N,T<:Interval}) = IntervalBox(v) | ||
wrap(v) = v | ||
|
||
Base.broadcast(f, X::IntervalBox) = wrap(f.(X.v)) | ||
Base.broadcast(f, X::IntervalBox, Y::IntervalBox) = wrap(f.(X.v, Y.v)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,76 @@ | ||
# This file is part of the IntervalArithmetic.jl package; MIT licensed | ||
|
||
"""An `IntervalBox` is an `N`-dimensional rectangular box, given | ||
by a Cartesian product of `N` `Interval`s. | ||
by a Cartesian product of a vector 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)) | ||
|
||
Base.@propagate_inbounds Base.getindex(X::IntervalBox, i) = X.v[i] | ||
|
||
setindex(X::IntervalBox, y, i) = IntervalBox( setindex(X.v, y, i) ) | ||
|
||
# iteration: | ||
|
||
|
||
start(X::IntervalBox{N,T}) where {N,T} = 1 | ||
|
||
next(X::IntervalBox{N,T}, state) where {N,T} = (X[state], state+1) | ||
|
||
done(X::IntervalBox{N,T}, state) where {N,T} = state > N | ||
|
||
eltype(::Type{IntervalBox{N,T}}) where {N,T} = Interval{T} # Note that this is defined for the type | ||
|
||
# length(X::IntervalBox{N,T}) where {N,T} = N | ||
|
||
|
||
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) | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
include("intervalbox.jl") | ||
include("setdiff.jl") | ||
include("arithmetic.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters