borel 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +53 -27
- data/borel.gemspec +1 -1
- data/lib/borel/version.rb +1 -1
- data/spec/interval_spec.rb +188 -67
- data/spec/numeric_spec.rb +33 -0
- data/tasks/rspec.rb +3 -1
- metadata +3 -2
data/README.md
CHANGED
@@ -1,42 +1,60 @@
|
|
1
1
|
Borel
|
2
2
|
=====
|
3
3
|
|
4
|
-
Borelian sets are
|
4
|
+
Borelian sets are formed by enumerable union, intersection or
|
5
|
+
complement, of intervals.
|
5
6
|
|
6
|
-
**Borel** enables performing regular operations on
|
7
|
+
**Borel** enables performing regular operations on intervals
|
8
|
+
of any comparable class.
|
7
9
|
|
8
|
-
**Borel**
|
10
|
+
**Borel** borrows many of the ideas _(and code)_
|
11
|
+
from the **Intervals** [gem][1].
|
9
12
|
|
10
13
|
[1]: http://intervals.rubyforge.org
|
11
14
|
|
15
|
+
Installation
|
16
|
+
------------
|
17
|
+
|
18
|
+
You may install it traditionally, for interactive sessions:
|
19
|
+
|
20
|
+
gem install borel
|
21
|
+
|
22
|
+
Or just put this somewhere on your application's `Gemfile`
|
23
|
+
|
24
|
+
gem 'borel'
|
25
|
+
|
12
26
|
Usage
|
13
27
|
-----
|
14
28
|
|
15
29
|
### Initializing
|
16
30
|
|
17
|
-
An Interval can be initialized with an empty, one or two sized array
|
31
|
+
An Interval can be initialized with an empty, one or two sized array
|
32
|
+
(respectively for an _empty_, _degenerate_ or _simple_ interval), or
|
33
|
+
an array of one or two sized arrays (for a _multiple_ interval).
|
18
34
|
|
19
35
|
```ruby
|
20
|
-
Interval[]
|
21
|
-
Interval[1]
|
22
|
-
Interval[0,1]
|
23
|
-
Interval[[0,1],[2,3],[5]]
|
36
|
+
Interval[]
|
37
|
+
Interval[1]
|
38
|
+
Interval[0,1]
|
39
|
+
Interval[[0,1],[2,3],[5]]
|
24
40
|
```
|
25
41
|
|
26
|
-
Another way to initialize an Interval is by using the
|
42
|
+
Another way to initialize an Interval is by using the
|
43
|
+
**to_interval** method on Ranges or Numbers.
|
27
44
|
|
28
45
|
```ruby
|
29
|
-
1.to_interval
|
30
|
-
(0..1).to_interval
|
31
|
-
(0...2).to_interval
|
46
|
+
1.to_interval
|
47
|
+
(0..1).to_interval
|
48
|
+
(0...2).to_interval
|
32
49
|
```
|
33
50
|
|
34
|
-
The **Infinity** constant is available for specifying intervals
|
51
|
+
The **Infinity** constant is available for specifying intervals
|
52
|
+
with no upper or lower boundary.
|
35
53
|
|
36
54
|
```ruby
|
37
|
-
Interval[-Infinity, 0]
|
38
|
-
Interval[1, Infinity]
|
39
|
-
Interval[-Infinity, Infinity]
|
55
|
+
Interval[-Infinity, 0]
|
56
|
+
Interval[1, Infinity]
|
57
|
+
Interval[-Infinity, Infinity]
|
40
58
|
```
|
41
59
|
|
42
60
|
### Properties
|
@@ -44,10 +62,10 @@ Interval[-Infinity, Infinity]
|
|
44
62
|
Some natural properties of intervals:
|
45
63
|
|
46
64
|
```ruby
|
47
|
-
Interval[1].degenerate? # true
|
48
|
-
Interval[[0,1],[2,3]].simple? # false
|
49
|
-
Interval[].empty? # true
|
50
|
-
Interval[1,5].include?(3.4) # true
|
65
|
+
Interval[1].degenerate? # -> true
|
66
|
+
Interval[[0,1],[2,3]].simple? # -> false
|
67
|
+
Interval[].empty? # -> true
|
68
|
+
Interval[1,5].include?(3.4) # -> true
|
51
69
|
```
|
52
70
|
|
53
71
|
### Operations
|
@@ -57,7 +75,7 @@ Interval[1,5].include?(3.4) # true
|
|
57
75
|
__complement__ and __~__
|
58
76
|
|
59
77
|
```ruby
|
60
|
-
~Interval[0,5]
|
78
|
+
~Interval[0,5] # -> Interval[[-Infinity, 0], [5, Infinity]]
|
61
79
|
```
|
62
80
|
|
63
81
|
* Union
|
@@ -65,7 +83,7 @@ __complement__ and __~__
|
|
65
83
|
__union__, __|__ and __+__
|
66
84
|
|
67
85
|
```ruby
|
68
|
-
Interval[0,5] | Interval[-1,3]
|
86
|
+
Interval[0,5] | Interval[-1,3] # -> Interval[-1,5]
|
69
87
|
```
|
70
88
|
|
71
89
|
* Intersection
|
@@ -73,7 +91,7 @@ Interval[0,5] | Interval[-1,3] == Interval[-1,5]
|
|
73
91
|
__intersect__, __&__, __^__
|
74
92
|
|
75
93
|
```ruby
|
76
|
-
Interval[0,5] ^ Interval[-1,3]
|
94
|
+
Interval[0,5] ^ Interval[-1,3] # -> Interval[0,3]
|
77
95
|
```
|
78
96
|
|
79
97
|
* Subtraction
|
@@ -81,11 +99,19 @@ Interval[0,5] ^ Interval[-1,3] == Interval[0,3]
|
|
81
99
|
__minus__ and __-__
|
82
100
|
|
83
101
|
```ruby
|
84
|
-
Interval[0,5] - Interval[-1,3]
|
102
|
+
Interval[0,5] - Interval[-1,3] # -> Interval[3,5]
|
85
103
|
```
|
86
104
|
|
87
|
-
###
|
105
|
+
### Classes of Intervals
|
88
106
|
|
89
|
-
|
107
|
+
You may use any comparable class
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
Interval['a','c'] ^ Interval['b','d'] # -> Interval['b','c']
|
111
|
+
Interval['a','c'] | Interval['b','d'] # -> Interval['a','d']
|
112
|
+
```
|
113
|
+
|
114
|
+
### Remarks
|
90
115
|
|
91
|
-
* There is
|
116
|
+
* There is no distinction between **open** and **closed** intervals
|
117
|
+
* Complement and Minus operations are not supported on any class
|
data/borel.gemspec
CHANGED
data/lib/borel/version.rb
CHANGED
data/spec/interval_spec.rb
CHANGED
@@ -1,142 +1,263 @@
|
|
1
1
|
require 'borel/interval'
|
2
2
|
|
3
3
|
describe Interval do
|
4
|
-
context '#intersection' do
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
context '#construction' do
|
6
|
+
specify "Interval[] -> []" do
|
7
|
+
Interval[].construction.should eq []
|
8
|
+
end
|
9
|
+
|
10
|
+
specify "Interval[1,1] -> [1]" do
|
11
|
+
Interval[1,1].construction.should eq [1]
|
12
|
+
end
|
13
|
+
|
14
|
+
specify "Interval[1,2] -> [1,2]" do
|
15
|
+
Interval[1,2].construction.should eq [1,2]
|
16
|
+
end
|
17
|
+
|
18
|
+
specify "Interval[[1,2],[3,4],[5]] -> [[1,2],[3,4],[5]]" do
|
19
|
+
Interval[[1,2],[3,4],[5]].construction.should eq [[1,2],[3,4],[5]]
|
20
|
+
end
|
21
|
+
|
22
|
+
specify "Interval[-Infinity, Infinity] -> [-Infinity, Infinity]" do
|
23
|
+
Interval[-Infinity, Infinity].construction.
|
24
|
+
should eq [-Infinity, Infinity]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context '#empty?' do
|
29
|
+
specify "Interval[] -> true" do
|
30
|
+
Interval[].should be_empty
|
31
|
+
end
|
32
|
+
|
33
|
+
specify "Interval[1] -> false" do
|
34
|
+
Interval[1].should_not be_empty
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context '#intersection' do
|
39
|
+
specify "[]^[] = []" do
|
40
|
+
(Interval[] ^ Interval[]).
|
41
|
+
construction.should eq []
|
8
42
|
end
|
9
43
|
|
10
|
-
|
11
|
-
(Interval[] ^ Interval[0,3]).
|
44
|
+
specify "[]^[0,3] = []" do
|
45
|
+
(Interval[] ^ Interval[0,3]).
|
46
|
+
construction.should eq []
|
12
47
|
end
|
13
48
|
|
14
|
-
|
15
|
-
(Interval[0,3] ^ Interval[]).
|
49
|
+
specify "[0,3]^[] = []" do
|
50
|
+
(Interval[0,3] ^ Interval[]).
|
51
|
+
construction.should eq []
|
16
52
|
end
|
17
53
|
|
18
|
-
|
19
|
-
(Interval[0,2] ^ Interval[1]).
|
54
|
+
specify "[0,2]^[1] = [1]" do
|
55
|
+
(Interval[0,2] ^ Interval[1]).
|
56
|
+
construction.should eq [1]
|
20
57
|
end
|
21
58
|
|
22
|
-
|
23
|
-
(Interval[-Infinity, Infinity] ^ Interval[0,1]).
|
59
|
+
specify "[-infty, infty]^[0,1] = [0,1]" do
|
60
|
+
(Interval[-Infinity, Infinity] ^ Interval[0,1]).
|
61
|
+
construction.should eq [0,1]
|
24
62
|
end
|
25
63
|
|
26
|
-
|
27
|
-
(Interval[0,3] ^ Interval[1,2]).
|
64
|
+
specify "[0,3]^[1,2] = [1,2]" do
|
65
|
+
(Interval[0,3] ^ Interval[1,2]).
|
66
|
+
construction.should eq [1,2]
|
28
67
|
end
|
29
68
|
|
30
|
-
|
31
|
-
(Interval[[-2,-1],[1,2]] ^ Interval[[0,3],[4,5]]).
|
69
|
+
specify "[[-2,-1],[1,2]]^[[0,3],[4,5]] = [1,2]" do
|
70
|
+
(Interval[[-2,-1],[1,2]] ^ Interval[[0,3],[4,5]]).
|
71
|
+
construction.should eq [1,2]
|
32
72
|
end
|
33
73
|
end
|
34
74
|
|
35
75
|
context '#union' do
|
36
|
-
|
37
|
-
(Interval[] | Interval[]).
|
76
|
+
specify "[]U[] = []" do
|
77
|
+
(Interval[] | Interval[]).construction.
|
78
|
+
should eq []
|
38
79
|
end
|
39
80
|
|
40
|
-
|
41
|
-
(Interval[] | Interval[1,2]).
|
81
|
+
specify "[]U[1,2] = [1,2]" do
|
82
|
+
(Interval[] | Interval[1,2]).construction.
|
83
|
+
should eq [1,2]
|
42
84
|
end
|
43
85
|
|
44
|
-
|
45
|
-
(Interval[1,2] | Interval[]).
|
86
|
+
specify "[1,2]U[] = [1,2]" do
|
87
|
+
(Interval[1,2] | Interval[]).construction.
|
88
|
+
should eq [1,2]
|
46
89
|
end
|
47
90
|
|
48
|
-
|
49
|
-
(Interval[0,3] | Interval[1,2]).
|
91
|
+
specify "[0,3]U[1,2] = [0,3]" do
|
92
|
+
(Interval[0,3] | Interval[1,2]).construction.
|
93
|
+
should eq [0,3]
|
50
94
|
end
|
51
95
|
|
52
|
-
|
53
|
-
(Interval[1,2] | Interval[0,3]).
|
96
|
+
specify "[1,2]U[0,3] = [0,3]" do
|
97
|
+
(Interval[1,2] | Interval[0,3]).construction.
|
98
|
+
should eq [0,3]
|
54
99
|
end
|
55
100
|
|
56
|
-
|
57
|
-
(Interval[1,2] | Interval[1,2]).
|
101
|
+
specify "[1,2]U[1,2] = [1,2]" do
|
102
|
+
(Interval[1,2] | Interval[1,2]).construction.
|
103
|
+
should eq [1,2]
|
58
104
|
end
|
59
105
|
|
60
|
-
|
61
|
-
(Interval[1,2] | Interval[2,3]).
|
106
|
+
specify "[1,2]U[2,3] = [1,3]" do
|
107
|
+
(Interval[1,2] | Interval[2,3]).construction.
|
108
|
+
should eq [1,3]
|
62
109
|
end
|
63
110
|
|
64
|
-
|
65
|
-
(Interval[1,2] | Interval[3,4]).
|
111
|
+
specify "[1,2]U[3,4] = [[1,2],[3,4]]" do
|
112
|
+
(Interval[1,2] | Interval[3,4]).construction.
|
113
|
+
should eq [[1,2],[3,4]]
|
66
114
|
end
|
67
115
|
end
|
68
116
|
|
69
117
|
context '#minus' do
|
70
|
-
|
71
|
-
(Interval[] - Interval[]).
|
118
|
+
specify "[]-[] = []" do
|
119
|
+
(Interval[] - Interval[]).construction.
|
120
|
+
should eq []
|
121
|
+
end
|
122
|
+
|
123
|
+
specify "[1,2]-[] = [1,2]" do
|
124
|
+
(Interval[1,2] - Interval[]).construction.
|
125
|
+
should eq [1,2]
|
72
126
|
end
|
73
127
|
|
74
|
-
|
75
|
-
(Interval[
|
128
|
+
specify "[]-[1,2] = []" do
|
129
|
+
(Interval[] - Interval[1,2]).construction.
|
130
|
+
should eq []
|
76
131
|
end
|
77
132
|
|
78
|
-
|
79
|
-
(Interval[] - Interval[1
|
133
|
+
specify "[2,3]-(0,1) = [2,3]" do
|
134
|
+
(Interval[2,3] - Interval[0,1]).construction.
|
135
|
+
should eq [2,3]
|
80
136
|
end
|
81
137
|
|
82
|
-
|
83
|
-
(Interval[
|
138
|
+
specify '[0,1]-(2,3) = [0,1]' do
|
139
|
+
(Interval[0,1] - Interval[2,3]).construction.
|
140
|
+
should eq [0,1]
|
84
141
|
end
|
85
142
|
|
86
|
-
|
87
|
-
(Interval[
|
143
|
+
specify '[1,2]-(0,3) = []' do
|
144
|
+
(Interval[1,2] - Interval[0,3]).construction.
|
145
|
+
should eq []
|
88
146
|
end
|
89
147
|
|
90
|
-
|
91
|
-
(Interval[
|
148
|
+
specify '[0,3]-(1,2) = [0,1]U[2,3]' do
|
149
|
+
(Interval[0,3] - Interval[1,2]).construction.
|
150
|
+
should eq [[0,1],[2,3]]
|
92
151
|
end
|
93
152
|
|
94
|
-
|
95
|
-
(Interval[0,
|
153
|
+
specify '[0,1]-(0,1) = [0]U[1]' do
|
154
|
+
(Interval[0,1] - Interval[0,1]).construction.
|
155
|
+
should eq [[0],[1]]
|
96
156
|
end
|
97
157
|
|
98
|
-
|
99
|
-
(Interval[
|
158
|
+
specify '[1,2]-(2,3) = [1,2]' do
|
159
|
+
(Interval[1,2] - Interval[2,3]).construction.
|
160
|
+
should eq [1,2]
|
100
161
|
end
|
101
162
|
|
102
|
-
|
103
|
-
(Interval[
|
163
|
+
specify '[2,3]-(1,2) = [2,3]' do
|
164
|
+
(Interval[2,3] - Interval[1,2]).construction.
|
165
|
+
should eq [2,3]
|
104
166
|
end
|
105
167
|
|
106
|
-
|
107
|
-
(Interval[
|
168
|
+
specify '[1,4]-(0,3) = [3,4]' do
|
169
|
+
(Interval[1,4] - Interval[0,3]).construction.
|
170
|
+
should eq [3,4]
|
108
171
|
end
|
109
172
|
|
110
|
-
|
111
|
-
(Interval[1,
|
173
|
+
specify '[1,3]-(2,4) = [1,2]' do
|
174
|
+
(Interval[1,3] - Interval[2,4]).construction.
|
175
|
+
should eq [1,2]
|
112
176
|
end
|
113
177
|
|
114
|
-
|
115
|
-
(Interval[1,
|
178
|
+
specify '[1,4]-(1,3) = [1]U[3,4]' do
|
179
|
+
(Interval[1,4] - Interval[1,3]).construction.
|
180
|
+
should eq [[1],[3,4]]
|
116
181
|
end
|
117
182
|
|
118
|
-
|
119
|
-
(Interval[1,4] - Interval[1,
|
183
|
+
specify '[1,4]-(1,5) = [1]' do
|
184
|
+
(Interval[1,4] - Interval[1,5]).construction.
|
185
|
+
should eq [1]
|
120
186
|
end
|
121
187
|
|
122
|
-
|
123
|
-
(Interval[1,
|
188
|
+
specify '[1,3]-(2,3) = [1,2]U[3]' do
|
189
|
+
(Interval[1,3] - Interval[2,3]).construction.
|
190
|
+
should eq [[1,2],[3]]
|
124
191
|
end
|
125
192
|
|
126
|
-
|
127
|
-
(Interval[1,3] - Interval[
|
193
|
+
specify '[1,3]-(0,3) = [3]' do
|
194
|
+
(Interval[1,3] - Interval[0,3]).construction.
|
195
|
+
should eq [3]
|
128
196
|
end
|
129
197
|
|
130
|
-
|
131
|
-
(Interval[
|
198
|
+
specify '[-infty,infty]-[0,1] = [-infty,0]U[1,infty]' do
|
199
|
+
(Interval[-Infinity,Infinity] - Interval[0,1]).construction.
|
200
|
+
should eq [[-Infinity,0], [1,Infinity]]
|
132
201
|
end
|
133
202
|
|
134
|
-
|
135
|
-
(Interval[
|
203
|
+
specify '[0,1]-[-infty,infty] = []' do
|
204
|
+
(Interval[0,1] - Interval[-Infinity,Infinity]).construction.
|
205
|
+
should eq []
|
136
206
|
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe "Generic Interval" do
|
211
|
+
describe "when I use String Intervals" do
|
212
|
+
it "should initialize ['a']" do
|
213
|
+
Interval["a"].construction.should eq ["a"]
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should initialize ['a','b']" do
|
217
|
+
Interval["a","b"].construction.should eq ["a","b"]
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should initialize [['a','c'],['b','d'],['e']] as [['a','d'],['e']]" do
|
221
|
+
Interval[["a","c"],["b","d"],["e"]].
|
222
|
+
construction.should eq [["a","d"],["e"]]
|
223
|
+
end
|
224
|
+
|
225
|
+
specify "['c','e']|['d','f']|['g']|[] = [['c','f'],['g']]" do
|
226
|
+
(Interval['c','e'] | Interval['d','f'] | Interval['g'] | Interval[]).
|
227
|
+
construction.should eq [['c','f'],['g']]
|
228
|
+
end
|
229
|
+
|
230
|
+
specify "['a','c']^[] = []" do
|
231
|
+
(Interval['a','c'] ^ Interval[]).
|
232
|
+
construction.should eq []
|
233
|
+
end
|
234
|
+
|
235
|
+
specify "['a','b']^['c','d'] = []" do
|
236
|
+
(Interval['a','b'] ^ Interval['c','d']).
|
237
|
+
construction.should eq []
|
238
|
+
end
|
239
|
+
|
240
|
+
specify "['a','c']^['b','d'] = ['b','c']" do
|
241
|
+
(Interval['a','c'] ^ Interval['b','d']).
|
242
|
+
construction.should eq ['b','c']
|
243
|
+
end
|
244
|
+
|
245
|
+
specify "['a','c']^['b','d']^['f'] = []" do
|
246
|
+
(Interval['a','c'] ^ Interval['b','d'] ^ Interval['f']).
|
247
|
+
construction.should eq []
|
248
|
+
end
|
249
|
+
|
250
|
+
describe "when upper bound is 'a' and lower bound is 'z'" do
|
251
|
+
specify "~['d','f'] = ['a','d']|['f','z']" do
|
252
|
+
pending
|
253
|
+
(~Interval['d','f']).construction.should eq [['a','d'],['f','z']]
|
254
|
+
end
|
137
255
|
|
138
|
-
|
139
|
-
|
256
|
+
specify "['d','f']-['a','e'] = ['e','f']" do
|
257
|
+
pending
|
258
|
+
(Interval['d','f']-Interval['a','e']).
|
259
|
+
construction.should eq ['e','f']
|
260
|
+
end
|
140
261
|
end
|
141
262
|
end
|
142
263
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'borel/interval'
|
2
|
+
require 'borel/numeric'
|
3
|
+
|
4
|
+
describe Numeric do
|
5
|
+
context "#to_interval" do
|
6
|
+
|
7
|
+
it "-1 should be [-1]" do
|
8
|
+
-1.to_interval.should eq Interval[-1]
|
9
|
+
end
|
10
|
+
|
11
|
+
it "0 should be [0]" do
|
12
|
+
0.to_interval.should eq Interval[0]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "1 should be [1]" do
|
16
|
+
1.to_interval.should eq Interval[1]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "-3.23 should be [-3.23]" do
|
20
|
+
-3.23.to_interval.should eq Interval[-3.23]
|
21
|
+
end
|
22
|
+
|
23
|
+
it "1/0 should be [Infinity]" do
|
24
|
+
(1/0.0).to_interval.should eq Interval[Infinity]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "Infinity should be [Infinity]" do
|
28
|
+
(-1/0.0).to_interval.should eq Interval[-Infinity]
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
data/tasks/rspec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: borel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-18 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Borel sets are made of enumerable union and intersection of intervals.
|
15
15
|
Borel performs regular set operations on any interval of a Comparable class.
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/borel/version.rb
|
34
34
|
- spec/interval_spec.rb
|
35
35
|
- spec/nil_class_spec.rb
|
36
|
+
- spec/numeric_spec.rb
|
36
37
|
- spec/range_spec.rb
|
37
38
|
- tasks/gem.rb
|
38
39
|
- tasks/rspec.rb
|