API
IntervalSets.AbstractInterval
— TypeA subtype of AbstractInterval{T}
represents an interval subset of type T
, that provides endpoints
, closedendpoints
.
IntervalSets.ClosedInterval
— TypeA ClosedInterval(left, right)
is an interval set that includes both its upper and lower bounds. In mathematical notation, the constructed range is [left, right]
.
IntervalSets.Domain
— TypeA subtype of Domain{T}
represents a subset of type T
, that provides in
.
IntervalSets.Interval
— TypeAn Interval{L,R}(left, right)
where L,R are :open or :closed is an interval set containg x
such that
left ≤ x ≤ right
ifL == R == :closed
left < x ≤ right
ifL == :open
andR == :closed
left ≤ x < right
ifL == :closed
andR == :open
, orleft < x < right
ifL == R == :open
IntervalSets.OpenInterval
— TypeAn 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)
.
IntervalSets.TypedEndpointsInterval
— TypeA subtype of TypedEndpointsInterval{L,R,T}
where L
and R
are :open
or :closed
, that represents an interval subset of type T
, and provides endpoints
.
Base.Math.clamp
— Methodclamp(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
Base.findall
— Methodfindall(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
Base.mod
— Methodmod(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
Base.range
— Methodrange(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
Base.range
— Methodrange(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
IntervalSets.:..
— Methodiv = 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
IntervalSets.:±
— Methodiv = center ± halfwidth
Construct a ClosedInterval iv
spanning the region from center - halfwidth
to center + halfwidth
.
Examples
julia> 3 ± 2
1 .. 5
IntervalSets.closedendpoints
— Methodclosedendpoints(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)
IntervalSets.endpoints
— Methodendpoints(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)
IntervalSets.isclosedset
— Methodisclosedset(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
IntervalSets.isleftclosed
— Methodisleftclosed(d::AbstractInterval)
Is the interval closed at the left endpoint?
Examples
julia> isleftclosed(iv"[1,2]")
true
julia> isleftclosed(iv"(2,1)")
false
IntervalSets.isleftopen
— Methodisleftopen(d::AbstractInterval)
Is the interval open at the left endpoint?
Examples
julia> isleftopen(iv"[1,2]")
false
julia> isleftopen(iv"(2,1)")
true
IntervalSets.isopenset
— Methodisopenset(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
IntervalSets.isrightclosed
— Methodisrightclosed(d::AbstractInterval)
Is the interval closed at the right endpoint?
Examples
julia> isrightclosed(iv"[1,2]")
true
julia> isrightclosed(iv"(2,1)")
false
IntervalSets.isrightopen
— Methodisrightopen(d::AbstractInterval)
Is the interval open at the right endpoint?
Examples
julia> isrightopen(iv"[1,2]")
false
julia> isrightopen(iv"(2,1)")
true
IntervalSets.leftendpoint
— Methodleftendpoint(d::AbstractInterval)
The left endpoint of the interval.
Examples
julia> leftendpoint(iv"[1,2]")
1
julia> leftendpoint(iv"(2,1)")
2
IntervalSets.rightendpoint
— Methodrightendpoint(d::AbstractInterval)
The right endpoint of the interval.
Examples
julia> rightendpoint(iv"[1,2]")
2
julia> rightendpoint(iv"(2,1)")
1
IntervalSets.searchsorted_interval
— Methodsearchsorted_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
IntervalSets.width
— Methodw = 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
IntervalSets.@iv_str
— Macro@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)