API

IntervalSets.ClosedIntervalType

A ClosedInterval(left, right) is an interval set that includes both its upper and lower bounds. In mathematical notation, the constructed range is [left, right].

source
IntervalSets.IntervalType

An Interval{L,R}(left, right) where L,R are :open or :closed is an interval set containg x such that

  1. left ≤ x ≤ right if L == R == :closed
  2. left < x ≤ right if L == :open and R == :closed
  3. left ≤ x < right if L == :closed and R == :open, or
  4. left < x < right if L == R == :open
source
IntervalSets.OpenIntervalType

An TypedEndpointsInterval{:open,:open}(left, right) is an interval set that includes both its upper and lower bounds. In mathematical notation, the constructed range is (left, right).

source
Base.Math.clampMethod
clamp(t, i::ClosedInterval)

Clamp the scalar t such that the result is in the interval i.

Examples

julia> clamp(1.2, 1..2)
1.2

julia> clamp(2.2, 1..2)
2.0
source
Base.findallMethod
findall(in(interval), x::AbstractRange)

Return all indices i for which x[i] ∈ interval, specialized for the case where x is a range, which enables constant-time complexity.

Examples

julia> x = range(0,stop=3,length=10)
0.0:0.3333333333333333:3.0

julia> collect(x)'
1×10 adjoint(::Vector{Float64}) with eltype Float64:
 0.0  0.333333  0.666667  1.0  1.33333  1.66667  2.0  2.33333  2.66667  3.0

julia> findall(in(1..6), x)
4:10

It also works for decreasing ranges:

julia> y = 8:-0.5:0
8.0:-0.5:0.0

julia> collect(y)'
1×17 adjoint(::Vector{Float64}) with eltype Float64:
 8.0  7.5  7.0  6.5  6.0  5.5  5.0  4.5  …  3.0  2.5  2.0  1.5  1.0  0.5  0.0

julia> findall(in(1..6), y)
5:15

julia> findall(in(Interval{:open,:closed}(1,6)), y) # (1,6], does not include 1
5:14
source
Base.modMethod
mod(x, i::AbstractInterval)

Find y in the i interval such that $x ≡ y \pmod w$, where w = width(i).

Examples

julia> I = 2.5..4.5;

julia> mod(3.0, I)
3.0

julia> mod(5.0, I)
3.0

julia> mod(2.5, I)
2.5

julia> mod(4.5, I)  # (a in I) does not imply (a == mod(a, I)) for closed intervals
2.5

julia> mod(4.5, Interval{:open, :closed}(2.5, 4.5))
4.5
source
Base.rangeMethod
range(i::ClosedInterval; step, length)
range(i::ClosedInterval, length::Integer)

Constructs a range of a specified step or length.

Examples

julia> range(1..2, 8)
1.0:0.14285714285714285:2.0

julia> range(1, 2, 8)
1.0:0.14285714285714285:2.0

julia> range(1..2, step=0.2)
1.0:0.2:2.0

julia> range(1, 2, step=0.2)
1.0:0.2:2.0
source
Base.rangeMethod
range(i::Interval{:closed,:open}; length)
range(i::Interval{:closed,:open}, length::Integer)

Constructs a range of a specified length with step=width(i)/length.

Examples

julia> range(iv"[1, 2)", 7)  # Does not contain right endpoint
1.0:0.14285714285714285:1.8571428571428572

julia> range(1, 2, 8)
1.0:0.14285714285714285:2.0
source
IntervalSets.:..Method
iv = l..r

Construct a ClosedInterval iv spanning the region from l to r.

Examples

julia> 1..2
1 .. 2

julia> 3..1  # Empty interval set can be defined
3 .. 1
source
IntervalSets.:±Method
iv = center ± halfwidth

Construct a ClosedInterval iv spanning the region from center - halfwidth to center + halfwidth.

Examples

julia> 3 ± 2
1 .. 5
source
IntervalSets.closedendpointsMethod
closedendpoints(d::AI) where AI<:AbstractInterval

