Skip to content

Commit fe1c7cd

Browse files
authored
Update docs (#186)
* add docstring to `@iv_str` * update autodocs * update docs with `iv_str` macro * update docs * update docstrings * add docs about `plot` visualization
1 parent ec3670b commit fe1c7cd

File tree

5 files changed

+282
-21
lines changed

5 files changed

+282
-21
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
33
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
4+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
45

56
[compat]
67
Documenter = "1"

docs/src/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
```@autodocs
44
Modules = [IntervalSets]
5-
Order = [:type, :function]
5+
Order = [:type, :function, :macro]
66
```

docs/src/index.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Optionally, `Interval{L,R}` can represent open and half-open intervals.
3838
The type parameters `L` and `R` correspond to the left and right endpoint respectively.
3939
The notation [`ClosedInterval`](@ref) is short for `Interval{:closed,:closed}`,
4040
while [`OpenInterval`](@ref) is short for `Interval{:open,:open}`.
41-
For example, the interval `Interval{:open,:closed}` corresponds to the set ``\{x \ | \ a < x ≤ b\}``.
41+
For example, the interval `Interval{:open,:closed}` corresponds to the set ``(a,b] = \{x \ | \ a < x ≤ b\}``.
4242

4343
## More examples
4444

@@ -51,13 +51,24 @@ using IntervalSets
5151
ClosedInterval{Float64}(1,3)
5252
OpenInterval{Float64}(1,3)
5353
Interval{:open, :closed}(1,3)
54-
0.5..2.5
55-
1.5 ± 1
56-
Interval{:open,:closed}(1,3)
5754
OpenInterval(0.5..2.5) # construct `OpenInterval` from `ClosedInterval`
5855
```
5956

60-
The [`±`](@ref) operator may be typed as `\pm<TAB>` (using Julia's LaTeX syntax tab-completion).
57+
The [`±`](@ref) operator and [`..`](@ref) creates [`ClosedInterval`](@ref) instance.
58+
59+
```@repl more
60+
0.5..2.5
61+
1.5 ± 1 # \pm<TAB>
62+
```
63+
64+
There is also a useful string macro [`@iv_str`](@ref) to define an interval with mathematical notations such as ``(a,b]``.
65+
66+
```@repl more
67+
iv"[1,2]"
68+
iv"[1,2)"
69+
iv"(1,2]"
70+
iv"(1,2)"
71+
```
6172

6273
### Set operations
6374

@@ -74,6 +85,31 @@ isleftopen(2..3)
7485
(0.25..5) ∪ (6..7.4) # union of interval must be an interval
7586
```
7687

88+
### Visualization
89+
`Interval`s can be visulalized with `Plots.plot` function.
90+
91+
```@example plot
92+
using IntervalSets, Plots
93+
plot(iv"(1,2)")
94+
plot!(iv"[3,6)")
95+
plot!(iv"[5,7)")
96+
savefig("plot-intervals.png") # hide
97+
nothing # hide
98+
```
99+
100+
![](plot-intervals.png)
101+
102+
The `offset` keyword argument is useful for avoid duplication.
103+
104+
```@example plot
105+
plot(iv"[1,3]")
106+
plot!(iv"(2,4)"; offset=-0.1, ylims=(-1,1))
107+
savefig("plot-intervals-offset.png") # hide
108+
nothing # hide
109+
```
110+
111+
![](plot-intervals-offset.png)
112+
77113
### Importing the `..` operator
78114

79115
To import the [`..`](@ref) operator, use `import IntervalSets: (..)`.

src/IntervalSets.jl

Lines changed: 203 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,184 @@ A subtype of `AbstractInterval{T}` represents an interval subset of type `T`, th
2525
"""
2626
abstract type AbstractInterval{T} <: Domain{T} end
2727

28+
"""
29+
endpoints(d::AI) where AI<:AbstractInterval
30+
31+
A tuple containing the left and right endpoints of the interval.
2832
29-
"A tuple containing the left and right endpoints of the interval."
33+
# Examples
34+
```jldoctest
35+
julia> endpoints(iv"[1,2]")
36+
(1, 2)
37+
38+
julia> endpoints(iv"(2,1)")
39+
(2, 1)
40+
```
41+
"""
3042
endpoints(d::AI) where AI<:AbstractInterval = error("Override endpoints(::$(AI))")
3143

32-
"The left endpoint of the interval."
44+
"""
45+
leftendpoint(d::AbstractInterval)
46+
47+
The left endpoint of the interval.
48+
49+
# Examples
50+
```jldoctest
51+
julia> leftendpoint(iv"[1,2]")
52+
1
53+
54+
julia> leftendpoint(iv"(2,1)")
55+
2
56+
```
57+
"""
3358
leftendpoint(d::AbstractInterval) = endpoints(d)[1]
3459

35-
"The right endpoint of the interval."
60+
"""
61+
rightendpoint(d::AbstractInterval)
62+
63+
The right endpoint of the interval.
64+
65+
# Examples
66+
```jldoctest
67+
julia> rightendpoint(iv"[1,2]")
68+
2
69+
70+
julia> rightendpoint(iv"(2,1)")
71+
1
72+
```
73+
"""
3674
rightendpoint(d::AbstractInterval) = endpoints(d)[2]
3775

38-
"A tuple of `Bool`'s encoding whether the left/right endpoints are closed."
76+
"""
77+
closedendpoints(d::AI) where AI<:AbstractInterval
78+
79+
A tuple of `Bool`'s encoding whether the left/right endpoints are closed.
80+
81+
# Examples
82+
```jldoctest
83+
julia> closedendpoints(iv"[1,2]")
84+
(true, true)
85+
86+
julia> closedendpoints(iv"(1,2]")
87+
(false, true)
88+
89+
julia> closedendpoints(iv"[2,1)")
90+
(true, false)
91+
92+
julia> closedendpoints(iv"(2,1)")
93+
(false, false)
94+
```
95+
"""
3996
closedendpoints(d::AI) where AI<:AbstractInterval = error("Override closedendpoints(::$(AI))")
4097

41-
"Is the interval closed at the left endpoint?"
98+
"""
99+
isleftclosed(d::AbstractInterval)
100+
101+
Is the interval closed at the left endpoint?
102+
103+
# Examples
104+
```jldoctest
105+
julia> isleftclosed(iv"[1,2]")
106+
true
107+
108+
julia> isleftclosed(iv"(2,1)")
109+
false
110+
```
111+
"""
42112
isleftclosed(d::AbstractInterval) = closedendpoints(d)[1]
43113

44-
"Is the interval closed at the right endpoint?"
114+
"""
115+
isrightclosed(d::AbstractInterval)
116+
117+
Is the interval closed at the right endpoint?
118+
119+
# Examples
120+
```jldoctest
121+
julia> isrightclosed(iv"[1,2]")
122+
true
123+
124+
julia> isrightclosed(iv"(2,1)")
125+
false
126+
```
127+
"""
45128
isrightclosed(d::AbstractInterval) = closedendpoints(d)[2]
46129

47130
# open_left and open_right are implemented in terms of closed_* above, so those
48131
# are the only ones that should be implemented for specific intervals
49-
"Is the interval open at the left endpoint?"
132+
"""
133+
isleftopen(d::AbstractInterval)
134+
135+
Is the interval open at the left endpoint?
136+
137+
# Examples
138+
```jldoctest
139+
julia> isleftopen(iv"[1,2]")
140+
false
141+
142+
julia> isleftopen(iv"(2,1)")
143+
true
144+
```
145+
"""
50146
isleftopen(d::AbstractInterval) = !isleftclosed(d)
51147

52-
"Is the interval open at the right endpoint?"
148+
"""
149+
isrightopen(d::AbstractInterval)
150+
151+
Is the interval open at the right endpoint?
152+
153+
# Examples
154+
```jldoctest
155+
julia> isrightopen(iv"[1,2]")
156+
false
157+
158+
julia> isrightopen(iv"(2,1)")
159+
true
160+
```
161+
"""
53162
isrightopen(d::AbstractInterval) = !isrightclosed(d)
54163

55-
# Only closed if closed at both endpoints, and similar for open
164+
"""
165+
isclosedset(d::AbstractInterval)
166+
167+
Is the interval closed set?
168+
169+
# Examples
170+
```jldoctest
171+
julia> isclosedset(iv"[1,2]")
172+
true
173+
174+
julia> isclosedset(iv"(1,2]")
175+
false
176+
177+
julia> isclosedset(iv"[1,2)")
178+
false
179+
180+
julia> isclosedset(iv"(1,2)")
181+
false
182+
```
183+
"""
56184
isclosedset(d::AbstractInterval) = isleftclosed(d) && isrightclosed(d)
57185

58-
"Is the interval open?"
186+
"""
187+
isopenset(d::AbstractInterval)
188+
189+
Is the interval open set?
190+
191+
# Examples
192+
```jldoctest
193+
julia> isopenset(iv"[1,2]")
194+
false
195+
196+
julia> isopenset(iv"(1,2]")
197+
false
198+
199+
julia> isopenset(iv"[1,2)")
200+
false
201+
202+
julia> isopenset(iv"(1,2)")
203+
true
204+
```
205+
"""
59206
isopenset(d::AbstractInterval) = isleftopen(d) && isrightopen(d)
60207

61208
eltype(::Type{AbstractInterval{T}}) where {T} = T
@@ -94,6 +241,15 @@ mean(d::AbstractInterval) = (leftendpoint(d) + rightendpoint(d))/2
94241
95242
Calculate the width (max-min) of interval `iv`. Note that for integers
96243
`l` and `r`, `width(l..r) = length(l:r) - 1`.
244+
245+
# Examples
246+
```jldoctest
247+
julia> width(2..7)
248+
5
249+
250+
julia> length(2:7)
251+
6
252+
```
97253
"""
98254
function width(A::AbstractInterval)
99255
_width = rightendpoint(A) - leftendpoint(A)
@@ -206,28 +362,61 @@ range(i::TypedEndpointsInterval{:closed,:closed,I}) where {I<:Integer} = UnitRan
206362

207363
"""
208364
range(i::ClosedInterval; step, length)
209-
range(i::ClosedInterval, len::Integer)
365+
range(i::ClosedInterval, length::Integer)
210366
211367
Constructs a range of a specified step or length.
368+
369+
# Examples
370+
```jldoctest
371+
julia> range(1..2, 8)
372+
1.0:0.14285714285714285:2.0
373+
374+
julia> range(1, 2, 8)
375+
1.0:0.14285714285714285:2.0
376+
377+
julia> range(1..2, step=0.2)
378+
1.0:0.2:2.0
379+
380+
julia> range(1, 2, step=0.2)
381+
1.0:0.2:2.0
382+
```
212383
"""
213384
range(i::TypedEndpointsInterval{:closed,:closed}; step=nothing, length=nothing) =
214385
range(leftendpoint(i); stop=rightendpoint(i), step=step, length=length)
215-
range(i::TypedEndpointsInterval{:closed,:closed}, len::Integer) = range(i; length=len)
386+
range(i::TypedEndpointsInterval{:closed,:closed}, length::Integer) = range(i; length=length)
216387

217388
"""
218389
range(i::Interval{:closed,:open}; length)
219-
range(i::Interval{:closed,:open}, len::Integer)
390+
range(i::Interval{:closed,:open}, length::Integer)
220391
221392
Constructs a range of a specified length with `step=width(i)/length`.
393+
394+
# Examples
395+
```jldoctest
396+
julia> range(iv"[1, 2)", 7) # Does not contain right endpoint
397+
1.0:0.14285714285714285:1.8571428571428572
398+
399+
julia> range(1, 2, 8)
400+
1.0:0.14285714285714285:2.0
401+
```
222402
"""
223403
range(i::TypedEndpointsInterval{:closed,:open}; length::Integer) =
224404
range(leftendpoint(i); step=width(i)/length, length=length)
225-
range(i::TypedEndpointsInterval{:closed,:open}, len::Integer) = range(i; length=len)
405+
range(i::TypedEndpointsInterval{:closed,:open}, length::Integer) = range(i; length=length)
226406

227407
"""
228408
clamp(t, i::ClosedInterval)
229409
230410
Clamp the scalar `t` such that the result is in the interval `i`.
411+
412+
# Examples
413+
```jldoctest
414+
julia> clamp(1.2, 1..2)
415+
1.2
416+
417+
julia> clamp(2.2, 1..2)
418+
2.0
419+
```
231420
"""
232421
clamp(t, i::TypedEndpointsInterval{:closed,:closed}) =
233422
clamp(t, leftendpoint(i), rightendpoint(i))

0 commit comments

Comments
 (0)