How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the zkl 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 zkl programming language

Source code in the zkl programming language

class NG{
   fcn init(_a1,_a, _b1,_b){ var a1=_a1,a=_a, b1=_b1,b=_b; }
   var [proxy] done    =fcn{ b==0 and b1==0 };
   var [proxy] needterm=fcn{ (b==0 or b1==0) or (a/b!=a1/b1) };
   fcn ingress(n){
      t:=self.copy(True);  // tmp copy of vars for eager vs late evaluation 
      a,a1=t.a1, t.a + n*t.a1;
      b,b1=t.b1, t.b + n*t.b1;
   }
   fcn egress{
      n,t:=a/b,self.copy(True);
      a,b  =t.b, t.a  - n*t.b;
      a1,b1=t.b1,t.a1 - n*t.b1;
      n
   }
   fcn egress_done{
      if(needterm) a,b=a1,b1;
      egress()
   }
}

   // from task: Continued fraction/Arithmetic/Construct from rational number
fcn r2cf(nom,dnom){ // -->Walker (iterator)
   Walker.tweak(fcn(_,state){
      nom,dnom:=state;
      if(dnom==0) return(Void.Stop);
      n,d:=nom.divr(dnom);
      state.clear(dnom,d);
      n
   }.fp1(List(nom,dnom)))
}

data:=T(T("[1;5,2] + 1/2",      T(2,1,0,2), T(13,11)),
        T("[3;7] + 1/2",        T(2,1,0,2), T(22, 7)),
        T("[3;7] divided by 4", T(1,0,0,4), T(22, 7)));
foreach string,ng,r in (data){
   print("%-20s-->".fmt(string));
   op:=NG(ng.xplode());
   foreach n in (r2cf(r.xplode())){
      if(not op.needterm) print(" %s".fmt(op.egress()));
      op.ingress(n);
   }
   do{ print(" ",op.egress_done()) }while(not op.done);
   println();
}

  

You may also check:How to resolve the algorithm Generic swap step by step in the Go programming language
You may also check:How to resolve the algorithm Universal Turing machine step by step in the C++ programming language
You may also check:How to resolve the algorithm Longest increasing subsequence step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Aliquot sequence classifications step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the PHP programming language