Skip to content

Commit

Permalink
Clarifying differences with IntervalSets
Browse files Browse the repository at this point in the history
  • Loading branch information
scheinerman committed Sep 4, 2018
1 parent cbe6289 commit 6c60032
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ Two operations are defined for intervals.

* The intersection `*` is the largest interval contained
in both. If the intervals are disjoint, this returns an
empty interval.
* The sum `+` is the smallest interval containing both.
empty interval. Also available as ``.
* The sum `+` is the smallest interval containing both
(i.e., the join of the intervals).
If the intervals overlap, then this is the same as their
union. Note that the empty interval serves as an identity
element for this operation.
element for this operation. Also available as ``.

```julia
julia> A = ClosedInterval(1,5)
Expand Down Expand Up @@ -314,16 +315,16 @@ ERROR: MethodError: no method matching zero(::Type{String})

## `ClosedIntervals` vs `IntervalSets`

The [IntervalSets](https://github.com/JuliaMath/IntervalSets.jl) module also defines a `ClosedInterval` type that
The [IntervalSets](https://github.com/JuliaMath/IntervalSets.jl) module also defines a `ClosedInterval` type that
has some notable differences in how intervals are handled.

### Construction

In `ClosedIntervals`, the end points may be specified in either order, while in `IntervalSets` if the left end point is
In `ClosedIntervals`, the end points may be specified in either order, while in `IntervalSets` if the left end point is
greater than the right, an empty interval results. Also, the `IntervalSets` module provides a nifty `..` operator for making
intervals.

```julia
```julia
julia> using ClosedIntervals

julia> ClosedInterval(1,2) == ClosedInterval(2,1)
Expand All @@ -340,31 +341,34 @@ julia> ClosedInterval(1,2) == 1..2
true
```

### Unions
### Union/Join

In the `ClosedIntervals` module, the union of two intervals is the smallest interval containing both. In particular, we
permit the union of disjoint intervals. We use the plus sign `+` (and `*` for intersection).
In the `ClosedIntervals` module, the join `J ∨ K` or `J + K` of two intervals is
the smallest interval containing both. In particular, we permit the join of
disjoint intervals. The intervals may be disjoint.

```
julia> ClosedInterval(1,2) + ClosedInterval(3,4)
julia> ClosedInterval(1,2) ClosedInterval(3,4)
[1,4]
julia> ClosedInterval(1,2) * ClosedInterval(3,4)
[]
```

`IntervalSets` is stricter; forming the union of disjoint intervals is an error.
The `IntervalSets` module provides for the union of intervals.
If the two intervals are disjoint, their set-theoretic union is not an
interval and results in an error.

```julia
julia> ClosedInterval(1,2) ClosedInterval(3,4)
ERROR: ArgumentError: Cannot construct union of disjoint sets.
```

### Length
Note that the intersection (`IntervalSets`) and meet (`ClosedIntervals`) of
two intervals are the same.

The two modules have different implementations of the `length` function.
* In the `ClosedIntervals` module, `length` is simply the difference between the right and left end point values.
* In `IntervalSets`, one can only apply `length` to intervals with integer end points, in which case the `length` is the number of integers in the set. Instead, use `width` to determine the distance between the end points.
### Length/Width

The two modules have different implementations of the `length` function.
* In the `ClosedIntervals` module, `length` is simply the difference between the right and left end point values.
* In `IntervalSets`, one can only apply `length` to intervals with integer end points, in which case the `length` is the number of integers in the set. Instead, use `width` to determine the distance between the end points.

```julia
julia> using ClosedIntervals
Expand All @@ -389,5 +393,3 @@ ERROR: MethodError: no method matching length(::ClosedInterval{Float64})
julia> width(ClosedInterval(1.0,4.0))
3.0
```


21 changes: 17 additions & 4 deletions src/ClosedIntervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Base.⊊, Base.⊇, Base.⊋


export ClosedInterval, EmptyInterval, ,
export show, left, right
export show, left, right, ,

# Create the ClosedInterval type
struct ClosedInterval{T}
Expand Down Expand Up @@ -108,7 +108,8 @@ end
# The intersection of two intervals is the largest interval contained
# in both. If the intervals are disjoint, we return an empty interval.
"""
For `ClosedInterval`s `I` and `J`, `I*J` is their intersection.
For `ClosedInterval`s `J` and `K`, `J*K` is their intersection.
Also available as `J ∧ K`.
"""
function *(J::ClosedInterval, K::ClosedInterval)

Expand All @@ -128,12 +129,18 @@ function *(J::ClosedInterval, K::ClosedInterval)
return ClosedInterval(a,b,false)
end

"""
`J ∧ K` is the largest `ClosedInterval` contained in both.
See also `*`.
"""
()(J::ClosedInterval, K::ClosedInterval) = J*K

# The + of two intervals is the smallest interval containing them
# both. This is the same as their union if they overlap.

"""
For `ClosedInterval`s `I` and `J`, `I+J` is the smallest `ClosedInterval`
containing them both.
For `ClosedInterval`s `J` and `K`, `J+K` is the smallest `ClosedInterval`
containing them both. This is also available as `J ∨ K`.
"""
function +(J::ClosedInterval, K::ClosedInterval)
# The empty interval acts as an identity element for this
Expand All @@ -152,6 +159,12 @@ function +(J::ClosedInterval, K::ClosedInterval)
return ClosedInterval(a,b,false)
end

"""
`J ∨ K` is the smalles `ClosedInterval` containing both. See also `+`.
"""
()(J::ClosedInterval, K::ClosedInterval)= J+K


# Compare intervals for equality
function isequal(I::ClosedInterval, J::ClosedInterval)
return (I.nil && J.nil) || (I.L==J.L && I.R==J.R)
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ B = ClosedInterval(8,3)
C = ClosedInterval(3,6)

@test A*B == C
@test A B == C
@test A+B == ClosedInterval(1,8)
@test A B == ClosedInterval(1,8)

0 comments on commit 6c60032

Please sign in to comment.