continued_fractions 1.3.1 → 1.4.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.
- checksums.yaml +7 -0
- data/CHANGELOG +0 -0
- data/Gemfile +11 -0
- data/LICENSE +22 -0
- data/Manifest +4 -1
- data/README.md +54 -0
- data/Rakefile +11 -5
- data/continued_fractions.gemspec +23 -21
- data/lib/continued_fractions.rb +132 -132
- data/spec/continued_fractions/continued_fraction_spec.rb +71 -75
- data/spec/spec_helper.rb +1 -2
- metadata +61 -76
- data/README.txt +0 -84
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e21b768abb130daf08c605cec1fbee721b24cff1
|
4
|
+
data.tar.gz: cff6e442d4ce41205cca55fa9157b289eba65a04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6c311ab38c8c5a670ed0da1bc7a70631b098c5a15322d5412c97a27f9b4b39e36b1e6111a3be6d6b4e588e88f4db4caf1324e205342d5bf801e75346acdaf75
|
7
|
+
data.tar.gz: bc70d86423da28fe8e112dee2a91d54d2d572ab36cced3b62a9bc4b7e950b1bf2c559001bb48e2e5bb11ac3e901806176e6285c9599bd66e0c93d02275af86f8
|
data/CHANGELOG
ADDED
File without changes
|
data/Gemfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in digital_measures.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
== License:
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jose Hales-Garcia
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
CHANGED
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
## ContinuedFractions
|
2
|
+
|
3
|
+
* http://rubygems.org/gems/continued_fractions
|
4
|
+
|
5
|
+
#### Description
|
6
|
+
|
7
|
+
Generates quotients and convergents for a given number.
|
8
|
+
|
9
|
+
#### Synopsis
|
10
|
+
|
11
|
+
require 'continued_fractions'
|
12
|
+
|
13
|
+
pi = Math::PI
|
14
|
+
=> 3.14159265358979
|
15
|
+
|
16
|
+
cf = ContinuedFraction.new(pi,10)
|
17
|
+
=> #<ContinuedFractions::ContinuedFraction:0x00000101101368 @number=3.14159265358979, @limit=10,
|
18
|
+
@quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
|
19
|
+
@convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
|
20
|
+
[355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
|
21
|
+
|
22
|
+
cf.convergents
|
23
|
+
=> [[3,1], [22,7], [333,106], [355,113], [103993,33102], [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]
|
24
|
+
|
25
|
+
# Convergents can be output as an array of Rationals too
|
26
|
+
cf.convergents_as_rationals
|
27
|
+
[(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
28
|
+
|
29
|
+
cf.convergent(1)
|
30
|
+
=> [22,7]
|
31
|
+
|
32
|
+
cf.quotients
|
33
|
+
=> [3, 7, 15, 1, 292, 1, 1, 1, 2, 1]
|
34
|
+
|
35
|
+
# Add, subtract, multiply and divide continued fractions. The limit of the result is the max limit of the operands.
|
36
|
+
cf2 = ContinuedFraction.new(rand,20)
|
37
|
+
=> #<ContinuedFractions::ContinuedFraction:0x000001009dde50 @number=0.03834175394982653, @limit=20, @quotients=[0, 26, 12, 3, 4, 1, 2, 24, 5, 1, 1, 1, 2, 1, 2, 2, 1, 7, 1, 5], @convergents=[[0, 1], [1, 26], [12, 313], [37, 965], [160, 4173], [197, 5138], [554, 14449], [13493, 351914], [68019, 1774019], [81512, 2125933], [149531, 3899952], [231043, 6025885], [611617, 15951722], [842660, 21977607], [2296937, 59906936], [5436534, 141791479], [7733471, 201698415], [59570831, 1553680384], [67304302, 1755378799], [396092341, 10330574379]]>
|
38
|
+
cf + cf2
|
39
|
+
=> #<ContinuedFractions::ContinuedFraction:0x000001009d2708 @number=3.1799344075396196, @limit=20, @quotients=[3, 5, 1, 1, 3, 1, 5, 3, 7, 25, 1, 2, 15, 6, 1, 1, 2, 3, 1, 2], @convergents=[[3, 1], [16, 5], [19, 6], [35, 11], [124, 39], [159, 50], [919, 289], [2916, 917], [21331, 6708], [536191, 168617], [557522, 175325], [1651235, 519267], [25326047, 7964330], [153607517, 48305247], [178933564, 56269577], [332541081, 104574824], [844015726, 265419225], [2864588259, 900832499], [3708603985, 1166251724], [10281796229, 3233335947]]>
|
40
|
+
|
41
|
+
#### Install
|
42
|
+
|
43
|
+
sudo gem install continued_fractions
|
44
|
+
|
45
|
+
#### Requirements/Dependencies
|
46
|
+
|
47
|
+
* Ruby 2.2+
|
48
|
+
* Depends on Ruby's Rational class
|
49
|
+
|
50
|
+
##### Developers
|
51
|
+
|
52
|
+
* RSpec required to run tests
|
53
|
+
* Echoe required to run rake tasks
|
54
|
+
|
data/Rakefile
CHANGED
@@ -2,14 +2,20 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('continued_fractions', '1.
|
6
|
-
config.summary = 'Generate continued fractions
|
7
|
-
config.description = 'Class for working with continued fractions
|
5
|
+
Echoe.new('continued_fractions', '1.4.0') do |config|
|
6
|
+
config.summary = 'Generate continued fractions'
|
7
|
+
config.description = 'Class for working with continued fractions'
|
8
|
+
|
9
|
+
config.ruby_version = ">=2.2.2"
|
10
|
+
config.licenses = 'MIT'
|
11
|
+
|
8
12
|
config.author = 'Jose Hales-Garcia'
|
13
|
+
config.email = 'jolohaga@me.com'
|
9
14
|
config.url = 'http://github.com/jolohaga/continued_fractions'
|
10
|
-
|
15
|
+
|
11
16
|
config.ignore_pattern = ["tmp/*",".hg/*", ".pkg/*", ".git/*"]
|
12
|
-
|
17
|
+
|
18
|
+
config.development_dependencies = ['rspec ~>3.2.0','echoe ~>4.6.6']
|
13
19
|
end
|
14
20
|
|
15
21
|
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|ext| load ext}
|
data/continued_fractions.gemspec
CHANGED
@@ -1,36 +1,38 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
# stub: continued_fractions 1.4.0 ruby lib
|
2
3
|
|
3
4
|
Gem::Specification.new do |s|
|
4
|
-
s.name =
|
5
|
-
s.version = "1.
|
5
|
+
s.name = "continued_fractions"
|
6
|
+
s.version = "1.4.0"
|
6
7
|
|
7
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
-
s.authors = ["Jose Hales-Garcia"]
|
9
|
-
s.date = %q{2011-06-10}
|
10
|
-
s.description = %q{Class for working with continued fractions.}
|
11
|
-
s.email = %q{jolohaga@me.com}
|
12
|
-
s.extra_rdoc_files = ["README.txt", "lib/continued_fractions.rb"]
|
13
|
-
s.files = ["History.txt", "Manifest", "README.txt", "Rakefile", "lib/continued_fractions.rb", "spec/continued_fractions/continued_fraction_spec.rb", "spec/spec_helper.rb", "continued_fractions.gemspec"]
|
14
|
-
s.homepage = %q{http://github.com/jolohaga/continued_fractions}
|
15
|
-
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Continued_fractions", "--main", "README.txt"]
|
16
9
|
s.require_paths = ["lib"]
|
17
|
-
s.
|
18
|
-
s.
|
19
|
-
s.
|
10
|
+
s.authors = ["Jose Hales-Garcia"]
|
11
|
+
s.date = "2015-05-03"
|
12
|
+
s.description = "Class for working with continued fractions"
|
13
|
+
s.email = "jolohaga@me.com"
|
14
|
+
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.md", "lib/continued_fractions.rb"]
|
15
|
+
s.files = ["CHANGELOG", "Gemfile", "History.txt", "LICENSE", "Manifest", "README.md", "Rakefile", "continued_fractions.gemspec", "lib/continued_fractions.rb", "spec/continued_fractions/continued_fraction_spec.rb", "spec/spec_helper.rb"]
|
16
|
+
s.homepage = "http://github.com/jolohaga/continued_fractions"
|
17
|
+
s.licenses = ["MIT"]
|
18
|
+
s.rdoc_options = ["--line-numbers", "--title", "Continued_fractions", "--main", "README.md"]
|
19
|
+
s.required_ruby_version = Gem::Requirement.new(">= 2.2.2")
|
20
|
+
s.rubyforge_project = "continued_fractions"
|
21
|
+
s.rubygems_version = "2.4.5"
|
22
|
+
s.summary = "Generate continued fractions"
|
20
23
|
|
21
24
|
if s.respond_to? :specification_version then
|
22
|
-
|
23
|
-
s.specification_version = 3
|
25
|
+
s.specification_version = 4
|
24
26
|
|
25
27
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
-
s.add_development_dependency(%q<rspec>, ["
|
27
|
-
s.add_development_dependency(%q<echoe>, ["
|
28
|
+
s.add_development_dependency(%q<rspec>, ["~> 3.2.0"])
|
29
|
+
s.add_development_dependency(%q<echoe>, ["~> 4.6.6"])
|
28
30
|
else
|
29
|
-
s.add_dependency(%q<rspec>, ["
|
30
|
-
s.add_dependency(%q<echoe>, ["
|
31
|
+
s.add_dependency(%q<rspec>, ["~> 3.2.0"])
|
32
|
+
s.add_dependency(%q<echoe>, ["~> 4.6.6"])
|
31
33
|
end
|
32
34
|
else
|
33
|
-
s.add_dependency(%q<rspec>, ["
|
34
|
-
s.add_dependency(%q<echoe>, ["
|
35
|
+
s.add_dependency(%q<rspec>, ["~> 3.2.0"])
|
36
|
+
s.add_dependency(%q<echoe>, ["~> 4.6.6"])
|
35
37
|
end
|
36
38
|
end
|
data/lib/continued_fractions.rb
CHANGED
@@ -9,138 +9,6 @@
|
|
9
9
|
#
|
10
10
|
#
|
11
11
|
module ContinuedFractions
|
12
|
-
class ContinuedFraction
|
13
|
-
attr_accessor :number, :quotients, :limit
|
14
|
-
attr_writer :convergents
|
15
|
-
|
16
|
-
# For a given number calculate its continued fraction quotients and convergents up to limit.
|
17
|
-
#
|
18
|
-
# The limit is 5 by default. Pass an integer in the second parameter to change it.
|
19
|
-
#
|
20
|
-
# Example:
|
21
|
-
# cf = ContinuedFraction.new(Math::PI,10)
|
22
|
-
# => #<ContinuedFractions::ContinuedFraction:0x000001010ed5c8 @number=3.14159265358979, @limit=10,
|
23
|
-
# @quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
|
24
|
-
# @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
|
25
|
-
# [355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
|
26
|
-
#
|
27
|
-
def initialize(number,limit=5)
|
28
|
-
@number = number
|
29
|
-
@limit = limit
|
30
|
-
@quotients = calculate_quotients
|
31
|
-
@convergents = calculate_convergents
|
32
|
-
end
|
33
|
-
|
34
|
-
# Return nth convergent.
|
35
|
-
#
|
36
|
-
# Example:
|
37
|
-
# cf = ContinuedFraction.new(Math::PI,10)
|
38
|
-
# cf.convergent(3)
|
39
|
-
# => [355,113]
|
40
|
-
#
|
41
|
-
def convergent(nth)
|
42
|
-
raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
|
43
|
-
convergents[nth-1]
|
44
|
-
end
|
45
|
-
|
46
|
-
# Return array of convergents of the continued fraction instance up to the nth convergent as an array
|
47
|
-
# comprising of numerators in [i,0] and denominators in [i,1].
|
48
|
-
# If nth is nil, then return the entire list.
|
49
|
-
#
|
50
|
-
# Example:
|
51
|
-
# cf = ContinuedFraction.new(Math::PI,10)
|
52
|
-
# cf.convergents
|
53
|
-
# => [[3,1], [22,7], [333,106], [355,113], [103993,33102],
|
54
|
-
# [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]
|
55
|
-
#
|
56
|
-
# Or:
|
57
|
-
# cf.convergents(3)
|
58
|
-
# => [[3,1], [22,7], [333,106]]
|
59
|
-
#
|
60
|
-
def convergents(nth=nil)
|
61
|
-
_convergents(nth)
|
62
|
-
end
|
63
|
-
|
64
|
-
# Return array of convergents of the continued fraction instance converted as Rationals.
|
65
|
-
# If nth is nil, then return the entire list.
|
66
|
-
#
|
67
|
-
# Example:
|
68
|
-
# cf = ContinuedFraction.new(Math::PI,10)
|
69
|
-
# cf.convergents_as_rationals
|
70
|
-
# => [(3/1), (22/7), (333/106), (355/113), (103993/33102),
|
71
|
-
# (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
72
|
-
#
|
73
|
-
# Or:
|
74
|
-
# cf.convergents_as_rationals(3)
|
75
|
-
# => [(3/1), (22/7), (333/106)]
|
76
|
-
#
|
77
|
-
def convergents_as_rationals(nth=nil)
|
78
|
-
ContinuedFractions.convergents_array_to_rationals(convergents,nth)
|
79
|
-
end
|
80
|
-
|
81
|
-
def +(other)
|
82
|
-
_number_of(other) do |num,prec|
|
83
|
-
_evaluate("#{@number} + #{num}",prec)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def -(other)
|
88
|
-
_number_of(other) do |num,prec|
|
89
|
-
_evaluate("#{@number} - #{num}",prec)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def /(other)
|
94
|
-
_number_of(other) do |num,prec|
|
95
|
-
_evaluate("#{@number} / #{num}",prec)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def *(other)
|
100
|
-
_number_of(other) do |num,prec|
|
101
|
-
_evaluate("#{@number} * #{num}",prec)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
private
|
106
|
-
def _evaluate(e,p) #:nodoc:
|
107
|
-
ContinuedFraction.new(eval(e), p)
|
108
|
-
end
|
109
|
-
|
110
|
-
def _number_of(n) #:nodoc:
|
111
|
-
num = nil
|
112
|
-
prec = nil
|
113
|
-
case n.class.name
|
114
|
-
when "Fixnum","Integer"
|
115
|
-
num = n
|
116
|
-
prec = @limit
|
117
|
-
when "ContinuedFractions::ContinuedFraction"
|
118
|
-
num = n.number
|
119
|
-
prec = [n.limit,@limit].max
|
120
|
-
end
|
121
|
-
yield(num,prec)
|
122
|
-
end
|
123
|
-
|
124
|
-
def _convergents(nth=nil) #:nodoc:
|
125
|
-
nth ||= @convergents.length
|
126
|
-
@convergents[0...nth]
|
127
|
-
end
|
128
|
-
|
129
|
-
def calculate_quotients #:nodoc:
|
130
|
-
qs = Array.new(@limit)
|
131
|
-
n = @number
|
132
|
-
@limit.times do |i|
|
133
|
-
qs[i] = n.to_i
|
134
|
-
n = 1.0/(n-qs[i])
|
135
|
-
end
|
136
|
-
qs
|
137
|
-
end
|
138
|
-
|
139
|
-
def calculate_convergents #:nodoc:
|
140
|
-
ContinuedFractions.convergents(quotients)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
12
|
class << self
|
145
13
|
def convergence_matrix(n,m,fill=nil) #:nodoc:
|
146
14
|
conv_mat = Array.new(n).map!{Array.new(m,fill)}
|
@@ -183,4 +51,136 @@ module ContinuedFractions
|
|
183
51
|
Rational(convergent[0],convergent[1])
|
184
52
|
end
|
185
53
|
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class ContinuedFraction
|
57
|
+
attr_accessor :number, :quotients, :limit
|
58
|
+
attr_writer :convergents
|
59
|
+
|
60
|
+
# For a given number calculate its continued fraction quotients and convergents up to limit.
|
61
|
+
#
|
62
|
+
# The limit is 5 by default. Pass an integer in the second parameter to change it.
|
63
|
+
#
|
64
|
+
# Example:
|
65
|
+
# cf = ContinuedFraction.new(Math::PI,10)
|
66
|
+
# => #<ContinuedFractions::ContinuedFraction:0x000001010ed5c8 @number=3.14159265358979, @limit=10,
|
67
|
+
# @quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
|
68
|
+
# @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
|
69
|
+
# [355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
|
70
|
+
#
|
71
|
+
def initialize(number,limit=5)
|
72
|
+
@number = number
|
73
|
+
@limit = limit
|
74
|
+
@quotients = calculate_quotients
|
75
|
+
@convergents = calculate_convergents
|
76
|
+
end
|
77
|
+
|
78
|
+
# Return nth convergent.
|
79
|
+
#
|
80
|
+
# Example:
|
81
|
+
# cf = ContinuedFraction.new(Math::PI,10)
|
82
|
+
# cf.convergent(3)
|
83
|
+
# => [355,113]
|
84
|
+
#
|
85
|
+
def convergent(nth)
|
86
|
+
raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
|
87
|
+
convergents[nth-1]
|
88
|
+
end
|
89
|
+
|
90
|
+
# Return array of convergents of the continued fraction instance up to the nth convergent as an array
|
91
|
+
# comprising of numerators in [i,0] and denominators in [i,1].
|
92
|
+
# If nth is nil, then return the entire list.
|
93
|
+
#
|
94
|
+
# Example:
|
95
|
+
# cf = ContinuedFraction.new(Math::PI,10)
|
96
|
+
# cf.convergents
|
97
|
+
# => [[3,1], [22,7], [333,106], [355,113], [103993,33102],
|
98
|
+
# [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]
|
99
|
+
#
|
100
|
+
# Or:
|
101
|
+
# cf.convergents(3)
|
102
|
+
# => [[3,1], [22,7], [333,106]]
|
103
|
+
#
|
104
|
+
def convergents(nth=nil)
|
105
|
+
_convergents(nth)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Return array of convergents of the continued fraction instance converted as Rationals.
|
109
|
+
# If nth is nil, then return the entire list.
|
110
|
+
#
|
111
|
+
# Example:
|
112
|
+
# cf = ContinuedFraction.new(Math::PI,10)
|
113
|
+
# cf.convergents_as_rationals
|
114
|
+
# => [(3/1), (22/7), (333/106), (355/113), (103993/33102),
|
115
|
+
# (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
116
|
+
#
|
117
|
+
# Or:
|
118
|
+
# cf.convergents_as_rationals(3)
|
119
|
+
# => [(3/1), (22/7), (333/106)]
|
120
|
+
#
|
121
|
+
def convergents_as_rationals(nth=nil)
|
122
|
+
ContinuedFractions.convergents_array_to_rationals(convergents,nth)
|
123
|
+
end
|
124
|
+
|
125
|
+
def +(other)
|
126
|
+
_number_of(other) do |num,prec|
|
127
|
+
_evaluate("#{@number} + #{num}",prec)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def -(other)
|
132
|
+
_number_of(other) do |num,prec|
|
133
|
+
_evaluate("#{@number} - #{num}",prec)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def /(other)
|
138
|
+
_number_of(other) do |num,prec|
|
139
|
+
_evaluate("#{@number} / #{num}",prec)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def *(other)
|
144
|
+
_number_of(other) do |num,prec|
|
145
|
+
_evaluate("#{@number} * #{num}",prec)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
def _evaluate(e,p) #:nodoc:
|
151
|
+
ContinuedFraction.new(eval(e), p)
|
152
|
+
end
|
153
|
+
|
154
|
+
def _number_of(n) #:nodoc:
|
155
|
+
num = nil
|
156
|
+
prec = nil
|
157
|
+
case n.class.name
|
158
|
+
when "Fixnum","Integer"
|
159
|
+
num = n
|
160
|
+
prec = @limit
|
161
|
+
when "ContinuedFraction"
|
162
|
+
num = n.number
|
163
|
+
prec = [n.limit,@limit].max
|
164
|
+
end
|
165
|
+
yield(num,prec)
|
166
|
+
end
|
167
|
+
|
168
|
+
def _convergents(nth=nil) #:nodoc:
|
169
|
+
nth ||= @convergents.length
|
170
|
+
@convergents[0...nth]
|
171
|
+
end
|
172
|
+
|
173
|
+
def calculate_quotients #:nodoc:
|
174
|
+
qs = Array.new(@limit)
|
175
|
+
n = @number
|
176
|
+
@limit.times do |i|
|
177
|
+
qs[i] = n.to_i
|
178
|
+
n = 1.0/(n-qs[i])
|
179
|
+
end
|
180
|
+
qs
|
181
|
+
end
|
182
|
+
|
183
|
+
def calculate_convergents #:nodoc:
|
184
|
+
ContinuedFractions.convergents(quotients)
|
185
|
+
end
|
186
186
|
end
|
@@ -1,81 +1,77 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../spec_helper")
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
0.upto(@cf.convergents.length-2) do |i|
|
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
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should contain convergents which are expressed in lowest terms" do
|
33
|
-
1.upto(@cf.convergents.length-1) do |i|
|
34
|
-
convergent_rational_i, convergent_rational_i_plus1 = ContinuedFractions.convergent_to_rational(@cf.convergent(i)), ContinuedFractions.convergent_to_rational(@cf.convergent(i+1))
|
35
|
-
(convergent_rational_i.numerator*convergent_rational_i_plus1.denominator - convergent_rational_i_plus1.numerator*convergent_rational_i.denominator).should == (-1)**(i)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should add a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
|
40
|
-
(@cf + 3).class.should == ContinuedFractions::ContinuedFraction
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should subtract a number (on the right-hand-side) from a continued fraction and return a continued fraction" do
|
44
|
-
(@cf - 3).class.should == ContinuedFractions::ContinuedFraction
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should multiply a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
|
48
|
-
(@cf * 3).class.should == ContinuedFractions::ContinuedFraction
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should divide a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
|
52
|
-
(@cf / 3).class.should == ContinuedFractions::ContinuedFraction
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should add a continued fraction with another continued fraction and return a continued fraction" do
|
56
|
-
c = ContinuedFraction.new(rand,20)
|
57
|
-
(@cf + c).class.should == ContinuedFractions::ContinuedFraction
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should subtract a continued fraction with another continued fraction and return a continued fraction" do
|
61
|
-
c = ContinuedFraction.new(rand,20)
|
62
|
-
(@cf - c).class.should == ContinuedFractions::ContinuedFraction
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should multiply a continued fraction with another continued fraction and return a continued fraction" do
|
66
|
-
c = ContinuedFraction.new(rand,20)
|
67
|
-
(@cf * c).class.should == ContinuedFractions::ContinuedFraction
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should divide a continued fraction with another continued fraction and return a continued fraction" do
|
71
|
-
c = ContinuedFraction.new(rand,20)
|
72
|
-
(@cf / c).class.should == ContinuedFractions::ContinuedFraction
|
3
|
+
describe ContinuedFraction do
|
4
|
+
let(:cf) { described_class.new(number,10) }
|
5
|
+
let(:number) { rand }
|
6
|
+
|
7
|
+
it "should accurately calculate the convergents of a real number" do
|
8
|
+
# First 10 convergents of PI are...
|
9
|
+
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)}
|
10
|
+
cf = described_class.new(Math::PI,10)
|
11
|
+
expect((ContinuedFractions.convergents_array_to_rationals(cf.convergents) - convs)).to be_empty
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return array for convergents method" do
|
15
|
+
expect(cf.convergents).to be_kind_of(Array)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should preserve the number input" do
|
19
|
+
expect(cf.number).to eq number
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should contain convergents approaching the number" do
|
23
|
+
0.upto(cf.convergents.length-2) do |i|
|
24
|
+
convergent_rational_i_plus1, convergent_rational_i = ContinuedFractions.convergent_to_rational(cf.convergents[i+1]), ContinuedFractions.convergent_to_rational(cf.convergents[i])
|
25
|
+
expect(((convergent_rational_i_plus1 - cf.number).abs <= (convergent_rational_i - cf.number).abs)).to be true
|
73
26
|
end
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should contain convergents which are expressed in lowest terms" do
|
30
|
+
1.upto(cf.convergents.length-1) do |i|
|
31
|
+
convergent_rational_i, convergent_rational_i_plus1 = ContinuedFractions.convergent_to_rational(cf.convergent(i)), ContinuedFractions.convergent_to_rational(cf.convergent(i+1))
|
32
|
+
expect((convergent_rational_i.numerator*convergent_rational_i_plus1.denominator - convergent_rational_i_plus1.numerator*convergent_rational_i.denominator)).to eq (-1)**(i)
|
79
33
|
end
|
80
34
|
end
|
35
|
+
|
36
|
+
it "should add a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
|
37
|
+
expect((cf + 3)).to be_kind_of(ContinuedFraction)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should subtract a number (on the right-hand-side) from a continued fraction and return a continued fraction" do
|
41
|
+
expect((cf - 3)).to be_kind_of(ContinuedFraction)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should multiply a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
|
45
|
+
expect((cf * 3)).to be_kind_of(ContinuedFraction)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should divide a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
|
49
|
+
expect((cf / 3)).to be_kind_of(ContinuedFraction)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should add a continued fraction with another continued fraction and return a continued fraction" do
|
53
|
+
c = described_class.new(rand,20)
|
54
|
+
expect((cf + c)).to be_kind_of(ContinuedFraction)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should subtract a continued fraction with another continued fraction and return a continued fraction" do
|
58
|
+
c = described_class.new(rand,20)
|
59
|
+
expect((cf - c)).to be_kind_of(ContinuedFraction)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should multiply a continued fraction with another continued fraction and return a continued fraction" do
|
63
|
+
c = described_class.new(rand,20)
|
64
|
+
expect((cf * c)).to be_kind_of(ContinuedFraction)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should divide a continued fraction with another continued fraction and return a continued fraction" do
|
68
|
+
c = described_class.new(rand,20)
|
69
|
+
expect((cf / c)).to be_kind_of(ContinuedFraction)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should assign the result continued fraction of a binary operation the max limit of the two operands" do
|
73
|
+
c = described_class.new(rand,20)
|
74
|
+
result = cf + c
|
75
|
+
expect(result.limit).to eq [cf.limit,c.limit].max
|
76
|
+
end
|
81
77
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,106 +1,91 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: continued_fractions
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 1
|
7
|
-
- 3
|
8
|
-
- 1
|
9
|
-
version: 1.3.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.4.0
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
6
|
+
authors:
|
12
7
|
- Jose Hales-Garcia
|
13
8
|
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2015-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: rspec
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 1
|
30
|
-
- 3
|
31
|
-
- 0
|
32
|
-
version: 1.3.0
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
33
20
|
type: :development
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: echoe
|
37
21
|
prerelease: false
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: echoe
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.6.6
|
47
34
|
type: :development
|
48
|
-
|
49
|
-
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.6.6
|
41
|
+
description: Class for working with continued fractions
|
50
42
|
email: jolohaga@me.com
|
51
43
|
executables: []
|
52
|
-
|
53
44
|
extensions: []
|
54
|
-
|
55
|
-
|
56
|
-
-
|
45
|
+
extra_rdoc_files:
|
46
|
+
- CHANGELOG
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
57
49
|
- lib/continued_fractions.rb
|
58
|
-
files:
|
50
|
+
files:
|
51
|
+
- CHANGELOG
|
52
|
+
- Gemfile
|
59
53
|
- History.txt
|
54
|
+
- LICENSE
|
60
55
|
- Manifest
|
61
|
-
- README.
|
56
|
+
- README.md
|
62
57
|
- Rakefile
|
58
|
+
- continued_fractions.gemspec
|
63
59
|
- lib/continued_fractions.rb
|
64
60
|
- spec/continued_fractions/continued_fraction_spec.rb
|
65
61
|
- spec/spec_helper.rb
|
66
|
-
- continued_fractions.gemspec
|
67
|
-
has_rdoc: true
|
68
62
|
homepage: http://github.com/jolohaga/continued_fractions
|
69
|
-
licenses:
|
70
|
-
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
71
66
|
post_install_message:
|
72
|
-
rdoc_options:
|
73
|
-
- --line-numbers
|
74
|
-
- --
|
75
|
-
- --title
|
67
|
+
rdoc_options:
|
68
|
+
- "--line-numbers"
|
69
|
+
- "--title"
|
76
70
|
- Continued_fractions
|
77
|
-
- --main
|
78
|
-
- README.
|
79
|
-
require_paths:
|
71
|
+
- "--main"
|
72
|
+
- README.md
|
73
|
+
require_paths:
|
80
74
|
- lib
|
81
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
-
|
83
|
-
requirements:
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
84
77
|
- - ">="
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
|
-
requirements:
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 2.2.2
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
92
82
|
- - ">="
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
|
95
|
-
- 1
|
96
|
-
- 2
|
97
|
-
version: "1.2"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '1.2'
|
98
85
|
requirements: []
|
99
|
-
|
100
86
|
rubyforge_project: continued_fractions
|
101
|
-
rubygems_version:
|
87
|
+
rubygems_version: 2.4.5
|
102
88
|
signing_key:
|
103
|
-
specification_version:
|
104
|
-
summary: Generate continued fractions
|
89
|
+
specification_version: 4
|
90
|
+
summary: Generate continued fractions
|
105
91
|
test_files: []
|
106
|
-
|
data/README.txt
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
= ContinuedFractions
|
2
|
-
|
3
|
-
* http://rubygems.org/gems/continued_fractions
|
4
|
-
|
5
|
-
== Description:
|
6
|
-
|
7
|
-
Generates quotients and convergents for a given number.
|
8
|
-
|
9
|
-
== Features/Problems:
|
10
|
-
|
11
|
-
* Requires Ruby 1.9+
|
12
|
-
* Depends on Ruby 1.9's Rational class
|
13
|
-
|
14
|
-
== Synopsis:
|
15
|
-
|
16
|
-
require 'continued_fractions'
|
17
|
-
include ContinuedFractions
|
18
|
-
|
19
|
-
pi = Math::PI
|
20
|
-
=> 3.14159265358979
|
21
|
-
|
22
|
-
cf = ContinuedFraction.new(pi,10)
|
23
|
-
=> #<ContinuedFractions::ContinuedFraction:0x00000101101368 @number=3.14159265358979, @limit=10,
|
24
|
-
@quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
|
25
|
-
@convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
|
26
|
-
[355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
|
27
|
-
|
28
|
-
cf.convergents
|
29
|
-
=> [[3,1], [22,7], [333,106], [355,113], [103993,33102], [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]
|
30
|
-
|
31
|
-
# Convergents can be output as an array of Rationals too
|
32
|
-
cf.convergents_as_rationals
|
33
|
-
[(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
34
|
-
|
35
|
-
cf.convergent(1)
|
36
|
-
=> [22,7]
|
37
|
-
|
38
|
-
cf.quotients
|
39
|
-
=> [3, 7, 15, 1, 292, 1, 1, 1, 2, 1]
|
40
|
-
|
41
|
-
# Add, subtract, multiply and divide continued fractions. The limit of the result is the max limit of the operands.
|
42
|
-
cf2 = ContinuedFraction.new(rand,20)
|
43
|
-
=> #<ContinuedFractions::ContinuedFraction:0x000001009dde50 @number=0.03834175394982653, @limit=20, @quotients=[0, 26, 12, 3, 4, 1, 2, 24, 5, 1, 1, 1, 2, 1, 2, 2, 1, 7, 1, 5], @convergents=[[0, 1], [1, 26], [12, 313], [37, 965], [160, 4173], [197, 5138], [554, 14449], [13493, 351914], [68019, 1774019], [81512, 2125933], [149531, 3899952], [231043, 6025885], [611617, 15951722], [842660, 21977607], [2296937, 59906936], [5436534, 141791479], [7733471, 201698415], [59570831, 1553680384], [67304302, 1755378799], [396092341, 10330574379]]>
|
44
|
-
cf + cf2
|
45
|
-
=> #<ContinuedFractions::ContinuedFraction:0x000001009d2708 @number=3.1799344075396196, @limit=20, @quotients=[3, 5, 1, 1, 3, 1, 5, 3, 7, 25, 1, 2, 15, 6, 1, 1, 2, 3, 1, 2], @convergents=[[3, 1], [16, 5], [19, 6], [35, 11], [124, 39], [159, 50], [919, 289], [2916, 917], [21331, 6708], [536191, 168617], [557522, 175325], [1651235, 519267], [25326047, 7964330], [153607517, 48305247], [178933564, 56269577], [332541081, 104574824], [844015726, 265419225], [2864588259, 900832499], [3708603985, 1166251724], [10281796229, 3233335947]]>
|
46
|
-
|
47
|
-
|
48
|
-
== Requirements:
|
49
|
-
|
50
|
-
* Ruby 1.9+
|
51
|
-
|
52
|
-
== Install:
|
53
|
-
|
54
|
-
sudo gem install continued_fractions
|
55
|
-
|
56
|
-
== Developers:
|
57
|
-
|
58
|
-
* Ruby 1.9+
|
59
|
-
* RSpec required to run tests.
|
60
|
-
* Echoe required to run rake tasks.
|
61
|
-
|
62
|
-
== License:
|
63
|
-
|
64
|
-
Copyright (c) 2010 Jose Hales-Garcia
|
65
|
-
|
66
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
67
|
-
a copy of this software and associated documentation files (the
|
68
|
-
'Software'), to deal in the Software without restriction, including
|
69
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
70
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
71
|
-
permit persons to whom the Software is furnished to do so, subject to
|
72
|
-
the following conditions:
|
73
|
-
|
74
|
-
The above copyright notice and this permission notice shall be
|
75
|
-
included in all copies or substantial portions of the Software.
|
76
|
-
|
77
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
78
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
79
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
80
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
81
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
82
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
83
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
84
|
-
|