diff --git a/src/bisect.jl b/src/bisect.jl index 0f6dc61b1..88fe4f471 100644 --- a/src/bisect.jl +++ b/src/bisect.jl @@ -40,15 +40,49 @@ function bisect(X::IntervalBox, i::Integer, α=where_bisect) return (X1, X2) end +# struct that allows to iterate over all the intervals generated by spliting a interval. +struct IntervalRange{T, S<:Integer} <: AbstractRange{T} + start::T + stop::T + n::S +end + +function Base.length(X::IntervalRange) return X.n end + +function Base.first(X::IntervalRange) + interval(X.start, X.start + step(X)) +end + +function Base.step(X::IntervalRange) + (X.stop - X.start)/X.n +end + +function Base.last(X::IntervalRange) + interval(X.stop - step(X), X.stop) +end + +function Base.getindex(X::IntervalRange, i::Integer) + @assert i<=X.n + return interval(X.start + (i-1)*step(X), X.start + i*step(X)) +end +# allows the use of x in X +function Base.in(x::Interval{T}, X::IntervalRange{T}) where {T<:Real} + for y in X + if x ⊆ y + return true + end + end + return false +end + """ mince(x::Interval, n) Split `x` in `n` intervals of the same diameter, which are returned as a vector. """ -function mince(x::Interval{T}, n) where {T<:NumTypes} - nodes = LinRange(inf(x), sup(x), n+1) - return [unsafe_interval(T, nodes[i], nodes[i+1]) for i in 1:n] +function IntervalArithmetic.mince(x::Interval, n::I) where {I <: Integer} + IntervalRange(x.lo, x.hi, n) end """ @@ -56,7 +90,7 @@ end Split `x` in `n` intervals in each dimension of the same diameter. These intervals are combined in all possible `IntervalBox`-es, which are returned -as a vector. +as a iterable struct. """ @generated function mince(x::IntervalBox{N,T}, n) where {N,T<:NumTypes} quote