continued_fractions 1.8.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9253bbf7cf75a29375e99c2bdec92f643413eba529a1d360e0165d2ce98fbdd7
4
- data.tar.gz: '09063bf865028894dd41c43bc47118d8445dd79811154f7c862cf1ab696cff93'
3
+ metadata.gz: ff0ff19ce7e60d7cd554c981adc6ecdc7cfd3591dbfbb0c893bee1b6cc2413fd
4
+ data.tar.gz: 9d5fad37f7c8eefa1be24eb210a395e4cbb39b35fa306e4f234f36ce984cfbbb
5
5
  SHA512:
6
- metadata.gz: ce2f1dbad5629bef37ea84ceaa549005e0c0c73931ecbf54e36f3f779b22c3256505e4719fe3095fbaa116937ed01307eeded90547491db96084994a6459e77b
7
- data.tar.gz: 1b881899377e3ba060db5d03666ac3451703a169bf768bee1aebc5e512b91c8bc1c5c15373669514854b33cf623449e8588a329f6a68026a22363d268efae2c7
6
+ metadata.gz: 8c2b1dfd1d72ac337d06f69da05975eafcf888e53cb4bfcc864630d73aa89189d3a297f0cd50a5bdcb440e60600a55ae0f0123300e20dbdb9c82a5d3770dff5c
7
+ data.tar.gz: b58393bf5eb004fad31e809a9b845eb757bd41919914a4d9afd41a265f8a1eac5461a075edf50a6c20c65c388a9efdfb933b26177baaad028f4671010be478d6
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  == License:
2
2
 
3
- Copyright (c) 2015 Jose Hales-Garcia
3
+ Copyright (c) 2021 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
@@ -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)
@@ -0,0 +1,3 @@
1
+ module ContinuedFractions
2
+ VERSION = "1.8.4"
3
+ end
@@ -1,16 +1,20 @@
1
- # ContinuedFractions
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) 2015 Jose Hales-Garcia
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=5)
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)
@@ -142,19 +146,27 @@ class ContinuedFraction
142
146
  end
143
147
 
144
148
  def calculate_convergents #:nodoc:
145
- convs = convergence_matrix(quotients.length+2,2,1)
146
- nth ||= convs.length
147
- 2.upto(quotients.length+1) do |i|
148
- i_minus1,i_minus2 = i-1,i-2
149
- convs[i][0] = convs[i_minus1][0]*quotients[i_minus2]+convs[i_minus2][0]
150
- convs[i][1] = convs[i_minus1][1]*quotients[i_minus2]+convs[i_minus2][1]
151
- end
152
- convs[2...nth+2]
149
+ nth = nil
150
+ convergence_matrix(quotients.length+2,2,1).tap do |convs|
151
+ nth ||= convs.length
152
+ 2.upto(quotients.length+1) do |i|
153
+ i_minus1,i_minus2 = i-1,i-2
154
+ convs[i][0] = convs[i_minus1][0]*quotients[i_minus2]+convs[i_minus2][0]
155
+ convs[i][1] = convs[i_minus1][1]*quotients[i_minus2]+convs[i_minus2][1]
156
+ end
157
+ end[2...nth+2]
153
158
  end
154
-
159
+
155
160
  def convergence_matrix(n,m,fill=nil) #:nodoc:
156
- conv_mat = Array.new(n).map!{Array.new(m,fill)}
157
- conv_mat[0][0],conv_mat[1][1] = 0,0
158
- conv_mat
161
+ Array.new(n).map!{Array.new(m,fill)}.tap do |conv_mat|
162
+ conv_mat[0][0],conv_mat[1][1] = 0,0
163
+ end
159
164
  end
160
165
  end
166
+
167
+ # @return [ContinuedFraction] new instance
168
+ # @see ContinuedFraction#initialize
169
+ #
170
+ def ContinuedFraction(number,limit=ContinuedFraction::DEFAULT_LIMIT)
171
+ ContinuedFraction.new(number,limit)
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.0
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: 2016-10-26 00:00:00.000000000 Z
11
+ date: 2022-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.1'
27
41
  description: Class for working with continued fractions
28
42
  email: jolohaga@me.com
29
43
  executables: []
@@ -33,23 +47,21 @@ extra_rdoc_files:
33
47
  - README.md
34
48
  - lib/continued_fractions.rb
35
49
  files:
36
- - Gemfile
37
50
  - LICENSE
38
51
  - README.md
39
- - Rakefile
40
- - continued_fractions.gemspec
41
52
  - lib/continued_fractions.rb
42
- - spec/continued_fractions/continued_fraction_spec.rb
43
- - spec/spec_helper.rb
53
+ - lib/continued_fractions/include.rb
54
+ - lib/continued_fractions/version.rb
44
55
  homepage: http://jolohaga.github.io/continued_fractions
45
56
  licenses:
46
57
  - MIT
47
- metadata: {}
58
+ metadata:
59
+ source_code_uri: https://github.com/jolohaga/continued_fractions
48
60
  post_install_message:
49
61
  rdoc_options:
50
62
  - "--line-numbers"
51
63
  - "--title"
52
- - Continued_fractions
64
+ - continued_fractions
53
65
  - "--main"
54
66
  - README.md
