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 IntervalSetsQuick start
julia> using IntervalSetsjulia> i1 = 1.0 .. 3.01.0 .. 3.0julia> i2 = OpenInterval(0..4)0 .. 4 (open)julia> i1 ⊆ i2truejulia> i2 ⊆ i1false
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.0julia> 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.50.5 .. 2.5julia> 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 .. 2julia> 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`truejulia> 0 ∈ 1.5±1falsejulia> 1 ∈ OpenInterval(0..1)falsejulia> intersect(1..5, 3..7) # can also use `a ∩ b`, where the symbol is \cap<TAB>3 .. 5julia> isempty(intersect(1..5, 10..11))truejulia> (0.25..5) ∪ (3..7.4) # \cup<TAB>; can also use `union()`0.25 .. 7.4julia> isclosedset(0.5..2.0)truejulia> isopenset(OpenInterval(0.5..2.5))truejulia> isleftopen(2..3)falsejulia> (0.25..5) ∪ (6..7.4) # union of interval must be an intervalERROR: ArgumentError: Cannot construct union of disjoint sets.
Visualization
Intervals 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