borel 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,7 +8,9 @@ Borelian sets are formed by enumerable union, intersection or
8
8
  of any comparable class.
9
9
 
10
10
  **Borel** borrows many of the ideas _(and code)_
11
- from the **Intervals** [gem][1].
11
+ from the **Intervals** [gem][1]. However it differs from Intervals in which
12
+ it's aim is not on numerical precision and calculation, but on ease of use and
13
+ solving some general interval related problems.
12
14
 
13
15
  [1]: http://intervals.rubyforge.org
14
16
 
@@ -31,8 +33,8 @@ Usage
31
33
  ### Initializing
32
34
 
33
35
  An Interval can be initialized with an empty, one or two sized array
34
- (respectively for an _empty_, _degenerate_ or _simple_ interval), or
35
- an array of one or two sized arrays (for a _multiple_ interval).
36
+ (respectively for an _empty_, _degenerate_ or `Simple` interval), or
37
+ an array of one or two sized arrays (for a `Multiple` interval).
36
38
 
37
39
  ```ruby
38
40
  Interval[]
@@ -42,15 +44,17 @@ An Interval can be initialized with an empty, one or two sized array
42
44
  ```
43
45
 
44
46
  Another way to initialize an Interval is by using the
45
- **to_interval** method on Ranges or Numbers.
47
+ `#to_interval` method extension.
46
48
 
47
49
  ```ruby
48
- 1.to_interval
49
- (0..1).to_interval
50
- (0...2).to_interval
50
+ nil.to_interval # -> Interval[]
51
+ 1.to_interval # -> Interval[1]
52
+ (1..2).to_interval # -> Interval[1,2]
53
+ (1...3).to_interval # -> Interval[1,2]
54
+ [1,2].to_interval # -> Interval[1,2]
51
55
  ```
52
56
 
53
- The **Infinity** constant is available for specifying intervals
57
+ The `Infinity` constant is available for specifying intervals
54
58
  with no upper or lower boundary.
55
59
 
56
60
  ```ruby
@@ -64,6 +68,8 @@ The **Infinity** constant is available for specifying intervals
64
68
  Some natural properties of intervals:
65
69
 
66
70
  ```ruby
71
+ Interval[1,2].inf # -> 1
72
+ Interval[1,2].sup # -> 2
67
73
  Interval[1].degenerate? # -> true
68
74
  Interval[[0,1],[2,3]].simple? # -> false
69
75
  Interval[].empty? # -> true
@@ -74,7 +80,7 @@ Some natural properties of intervals:
74
80
 
75
81
  * Complement
76
82
 
77
- __complement__ and __~__
83
+ `#complement`, alias: `#~`
78
84
 
79
85
  ```ruby
80
86
  ~Interval[0,5] # -> Interval[[-Infinity, 0], [5, Infinity]]
@@ -82,7 +88,7 @@ __complement__ and __~__
82
88
 
83
89
  * Union
84
90
 
85
- __union__, __|__ and __+__
91
+ `#union`, aliases: `#|`,`#+`
86
92
 
87
93
  ```ruby
88
94
  Interval[0,5] | Interval[-1,3] # -> Interval[-1,5]
@@ -90,7 +96,7 @@ Interval[0,5] | Interval[-1,3] # -> Interval[-1,5]
90
96
 
91
97
  * Intersection
92
98
 
93
- __intersect__, __&__, __^__
99
+ `#intersect`, aliases: `#&`,`#^`
94
100
 
95
101
  ```ruby
96
102
  Interval[0,5] ^ Interval[-1,3] # -> Interval[0,3]
@@ -98,7 +104,7 @@ Interval[0,5] ^ Interval[-1,3] # -> Interval[0,3]
98
104
 
99
105
  * Subtraction
100
106
 
101
- __minus__ and __-__
107
+ `#minus`, alias: `#-`
102
108
 
103
109
  ```ruby
104
110
  Interval[0,5] - Interval[-1,3] # -> Interval[3,5]
@@ -140,13 +146,13 @@ Interval[1,5].rand # -> Random.new.rand 1..5
140
146
  Interval[1,5].width # -> 5-1, only for simple intervals
141
147
  ```
142
148
 
