borel 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,91 @@
1
1
  Borel
2
2
  =====
3
3
 
4
- Borelian sets are sets formed by the enumerable union and intersection, or the complement, of intervals in the Real line.
4
+ Borelian sets are sets formed by enumerable union, intersection or complement, of intervals.
5
5
 
6
- **Borel** aims to make it really easy to make union and intersection operations of interval in any ordered set.
6
+ **Borel** enables performing regular operations on any ordered set.
7
+
8
+ **Borel** was mainly inspired on the **Intervals** [gem][1].
9
+
10
+ [1]: http://intervals.rubyforge.org
11
+
12
+ Usage
13
+ -----
14
+
15
+ ### Initializing
16
+
17
+ An Interval can be initialized with an empty, one or two sized array (respectively for an _empty_, _degenerate_ or _simple_ interval), or an array of one or two sized arrays (for a _multiple_ interval).
18
+
19
+ ```ruby
20
+ Interval[]
21
+ Interval[1]
22
+ Interval[0,1]
23
+ Interval[[0,1],[2,3],[5]]
24
+ ```
25
+
26
+ Another way to initialize an Interval is by using the **to_interval** method on Ranges or Numbers.
27
+
28
+ ```ruby
29
+ 1.to_interval
30
+ (0..1).to_interval
31
+ (0...2).to_interval
32
+ ```
33
+
34
+ The **Infinity** constant is available for specifying intervals with no upper or lower boundary.
35
+
36
+ ```ruby
37
+ Interval[-Infinity, 0]
38
+ Interval[1, Infinity]
39
+ Interval[-Infinity, Infinity]
40
+ ```
41
+
42
+ ### Properties
43
+
44
+ Some natural properties of intervals:
45
+
46
+ ```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
51
+ ```
52
+
53
+ ### Operations
54
+
55
+ * Complement
56
+
57
+ __complement__ and __~__
58
+
59
+ ```ruby
60
+ ~Interval[0,5] == Interval[[-Infinity, 0], [5, Infinity]]
61
+ ```
62
+
63
+ * Union
64
+
65
+ __union__, __|__ and __+__
66
+
67
+ ```ruby
68
+ Interval[0,5] | Interval[-1,3] == Interval[-1,5]
69
+ ```
70
+
71
+ * Intersection
72
+
73
+ __intersect__, __&__, __^__
74
+
75
+ ```ruby
76
+ Interval[0,5] ^ Interval[-1,3] == Interval[0,3]
77
+ ```
78
+
79
+ * Subtraction
80
+
81
+ __minus__ and __-__
82
+
83
+ ```ruby
84
+ Interval[0,5] - Interval[-1,3] == Interval[3,5]
85
+ ```
86
+
87
+ ### Remarks
88
+
89
+ * At the moment there is no distinction between **open** and **closed** intervals.
90
+
91
+ * There is limited support for non-numeric classes intervals
@@ -71,18 +71,6 @@ class Interval
71
71
  [other.to_interval, self]
72
72
  end
73
73
 
74
- def +(other)
75
- self | other
76
- end
77
-
78
- def ^(other)
79
- self & other
80
- end
81
-
82
- def |(other)
83
- Interval.union(other.to_interval, self)
84
- end
85
-
86
74
  def empty?
87
75
  components.empty?
88
76
  end
@@ -99,9 +87,25 @@ class Interval
99
87
  end
100
88
  end
101
89
 
90
+ def union(other)
91
+ Interval.union(other.to_interval, self)
92
+ end
93
+
94
+ def +(other)
95
+ self | other
96
+ end
97
+
98
+ def ^(other)
99
+ self & other
100
+ end
101
+
102
+ def |(other)
103
+ self.union other.to_interval
104
+ end
105
+
102
106
  [[:&, :intersect]].each do |op, meth|
103
107
  define_method(op) {|other|
104
- (other.to_interval.map{|y| map{|x| x.send(meth,y)}}.flatten).reduce(:|)
108
+ (other.to_interval.map{|y| map{|x| x.send(meth,y)}}.flatten).reduce(:|) || Interval[]
105
109
  }
106
110
  end
107
111
 
@@ -113,7 +117,11 @@ class Interval
113
117
 
114
118
  [[:-, :minus]].each do |op, meth|
115
119
  define_method(op) {|other|
116
- map{|x| other.to_interval.map{|y| x.send(meth,y)}.reduce(:&)}.flatten.reduce(:|)
120
+ if other.empty?
121
+ self
122
+ else
123
+ map{|x| other.to_interval.map{|y| x.send(meth,y)}.reduce(:&)}.flatten.reduce(:|) || Interval[]
124
+ end
117
125
  }
118
126
  end
119
127
  end
@@ -180,7 +188,6 @@ class Interval::Simple < Interval
180
188
  end
181
189
 
182
190
  class Interval::Multiple < Interval
183
-
184
191
  attr :components
185
192
 
186
193
  def initialize(array)
@@ -0,0 +1,5 @@
1
+ class NilClass
2
+ def to_interval
3
+ Interval[]
4
+ end
5
+ end
@@ -1,7 +1,7 @@
1
1
  module Borel
2
2
  MAJOR = 0
3
- MINOR = 0
4
- TINY = 1
3
+ MINOR = 1
4
+ TINY = 0
5
5
 
