Convergents of a Continued Fraction
|
03-13-2022, 11:41 PM
(This post was last modified: 11-25-2022 05:47 PM by Thomas Klemm.)
Post: #1
|
|||
|
|||
Convergents of a Continued Fraction
Convergents of a Continued Fraction
References Goal We want to calculate the convergents of a continued fraction. Formula For example, here are the convergents for \([0;1,5,2,2]\). \( \begin{matrix} n & −2 & −1 & 0 & 1 & 2 & 3 & 4 \\ a_n & & & 0 & 1 & 5 & 2 & 2 \\ h_n & 0 & 1 & 0 & 1 & 5 & 11 & 27 \\ k_n & 1 & 0 & 1 & 1 & 6 & 13 & 32 \\ \end{matrix} \) If successive convergents are found, with numerators \(h_1\), \(h_2\), ... and denominators \(k_1\), \(k_2\), ... then the relevant recursive relation is: \( \begin{align} h_n &= a_n h_{n − 1} + h_{n − 2} \\ k_n &= a_n k_{n − 1} + k_{n − 2} \\ \end{align} \) The successive convergents are given by the expression: \( \begin{align} \frac{h_n}{k_n} \end{align} \) Using this formula, we would need to track four values on the stack. However, I couldn't figure out how to do that without using registers. But we can divide all the equations by \(k_{n − 1}\) and get: \( \begin{align} \frac{h_n}{k_{n − 1}} &= a_n \frac{h_{n − 1}}{k_{n − 1}} + \frac{h_{n − 2}}{k_{n − 1}} \\ \\ \frac{k_n}{k_{n − 1}} &= a_n + \frac{k_{n − 2}}{k_{n − 1}} \\ \end{align} \) Now we introduce three variables \(u\), \(v\) and \(w\): \( \begin{align} u &= \frac{h_{n − 1}}{k_{n − 2}} \\ \\ v &= \frac{k_{n − 1}}{k_{n − 2}} \\ \\ w &= \frac{h_{n − 2}}{k_{n − 2}} \\ \end{align} \) With \(u{'}\), \(v{'}\) and \(w{'}\) we denote the successors: \( \begin{align} u{'} &= \frac{h_{n}}{k_{n − 1}} =& a_n w{'} + \frac{w}{v} \\ \\ v{'} &= \frac{k_{n}}{k_{n − 1}} =& a_n + \frac{1}{v} \\ \\ w{'} &= \frac{h_{n − 1}}{k_{n − 1}} =& \frac{u}{v} \\ \end{align} \) So we only need to keep track of the three values \(v\), \(w\), and \(w{'}\) on the stack. The sequence \(w, w{'}, w{''}, \cdots\) are the convergents of the continued fraction. Programs This program is for the HP-42S, but it will work on most HP calculators with little or no modification: Code: 00 { 18-Byte Prgm } This the same program for the HP-15C: Code: 001 { 34 } x↔y Stack Diagram We enter the program at label 00. At that point you can consider \(\frac{w}{v} = 1\) and \(\frac{1}{v} = 0\). Code: | L | T | Z | Y | X | Usage We start with the first two elements of the continued fraction and place them on the stack. However, we must enter the values \(1\) and \(0\) in between: \(a_0\) ENTER 1 ENTER 0 ENTER \(a_1\) ENTER XEQ 00 In the following examples we write only briefly: \(a_0\) 1 0 \(a_1\) XEQ 00 Now we can proceed with: \(a_2\) R/S \(a_3\) R/S \(a_4\) R/S \(a_5\) R/S … Examples Rational Numbers This is the finite continued fraction example from above and represents a rational number. \( [0; 1, 5, 2, 2] \) 0 1 0 1 XEQ 00 5 R/S 2 R/S 2 R/S Code: 0 0.00000000000 0 1 Golden Ratio The golden ratio \(\varphi\) has terms equal to \(1\) everywhere—the smallest values possible—which makes \(\varphi\) the most difficult number to approximate rationally. In this sense, therefore, it is the "most irrational" of all irrational numbers. \( \varphi = [\overline{1}] \) 1 1 0 1 XEQ 00 1 R/S 1 R/S 1 R/S 1 R/S 1 R/S 1 R/S 1 R/S 1 R/S … Code: 1 1.00000000000 1 1 Also it produces the Fibonacci sequence. Periodic Continued Fractions The numbers with periodic continued fraction expansion are precisely the irrational solutions of quadratic equations with rational coefficients. \( \sqrt{42} = [6; \overline{2, 12}] \) 6 1 0 2 XEQ 00 12 R/S 2 R/S 12 R/S 2 R/S 12 R/S 2 R/S 12 R/S 2 R/S 12 R/S 2 R/S … Code: 6 6.00000000000 6 1 Other Patterns While there is no discernible pattern in the simple continued fraction expansion of \(\pi\), there is one for \(e\), the base of the natural logarithm: \( e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, 14, 1, 1, 16, 1, 1, 18, 1, 1, \cdots] \) 2 1 0 1 XEQ 00 2 R/S 1 R/S 1 R/S 4 R/S 1 R/S 1 R/S 6 R/S 1 R/S 1 R/S 8 R/S 1 R/S … Code: 2 2.00000000000 2 1 \(\pi\) No pattern has ever been found in this representation. \( \pi = [3; 7,15,1,292,1,1,1,2,1,3,1,14,2,1,1,2,2,2,2,1,84,2,1,1,15,3,13, \cdots] \) 3 1 0 7 XEQ 00 15 R/S 1 R/S 292 R/S 1 R/S 1 R/S 1 R/S 2 R/S 1 R/S 3 R/S 1 R/S 14 R/S 2 R/S … Code: 3 3.00000000000 3 1 |
|||
09-18-2024, 04:50 PM
Post: #2
|
|||
|
|||
RE: Convergents of a Continued Fraction
Code work even if continued fraction coefficient is 0. Very nice!
OP HP-42S code: 1 1 0 2 XEQ 00 --> 1 + 1/2 = 1.5 0 R/S --> 1 + 1/(2 + 1/0) = 1 + 1/inf = 1 3 R/S --> 1 + 1/(2 + 1/(0 + 1/3)) = 1 + 1/(2 + 3) = 1.2 Here is lua code to allow for Generalized Continued Fraction We simply scale CF numerator to 1, then use OP simple continued fraction code. x = b0 + a1/(b1 + a2/(b2 + a3/(b3 + ... = b0 + 1/(b1/a1 + (a2/a1)/(b2 + a3/(b3 + ... = b0 + 1/(b1/a1 + 1/(b2/(a2/a1) + a3/(a2/a1)/(b3 + ... Code: function dfc2f(b0) Lets try tan(pi/4)=1, using generalized CF form, and simple CF form (both are equivalent) lua> CF1, CF2 = dfc2f(0), dfc2f(0) lua> x = pi/4 lua> CF1(x,1), CF2(1/x) 0.7853981633974483 0.7853981633974483 lua> for k=1,8 do n=2*k+1; print(CF1(-x*x, n), CF2((-1)^k*n/x)) end 0.988689239934205 0.988689239934205 0.9997876809149683 0.9997876809149683 0.9999978684156949 0.9999978684156949 0.9999999865263551 0.9999999865263551 0.9999999999413254 0.9999999999413254 0.9999999999998131 0.9999999999998131 0.9999999999999997 0.9999999999999997 1.0000000000000002 1.0000000000000002 |
|||
09-18-2024, 08:24 PM
Post: #3
|
|||
|
|||
RE: Convergents of a Continued Fraction
(03-13-2022 11:41 PM)Thomas Klemm Wrote: With \(u{'}\), \(v{'}\) and \(w{'}\) we denote the successors: Assuming {u,v,w} start out finite. (i.e. a1 ≠ 0) This is what happen if we have double-zero CF coeffs, an = an+1 = 0, n>1 w' = u/v u' = w/v v' = 1/v w'' = u'/v' = (w/v) / (1/v) = w u'' = w'/v' = (u/v) / (1/v) = u v'' = 1/v' = 1/(1/v) = v 0 + 1/(0 + 1/CF) = 1/(1/CF) = CF {u'',v'',w''} = {u,v,w}, matching expectation. |
|||
09-20-2024, 12:04 AM
Post: #4
|
|||
|
|||
RE: Convergents of a Continued Fraction
(03-13-2022 11:41 PM)Thomas Klemm Wrote: For example, here are the convergents for \([0;1,5,2,2]\). Just a visual explanation why OP setup only need to track 3 values on the stack. Scaling the entries does not change convergent ratios. h(n)/k(n) = w(n)/1, and we don't need to store denominator 1 Code: a(n) 0 1 5 2 2 But, there is a cost. Convergent integer ratio is lost, only its value remained. Also, a1≠0 is required, because we can't scale denominator of 0 to 1. Also, we can't start with a0 alone, because k(-1)=0, unable to scale to 1 |
|||
09-20-2024, 05:27 PM
(This post was last modified: 09-20-2024 05:31 PM by Albert Chan.)
Post: #5
|
|||
|
|||
RE: Convergents of a Continued Fraction
(03-13-2022 11:41 PM)Thomas Klemm Wrote: We enter the program at label 00. I was curious if we enter some other value pairs between a0, a1 Code: a0 a1 a2 \(\displaystyle \frac{h_1}{k_1} = \frac{a_0\;a_1 + z}{a_1 + y} \;=\; a_0 + \frac{z - a_0\;y}{a_1 + y} \) \(\displaystyle \frac{h_2}{k_2} = \frac{(a_0\;a_1+z)×a_2 + a_0}{(a_1+y)×a_2+1} \;=\; a_0 + \frac{z - a_0\;y}{a_1 + y + \frac{1}{a_2}} \) ... \(\displaystyle \frac{h_∞}{k_∞} = a_0 + \frac{z - a_0\;y}{[a_1 + y ;\; a_2 , a_3 ,\; ...]}\) If {z,y} = {1,0}, this reduced to simple continued fraction \([a_0 ;\; a_1, a_2, a_3, \; ...]\) Confirm by example: 3 Enter 14 Enter 159 Enter 2654 XEQ 00 → 2.835407038748666903661571276217561 1 R/S → 2.835465529495380241648898365316275 2 R/S → 2.835446037199383959246534770761757 lua> a0, z, y, a1 = 3, 14, 159, 2654 lua> f = dfc2f(a0) lua> f(z-a0*y, a1+y) 2.835407038748667 lua> f(1) 2.83546552949538 lua> f(2) 2.835446037199384 |
|||
09-20-2024, 10:24 PM
(This post was last modified: 09-21-2024 04:17 AM by C.Ret.)
Post: #6
|
|||
|
|||
RE: Convergents of a Continued Fraction
(03-13-2022 11:41 PM)Thomas Klemm Wrote: For example, here are the convergents for \([0;1,5,2,2]\). Thank you for this very informative post and for sharing such a concise and elegant code. I also don't know how to manage four elements on the stack. However, since there are only two numerators and two denominators, we only need to use two stack elements for each new \(a_i\). The four variables come from the two numerator and denominator pairs. If we can use just one stack level for each element, we’re safe. One easy way to achieve this on an HP-15C is to use the complex-stack. I’m not sure if the same trick can be applied to other HP calculators that support complex arithmetic. Here is a simple program for the HP-15C that uses the complex stack to compute the convergents: Code: 001 { 42 21 11 } f LBL A ; Add next element a(i) to the set [ a₀ ; a₁ , a₂ , … ] Code: 011 { 42 21 15 } f LBL E ; Show Estimated value of convergent h/k Code: 016 { 42 21 13 } f LBL C ; CLEAR / redo from start LBL A or R/S: Adds the next integer \(a_i\) and compute a new convergent (h,k) where h is the real part and k the imaginary part of X: register. Also h/k is store in register R0. LBL E: Briefly displays an estimate of the current convergent (which is stored in register R0). At each step, f-(i) can be used to view the denominator; h (Re-part) and k (Im-part). The register R0 contains estimated value of convergent h/k. However, make sure to leave (h−2,k−2) and (h−1,k−1) in registers Y: and X: respectively before pressing f−A or R/S. Code: Example: [ 0 ; 1 , 5 , 2 , 2 ] Code: Example: Euler's Constant γ [ 0; 1, 1, 2, 1, 2, 1, 4, 3, 13, 5, 1, 1, 8, 1, 2, 4, 1, 1, 40, . . . I didn’t quite reach my goal, as my code is a bit longer and uses one register (I couldn’t figure out how to compute Re/Im from the stack without a register), whereas your brilliant code is shorter and only uses the stack. That said, my approach is somewhat simpler for the user, as it only requires entering the continued fraction elements in order and nothing else. |
|||
09-21-2024, 08:33 AM
Post: #7
|
|||
|
|||
RE: Convergents of a Continued Fraction
(09-20-2024 10:24 PM)C.Ret Wrote: One easy way to achieve this on an HP-15C is to use the complex-stack. I’m not sure if the same trick can be applied to other HP calculators that support complex arithmetic. This program is for the HP-42S: Code: 00 { 27-Byte Prgm } To use it set LCLBL and select the CUSTOM menu. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)