How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Ruby programming language
How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Ruby programming language
Table of Contents
Problem Statement
This task investigates mathmatical operations that can be performed on a single continued fraction. This requires only a baby version of NG: I may perform perform the following operations: I output a term if the integer parts of
a b
{\displaystyle {\frac {a}{b}}}
and
a
1
b
1
{\displaystyle {\frac {a_{1}}{b_{1}}}}
are equal. Otherwise I input a term from N. If I need a term from N but N has no more terms I inject
∞
{\displaystyle \infty }
. When I input a term t my internal state:
[
a
1
a
b
1
b
]
{\displaystyle {\begin{bmatrix}a_{1}&a\b_{1}&b\end{bmatrix}}}
is transposed thus
[
a +
a
1
∗ t
a
1
b +
b
1
∗ t
b
1
]
{\displaystyle {\begin{bmatrix}a+a_{1}*t&a_{1}\b+b_{1}*t&b_{1}\end{bmatrix}}}
When I output a term t my internal state:
[
a
1
a
b
1
b
]
{\displaystyle {\begin{bmatrix}a_{1}&a\b_{1}&b\end{bmatrix}}}
is transposed thus
[
b
1
b
a
1
−
b
1
∗ t
a − b ∗ t
]
{\displaystyle {\begin{bmatrix}b_{1}&b\a_{1}-b_{1}t&a-bt\end{bmatrix}}}
When I need a term t but there are no more my internal state:
[
a
1
a
b
1
b
]
{\displaystyle {\begin{bmatrix}a_{1}&a\b_{1}&b\end{bmatrix}}}
is transposed thus
[
a
1
a
1
b
1
b
1
]
{\displaystyle {\begin{bmatrix}a_{1}&a_{1}\b_{1}&b_{1}\end{bmatrix}}}
I am done when b1 and b are zero. Demonstrate your solution by calculating: Using a generator for
2
{\displaystyle {\sqrt {2}}}
(e.g., from Continued fraction) calculate
1
2
{\displaystyle {\frac {1}{\sqrt {2}}}}
. You are now at the starting line for using Continued Fractions to implement Arithmetic-geometric mean without ulps and epsilons. The first step in implementing Arithmetic-geometric mean is to calculate
1 +
1
2
2
{\displaystyle {\frac {1+{\frac {1}{\sqrt {2}}}}{2}}}
do this now to cross the starting line and begin the race.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Ruby programming language
The provided Ruby code defines a baby NG (number generator) algorithm and uses it to generate rational numbers from a given string representation. Here's a breakdown:
Class Definition (NG
):
- It defines a class
NG
that implements the baby NG algorithm. - The constructor
initialize
takes four arguments:a1
,a
,b1
, andb
. These are initial values for the algorithm.
Instance Methods of NG
:
ingress(n)
: Updates the internal state of the NG algorithm by performing the ingress operation. It takes an input numbern
.needterm?
: Checks if the NG algorithm needs to terminate (i.e., if the fraction has terminated).egress
: Performs the egress operation and calculates the next rational number.egress_done
: Callsegress
and setsa
andb
toa1
andb1
if termination is required.done?
: Checks if the NG algorithm is finished (i.e., if botha
andb
are zero).
Usage of the NG Algorithm:
- The code includes a list of string representations of rational numbers (
data
) and their corresponding initial values for the NG algorithm (ng
). - It iterates through the
data
list and for each entry:- Creates an
NG
object using the provided initial values. - Calls
r2cf(*r)
to generate a series of numbers from the string representation of the rational number. - For each number
n
, it callsegress
on theNG
object and prints the result. - Calls
egress_done
to handle any necessary termination steps.
- Creates an
Utility function r2cf(x, y)
:
- This function generates a series of numbers from a string representation of a rational number.
- It takes two numbers,
x
andy
, and yields each number in the sequence.
Example:
For the string "[3;7] + 1/2"
, the code will generate the following sequence of rational numbers:
2 5/7 22/7
Overall:
The code uses the NG
class to generate rational numbers from string representations. It iterates through a list of test cases, demonstrates the ingress/egress operations, and handles termination gracefully. This code can be useful for generating continued fraction representations of rational numbers and solving simple fraction arithmetic problems.
Source code in the ruby programming language
# I define a class to implement baby NG
class NG
def initialize(a1, a, b1, b)
@a1, @a, @b1, @b = a1, a, b1, b
end
def ingress(n)
@a, @a1 = @a1, @a + @a1 * n
@b, @b1 = @b1, @b + @b1 * n
end
def needterm?
return true if @b == 0 or @b1 == 0
return true unless @a/@b == @a1/@b1
false
end
def egress
n = @a / @b
@a, @b = @b, @a - @b * n
@a1, @b1 = @b1, @a1 - @b1 * n
n
end
def egress_done
@a, @b = @a1, @b1 if needterm?
egress
end
def done?
@b == 0 and @b1 == 0
end
end
data = [["[1;5,2] + 1/2", [2,1,0,2], [13,11]],
["[3;7] + 1/2", [2,1,0,2], [22, 7]],
["[3;7] divided by 4", [1,0,0,4], [22, 7]]]
data.each do |str, ng, r|
printf "%-20s->", str
op = NG.new(*ng)
r2cf(*r) do |n|
print " #{op.egress}" unless op.needterm?
op.ingress(n)
end
print " #{op.egress_done}" until op.done?
puts
end
You may also check:How to resolve the algorithm Boustrophedon transform step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Julia programming language
You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the Ada programming language
You may also check:How to resolve the algorithm Window creation step by step in the BaCon programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the jq programming language