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

Published on 12 May 2024 09:40 PM
#J

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

Source code in the j programming language

ng4cf=: 4 : 0
  cf=. 1000{.!._ y
  ng=. x
  r=.i. ndx=.0
  while. +./0~:{:ng do.
    if.=/<.%/ng do.
      r=.r, t=.{.<.%/ng
      ng=. t (|.@] - ]*0,[) ng 
    else.
      if. _=t=.ndx{cf do.
        ng=. ng+/ .*2 2$1 1 0 0
      else.
        ng=. ng+/ .*2 2$t,1 1 0
      end.
      if. (#cf)=ndx=. ndx+1 do. r return. end.
    end.
  end.
  r
)


   arbs=:(,1);(,3);?~&.>3+i.10
   ":@>arbs
1                        
3                        
1 2 0                    
0 2 3 1                  
1 0 3 2 4                
0 2 3 5 1 4              
2 5 0 1 6 3 4            
7 5 6 3 0 4 1 2          
7 0 1 2 6 3 8 4 5        
8 0 5 6 3 7 4 9 1 2      
0 9 8 1 3 10 2 5 6 7 4   
1 7 3 4 5 8 9 10 6 11 0 2
   (+%)/@>arbs
1 3 1 0.444444 4.44444 0.431925 2.16238 7.19368 8.46335 13.1583 0.109719 1.13682


   plus1r2=: (2 1,:0 2)&ng4cf
   (plus1r2 each  -&((+%)/@>) ]) arbs 
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5


   times7r22=: (7 0,:0 22)&ng4cf 
   (times7r22 each %&((+%)/@>) ]) arbs 
0.318182 0.318182 0.318182 0.318182 0.318182 0.318182 0.318182 0.318182 0.318182 0.318182 0.318182 0.318182
   (times7r22 each %&((+%)/@x:@>) ]) arbs 
7r22 7r22 7r22 7r22 7r22 7r22 7r22 7r22 7r22 7r22 7r22 7r22


   times1r4=:(1 0,:0 4)&ng4cf
   (times1r4 each %&((+%)/@>) ]) arbs 
0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25


   reciprocal=:(0 1,:1 0)&ng4cf
   (reciprocal each *&((+%)/@>) ]) arbs 
1 1 1 1 1 1 1 1 1 1 1 1


   plus1r2times1r2=: (1 1,:0 2)&ng4cf
   (plus1r2times1r2 each (= 0.5+0.5*])&((+%)/@>) ]) arbs 
1 1 1 1 1 1 1 1 1 1 1 1


   (+%)/1 5 2
1.18182
   plus1r2 1 5 2
1 1 2 7
   (+%)/plus1r2 1 5 2
1.68182


   (+%)/3 7x
22r7
   times7r22 3 7x
1


   plus1r2 3 7x
3 1 1 1 4
   (+%)/plus1r2 3 7x
3.64286
   (+%)/x:plus1r2 3 7x
51r14


   times1r4 3 7x
0 1 3 1 2
   (+%)/x:times1r4 3 7x
11r14


   reciprocal 1,999$2
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...
   (+%)/1,999$2
1.41421
   (+%)/reciprocal 1,999$2
0.707107


   plus1r2times1r2 1,999$2
1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 ...
   (+%)/plus1r2times1r2 1,999$2
1.20711


   plus1r2times1r2 0 1,999$2
0 1 5 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 ...
   (+%)/plus1r2times1r2 0 1,999$2
0.853553


  

You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Compare sorting algorithms' performance step by step in the REXX programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Oforth programming language
You may also check:How to resolve the algorithm Fractran step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Objeck programming language