class ContinuedFraction

ContinuedFractions

Generates quotients and convergents for a given number

Author

Jose Hales-Garcia (jolohaga@me.com)

Copyright

Copyright © 2015 Jose Hales-Garcia

Attributes

limit[RW]
number[RW]
quotients[RW]

Public Class Methods

new(number,limit=5) click to toggle source

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]]>
   # File lib/continued_fractions.rb
25 def initialize(number,limit=5)
26   @number = number
27   @limit = limit
28   @quotients = calculate_quotients
29   @convergents = calculate_convergents
30 end

Public Instance Methods

*(other) click to toggle source
    # File lib/continued_fractions.rb
 99 def *(other)
100   number_of(other) do |num,prec|
101     evaluate("#{number} * #{num}",prec)
102   end
103 end
+(other) click to toggle source
   # File lib/continued_fractions.rb
81 def +(other)
82   number_of(other) do |num,prec|
83     evaluate("#{number} + #{num}",prec)
84   end
85 end
-(other) click to toggle source
   # File lib/continued_fractions.rb
87 def -(other)
88   number_of(other) do |num,prec|
89     evaluate("#{number} - #{num}",prec)
90   end
91 end
/(other) click to toggle source
   # File lib/continued_fractions.rb
93 def /(other)
94   number_of(other) do |num,prec|
95     evaluate("#{number} / #{num}",prec)
96   end
97 end
<=>(other) click to toggle source
    # File lib/continued_fractions.rb
105 def <=>(other)
106   number <=> other.number
107 end
convergent(nth) click to toggle source

Return nth convergent.

Example:

cf = ContinuedFraction.new(Math::PI,10)
cf.convergent(3)
=> [355,113]
   # File lib/continued_fractions.rb
39 def convergent(nth)
40   raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
41   convergents[nth-1]
42 end
convergents(nth=nil) click to toggle source

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]]
   # File lib/continued_fractions.rb
58 def convergents(nth=nil)
59   nth ||= @convergents.length
60   @convergents[0...nth]
61 end
convergents_as_rationals(nth=nil) click to toggle source

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)]
   # File lib/continued_fractions.rb
76 def convergents_as_rationals(nth=nil)
77   nth ||= convergents.length
78   convergents[0...nth].map{|convergent| convergent_to_rational(convergent)}
79 end