borel 0.3.3 → 0.3.4
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/Gemfile +4 -2
- data/lib/borel/array.rb +6 -0
- data/lib/borel/interval.rb +39 -4
- data/lib/borel/interval_simple.rb +12 -2
- data/lib/borel/math_extensions/interval_arithmetic.rb +3 -0
- data/lib/borel/math_extensions/randomizable.rb +3 -0
- data/lib/borel/nil_class.rb +2 -0
- data/lib/borel/range.rb +4 -0
- data/lib/borel/version.rb +1 -1
- data/spec/interval_spec.rb +6 -6
- data/spec/numeric_spec.rb +2 -2
- data/spec/range_spec.rb +9 -8
- metadata +3 -3
data/Gemfile
CHANGED
data/lib/borel/array.rb
CHANGED
@@ -3,6 +3,12 @@ class Array
|
|
3
3
|
|
4
4
|
# Initializes an interval with the calling array as argument
|
5
5
|
# @return [Interval]
|
6
|
+
# @example
|
7
|
+
# [].to_interval # -> Interval[]
|
8
|
+
# @example
|
9
|
+
# [1,2].to_interval # -> Interval[1,2]
|
10
|
+
# @example
|
11
|
+
# [[1,2],[3,4]].to_interval # -> Interval[[1,2],[3,4]]
|
6
12
|
def to_interval
|
7
13
|
Interval[*self]
|
8
14
|
end
|
data/lib/borel/interval.rb
CHANGED
@@ -1,13 +1,33 @@
|
|
1
1
|
require 'borel/errors'
|
2
2
|
|
3
|
-
# Exposes
|
4
|
-
|
3
|
+
# Exposes `Infinity` and `-Infinity` for initializing unbounded intervals
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# Interval[-Infinity, 0]
|
7
|
+
# @example
|
8
|
+
# Interval[1, Infinity]
|
9
|
+
# @example
|
10
|
+
# Interval[-Infinity, Infinity]
|
11
|
+
Infinity = Float::INFINITY
|
5
12
|
|
6
13
|
# Retains all non-specific Interval logic
|
7
14
|
class Interval
|
8
15
|
include Enumerable
|
9
16
|
|
17
|
+
# Initializes an Interval
|
18
|
+
#
|
19
|
+
# @param [Array<Comparable>,Array<Array<Comparable>>] array none, one or
|
20
|
+
# multiple 1,2-length arrays
|
10
21
|
# @return [Interval]
|
22
|
+
#
|
23
|
+
# @example Empty interval
|
24
|
+
# Interval[]
|
25
|
+
# @example An interval with only one point
|
26
|
+
# Interval[1]
|
27
|
+
# @example A simple interval
|
28
|
+
# Interval[0,1]
|
29
|
+
# @example A non simple interval
|
30
|
+
# Interval[[0,1],[2,3],[5]]
|
11
31
|
def self.[](*array)
|
12
32
|
union(*
|
13
33
|
if array.empty?
|
@@ -25,6 +45,8 @@ class Interval
|
|
25
45
|
raise
|
26
46
|
end
|
27
47
|
|
48
|
+
# Performs the union of Intervals
|
49
|
+
# @param Array<Interval> array the intervals to be united
|
28
50
|
# @return [Interval]
|
29
51
|
def self.union(*array)
|
30
52
|
intervals = []
|
@@ -40,7 +62,11 @@ class Interval
|
|
40
62
|
intervals.size == 1 ? intervals.first : Multiple.new(intervals)
|
41
63
|
end
|
42
64
|
|
65
|
+
# Performs the operation of union with other interval
|
66
|
+
# @param [#to_interval] other the other interval-compatible object
|
43
67
|
# @return [Interval]
|
68
|
+
# @example
|
69
|
+
# Interval[1,2].union(3..4) == Interval[[1,2],[3,4]] # true
|
44
70
|
def union(other)
|
45
71
|
Interval.union(other.to_interval, self)
|
46
72
|
end
|
@@ -83,7 +109,8 @@ class Interval
|
|
83
109
|
components.empty?
|
84
110
|
end
|
85
111
|
|
86
|
-
#
|
112
|
+
# Implemented for establishing obvious interval-compatibility
|
113
|
+
# @return [Interval] returns `self`
|
87
114
|
def to_interval
|
88
115
|
self
|
89
116
|
end
|
@@ -98,7 +125,15 @@ class Interval
|
|
98
125
|
inspect
|
99
126
|
end
|
100
127
|
|
101
|
-
#
|
128
|
+
# The convex hull of the Interval
|
129
|
+
# Note that for simple Intervals it is itself
|
130
|
+
#
|
131
|
+
# @example
|
132
|
+
# Interval[].hull == Interval[] # true
|
133
|
+
# @example
|
134
|
+
# Interval[[1,2],[3,4]].hull == Interval[1,4] # true
|
135
|
+
#
|
136
|
+
# @return [Interval] a simple Interval composed by the inf and sup points
|
102
137
|
def hull
|
103
138
|
if empty? then Interval[] else Interval[inf, sup] end
|
104
139
|
end
|
@@ -6,7 +6,7 @@ class Interval::Simple < Interval
|
|
6
6
|
|
7
7
|
def initialize (a, b = a)
|
8
8
|
if (a.respond_to?(:nan?) && a.nan?) || (b.respond_to?(:nan?) && b.nan?)
|
9
|
-
@inf, @sup = -
|
9
|
+
@inf, @sup = -Float::INFINITY, Float::INFINITY
|
10
10
|
else
|
11
11
|
@inf, @sup = a, b
|
12
12
|
end
|
@@ -26,7 +26,7 @@ class Interval::Simple < Interval
|
|
26
26
|
|
27
27
|
# @return [Interval::Multiple]
|
28
28
|
def complement
|
29
|
-
Interval[[-
|
29
|
+
Interval[[-Float::INFINITY, inf], [sup, Float::INFINITY]]
|
30
30
|
end
|
31
31
|
|
32
32
|
# @return [Interval]
|
@@ -49,16 +49,26 @@ class Interval::Simple < Interval
|
|
49
49
|
extrema.uniq
|
50
50
|
end
|
51
51
|
|
52
|
+
# Verifies that a point is inside the interval
|
53
|
+
#
|
54
|
+
# @param [Comparable] x the point to be verified
|
52
55
|
# @return [Boolean]
|
56
|
+
# @example
|
57
|
+
# Interval[1,5].include?(3.4) # -> true
|
53
58
|
def include?(x)
|
54
59
|
inf <= x && x <= sup
|
55
60
|
end
|
56
61
|
|
62
|
+
# Verifies that the Interval is degenerate, i.e. the extrema are equal
|
63
|
+
# @example
|
64
|
+
# Interval[1,2].degenerate? # -> false
|
65
|
+
# Interval[1].degenerate? # -> true
|
57
66
|
# @return [Boolean]
|
58
67
|
def degenerate?
|
59
68
|
inf == sup
|
60
69
|
end
|
61
70
|
|
71
|
+
# Verifies that this class represents simple intervals
|
62
72
|
# @return [Boolean]
|
63
73
|
def simple?
|
64
74
|
true
|
@@ -3,7 +3,10 @@ require 'borel'
|
|
3
3
|
module Borel
|
4
4
|
# Includes numeric intervals related methods
|
5
5
|
module IntervalArithmetic
|
6
|
+
|
6
7
|
# @return [Number] the width of the interval
|
8
|
+
# @example
|
9
|
+
# Interval[1,5].width # -> 5-1
|
7
10
|
def width
|
8
11
|
raise NonSimple, self unless respond_to?(:simple?) and simple?
|
9
12
|
extrema.last - extrema.first
|
@@ -3,6 +3,9 @@ require 'borel/math_extensions/interval_arithmetic'
|
|
3
3
|
module Borel
|
4
4
|
# Includes methods for picking random elements on intervals
|
5
5
|
module Randomizable
|
6
|
+
|
7
|
+
# @example
|
8
|
+
# Interval[1,5].rand # -> Random.new.rand 1..5
|
6
9
|
# @return [Object] a random element of the interval
|
7
10
|
def rand
|
8
11
|
raise EmptyInterval if respond_to?(:empty?) and empty?
|
data/lib/borel/nil_class.rb
CHANGED
data/lib/borel/range.rb
CHANGED
data/lib/borel/version.rb
CHANGED
data/spec/interval_spec.rb
CHANGED
@@ -20,8 +20,8 @@ describe Interval do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
specify "Interval[-Infinity, Infinity] -> [-Infinity, Infinity]" do
|
23
|
-
Interval[-
|
24
|
-
should eq [-
|
23
|
+
Interval[-Float::INFINITY, Float::INFINITY].construction.
|
24
|
+
should eq [-Float::INFINITY, Float::INFINITY]
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -57,7 +57,7 @@ describe Interval do
|
|
57
57
|
end
|
58
58
|
|
59
59
|
specify "[-infty, infty]^[0,1] = [0,1]" do
|
60
|
-
(Interval[-
|
60
|
+
(Interval[-Float::INFINITY, Float::INFINITY] ^ Interval[0,1]).
|
61
61
|
construction.should eq [0,1]
|
62
62
|
end
|
63
63
|
|
@@ -196,12 +196,12 @@ describe Interval do
|
|
196
196
|
end
|
197
197
|
|
198
198
|
specify '[-infty,infty]-[0,1] = [-infty,0]U[1,infty]' do
|
199
|
-
(Interval[-
|
200
|
-
should eq [[-
|
199
|
+
(Interval[-Float::INFINITY,Float::INFINITY] - Interval[0,1]).construction.
|
200
|
+
should eq [[-Float::INFINITY,0], [1,Float::INFINITY]]
|
201
201
|
end
|
202
202
|
|
203
203
|
specify '[0,1]-[-infty,infty] = []' do
|
204
|
-
(Interval[0,1] - Interval[-
|
204
|
+
(Interval[0,1] - Interval[-Float::INFINITY,Float::INFINITY]).construction.
|
205
205
|
should eq []
|
206
206
|
end
|
207
207
|
end
|
data/spec/numeric_spec.rb
CHANGED
@@ -21,11 +21,11 @@ describe Numeric do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "1/0 should be [Infinity]" do
|
24
|
-
(1/0.0).to_interval.should eq Interval[
|
24
|
+
(1/0.0).to_interval.should eq Interval[Float::INFINITY]
|
25
25
|
end
|
26
26
|
|
27
27
|
it "Infinity should be [Infinity]" do
|
28
|
-
(-1/0.0).to_interval.should eq Interval[-
|
28
|
+
(-1/0.0).to_interval.should eq Interval[-Float::INFINITY]
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
data/spec/range_spec.rb
CHANGED
@@ -11,20 +11,21 @@ describe Range do
|
|
11
11
|
(1...3).to_interval.should eq Interval[1,2]
|
12
12
|
end
|
13
13
|
|
14
|
-
it '(1..
|
15
|
-
(1..
|
14
|
+
it '(1..Float::INFINITY) should be [1,infty]' do
|
15
|
+
(1..Float::INFINITY).to_interval.should eq Interval[1,Float::INFINITY]
|
16
16
|
end
|
17
17
|
|
18
|
-
it '(1...
|
19
|
-
expect{(1...
|
18
|
+
it '(1...Float::INFINITY) should raise OpenRight' do
|
19
|
+
expect{(1...Float::INFINITY).to_interval}.to raise_error Borel::OpenRight
|
20
20
|
end
|
21
21
|
|
22
|
-
it '(-
|
23
|
-
(-
|
22
|
+
it '(-Float::INFINITY..1) should be [-infty,1]' do
|
23
|
+
(-Float::INFINITY..1).to_interval.should eq Interval[-Float::INFINITY,1]
|
24
24
|
end
|
25
25
|
|
26
|
-
it '(-
|
27
|
-
(-
|
26
|
+
it '(-Float::INFINITY..Float::INFINITY) should be [-infty,infty]' do
|
27
|
+
(-Float::INFINITY..Float::INFINITY).to_interval.
|
28
|
+
should eq Interval[-Float::INFINITY,Float::INFINITY]
|
28
29
|
end
|
29
30
|
end
|
30
31
|
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.3.
|
4
|
+
version: 0.3.4
|
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:
|
12
|
+
date: 2013-02-24 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'Borel sets are made of enumerable union and intersection of
|
15
15
|
|
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
73
|
version: 1.3.6
|
74
74
|
requirements: []
|
75
75
|
rubyforge_project: borel
|
76
|
-
rubygems_version: 1.8.
|
76
|
+
rubygems_version: 1.8.25
|
77
77
|
signing_key:
|
78
78
|
specification_version: 3
|
79
79
|
summary: Interval operation on Comparable classes
|