Skip to content

Commit

Permalink
Update bisect.jl
Browse files Browse the repository at this point in the history
Solves issue JuliaIntervals#431
  • Loading branch information
edtrelo authored Sep 29, 2023
1 parent 9ac59c7 commit 5fea9f7
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/bisect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,57 @@ 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

"""
mince(x::IntervalBox, n)
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
Expand Down

0 comments on commit 5fea9f7

Please sign in to comment.