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

Source code in the racket programming language

#lang racket/base

(struct ng (a1 a b1 b) #:transparent #:mutable)
 
(define (ng-ingress! v t)
  (define a (ng-a v))
  (define a1 (ng-a1 v))
  (define b (ng-b v))
  (define b1 (ng-b1 v))
  (set-ng-a! v a1)
  (set-ng-a1! v (+ a (* a1 t)))
  (set-ng-b! v b1)
  (set-ng-b1! v (+ b (* b1 t))))
 
(define (ng-needterm? v)
  (or (zero? (ng-b v)) 
      (zero? (ng-b1 v)) 
      (not (= (quotient (ng-a v) (ng-b v)) (quotient (ng-a1 v) (ng-b1 v))))))
 
(define (ng-egress! v)
  (define t (quotient (ng-a v) (ng-b v)))
  (define a (ng-a v))
  (define a1 (ng-a1 v))
  (define b (ng-b v))
  (define b1 (ng-b1 v))
  (set-ng-a! v b)
  (set-ng-a1! v b1)
  (set-ng-b! v (- a (* b t)))
  (set-ng-b1! v (- a1 (* b1 t)))
  t)
 
(define (ng-infty! v)
  (when (ng-needterm? v)
    (set-ng-a! v (ng-a1 v))
    (set-ng-b! v (ng-b1 v))))
 
(define (ng-done? v)
  (and (zero? (ng-b v)) (zero? (ng-b1 v))))


(define ((rational->cf n d))
  (and (not (zero? d))
       (let-values ([(q r) (quotient/remainder n d)])
         (set! n d)
         (set! d r)
         q)))

(define (sqrt2->cf)
  (define first? #t)
  (lambda ()
    (if first?
        (begin 
          (set! first? #f)
          1)
        2)))


(define (combine-ng-cf->cf ng cf)
  (define empty-producer? #f)
  (lambda ()
    (let loop ()
      (cond 
        [(not empty-producer?) (define t (cf))
                               (cond 
                                   [t (ng-ingress! ng t)
                                      (if (ng-needterm? ng)
                                          (loop)
                                          (ng-egress! ng))]
                                   [else (set! empty-producer? #t)
                                         (loop)])]
        [(ng-done? ng) #f]
        [(ng-needterm? ng) (ng-infty! ng) 
                           (loop)]
        [else (ng-egress! ng)]))))

(define (cf-showln cf n)
  (for ([i (in-range n)])
    (define val (cf))
    (when val
      (printf " ~a" val)))
  (when (cf)
    (printf " ..."))
  (printf "~n"))


(display "[1;5,2] + 1/2 ->")
(cf-showln (combine-ng-cf->cf (ng 2 1 0 2) (rational->cf 13 11)) 20)

(display "[3;7] + 1/2 ->")
(cf-showln (combine-ng-cf->cf (ng 2 1 0 2) (rational->cf 22 7)) 20)

(display "[3;7] / 4 ->")
(cf-showln (combine-ng-cf->cf (ng 1 0 0 4) (rational->cf 22 7)) 20)

(display "sqrt(2)/2 ->")
(cf-showln (combine-ng-cf->cf (ng 1 0 0 2) (sqrt2->cf)) 20)

(display "1/sqrt(2) ->")
(cf-showln (combine-ng-cf->cf (ng 0 1 1 0) (sqrt2->cf)) 20)

(display "(1+sqrt(2))/2 ->")
(cf-showln (combine-ng-cf->cf (ng 1 1 0 2) (sqrt2->cf)) 20)


  

You may also check:How to resolve the algorithm Generator/Exponential step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Department numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the jq programming language
You may also check:How to resolve the algorithm A+B step by step in the i programming language