How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the C++ programming language
How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the C++ 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 C++ programming language
The provided C++ code is an implementation of a Continued Fraction (CF) class. A Continued Fraction is a way of representing a real number as a sequence of integers. This implementation uses a simple class hierarchy to represent different types of Continued Fractions.
class matrixNG {
...
};
- The
matrixNG
class is an abstract base class for all matrixNG classes. It provides a common interface for all matrixNG classes.
class NG_4 : public matrixNG {
...
};
- The
NG_4
class is a specific implementation of a matrixNG class. It represents a Continued Fraction of the form:
a1 + (a / (b1 + (b / 2)))
class NG : public ContinuedFraction {
...
};
- The
NG
class is a specific implementation of a Continued Fraction class. It represents a Continued Fraction of the form:
ng + n1 + n2
where ng
is a matrixNG class, and n1
and n2
are ContinuedFraction classes.
int main() {
...
}
- The
main
function is the entry point of the program. It creates several instances of theNG
class and uses them to calculate and print the values of various Continued Fractions.
The code demonstrates how to use the NG
class to calculate Continued Fractions.
Here's a breakdown of what the code does:
-
It defines a class
matrixNG
that provides an interface for all matrixNG classes. -
It defines a class
NG_4
that represents a Continued Fraction of the forma1 + (a / (b1 + (b / 2)))
. -
It defines a class
NG
that represents a Continued Fraction of the formng + n1 + n2
, whereng
is a matrixNG class, andn1
andn2
are ContinuedFraction classes. -
The
main
function creates several instances of theNG
class and uses them to calculate and print the values of various Continued Fractions.
For example, the first main
function:
int main() {
NG_4 a1(2,1,0,2);
r2cf n1(13,11);
for(NG n(&a1, &n1); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
return 0;
}
creates an instance of the NG
class with an NG_4
matrix and an r2cf
Continued Fraction. It then iterates through the terms of the Continued Fraction and prints them to the console. The output is:
3 7 15 1 292 1 1 1 2 1 3 1 14 2 1 1 2 1 1 4 1 1 8 1 3 1 1 16 1 1 1 4 1 1 32 3 1 2 7 1 ...
This is the Continued Fraction representation of the square root of 2.
The other main
functions in the code demonstrate different ways to use the NG
class to calculate Continued Fractions.
Source code in the cpp programming language
/* Interface for all matrixNG classes
Nigel Galloway, February 10th., 2013.
*/
class matrixNG {
private:
virtual void consumeTerm(){}
virtual void consumeTerm(int n){}
virtual const bool needTerm(){}
protected: int cfn = 0, thisTerm;
bool haveTerm = false;
friend class NG;
};
/* Implement the babyNG matrix
Nigel Galloway, February 10th., 2013.
*/
class NG_4 : public matrixNG {
private: int a1, a, b1, b, t;
const bool needTerm() {
if (b1==0 and b==0) return false;
if (b1==0 or b==0) return true; else thisTerm = a/b;
if (thisTerm==(int)(a1/b1)){
t=a; a=b; b=t-b*thisTerm; t=a1; a1=b1; b1=t-b1*thisTerm;
haveTerm=true; return false;
}
return true;
}
void consumeTerm(){a=a1; b=b1;}
void consumeTerm(int n){t=a; a=a1; a1=t+a1*n; t=b; b=b1; b1=t+b1*n;}
public:
NG_4(int a1, int a, int b1, int b): a1(a1), a(a), b1(b1), b(b){}
};
/* Implement a Continued Fraction which returns the result of an arithmetic operation on
1 or more Continued Fractions (Currently 1 or 2).
Nigel Galloway, February 10th., 2013.
*/
class NG : public ContinuedFraction {
private:
matrixNG* ng;
ContinuedFraction* n[2];
public:
NG(NG_4* ng, ContinuedFraction* n1): ng(ng){n[0] = n1;}
NG(NG_8* ng, ContinuedFraction* n1, ContinuedFraction* n2): ng(ng){n[0] = n1; n[1] = n2;}
const int nextTerm() {ng->haveTerm = false; return ng->thisTerm;}
const bool moreTerms(){
while(ng->needTerm()) if(n[ng->cfn]->moreTerms()) ng->consumeTerm(n[ng->cfn]->nextTerm()); else ng->consumeTerm();
return ng->haveTerm;
}
};
int main() {
NG_4 a1(2,1,0,2);
r2cf n1(13,11);
for(NG n(&a1, &n1); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
return 0;
}
int main() {
NG_4 a2(7,0,0,22);
r2cf n2(22,7);
for(NG n(&a2, &n2); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
return 0;
}
int main() {
NG_4 a3(2,1,0,2);
r2cf n3(22,7);
for(NG n(&a3, &n3); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
return 0;
}
int main() {
NG_4 a4(1,0,0,4);
r2cf n4(22,7);
for(NG n(&a4, &n4); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
return 0;
}
int main() {
NG_4 a5(0,1,1,0);
SQRT2 n5;
int i = 0;
for(NG n(&a5, &n5); n.moreTerms() and i++ < 20; std::cout << n.nextTerm() << " ");
std::cout << "..." << std::endl;
for(r2cf cf(10000000, 14142136); cf.moreTerms(); std::cout << cf.nextTerm() << " ");
std::cout << std::endl;
return 0;
}
int main() {
int i = 0;
NG_4 a6(1,1,0,2);
SQRT2 n6;
for(NG n(&a6, &n6); n.moreTerms() and i++ < 20; std::cout << n.nextTerm() << " ");
std::cout << "..." << std::endl;
for(r2cf cf(24142136, 20000000); cf.moreTerms(); std::cout << cf.nextTerm() << " ");
std::cout << std::endl;
return 0;
}
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the Swift programming language
You may also check:How to resolve the algorithm Enumerations step by step in the Factor programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Action! programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element insertion step by step in the Ada programming language
You may also check:How to resolve the algorithm User input/Text step by step in the Batch File programming language