143
- It's supported only for Numeric Comparable and arithmetic supported classes
149
+ It's supported only for `Numeric`, `Comparable` and arithmetic supported classes
144
150
 
145
151
  Remarks
146
152
  -------
147
153
 
148
154
  * There is no distinction between _open_ and _closed_ intervals
149
- * _complement_ and _minus_ operations, and also _Math Extensions_ have limited
155
+ * `complement` and `minus` operations, and also _Math Extensions_ have limited
150
156
  support for non numeric-comparable classes
151
157
 
152
158
  License
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Amadeus Folego"]
8
8
  s.email = ["amadeusfolego@gmail.com"]
9
- s.homepage = "http://github.com/amadeusfolego/borel"
9
+ s.homepage = "http://github.com/badosu/borel"
10
10
  s.summary = "Generic ordered set's operations"
11
11
  s.description = "Borel sets are made of enumerable union and intersection of
12
12
  intervals. Borel performs regular operations on any interval of any
@@ -5,7 +5,7 @@ Infinity = 1/0.0
5
5
  class Interval
6
6
  include Enumerable
7
7
 
8
- def Interval.[](*array)
8
+ def self.[](*array)
9
9
  union(*
10
10
  if array.empty?
11
11
  []
@@ -16,14 +16,13 @@ class Interval
16
16
  end
17
17
  )
18
18
  rescue
19
- unless
20
- array.size <= 2 || array.all?{|x| Array === x && x.size <= 2}
19
+ unless array.size <= 2 || array.all?{|x| Array === x && x.size <= 2}
21
20
  raise Exception::Construction, array
22
21
  end
23
22
  raise
24
23
  end
25
24
 
26
- def Interval.union(*array)
25
+ def self.union(*array)
27
26
  l = []
28
27
  array.map(&:components).flatten.sort_by(&:inf).each do |x|
29
28
  if x.sup < x.inf
@@ -51,6 +50,11 @@ class Interval
51
50
  flatten.reduce(:union)
52
51
  end
53
52
 
53
+ def intersect(other)
54
+ other.to_interval.map{|y| map{|x| x.intersect(y)}}.
55
+ flatten.reduce(:union) || Interval[]
56
+ end
57
+
54
58
  def minus(other)
55
59
  if other.empty?
56
60
  self
@@ -64,22 +68,10 @@ class Interval
64
68
  construction == other.construction
65
69
  end
66
70
 
67
- def include?(x)
68
- any?{|i| i.include? x}
69
- end
70
-
71
71
  def empty?
72
72
  components.empty?
73
73
  end
74
74
 
75
- def construction
76
- map &:construction
77
- end
78
-
79
- def degenerate?
80
- all? &:degenerate?
81
- end
82
-
83
75
  def to_interval
84
76
  self
85
77
  end
@@ -92,14 +84,6 @@ class Interval
92
84
  inspect
93
85
  end
94
86
 
95
- def inf
96
- first.inf
97
- end
98
-
99
- def sup
100
- last.sup
101
- end
102
-
103
87
  def hull
104
88
  if empty?
105
89
  Interval[]
@@ -11,6 +11,26 @@ class Interval::Multiple < Interval
11
11
  self
12
12
  end
13
13
 
14
+ def inf
15
+ first.inf
16
+ end
17
+
18
+ def sup
19
+ last.sup
20
+ end
21
+
22
+ def construction
23
+ map &:construction
24
+ end
25
+
26
+ def include?(x)
27
+ any?{|i| i.include? x}
28
+ end
29
+
30
+ def degenerate?
31
+ all? &:degenerate?
32
+ end
33
+
14
34
  def simple?
15
35
  false
16
36
  end
@@ -1,7 +1,7 @@
1
1
  module Borel
2
2
  MAJOR = 0
3
3
  MINOR = 3
4
- TINY = 0
4
+ TINY = 1
5
5
 
6
6
  VERSION = [MAJOR, MINOR, TINY].join('.')
7
7
 
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.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -53,7 +53,7 @@ files:
53
53
  - spec/range_spec.rb
54
54
  - tasks/gem.rb
55
55
  - tasks/rspec.rb
56
- homepage: http://github.com/amadeusfolego/borel
56
+ homepage: http://github.com/badosu/borel
57
57
  licenses: []
58
58
  post_install_message:
59
59
  rdoc_options: []