6
6
  VERSION = [MAJOR, MINOR, TINY].join('.')
7
7
 
@@ -3,13 +3,15 @@ require 'borel/interval'
3
3
  describe Interval do
4
4
  context '#intersection' do
5
5
 
6
+ it "[]^[] = []" do
7
+ (Interval[] ^ Interval[]).should eq Interval[]
8
+ end
9
+
6
10
  it "[]^[0,3] = []" do
7
- pending
8
11
  (Interval[] ^ Interval[0,3]).should eq Interval[]
9
12
  end
10
13
 
11
14
  it "[0,3]^[] = []" do
12
- pending
13
15
  (Interval[0,3] ^ Interval[]).should eq Interval[]
14
16
  end
15
17
 
@@ -31,6 +33,18 @@ describe Interval do
31
33
  end
32
34
 
33
35
  context '#union' do
36
+ it "[]U[] = []" do
37
+ (Interval[] | Interval[]).should eq Interval[]
38
+ end
39
+
40
+ it "[]U[1,2] = [1,2]" do
41
+ (Interval[] | Interval[1,2]).should eq Interval[1,2]
42
+ end
43
+
44
+ it "[1,2]U[] = [1,2]" do
45
+ (Interval[1,2] | Interval[]).should eq Interval[1,2]
46
+ end
47
+
34
48
  it "[0,3]U[1,2] = [0,3]" do
35
49
  (Interval[0,3] | Interval[1,2]).should eq Interval[0,3]
36
50
  end
@@ -53,6 +67,18 @@ describe Interval do
53
67
  end
54
68
 
55
69
  context '#minus' do
70
+ it "[]-[] = []" do
71
+ (Interval[] - Interval[]).should eq Interval[]
72
+ end
73
+
74
+ it "[1,2]-[] = [1,2]" do
75
+ (Interval[1,2] - Interval[]).should eq Interval[1,2]
76
+ end
77
+
78
+ it "[]-[1,2] = []" do
79
+ (Interval[] - Interval[1,2]).should eq Interval[]
80
+ end
81
+
56
82
  it "[2,3]-(0,1) = [2,3]" do
57
83
  (Interval[2,3] - Interval[0,1]).should eq Interval[2,3]
58
84
  end
@@ -70,7 +96,7 @@ describe Interval do
70
96
  end
71
97
 
72
98
  it '[0,1]-(0,1) = [0]U[1]' do
73
- (Interval[0,1] - Interval[0,1]).should eq Interval[0] | Interval[1]
99
+ (Interval[0,1] - Interval[0,1]).should eq Interval[[0],[1]]
74
100
  end
75
101
 
76
102
  it '[1,2]-(2,3) = [1,2]' do
@@ -90,7 +116,7 @@ describe Interval do
90
116
  end
91
117
 
92
118
  it '[1,4]-(1,3) = [1]U[3,4]' do
93
- (Interval[1,4] - Interval[1,3]).should eq Interval[3,4] | Interval[1]
119
+ (Interval[1,4] - Interval[1,3]).should eq Interval[[1],[3,4]]
94
120
  end
95
121
 
96
122
  it '[1,4]-(1,5) = [1]' do
@@ -98,7 +124,7 @@ describe Interval do
98
124
  end
99
125
 
100
126
  it '[1,3]-(2,3) = [1,2]U[3]' do
101
- (Interval[1,3] - Interval[2,3]).should eq Interval[1,2] | Interval[3]
127
+ (Interval[1,3] - Interval[2,3]).should eq Interval[[1,2],[3]]
102
128
  end
103
129
 
104
130
  it '[1,3]-(0,3) = [3]' do
@@ -106,7 +132,7 @@ describe Interval do
106
132
  end
107
133
 
108
134
  it '[-infty,infty]-[0,1] = [-infty,0]U[1,infty]' do
109
- (Interval[-Infinity,Infinity] - Interval[0,1]).should eq Interval[-Infinity,0]|Interval[1,Infinity]
135
+ (Interval[-Infinity,Infinity] - Interval[0,1]).should eq Interval[[-Infinity,0],[1,Infinity]]
110
136
  end
111
137
 
112
138
  it '[0,1]-[-infty,infty] = []' do
@@ -0,0 +1,10 @@
1
+ require 'borel/interval'
2
+ require 'borel/nil_class.rb'
3
+
4
+ describe NilClass do
5
+ context "#to_interval" do
6
+ it "should be Interval[]" do
7
+ nil.to_interval.should eq Interval[]
8
+ end
9
+ end
10
+ end
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.0.1
4
+ version: 0.1.0
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 00:00:00.000000000 Z
12
+ date: 2012-02-13 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.
@@ -27,10 +27,12 @@ files:
27
27
  - borel.gemspec
28
28
  - lib/borel.rb
29
29
  - lib/borel/interval.rb
30
+ - lib/borel/nil_class.rb
30
31
  - lib/borel/numeric.rb
31
32
  - lib/borel/range.rb
32
33
  - lib/borel/version.rb
33
34
  - spec/interval_spec.rb
35
+ - spec/nil_class_spec.rb
34
36
  - spec/range_spec.rb
35
37
  - tasks/gem.rb
36
38
  - tasks/rspec.rb