How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2) step by step in the Kotlin programming language
How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2) step by step in the Kotlin programming language
Table of Contents
Problem Statement
This task performs the basic mathematical functions on 2 continued fractions. This requires the full version of matrix 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}}}}
and
a
2
b
2
{\displaystyle {\frac {a_{2}}{b_{2}}}}
and
a
12
b
12
{\displaystyle {\frac {a_{12}}{b_{12}}}}
are equal. Otherwise I input a term from continued fraction N1 or continued fraction N2. If I need a term from N but N has no more terms I inject
∞
{\displaystyle \infty }
. When I input a term t from continued fraction N1 I change my internal state: When I need a term from exhausted continued fraction N1 I change my internal state: When I input a term t from continued fraction N2 I change my internal state: When I need a term from exhausted continued fraction N2 I change my internal state: When I output a term t I change my internal state: When I need to choose to input from N1 or N2 I act: When performing arithmetic operation on two potentially infinite continued fractions it is possible to generate a rational number. eg
2
{\displaystyle {\sqrt {2}}}
2
{\displaystyle {\sqrt {2}}}
should produce 2. This will require either that I determine that my internal state is approaching infinity, or limiting the number of terms I am willing to input without producing any output.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2) step by step in the Kotlin programming language
This code implements the computation of continued fractions in Kotlin.
It has a collection of classes that implement the ContinuedFraction
interface, which has two functions: nextTerm
and moreTerms
.
nextTerm
returns the next term in the continued fraction.moreTerms
returnstrue
if there are more terms to compute, andfalse
otherwise.
The main class is NG
, which implements ContinuedFraction
and contains a MatrixNG
object and a list of ContinuedFraction
objects.
The MatrixNG
class is an abstract class that represents a matrix, and it has three abstract methods: consumeTerm
, consumeTerm(n)
, and needTerm
.
These methods are used to consume terms from the matrix and to determine if the matrix needs more terms.
The NG
class uses the MatrixNG
object and the list of ContinuedFraction
objects to compute the next term in the continued fraction.
The moreTerms
method calls the needTerm
method of the MatrixNG
object to determine if the matrix needs more terms.
If the matrix needs more terms, the moreTerms
method calls the consumeTerm
or consumeTerm(n)
method of the MatrixNG
object to consume terms from the matrix.
The nextTerm
method of the NG
class returns the thisTerm
variable of the MatrixNG
object, which is the next term in the continued fraction.
The test
function is used to test the ContinuedFraction
implementation.
It takes a description of the test and a variable number of ContinuedFraction
objects as arguments.
The test
function calls the moreTerms
method of each ContinuedFraction
object to determine if it has more terms to compute.
If the ContinuedFraction
object has more terms, the test
function calls the nextTerm
method to get the next term in the continued fraction.
The test
function prints the terms of the continued fraction to the console.
The main
function creates several ContinuedFraction
objects and calls the test
function to test them.
Source code in the kotlin programming language
// version 1.2.10
import kotlin.math.abs
abstract class MatrixNG {
var cfn = 0
var thisTerm = 0
var haveTerm = false
abstract fun consumeTerm()
abstract fun consumeTerm(n: Int)
abstract fun needTerm(): Boolean
}
class NG4(
var a1: Int, var a: Int, var b1: Int, var b: Int
) : MatrixNG() {
private var t = 0
override fun needTerm(): Boolean {
if (b1 == 0 && b == 0) return false
if (b1 == 0 || b == 0) return true
thisTerm = a / b
if (thisTerm == a1 / b1) {
t = a; a = b; b = t - b * thisTerm
t = a1; a1 = b1; b1 = t - b1 * thisTerm
haveTerm = true
return false
}
return true
}
override fun consumeTerm() {
a = a1
b = b1
}
override fun consumeTerm(n: Int) {
t = a; a = a1; a1 = t + a1 * n
t = b; b = b1; b1 = t + b1 * n
}
}
class NG8(
var a12: Int, var a1: Int, var a2: Int, var a: Int,
var b12: Int, var b1: Int, var b2: Int, var b: Int
) : MatrixNG() {
private var t = 0
private var ab = 0.0
private var a1b1 = 0.0
private var a2b2 = 0.0
private var a12b12 = 0.0
fun chooseCFN() = if (abs(a1b1 - ab) > abs(a2b2-ab)) 0 else 1
override fun needTerm(): Boolean {
if (b1 == 0 && b == 0 && b2 == 0 && b12 == 0) return false
if (b == 0) {
cfn = if (b2 == 0) 0 else 1
return true
}
else ab = a.toDouble() / b
if (b2 == 0) {
cfn = 1
return true
}
else a2b2 = a2.toDouble() / b2
if (b1 == 0) {
cfn = 0
return true
}
else a1b1 = a1.toDouble() / b1
if (b12 == 0) {
cfn = chooseCFN()
return true
}
else a12b12 = a12.toDouble() / b12
thisTerm = ab.toInt()
if (thisTerm == a1b1.toInt() && thisTerm == a2b2.toInt() &&
thisTerm == a12b12.toInt()) {
t = a; a = b; b = t - b * thisTerm
t = a1; a1 = b1; b1 = t - b1 * thisTerm
t = a2; a2 = b2; b2 = t - b2 * thisTerm
t = a12; a12 = b12; b12 = t - b12 * thisTerm
haveTerm = true
return false
}
cfn = chooseCFN()
return true
}
override fun consumeTerm() {
if (cfn == 0) {
a = a1; a2 = a12
b = b1; b2 = b12
}
else {
a = a2; a1 = a12
b = b2; b1 = b12
}
}
override fun consumeTerm(n: Int) {
if (cfn == 0) {
t = a; a = a1; a1 = t + a1 * n
t = a2; a2 = a12; a12 = t + a12 * n
t = b; b = b1; b1 = t + b1 * n
t = b2; b2 = b12; b12 = t + b12 * n
}
else {
t = a; a = a2; a2 = t + a2 * n
t = a1; a1 = a12; a12 = t + a12 * n
t = b; b = b2; b2 = t + b2 * n
t = b1; b1 = b12; b12 = t + b12 * n
}
}
}
interface ContinuedFraction {
fun nextTerm(): Int
fun moreTerms(): Boolean
}
class R2cf(var n1: Int, var n2: Int) : ContinuedFraction {
override fun nextTerm(): Int {
val thisTerm = n1 /n2
val t2 = n2
n2 = n1 - thisTerm * n2
n1 = t2
return thisTerm
}
override fun moreTerms() = abs(n2) > 0
}
class NG : ContinuedFraction {
val ng: MatrixNG
val n: List<ContinuedFraction>
constructor(ng: NG4, n1: ContinuedFraction) {
this.ng = ng
n = listOf(n1)
}
constructor(ng: NG8, n1: ContinuedFraction, n2: ContinuedFraction) {
this.ng = ng
n = listOf(n1, n2)
}
override fun nextTerm(): Int {
ng.haveTerm = false
return ng.thisTerm
}
override fun moreTerms(): Boolean {
while (ng.needTerm()) {
if (n[ng.cfn].moreTerms())
ng.consumeTerm(n[ng.cfn].nextTerm())
else
ng.consumeTerm()
}
return ng.haveTerm
}
}
fun test(desc: String, vararg cfs: ContinuedFraction) {
println("TESTING -> $desc")
for (cf in cfs) {
while (cf.moreTerms()) print ("${cf.nextTerm()} ")
println()
}
println()
}
fun main(args: Array<String>) {
val a = NG8(0, 1, 1, 0, 0, 0, 0, 1)
val n2 = R2cf(22, 7)
val n1 = R2cf(1, 2)
val a3 = NG4(2, 1, 0, 2)
val n3 = R2cf(22, 7)
test("[3;7] + [0;2]", NG(a, n1, n2), NG(a3, n3))
val b = NG8(1, 0, 0, 0, 0, 0, 0, 1)
val b1 = R2cf(13, 11)
val b2 = R2cf(22, 7)
test("[1;5,2] * [3;7]", NG(b, b1, b2), R2cf(286, 77))
val c = NG8(0, 1, -1, 0, 0, 0, 0, 1)
val c1 = R2cf(13, 11)
val c2 = R2cf(22, 7)
test("[1;5,2] - [3;7]", NG(c, c1, c2), R2cf(-151, 77))
val d = NG8(0, 1, 0, 0, 0, 0, 1, 0)
val d1 = R2cf(22 * 22, 7 * 7)
val d2 = R2cf(22,7)
test("Divide [] by [3;7]", NG(d, d1, d2))
val na = NG8(0, 1, 1, 0, 0, 0, 0, 1)
val a1 = R2cf(2, 7)
val a2 = R2cf(13, 11)
val aa = NG(na, a1, a2)
val nb = NG8(0, 1, -1, 0, 0, 0, 0, 1)
val b3 = R2cf(2, 7)
val b4 = R2cf(13, 11)
val bb = NG(nb, b3, b4)
val nc = NG8(1, 0, 0, 0, 0, 0, 0, 1)
val desc = "([0;3,2] + [1;5,2]) * ([0;3,2] - [1;5,2])"
test(desc, NG(nc, aa, bb), R2cf(-7797, 5929))
}
You may also check:How to resolve the algorithm Logical operations step by step in the EMal programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the Go programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Tcl programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the C programming language
You may also check:How to resolve the algorithm Guess the number step by step in the C# programming language