continued_fractions 1.8.6 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5eab62cd6bdda7fe01267a8fb9e1bb34315965c8abfb5432e9a56e375072aef
4
- data.tar.gz: 5e622e355e22d1178e851c0be379d585b1e1532ccf12cf7848ee36353eed9a91
3
+ metadata.gz: 3fc86b49a35fff8bfffba3ee95a21ee8f2c09e30e4bba04a25aac57a49cacfef
4
+ data.tar.gz: 27b47d0bd7c4e75bb89741bf383dad99c2bd5f4919d7fb5c47936f8de442e175
5
5
  SHA512:
6
- metadata.gz: b63ae5b51dde6a0dbf94caf43d28c90018a4b37165e3b99c7379a5f1a18493e3c198c9d8ee3dedebe358da5dd8a9543335b51ff3883b1bd750c9378c4cd5760e
7
- data.tar.gz: ad8e4e948994e0087ae46e9bb31f6c569adb68f6719a4537ab4d86e214a7033c0cf2a747963c5f936ad0a84c8483ce293dd1965d90f1706c21682ace09f87090
6
+ metadata.gz: e915584d227d3fd295ff5ad5a09a11e0dbfa1073e61761d5edcb6ab229a6b7b3b855260265fcb816c5847e4304c2463b85927b9bfb2a7ac3076b4bd8dd75b778
7
+ data.tar.gz: 1756e4b64ad3a71db1852db5c8faa6a42cbc99f0223a771e870cc19ec1f2135f0eafd5d1ad35f4235c8f56696f45cd6e27c818696392441ae10f4dae716ac0d2
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  == License:
2
2
 
3
- Copyright (c) 2021 Jose Hales-Garcia
3
+ Copyright (c) 2023 Jose Hales-Garcia
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -14,29 +14,22 @@ Generates quotients and convergents for a given number.
14
14
  => 3.14159265358979
15
15
 
16
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]]>
17
+ => 3.141592653589793, quotients: [3, 7, 15, 1, 292, 1, 1, 1, 2, 1], convergents: [(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
21
18
 
22
19
  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
-
20
+ => [(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
21
+
29
22
  cf.convergent(1)
30
- => [22,7]
23
+ => (22/7)
31
24
 
32
25
  cf.quotients
33
26
  => [3, 7, 15, 1, 292, 1, 1, 1, 2, 1]
34
27
 
35
28
  # Add, subtract, multiply and divide continued fractions. The limit of the result is the max limit of the operands.
36
29
  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]]>
30
+ => 0.9465929215086878, quotients: [0, 1, 17, 1, 2, 1, 1, 1, 1, 1, 41, 1, 1, 2, 22, 1, 1, 11, 1, 3], convergents: [(0/1), (1/1), (17/18), (18/19), (53/56), (71/75), (124/131), (195/206), (319/337), (514/543), (21393/22600), (21907/23143), (43300/45743), (108507/114629), (2430454/2567581), (2538961/2682210), (4969415/5249791), (57202526/60429911), (62171941/65679702), (243718349/257469017)]
38
31
  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]]>
32
+ => 4.088185575098481, quotients: [4, 11, 2, 1, 16, 1, 2, 1, 1, 1, 1, 25, 7, 1, 3, 1, 3], convergents: [(4/1), (45/11), (94/23), (139/34), (2318/567), (2457/601), (7232/1769), (9689/2370), (16921/4139), (26610/6509), (43531/10648), (1114885/272709), (7847726/1919611), (8962611/2192320), (34735559/8496571), (43698170/10688891), (165830069/40563244)]
40
33
 
41
34
  ## Install
42
35
 
@@ -44,14 +37,14 @@ sudo gem install continued_fractions
44
37
 
45
38
  ## Requirements/Dependencies
46
39
 
47
- * Ruby 2.2+
40
+ * Ruby 3.1+
48
41
  * Depends on Ruby's Rational class
49
42
 
50
43
  ## Developers
51
44
 
52
45
  * RSpec required to run tests
46
+ * Yard to compile documentation
53
47
 
54
48
  ## Contributors
55
49
 
