Interval Sets for Julia
Clone or download
Latest commit 90576a7 Sep 5, 2018


Interval Sets for Julia

Build Status

Coverage Status

This package represents intervals of an ordered set. For an interval spanning from a to b, all values x that lie between a and b are defined as being members of the interval.

This package is intended to implement a "minimal" foundation for intervals upon which other packages might build. In particular, we encourage type-piracy for the reason that only one interval package can unambiguously define the .. and ± operators (see below).

Currently this package defines one concrete type, ClosedInterval. These define the closed set spanning from a to b, meaning the interval is defined as the set {x} satisfying a ≤ x ≤ b. This is sometimes written [a,b] (mathematics syntax, not Julia syntax) or a..b.


You can construct ClosedIntervals in a variety of ways:

julia> using IntervalSets

julia> ClosedInterval{Float64}(1,3)

julia> 0.5..2.5

julia> 1.5±1

Similarly, you can construct OpenIntervals and Interval{:open,:closed}s, and Interval{:closed,:open}:

julia> OpenInterval{Float64}(1,3)
1.0..3.0 (open)

julia> OpenInterval(0.5..2.5)
0.5..2.5 (open)

julia> Interval{:open,:closed}(1,3)
1..3 (open–closed)

The ± operator may be typed as \pm<TAB> (using Julia's LaTeX syntax tab-completion).

Intervals also support the expected set operations:

julia> 1.751.5±1  # \in<TAB>; can also use `in`

julia> 01.5±1

julia> 1OpenInterval(0..1)

julia> intersect(1..5, 3..7)   # can also use `a ∩ b`, where the symbol is \cap<TAB>

julia> isempty(intersect(1..5, 10..11))

julia> (0.25..5) ∪ (3..7.4)    # \cup<TAB>; can also use union()

When computing the union, the result must also be an interval:

julia> (0.25..5) ∪ (6..7.4)
------ ArgumentError ------------------- Stacktrace (most recent call last)

 [1] — union(::IntervalSets.ClosedInterval{Float64}, ::IntervalSets.ClosedInterval{Float64}) at closed.jl:34

ArgumentError: Cannot construct union of disjoint sets.