continued_fractions 1.8.3 → 1.8.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.
- checksums.yaml +4 -4
- data/lib/continued_fractions/include.rb +12 -0
- data/lib/continued_fractions/version.rb +1 -1
- data/lib/continued_fractions.rb +20 -16
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff0ff19ce7e60d7cd554c981adc6ecdc7cfd3591dbfbb0c893bee1b6cc2413fd
|
4
|
+
data.tar.gz: 9d5fad37f7c8eefa1be24eb210a395e4cbb39b35fa306e4f234f36ce984cfbbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c2b1dfd1d72ac337d06f69da05975eafcf888e53cb4bfcc864630d73aa89189d3a297f0cd50a5bdcb440e60600a55ae0f0123300e20dbdb9c82a5d3770dff5c
|
7
|
+
data.tar.gz: b58393bf5eb004fad31e809a9b845eb757bd41919914a4d9afd41a265f8a1eac5461a075edf50a6c20c65c388a9efdfb933b26177baaad028f4671010be478d6
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ContinuedFractions
|
2
|
+
module Numeric
|
3
|
+
def to_cf(limit: ContinuedFraction::DEFAULT_LIMIT)
|
4
|
+
ContinuedFraction.new(self, limit)
|
5
|
+
end
|
6
|
+
alias :to_continued_fraction :to_cf
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
Float.include(ContinuedFractions::Numeric)
|
11
|
+
Integer.include(ContinuedFractions::Numeric)
|
12
|
+
Rational.include(ContinuedFractions::Numeric)
|
data/lib/continued_fractions.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
|
-
|
1
|
+
require 'continued_fractions/include'
|
2
|
+
|
3
|
+
# ContinuedFraction
|
2
4
|
#
|
3
5
|
# Generates quotients and convergents for a given number
|
4
6
|
#
|
5
7
|
# Author:: Jose Hales-Garcia (mailto:jolohaga@me.com)
|
6
|
-
# Copyright:: Copyright (c)
|
8
|
+
# Copyright:: Copyright (c) 2022 Jose Hales-Garcia
|
7
9
|
#
|
8
10
|
#
|
9
11
|
class ContinuedFraction
|
10
12
|
include ::Comparable
|
11
13
|
|
12
14
|
attr_accessor :number, :quotients, :limit
|
13
|
-
|
15
|
+
|
16
|
+
DEFAULT_LIMIT = 5
|
17
|
+
|
14
18
|
# For a given number calculate its continued fraction quotients and convergents up to limit.
|
15
19
|
#
|
16
20
|
# The limit is 5 by default. Pass an integer in the second parameter to change it.
|
@@ -22,13 +26,13 @@ class ContinuedFraction
|
|
22
26
|
# @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
|
23
27
|
# [355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
|
24
28
|
#
|
25
|
-
def initialize(number,limit=
|
29
|
+
def initialize(number,limit=DEFAULT_LIMIT)
|
26
30
|
@number = number
|
27
31
|
@limit = limit
|
28
32
|
@quotients = calculate_quotients
|
29
33
|
@convergents = calculate_convergents
|
30
34
|
end
|
31
|
-
|
35
|
+
|
32
36
|
# Return nth convergent.
|
33
37
|
#
|
34
38
|
# Example:
|
@@ -40,7 +44,7 @@ class ContinuedFraction
|
|
40
44
|
raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
|
41
45
|
convergents[nth-1]
|
42
46
|
end
|
43
|
-
|
47
|
+
|
44
48
|
# Return array of convergents of the continued fraction instance up to the nth convergent as an array
|
45
49
|
# comprising of numerators in [i,0] and denominators in [i,1].
|
46
50
|
# If nth is nil, then return the entire list.
|
@@ -59,7 +63,7 @@ class ContinuedFraction
|
|
59
63
|
nth ||= @convergents.length
|
60
64
|
@convergents[0...nth]
|
61
65
|
end
|
62
|
-
|
66
|
+
|
63
67
|
# Return array of convergents of the continued fraction instance converted as Rationals.
|
64
68
|
# If nth is nil, then return the entire list.
|
65
69
|
#
|
@@ -77,25 +81,25 @@ class ContinuedFraction
|
|
77
81
|
nth ||= convergents.length
|
78
82
|
convergents[0...nth].map{|convergent| convergent_to_rational(convergent)}
|
79
83
|
end
|
80
|
-
|
84
|
+
|
81
85
|
def +(other)
|
82
86
|
number_of(other) do |num,prec|
|
83
87
|
evaluate("#{number} + #{num}",prec)
|
84
88
|
end
|
85
89
|
end
|
86
|
-
|
90
|
+
|
87
91
|
def -(other)
|
88
92
|
number_of(other) do |num,prec|
|
89
93
|
evaluate("#{number} - #{num}",prec)
|
90
94
|
end
|
91
95
|
end
|
92
|
-
|
96
|
+
|
93
97
|
def /(other)
|
94
98
|
number_of(other) do |num,prec|
|
95
99
|
evaluate("#{number} / #{num}",prec)
|
96
100
|
end
|
97
101
|
end
|
98
|
-
|
102
|
+
|
99
103
|
def *(other)
|
100
104
|
number_of(other) do |num,prec|
|
101
105
|
evaluate("#{number} * #{num}",prec)
|
@@ -105,11 +109,11 @@ class ContinuedFraction
|
|
105
109
|
def <=>(other)
|
106
110
|
number <=> other.number
|
107
111
|
end
|
108
|
-
|
112
|
+
|
109
113
|
def convergent_to_rational(convergent) #:nodoc:
|
110
114
|
Rational(convergent[0],convergent[1])
|
111
115
|
end
|
112
|
-
|
116
|
+
|
113
117
|
private
|
114
118
|
def evaluate(exp, prec) #:nodoc:
|
115
119
|
ContinuedFraction.new(eval(exp), prec)
|
@@ -152,7 +156,7 @@ class ContinuedFraction
|
|
152
156
|
end
|
153
157
|
end[2...nth+2]
|
154
158
|
end
|
155
|
-
|
159
|
+
|
156
160
|
def convergence_matrix(n,m,fill=nil) #:nodoc:
|
157
161
|
Array.new(n).map!{Array.new(m,fill)}.tap do |conv_mat|
|
158
162
|
conv_mat[0][0],conv_mat[1][1] = 0,0
|
@@ -163,6 +167,6 @@ end
|
|
163
167
|
# @return [ContinuedFraction] new instance
|
164
168
|
# @see ContinuedFraction#initialize
|
165
169
|
#
|
166
|
-
def ContinuedFraction(number,limit=
|
167
|
-
ContinuedFraction.new(number,limit
|
170
|
+
def ContinuedFraction(number,limit=ContinuedFraction::DEFAULT_LIMIT)
|
171
|
+
ContinuedFraction.new(number,limit)
|
168
172
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: continued_fractions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jose Hales-Garcia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- LICENSE
|
51
51
|
- README.md
|
52
52
|
- lib/continued_fractions.rb
|
53
|
+
- lib/continued_fractions/include.rb
|
53
54
|
- lib/continued_fractions/version.rb
|
54
55
|
homepage: http://jolohaga.github.io/continued_fractions
|
55
56
|
licenses:
|