55
67
  require_paths:
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in continued_fractions.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
@@ -1,37 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'continued_fractions/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "continued_fractions"
8
- spec.version = ContinuedFractions::VERSION
9
-
10
- spec.required_rubygems_version = Gem::Requirement.new(">= 1.2") if spec.respond_to? :required_rubygems_version=
11
- spec.require_paths = ["lib"]
12
- spec.authors = ["Jose Hales-Garcia"]
13
- spec.date = "2016-10-26"
14
- spec.description = "Class for working with continued fractions"
15
- spec.email = "jolohaga@me.com"
16
- spec.extra_rdoc_files = ["LICENSE", "README.md", "lib/continued_fractions.rb"]
17
- spec.files = ["Gemfile", "LICENSE", "README.md", "Rakefile", "continued_fractions.gemspec", "lib/continued_fractions.rb", "spec/continued_fractions/continued_fraction_spec.rb", "spec/spec_helper.rb"]
18
- spec.homepage = "http://jolohaga.github.io/continued_fractions"
19
- spec.licenses = ["MIT"]
20
- spec.rdoc_options = ["--line-numbers", "--title", "Continued_fractions", "--main", "README.md"]
21
- spec.required_ruby_version = Gem::Requirement.new(">= 2.2.2")
22
- spec.rubyforge_project = "continued_fractions"
23
- spec.rubygems_version = "2.5.1"
24
- spec.summary = "Generate continued fractions"
25
-
26
- if spec.respond_to? :specification_version then
27
- spec.specification_version = 4
28
-
29
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
30
- spec.add_development_dependency(%q<rspec>, ["~> 3.2"])
31
- else
32
- spec.add_dependency(%q<rspec>, ["~> 3.2"])
33
- end
34
- else
35
- spec.add_dependency(%q<rspec>, ["~> 3.2"])
36
- end
37
- end
@@ -1,90 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "/../spec_helper")
2
-
3
- describe ContinuedFraction do
4
- let(:cf) { described_class.new(number, 10) }
5
- let(:number) { rand }
6
-
7
- describe '#convergents' do
8
- it "returns an array" do
9
- expect(cf.convergents).to be_kind_of(Array)
10
- end
11
-
12
- context 'with irrational numbers' do
13
- it "accurately calculates the convergents" do
14
- # First 10 convergents of PI are...
15
- convs = [3/1r, 22/7r, 333/106r, 355/113r, 103993/33102r, 104348/33215r, 208341/66317r, 312689/99532r, 833719/265381r, 1146408/364913r]
16
- cf = described_class.new(Math::PI,10)
17
- expect((cf.convergents_as_rationals - convs)).to be_empty
18
- end
19
-
20
- it "contains convergents approaching the number" do
21
- 0.upto(cf.convergents.length-2) do |i|
22
- convergent_rational_i_plus1, convergent_rational_i = cf.convergent_to_rational(cf.convergents[i+1]), cf.convergent_to_rational(cf.convergents[i])
23
- expect(((convergent_rational_i_plus1 - cf.number).abs <= (convergent_rational_i - cf.number).abs)).to be true
24
- end
25
- end
26
-
27
- it "contains convergents which are expressed in lowest terms" do
28
- 1.upto(cf.convergents.length-1) do |i|
29
- convergent_rational_i, convergent_rational_i_plus1 = cf.convergent_to_rational(cf.convergent(i)), cf.convergent_to_rational(cf.convergent(i+1))
30
- expect((convergent_rational_i.numerator*convergent_rational_i_plus1.denominator - convergent_rational_i_plus1.numerator*convergent_rational_i.denominator)).to eq (-1)**(i)
31
- end
32
- end
33
- end
34
-
35
- context 'with rational numbers' do
36
- it 'changes the limit to the number of convergents calculated' do
37
- expect(described_class.new(1.5, 10).limit).to be 2
38
- end
39
-
40
- it 'calculates the convergents' do
41
- convs = [ 1/1r, 3/2r ]
42
- expect(described_class.new(1.5, 10).convergents_as_rationals - convs).to be_empty
43
- end
44
- end
45
- end
46
-
47
- describe '#number' do
48
- it "preserves the number input" do
49
- expect(cf.number).to eq number
50
- end
51
- end
52
-
53
- describe 'operators' do
54
- let(:cf2) { described_class.new(rand, 20) }
55
-
56
- %i{+ - * /}.each do |op|
57
- describe "#{op}" do
58
- [3, 3.0, 3/1r, described_class.new(rand, 20)].each do |rhs|
59
- it "#{rhs.class.name} operates on the right-hand side and returns a ContinuedFraction" do
60
- expect(cf.send(op, rhs)).to be_kind_of(ContinuedFraction)
61
- end
62
- end
63
-
64
- it "Assigns the max limit of the two operands" do
65
- result = cf.send(op, cf2)
66
- expect(result.limit).to eq [cf.limit, cf2.limit].max
67
- end
68
- end
69
- end
70
- end
71
-
72
- describe 'comparing' do
73
- context 'when numbers are the same' do
74
- let(:cf2) { described_class.new(cf.number) }
75
-
76
- it "the ContinuedFractions are equal" do
77
- expect(cf).to eq cf2
78
- end
79
- end
80
-
81
- context 'when numbers are different' do
82
- let(:cond) { [['+', '<'], ['-', '>']].sample }
83
- let(:cf2) { described_class.new(cf.number.send(cond[0], 1.0)) }
84
-
85
- it "the ContinuedFractions are unequal" do
86
- expect(cf.send(cond[1], cf2)).to be true
87
- end
88
- end
89
- end
90
- end
data/spec/spec_helper.rb DELETED
@@ -1,13 +0,0 @@
1
- $LOAD_PATH << File.join(File.dirname(__FILE__), "/../lib")
2
- require 'rubygems'
3
- require 'rspec'
4
- require 'continued_fractions'
5
-
6
- RSpec.configure do |config|
7
- # Run specs in random order to surface order dependencies. If you find an
8
- # order dependency and want to debug it, you can fix the order by providing
9
- # the seed, which is printed after each run.
10
- # --seed 1234
11
- config.order = "random"
12
- config.formatter = :documentation
13
- end