IntervalSets.jl
A Julia package implementing interval sets.
The documentation is still work in progress. For more information, see also
Feel free to open pull requests and improve this document!
Installation
pkg> add IntervalSets
Quick start
julia> using IntervalSets
julia> i1 = 1.0 .. 3.0
1.0 .. 3.0
julia> i2 = OpenInterval(0..4)
0 .. 4 (open)
julia> i1 ⊆ i2
true
julia> i2 ⊆ i1
false
Currently this package defines one concrete type, Interval
. These define the set spanning from a
to b
, meaning the interval is defined as the set $\{x \ | \ a ≤ x ≤ b\}$. This is sometimes written $[a,b]$ (mathematics syntax, not Julia syntax) or $a..b$.
Optionally, Interval{L,R}
can represent open and half-open intervals. The type parameters L
and R
correspond to the left and right endpoint respectively. The notation ClosedInterval
is short for Interval{:closed,:closed}
, while OpenInterval
is short for Interval{:open,:open}
. For example, the interval Interval{:open,:closed}
corresponds to the set $(a,b] = \{x \ | \ a < x ≤ b\}$.
More examples
Constructors
julia> ClosedInterval{Float64}(1,3)
1.0 .. 3.0
julia> OpenInterval{Float64}(1,3)
1.0 .. 3.0 (open)
julia> Interval{:open, :closed}(1,3)
1 .. 3 (open-closed)
julia> OpenInterval(0.5..2.5) # construct `OpenInterval` from `ClosedInterval`
0.5 .. 2.5 (open)
The ±
operator and ..
creates ClosedInterval
instance.
julia> 0.5..2.5
0.5 .. 2.5
julia> 1.5 ± 1 # \pm<TAB>
0.5 .. 2.5
There is also a useful string macro @iv_str
to define an interval with mathematical notations such as $(a,b]$.
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)
Set operations
julia> 1.75 ∈ 1.5±1 # \in<TAB>; can also use `in`
true
julia> 0 ∈ 1.5±1
false
julia> 1 ∈ OpenInterval(0..1)
false
julia> intersect(1..5, 3..7) # can also use `a ∩ b`, where the symbol is \cap<TAB>
3 .. 5
julia> isempty(intersect(1..5, 10..11))
true
julia> (0.25..5) ∪ (3..7.4) # \cup<TAB>; can also use `union()`
0.25 .. 7.4
julia> isclosedset(0.5..2.0)
true
julia> isopenset(OpenInterval(0.5..2.5))
true
julia> isleftopen(2..3)
false
julia> (0.25..5) ∪ (6..7.4) # union of interval must be an interval
ERROR: ArgumentError: Cannot construct union of disjoint sets.
Visualization
Interval
s can be visulalized with Plots.plot
function.
using IntervalSets, Plots
plot(iv"(1,2)")
plot!(iv"[3,6)")
plot!(iv"[5,7)")
The offset
keyword argument is useful for avoid duplication.
plot(iv"[1,3]")
plot!(iv"(2,4)"; offset=-0.1, ylims=(-1,1))
Importing the ..
operator
To import the ..
operator, use import IntervalSets: (..)
. The parantheses are necessary to avoid parsing issues.
julia> import IntervalSets: (..)
julia> import IntervalSets.(..) # This is also okay