A tuple of Bool's encoding whether the left/right endpoints are closed.

Examples

julia> closedendpoints(iv"[1,2]")
(true, true)

julia> closedendpoints(iv"(1,2]")
(false, true)

julia> closedendpoints(iv"[2,1)")
(true, false)

julia> closedendpoints(iv"(2,1)")
(false, false)
source
IntervalSets.endpointsMethod
endpoints(d::AI) where AI<:AbstractInterval

A tuple containing the left and right endpoints of the interval.

Examples

julia> endpoints(iv"[1,2]")
(1, 2)

julia> endpoints(iv"(2,1)")
(2, 1)
source
IntervalSets.isclosedsetMethod
isclosedset(d::AbstractInterval)

Is the interval closed set?

Examples

julia> isclosedset(iv"[1,2]")
true

julia> isclosedset(iv"(1,2]")
false

julia> isclosedset(iv"[1,2)")
false

julia> isclosedset(iv"(1,2)")
false
source
IntervalSets.isleftclosedMethod
isleftclosed(d::AbstractInterval)

Is the interval closed at the left endpoint?

Examples

julia> isleftclosed(iv"[1,2]")
true

julia> isleftclosed(iv"(2,1)")
false
source
IntervalSets.isleftopenMethod
isleftopen(d::AbstractInterval)

Is the interval open at the left endpoint?

Examples

julia> isleftopen(iv"[1,2]")
false

julia> isleftopen(iv"(2,1)")
true
source
IntervalSets.isopensetMethod
isopenset(d::AbstractInterval)

Is the interval open set?

Examples

julia> isopenset(iv"[1,2]")
false

julia> isopenset(iv"(1,2]")
false

julia> isopenset(iv"[1,2)")
false

julia> isopenset(iv"(1,2)")
true
source
IntervalSets.isrightclosedMethod
isrightclosed(d::AbstractInterval)

Is the interval closed at the right endpoint?

Examples

julia> isrightclosed(iv"[1,2]")
true

julia> isrightclosed(iv"(2,1)")
false
source
IntervalSets.isrightopenMethod
isrightopen(d::AbstractInterval)

Is the interval open at the right endpoint?

Examples

julia> isrightopen(iv"[1,2]")
false

julia> isrightopen(iv"(2,1)")
true
source
IntervalSets.leftendpointMethod
leftendpoint(d::AbstractInterval)

The left endpoint of the interval.

Examples

julia> leftendpoint(iv"[1,2]")
1

julia> leftendpoint(iv"(2,1)")
2
source
IntervalSets.rightendpointMethod
rightendpoint(d::AbstractInterval)

The right endpoint of the interval.

Examples

julia> rightendpoint(iv"[1,2]")
2

julia> rightendpoint(iv"(2,1)")
1
source
IntervalSets.searchsorted_intervalMethod
searchsorted_interval(a, i::Interval; [rev=false])

Return the range of indices of a which is inside of the interval i (using binary search), assuming that a is already sorted. Return an empty range located at the insertion point if a does not contain values in i.

Examples

julia> searchsorted_interval([1,2,3,5], 2..4)
2:3

julia> searchsorted_interval([1,2,3,5], 4..1)
4:3

julia> searchsorted_interval(Float64[], 1..3)
1:0
source
IntervalSets.widthMethod
w = width(iv)

Calculate the width (max-min) of interval iv. Note that for integers l and r, width(l..r) = length(l:r) - 1.

Examples

julia> width(2..7)
5

julia> length(2:7)
6
source
IntervalSets.@iv_strMacro
@iv_str -> Interval

Construct an interval with mathematical notation such as iv"(1,2]".

Examples

julia> iv"[1,2]"
1 .. 2

julia> iv"[1,2)"
1 .. 2 (closed-open)

julia> iv"(1,2]"
1 .. 2 (open-closed)

julia> iv"(1,2)"
1 .. 2 (open)
source