Class: ContinuedFraction

Inherits:
Object
  • Object
show all
Defined in:
lib/continued_fractions.rb

Overview

ContinuedFractions

Generates quotients and convergents for a given number

Author

Jose Hales-Garcia ([email protected])

Copyright

Copyright © 2015 Jose Hales-Garcia

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, limit = 5) ⇒ ContinuedFraction

For a given number calculate its continued fraction quotients and convergents up to limit.

The limit is 5 by default. Pass an integer in the second parameter to change it.

Example:

cf = ContinuedFraction.new(Math::PI,10)
=> #<ContinuedFractions::ContinuedFraction:0x000001010ed5c8 @number=3.14159265358979, @limit=10,
    @quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
    @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
                  [355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>


23
24
25
26
27
28
# File 'lib/continued_fractions.rb', line 23

def initialize(number,limit=5)
  @number = number
  @limit = limit
  @quotients = calculate_quotients
  @convergents = calculate_convergents
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



10
11
12
# File 'lib/continued_fractions.rb', line 10

def limit
  @limit
end

#numberObject

Returns the value of attribute number.



10
11
12
# File 'lib/continued_fractions.rb', line 10

def number
  @number
end

#quotientsObject

Returns the value of attribute quotients.



10
11
12
# File 'lib/continued_fractions.rb', line 10

def quotients
  @quotients
end

Instance Method Details

#*(other) ⇒ Object



97
98
99
100
101
# File 'lib/continued_fractions.rb', line 97

def *(other)
  number_of(other) do |num,prec|
    evaluate("#{number} * #{num}",prec)
  end
end

#+(other) ⇒ Object



79
80
81
82
83
# File 'lib/continued_fractions.rb', line 79

def +(other)
  number_of(other) do |num,prec|
    evaluate("#{number} + #{num}",prec)
  end
end

#-(other) ⇒ Object



85
86
87
88
89
# File 'lib/continued_fractions.rb', line 85

def -(other)
  number_of(other) do |num,prec|
    evaluate("#{number} - #{num}",prec)
  end
end

#/(other) ⇒ Object



91
92
93
94
95
# File 'lib/continued_fractions.rb', line 91

def /(other)
  number_of(other) do |num,prec|
    evaluate("#{number} / #{num}",prec)
  end
end

#convergent(nth) ⇒ Object

Return nth convergent.

Example:

cf = ContinuedFraction.new(Math::PI,10)
cf.convergent(3)
=> [355,113]

Raises:

  • (IndexError)


37
38
39
40
# File 'lib/continued_fractions.rb', line 37

def convergent(nth)
  raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
  convergents[nth-1]
end

#convergent_to_rational(convergent) ⇒ Object

:nodoc:



103
104
105
# File 'lib/continued_fractions.rb', line 103

def convergent_to_rational(convergent) #:nodoc:
  Rational(convergent[0],convergent[1])
end

#convergents(nth = nil) ⇒ Object

Return array of convergents of the continued fraction instance up to the nth convergent as an array comprising of numerators in [i,0] and denominators in [i,1]. If nth is nil, then return the entire list.

Example:

cf = ContinuedFraction.new(Math::PI,10)
cf.convergents
=> [[3,1], [22,7], [333,106], [355,113], [103993,33102],
    [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]

Or:

cf.convergents(3)
=> [[3,1], [22,7], [333,106]]


56
57
58
59
# File 'lib/continued_fractions.rb', line 56

def convergents(nth=nil)
  nth ||= @convergents.length
  @convergents[0...nth]
end

#convergents_as_rationals(nth = nil) ⇒ Object

Return array of convergents of the continued fraction instance converted as Rationals. If nth is nil, then return the entire list.

Example:

cf = ContinuedFraction.new(Math::PI,10)
cf.convergents_as_rationals
=> [(3/1), (22/7), (333/106), (355/113), (103993/33102),
    (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]

Or:

cf.convergents_as_rationals(3)
=> [(3/1), (22/7), (333/106)]


74
75
76
77
# File 'lib/continued_fractions.rb', line 74

def convergents_as_rationals(nth=nil)
  nth ||= convergents.length
  convergents[0...nth].map{|convergent| convergent_to_rational(convergent)}
end