continued_fractions 0.1.11 → 1.2.0
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/History.txt +19 -0
- data/README.txt +1 -0
- data/Rakefile +2 -2
- data/continued_fractions.gemspec +8 -5
- data/lib/continued_fractions.rb +76 -18
- data/spec/continued_fractions/continued_fraction_spec.rb +5 -3
- metadata +20 -5
data/History.txt
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
+
=== 1.2.0 / 2010-03-18
|
2
|
+
|
3
|
+
* Changed parameter signature of ContinuedFraction#convergents_as_rationals. Added a nth parameter to select the length of the convergents to return.
|
4
|
+
|
5
|
+
=== 1.1.0 / 2010-03-18
|
6
|
+
|
7
|
+
* Changed the public class representation of convergents from an array of Rationals to an array of arrays.
|
8
|
+
* Removed convergents_array (unnecessary since ContinuedFraction#convergents now returns an array).
|
9
|
+
* Added ContinuedFraction#convergents_as_rationals which facilitate returning convergents as Rationals.
|
10
|
+
* Changed names of index variables to be more descriptive.
|
11
|
+
* Changed documentation to reflect change of class type returned by ContinuedFraction#convergents.
|
12
|
+
* Changed tests to deal with new class type returned by ContinuedFraction#convergents.
|
13
|
+
|
14
|
+
=== 0.1.12 / 2010-03-18
|
15
|
+
|
16
|
+
* Added examples in the documentation.
|
17
|
+
* Moved convergents to the Class level to make it accessible to use with any quotient array passed to it.
|
18
|
+
|
19
|
+
|
1
20
|
=== 0.1.11 / 2010-03-16
|
2
21
|
|
3
22
|
* Changed names of some variable to provide better in-code documentation.
|
data/README.txt
CHANGED
data/Rakefile
CHANGED
@@ -2,13 +2,13 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('continued_fractions', '
|
5
|
+
Echoe.new('continued_fractions', '1.2.0') do |config|
|
6
6
|
config.summary = 'Generate continued fractions.'
|
7
7
|
config.author = 'Jose Hales-Garcia'
|
8
8
|
config.url = 'http://bitbucket.org/jolohaga/continued_fractions/'
|
9
9
|
config.email = 'jolohaga@me.com'
|
10
10
|
config.ignore_pattern = ["tmp/*",".hg/*", ".pkg/*"]
|
11
|
-
config.development_dependencies = ['rspec']
|
11
|
+
config.development_dependencies = ['rspec >=1.3.0','echoe >=4.3']
|
12
12
|
end
|
13
13
|
|
14
14
|
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|ext| load ext}
|
data/continued_fractions.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{continued_fractions}
|
5
|
-
s.version = "
|
5
|
+
s.version = "1.2.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Jose Hales-Garcia"]
|
9
|
-
s.date = %q{2010-03-
|
9
|
+
s.date = %q{2010-03-18}
|
10
10
|
s.description = %q{Generate continued fractions.}
|
11
11
|
s.email = %q{jolohaga@me.com}
|
12
12
|
s.extra_rdoc_files = ["README.txt", "lib/continued_fractions.rb"]
|
@@ -23,11 +23,14 @@ Gem::Specification.new do |s|
|
|
23
23
|
s.specification_version = 3
|
24
24
|
|
25
25
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
-
s.add_development_dependency(%q<rspec>, [">= 0"])
|
26
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
27
|
+
s.add_development_dependency(%q<echoe>, [">= 4.3"])
|
27
28
|
else
|
28
|
-
s.add_dependency(%q<rspec>, [">= 0"])
|
29
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
30
|
+
s.add_dependency(%q<echoe>, [">= 4.3"])
|
29
31
|
end
|
30
32
|
else
|
31
|
-
s.add_dependency(%q<rspec>, [">= 0"])
|
33
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
34
|
+
s.add_dependency(%q<echoe>, [">= 4.3"])
|
32
35
|
end
|
33
36
|
end
|
data/lib/continued_fractions.rb
CHANGED
@@ -31,27 +31,56 @@ module ContinuedFractions
|
|
31
31
|
@convergents = calculate_convergents
|
32
32
|
end
|
33
33
|
|
34
|
-
# Return nth
|
34
|
+
# Return nth convergent.
|
35
35
|
#
|
36
|
-
|
37
|
-
|
36
|
+
# Example:
|
37
|
+
# cf = ContinuedFraction.new(Math::PI,10)
|
38
|
+
# cf.convergent(3)
|
39
|
+
# => [355,113]
|
40
|
+
#
|
41
|
+
def convergent(nth)
|
42
|
+
convergents[nth]
|
38
43
|
end
|
39
44
|
|
40
|
-
# Return array of convergents as
|
45
|
+
# Return array of convergents of the continued fraction instance up to the nth convergent as an array
|
46
|
+
# comprising of numerators in [i,0] and denominators in [i,1].
|
47
|
+
# If nth is nil, then return the entire list.
|
48
|
+
#
|
49
|
+
# Example:
|
50
|
+
# cf = ContinuedFraction.new(Math::PI,10)
|
51
|
+
# cf.convergents
|
52
|
+
# => [[3,1], [22,7], [333,106], [355,113], [103993,33102],
|
53
|
+
# [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]
|
54
|
+
#
|
55
|
+
# Or:
|
56
|
+
# cf.convergents(3)
|
57
|
+
# => [[3,1], [22,7], [333,106]]
|
41
58
|
#
|
42
|
-
def convergents
|
43
|
-
_convergents
|
59
|
+
def convergents(nth=nil)
|
60
|
+
_convergents(nth)
|
44
61
|
end
|
45
62
|
|
46
|
-
# Return array of convergents
|
63
|
+
# Return array of convergents of the continued fraction instance converted as Rationals.
|
64
|
+
# If nth is nil, then return the entire list.
|
47
65
|
#
|
48
|
-
|
49
|
-
|
66
|
+
# Example:
|
67
|
+
# cf = ContinuedFraction.new(Math::PI,10)
|
68
|
+
# cf.convergents_as_rationals
|
69
|
+
# => [(3/1), (22/7), (333/106), (355/113), (103993/33102),
|
70
|
+
# (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
71
|
+
#
|
72
|
+
# Or:
|
73
|
+
# cf.convergents_as_rationals(3)
|
74
|
+
# => [(3/1), (22/7), (333/106)]
|
75
|
+
#
|
76
|
+
def convergents_as_rationals(nth=nil)
|
77
|
+
ContinuedFractions.convergents_array_to_rationals(convergents,nth)
|
50
78
|
end
|
51
79
|
|
52
80
|
private
|
53
|
-
def _convergents
|
54
|
-
@convergents
|
81
|
+
def _convergents(nth=nil)
|
82
|
+
nth ||= @convergents.length
|
83
|
+
@convergents[0...nth]
|
55
84
|
end
|
56
85
|
|
57
86
|
def calculate_quotients
|
@@ -65,13 +94,7 @@ module ContinuedFractions
|
|
65
94
|
end
|
66
95
|
|
67
96
|
def calculate_convergents
|
68
|
-
|
69
|
-
2.upto(@limit+1) do |i|
|
70
|
-
i_1,i_2 = i-1,i-2
|
71
|
-
convs[i][0] = convs[i_1][0]*quotients[i_2]+convs[i_2][0]
|
72
|
-
convs[i][1] = convs[i_1][1]*quotients[i_2]+convs[i_2][1]
|
73
|
-
end
|
74
|
-
convs
|
97
|
+
ContinuedFractions.convergents(quotients)
|
75
98
|
end
|
76
99
|
end
|
77
100
|
|
@@ -81,5 +104,40 @@ module ContinuedFractions
|
|
81
104
|
conv_mat[0][0],conv_mat[1][1] = 0,0
|
82
105
|
conv_mat
|
83
106
|
end
|
107
|
+
|
108
|
+
# Given an array of quotients return the array of convergents up to the nth convergent.
|
109
|
+
# If nth is nil, return the entire list.
|
110
|
+
#
|
111
|
+
# Example:
|
112
|
+
# ContinuedFractions.convergents([3,7,15,1])
|
113
|
+
# => [[3, 1], [22, 7], [333, 106], [355, 113]]
|
114
|
+
#
|
115
|
+
# Or:
|
116
|
+
# ContinuedFractions.convergents([3,7,15,1], 3)
|
117
|
+
# => [[3, 1], [22, 7], [333, 106]]
|
118
|
+
#
|
119
|
+
def convergents(quotients,nth=nil)
|
120
|
+
convs = ContinuedFractions.convergence_matrix(quotients.length+2,2,1)
|
121
|
+
nth ||= convs.length
|
122
|
+
2.upto(quotients.length+1) do |i|
|
123
|
+
i_minus1,i_minus2 = i-1,i-2
|
124
|
+
convs[i][0] = convs[i_minus1][0]*quotients[i_minus2]+convs[i_minus2][0]
|
125
|
+
convs[i][1] = convs[i_minus1][1]*quotients[i_minus2]+convs[i_minus2][1]
|
126
|
+
end
|
127
|
+
convs[2...nth+2]
|
128
|
+
end
|
129
|
+
|
130
|
+
# Convert the convergents array to Rationals
|
131
|
+
#
|
132
|
+
def convergents_array_to_rationals(convergents,nth=nil) #:nodoc:
|
133
|
+
nth ||= convergents.length
|
134
|
+
convergents[0...nth].map{|convergent| convergent_to_rational(convergent)}
|
135
|
+
end
|
136
|
+
|
137
|
+
# Convert a convergent element to a Rational
|
138
|
+
#
|
139
|
+
def convergent_to_rational(convergent) #:nodoc:
|
140
|
+
Rational(convergent[0],convergent[1])
|
141
|
+
end
|
84
142
|
end
|
85
143
|
end
|
@@ -6,7 +6,7 @@ module ContinuedFractions
|
|
6
6
|
# First 10 convergents of PI are...
|
7
7
|
convs = ['3/1', '22/7', '333/106', '355/113', '103993/33102', '104348/33215', '208341/66317', '312689/99532', '833719/265381', '1146408/364913'].map{|c| Rational(c)}
|
8
8
|
@cf = ContinuedFraction.new(Math::PI,10)
|
9
|
-
(@cf.convergents - convs).empty?.should == true
|
9
|
+
(ContinuedFractions.convergents_array_to_rationals(@cf.convergents) - convs).empty?.should == true
|
10
10
|
end
|
11
11
|
|
12
12
|
before(:each) do
|
@@ -24,13 +24,15 @@ module ContinuedFractions
|
|
24
24
|
|
25
25
|
it "should contain convergents approaching the number" do
|
26
26
|
0.upto(@cf.convergents.length-2) do |i|
|
27
|
-
(
|
27
|
+
convergent_rational_i_plus1, convergent_rational_i = ContinuedFractions.convergent_to_rational(@cf.convergents[i+1]), ContinuedFractions.convergent_to_rational(@cf.convergents[i])
|
28
|
+
((convergent_rational_i_plus1 - @cf.number).abs <= (convergent_rational_i - @cf.number).abs).should == true
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
31
32
|
it "should contain convergents which are expressed in lowest terms" do
|
32
33
|
1.upto(@cf.convergents.length-1) do |i|
|
33
|
-
|
34
|
+
convergent_rational_i_minus1, convergent_rational_i = ContinuedFractions.convergent_to_rational(@cf.convergent(i-1)), ContinuedFractions.convergent_to_rational(@cf.convergent(i))
|
35
|
+
(convergent_rational_i_minus1.numerator*convergent_rational_i.denominator - convergent_rational_i.numerator*convergent_rational_i_minus1.denominator).should == (-1)**(i)
|
34
36
|
end
|
35
37
|
end
|
36
38
|
end
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: continued_fractions
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
-
- 0
|
7
6
|
- 1
|
8
|
-
-
|
9
|
-
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 1.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jose Hales-Garcia
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-18 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -25,10 +25,25 @@ dependencies:
|
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
28
30
|
- 0
|
29
|
-
version:
|
31
|
+
version: 1.3.0
|
30
32
|
type: :development
|
31
33
|
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: echoe
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 4
|
43
|
+
- 3
|
44
|
+
version: "4.3"
|
45
|
+
type: :development
|
46
|
+
version_requirements: *id002
|
32
47
|
description: Generate continued fractions.
|
33
48
|
email: jolohaga@me.com
|
34
49
|
executables: []
|