56
- * [Jose Hales-Garcia](https://github.com/jolohaga)
57
- * [Eoin Kelly](https://github.com/eoinkelly)
50
+ * [Jose Hales-Garcia](https://github.com/jolohaga)
@@ -1,9 +1,15 @@
1
1
  module ContinuedFractions
2
2
  module Numeric
3
+
4
+ # @return [ContinuedFraction]
5
+ # @example
6
+ # 1.5.to_cf
7
+ # => 1.5, quotients: [1, 2], convergents: [(1/1), (3/2)]
8
+ # @param limit [Float]
9
+ #
3
10
  def to_cf(limit: ContinuedFraction::DEFAULT_LIMIT)
4
11
  ContinuedFraction.new(self, limit)
5
12
  end
6
- alias :to_continued_fraction :to_cf
7
13
  end
8
14
  end
9
15
 
@@ -4,101 +4,67 @@ require 'continued_fractions/include'
4
4
  #
5
5
  # Generates quotients and convergents for a given number
6
6
  #
7
- # Author:: Jose Hales-Garcia (mailto:jose@halesgarcia.com)
8
- # Copyright:: Copyright (c) 2023 Jose Hales-Garcia
7
+ # @author Jose Hales-Garcia
9
8
  #
9
+ # @attr_reader number [Numeric]
10
+ # The number who's continued fraction is being calculated
11
+ # @attr_reader quotients [Array]
12
+ # The array of quotients of the continued fraction
13
+ # @attr_reader convergents [Array]
14
+ # The convergents of the continued fraction
15
+ # @attr_reader limit [Integer]
16
+ # The limit to calculate. Practical for real numbers, although they're limited by the system.
10
17
  #
11
18
  class ContinuedFraction
12
19
  include Comparable
13
20
 
14
- attr_accessor :number, :quotients, :limit
21
+ attr_reader :number, :quotients, :convergents, :limit
15
22
 
16
23
  DEFAULT_LIMIT = 5
17
24
 
18
- # For a given number calculate its continued fraction quotients and convergents up to limit.
19
- #
20
- # The limit is 5 by default. Pass an integer in the second parameter to change it.
21
- #
22
- # Example:
23
- # cf = ContinuedFraction.new(Math::PI,10)
24
- # => #<ContinuedFractions::ContinuedFraction:0x000001010ed5c8 @number=3.14159265358979, @limit=10,
25
- # @quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
26
- # @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
27
- # [355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
25
+ # @return [ContinuedFraction] the quotients and convergents of a continued fraction for a given number.
26
+ # @example
27
+ # ContinuedFraction.new(Math::PI,10)
28
+ # => 3.141592653589793, quotients: [3, 7, 15, 1, 292, 1, 1, 1, 2, 1], convergents: [(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
29
+ # @param number [Numeric]
30
+ # @param limit [Integer]
28
31
  #
29
32
  def initialize(number, limit=DEFAULT_LIMIT)
30
33
  @number = number
31
34
  @ratioed_number = number.to_r
32
- @quotients, @convergents, @limit = calculate_quotients_and_convergents(number, limit)
33
- end
34
-
35
- # Return nth convergent.
36
- #
37
- # Example:
38
- # cf = ContinuedFraction.new(Math::PI,10)
39
- # cf.convergent(3)
40
- # => [355,113]
41
- #
42
- def convergent(nth)
43
- raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
44
- convergents[nth-1]
35
+ @quotients, @convergents, @limit = calculate_quotients_and_convergents(limit)
45
36
  end
46
37
 
47
- # Return array of convergents of the continued fraction instance up to the nth convergent as an array
48
- # comprising of numerators in [i,0] and denominators in [i,1].
49
- # If nth is nil, then return the entire list.
50
- #
51
- # Example:
52
- # cf = ContinuedFraction.new(Math::PI,10)
53
- # cf.convergents
54
- # => [[3,1], [22,7], [333,106], [355,113], [103993,33102],
55
- # [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]
56
- #
57
- # Or:
58
- # cf.convergents(3)
59
- # => [[3,1], [22,7], [333,106]]
60
- #
61
- def convergents(nth=nil)
62
- nth ||= @convergents.length
63
- @convergents[0...nth]
64
- end
65
-
66
- # Return array of convergents of the continued fraction instance converted as Rationals.
67
- # If nth is nil, then return the entire list.
68
- #
69
- # Example:
70
- # cf = ContinuedFraction.new(Math::PI,10)
71
- # cf.convergents_as_rationals
72
- # => [(3/1), (22/7), (333/106), (355/113), (103993/33102),
73
- # (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
38
+ # @return [ContinuedFraction] the CF sum of self and another CF
39
+ # @param other [ContinuedFraction]
74
40
  #
75
- # Or:
76
- # cf.convergents_as_rationals(3)
77
- # => [(3/1), (22/7), (333/106)]
78
- #
79
- def convergents_as_rationals(nth=nil)
80
- nth ||= convergents.length
81
- convergents[0...nth].map{|convergent| convergent_to_rational(convergent)}
82
- end
83
-
84
41
  def +(other)
85
42
  number_of(other) do |num,prec|
86
43
  evaluate("#{number} + #{num}",prec)
87
44
  end
88
45
  end
89
46
 
47
+ # @return [ContinuedFraction] the CF difference of self and another CF
48
+ # @param other [ContinuedFraction]
49
+ #
90
50
  def -(other)
91
51
  number_of(other) do |num,prec|
92
52
  evaluate("#{number} - #{num}",prec)
93
53
  end
94
54
  end
95
55
 
56
+ # @return [ContinuedFraction] the CF quotient of self and another CF
57
+ # @param other [ContinuedFraction]
58
+ #
96
59
  def /(other)
97
60
  number_of(other) do |num,prec|
98
61
  evaluate("#{number} / #{num}",prec)
99
62
  end
100
63
  end
101
64
 
65
+ # @return [ContinuedFraction] the CF product of self and another CF
66
+ # @param other [ContinuedFraction]
67
+ #
102
68
  def *(other)
103
69
  number_of(other) do |num,prec|
104
70
  evaluate("#{number} * #{num}",prec)
@@ -109,8 +75,8 @@ class ContinuedFraction
109
75
  number <=> other.number
110
76
  end
111
77
 
112
- def convergent_to_rational(convergent) #:nodoc:
113
- Rational(convergent[0],convergent[1])
78
+ def inspect
79
+ "#{number}, quotients: #{quotients}, convergents: #{convergents[0..-1]}"
114
80
  end
115
81
 
116
82
  private
@@ -132,43 +98,44 @@ class ContinuedFraction
132
98
  yield(num,prec)
133
99
  end
134
100
 
135
- def calculate_quotients_and_convergents(x, limit)
101
+ def calculate_quotients_and_convergents(limit)
102
+ x = @number
103
+
136
104
  _quotients = []
137
- _convergents = []
105
+ _convergents = [] #Convergents.new
138
106
 
139
107
  # Initialize the initial values for p and q
140
108
  p_minus_1, q_minus_1 = 0, 1
141
109
  p_0, q_0 = 1, 0
142
110
 
143
- n = x.to_i
111
+ q = x.to_i
144
112
 
145
113
  # Loop for maximum specified terms or until the fractional part becomes zero
146
114
  limit.times do
147
- _quotients << n
115
+ _quotients << q
148
116
 
149
117
  # Calculate the new p and q
150
- p_n = n * p_0 + p_minus_1
151
- q_n = n * q_0 + q_minus_1
118
+ p_n = q * p_0 + p_minus_1
119
+ q_n = q * q_0 + q_minus_1
120
+
121
+ convergent = Rational(p_n, q_n)
122
+ _convergents << convergent
123
+
124
+ break if ((@number - convergent).to_f).zero?
152
125
 
153
- _convergents << [p_n, q_n]
126
+ x_rem = x - q
127
+ break if x_rem.zero?
154
128
 
155
129
  # Recalculate the fractional part
156
- x = 1.0 / (x - n)
130
+ x = 1/1r / x_rem
157
131
  break if x.infinite? || _convergents.include?([@ratioed_number.numerator, @ratioed_number.denominator])
158
132
 
159
133
  # Update the old values
160
134
  p_minus_1, q_minus_1 = p_0, q_0
161
135
  p_0, q_0 = p_n, q_n
162
- n = x.to_i
136
+ q = x.to_i
163
137
  end
164
138
 
165
139
  return _quotients, _convergents, _quotients.length
166
140
  end
167
141
  end
168
-
169
- # @return [ContinuedFraction] new instance
170
- # @see ContinuedFraction#initialize
171
- #
172
- def ContinuedFraction(number,limit=ContinuedFraction::DEFAULT_LIMIT)
173
- ContinuedFraction.new(number,limit)
174
- 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.6
4
+ version: 2.0.1
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: 2023-09-30 00:00:00.000000000 Z
11
+ date: 2023-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -69,14 +69,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
69
  requirements:
70
70
  - - ">="
71
71
  - !ruby/object:Gem::Version
72
- version: 2.2.2
72
+ version: '3.1'
73
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
77
  version: '1.2'
78
78
  requirements: []
79
- rubygems_version: 3.4.19
79
+ rubygems_version: 3.4.22
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: Generate continued fractions