How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Wren programming language
How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Wren 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 Wren programming language
Source code in the wren programming language
import "./dynamic" for Tuple
var CFData = Tuple.create("Tuple", ["str", "ng", "r", "gen"])
var r2cf = Fn.new { |frac|
var num = frac[0]
var den = frac[1]
while (den.abs != 0) {
var div = (num/den).truncate
var rem = num % den
num = den
den = rem
Fiber.yield(div)
}
}
var d2cf = Fn.new { |d|
while (true) {
var div = d.floor
var rem = d - div
Fiber.yield(div)
if (rem == 0) break
d = 1 / rem
}
}
var root2 = Fn.new {
Fiber.yield(1)
while (true) Fiber.yield(2)
}
var recipRoot2 = Fn.new {
Fiber.yield(0)
Fiber.yield(1)
while (true) Fiber.yield(2)
}
class NG {
construct new(a1, a, b1, b) {
_a1 = a1
_a = a
_b1 = b1
_b = b
}
ingress(n) {
var t = _a
_a = _a1
_a1 = t + _a1 * n
t = _b
_b = _b1
_b1 = t + _b1 * n
}
egress() {
var n = (_a/_b).truncate
var t = _a
_a = _b
_b = t - _b * n
t = _a1
_a1 = _b1
_b1 = t - _b1 * n
return n
}
needTerm { (_b == 0 || _b1 == 0) || ((_a / _b) != (_a1 / _b1)) }
egressDone {
if (needTerm) {
_a = _a1
_b = _b1
}
return egress()
}
done { _b == 0 && _b1 == 0 }
}
var data = [
CFData.new("[1;5,2] + 1/2 ", [2, 1, 0, 2], [13, 11], r2cf),
CFData.new("[3;7] + 1/2 ", [2, 1, 0, 2], [22, 7], r2cf),
CFData.new("[3;7] divided by 4 ", [1, 0, 0, 4], [22, 7], r2cf),
CFData.new("sqrt(2) ", [0, 1, 1, 0], [ 0, 0], recipRoot2),
CFData.new("1 / sqrt(2) ", [0, 1, 1, 0], [ 0, 0], root2),
CFData.new("(1 + sqrt(2)) / 2 ", [1, 1, 0, 2], [ 0, 0], root2),
CFData.new("(1 + 1 / sqrt(2)) / 2", [1, 1, 0, 2], [ 0, 0], recipRoot2)
]
System.print("Produced by NG class:")
for (cfd in data) {
System.write("%(cfd.str) -> ")
var a1 = cfd.ng[0]
var a = cfd.ng[1]
var b1 = cfd.ng[2]
var b = cfd.ng[3]
var op = NG.new(a1, a, b1, b)
var seq = []
var i = 0
var fib = Fiber.new(cfd.gen)
while (i < 20) {
var j = fib.call(cfd.r)
if (j) seq.add(j) else break
i = i + 1
}
for (n in seq) {
if (!op.needTerm) System.write(" %(op.egress()) ")
op.ingress(n)
}
while (true) {
System.write(" %(op.egressDone) ")
if (op.done) break
}
System.print()
}
System.print("\nProduced by direct calculation:")
var data2 = [
["(1 + sqrt(2)) / 2 ", (1 + 2.sqrt) / 2],
["(1 + 1 / sqrt(2)) / 2", (1 + 1 / 2.sqrt) / 2]
]
for (p in data2) {
var seq = []
var fib = Fiber.new(d2cf)
var i = 0
while (i < 20) {
var j = fib.call(p[1])
if (j) seq.add(j) else break
i = i + 1
}
System.print("%(p[0]) -> %(seq.join(" "))")
}
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the Lua programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Q programming language
You may also check:How to resolve the algorithm Add a variable to a class instance at runtime step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the Racket programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Vim Script programming language