Class: ContinuedFraction

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

Overview

ContinuedFraction

Generates quotients and convergents for a given number

Author:

  • Jose Hales-Garcia

Constant Summary collapse

DEFAULT_LIMIT =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, limit = DEFAULT_LIMIT) ⇒ ContinuedFraction

Returns the quotients and convergents of a continued fraction for a given number.

Examples:

ContinuedFraction.new(Math::PI,10)
=> 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)]

Parameters:

  • number (Numeric)
  • limit (Integer) (defaults to: DEFAULT_LIMIT)


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

def initialize(number, limit=DEFAULT_LIMIT)
  @number = number
  @ratioed_number = number.to_r
  @quotients, @convergents, @limit = calculate_quotients_and_convergents(limit)
end

Instance Attribute Details

#convergentsObject (readonly)

Returns the value of attribute convergents.



12
13
14
# File 'lib/continued_fractions.rb', line 12

def convergents
  @convergents
end

#limitObject (readonly)

Returns the value of attribute limit.



12
13
14
# File 'lib/continued_fractions.rb', line 12

def limit
  @limit
end

#numberObject (readonly)

Returns the value of attribute number.



12
13
14
# File 'lib/continued_fractions.rb', line 12

def number
  @number
end

#quotientsObject (readonly)

Returns the value of attribute quotients.



12
13
14
# File 'lib/continued_fractions.rb', line 12

def quotients
  @quotients
end

Class Method Details

.from_quotients(*quotients) ⇒ ContinuedFraction

Returns the result from the provided array of quotients.

Examples:

ContinuedFraction.from_quotients(2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10)
=> 2.7182818284454013, quotients: [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 9], convergents: [(2/1), (3/1), (8/3), (11/4), (19/7), (87/32), (106/39), (193/71), (1264/465), (1457/536), (2721/1001), (23225/8544), (25946/9545), (49171/18089), (468485/172346)]

Parameters:

  • quotients (Array)

    or comma separated list of quotients

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/continued_fractions.rb', line 71

def self.from_quotients(*quotients)
  quotients = quotients.flatten
  return quotients.first if quotients.size == 1

  p_minus_2, p_minus_1 = 0, 1
  q_minus_2, q_minus_1 = 1, 0

  quotients.each do |quotient|
    p = quotient * p_minus_1 + p_minus_2
    q = quotient * q_minus_1 + q_minus_2

    p_minus_2, p_minus_1 = p_minus_1, p
    q_minus_2, q_minus_1 = q_minus_1, q
  end

  ContinuedFraction.new((p_minus_1.to_r / q_minus_1).to_f, quotients.length)
end

Instance Method Details

#*(other) ⇒ ContinuedFraction

Returns the CF product of self and another CF.

Parameters:

Returns:



59
60
61
62
63
# File 'lib/continued_fractions.rb', line 59

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

#+(other) ⇒ ContinuedFraction

Returns the CF sum of self and another CF.

Parameters:

Returns:



32
33
34
35
36
# File 'lib/continued_fractions.rb', line 32

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

#-(other) ⇒ ContinuedFraction

Returns the CF difference of self and another CF.

Parameters:

Returns:



41
42
43
44
45
# File 'lib/continued_fractions.rb', line 41

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

#/(other) ⇒ ContinuedFraction

Returns the CF quotient of self and another CF.

Parameters:

Returns:



50
51
52
53
54
# File 'lib/continued_fractions.rb', line 50

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

#<=>(other) ⇒ Object



89
90
91
# File 'lib/continued_fractions.rb', line 89

def <=>(other)
  number <=> other.number
end

#inspectObject



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

def inspect
  "#{number}, quotients: #{quotients}, convergents: #{convergents[0..-1]}"
end