How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Kotlin programming language
How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Kotlin 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 Kotlin programming language
This code showcases two different methods for generating continued fractions (CFs) and demonstrates their use in approximating various mathematical expressions.
Continued Fractions (CFs) are mathematical representations of real numbers as an infinite sequence of integers. They provide a way to represent real numbers in a structured way and perform various operations on them.
The code consists of several functions and classes:
- r2cf: Generates a CF for a rational number represented as a pair of integers.
- d2cf: Generates a CF for a floating-point number.
- root2: Generates a CF that converges to the square root of 2.
- recipRoot2: Generates a CF that converges to the reciprocal of the square root of 2.
- NG: Implements the Named Group Method (NG) for computing continued fractions. It maintains a stateful representation of the fraction and allows efficient calculation of digits.
Main Function:
main
defines a list ofCFData
objects, each representing a mathematical expression and the parameters needed to generate its CF.- It prints the CFs generated using the
NG
class. - It also directly calculates and prints the CFs for a subset of the expressions in the list.
Observations:
- The CFs generated using the
NG
class and the direct calculation method produce the same results for the given expressions. - The
NG
class provides a convenient way to compute CFs, which can be useful in certain scenarios. However, for simple expressions, direct calculation may be more straightforward.
In summary, this code provides a practical demonstration of CF generation and approximation using both the NG class and direct calculation methods. It showcases the versatility of continued fractions in representing and manipulating real numbers.
Source code in the kotlin programming language
// version 1.1.3
// compile with -Xcoroutines=enable flag from command line
import kotlin.coroutines.experimental.*
typealias CFGenerator = (Pair<Int, Int>) -> Sequence<Int>
data class CFData(
val str: String,
val ng: IntArray,
val r: Pair<Int,Int>,
val gen: CFGenerator
)
fun r2cf(frac: Pair<Int, Int>) =
buildSequence {
var num = frac.first
var den = frac.second
while (Math.abs(den) != 0) {
val div = num / den
val rem = num % den
num = den
den = rem
yield(div)
}
}
fun d2cf(d: Double) =
buildSequence {
var dd = d
while (true) {
val div = Math.floor(dd)
val rem = dd - div
yield(div.toInt())
if (rem == 0.0) break
dd = 1.0 / rem
}
}
@Suppress("UNUSED_PARAMETER")
fun root2(dummy: Pair<Int, Int>) =
buildSequence {
yield(1)
while (true) yield(2)
}
@Suppress("UNUSED_PARAMETER")
fun recipRoot2(dummy: Pair<Int, Int>) =
buildSequence {
yield(0)
yield(1)
while (true) yield(2)
}
class NG(var a1: Int, var a: Int, var b1: Int, var b: Int) {
fun ingress(n: Int) {
var t = a
a = a1
a1 = t + a1 * n
t = b
b = b1
b1 = t + b1 * n
}
fun egress(): Int {
val n = a / b
var t = a
a = b
b = t - b * n
t = a1
a1 = b1
b1 = t - b1 * n
return n
}
val needTerm get() = (b == 0 || b1 == 0) || ((a / b) != (a1 / b1))
val egressDone: Int
get() {
if (needTerm) {
a = a1
b = b1
}
return egress()
}
val done get() = b == 0 && b1 == 0
}
fun main(args: Array<String>) {
val data = listOf(
CFData("[1;5,2] + 1/2 ", intArrayOf(2, 1, 0, 2), 13 to 11, ::r2cf),
CFData("[3;7] + 1/2 ", intArrayOf(2, 1, 0, 2), 22 to 7, ::r2cf),
CFData("[3;7] divided by 4 ", intArrayOf(1, 0, 0, 4), 22 to 7, ::r2cf),
CFData("sqrt(2) ", intArrayOf(0, 1, 1, 0), 0 to 0, ::recipRoot2),
CFData("1 / sqrt(2) ", intArrayOf(0, 1, 1, 0), 0 to 0, ::root2),
CFData("(1 + sqrt(2)) / 2 ", intArrayOf(1, 1, 0, 2), 0 to 0, ::root2),
CFData("(1 + 1 / sqrt(2)) / 2", intArrayOf(1, 1, 0, 2), 0 to 0, ::recipRoot2)
)
println("Produced by NG class:")
for ((str, ng, r, gen) in data) {
print("$str -> ")
val (a1, a, b1, b) = ng
val op = NG(a1, a, b1, b)
for (n in gen(r).take(20)) {
if (!op.needTerm) print(" ${op.egress()} ")
op.ingress(n)
}
while (true) {
print(" ${op.egressDone} ")
if (op.done) break
}
println()
}
println("\nProduced by direct calculation:")
val data2 = listOf(
Pair("(1 + sqrt(2)) / 2 ", (1 + Math.sqrt(2.0)) / 2),
Pair("(1 + 1 / sqrt(2)) / 2", (1 + 1 / Math.sqrt(2.0)) / 2)
)
for ((str, d) in data2) {
println("$str -> ${d2cf(d).take(20).joinToString(" ")}")
}
}
You may also check:How to resolve the algorithm String matching step by step in the Elixir programming language
You may also check:How to resolve the algorithm Copy a string step by step in the PL/I programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the BASIC programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Wolfram Language programming language