Continued fraction/Arithmetic: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(→‎Matrix NG: Make everything look prettier)
Line 30: Line 30:
\end{bmatrix}
\end{bmatrix}
</math>
</math>
and a function G(matrix NG, Number N<sub>1</sub>, Number N<sub>2</sub>)
and a function <math>G(\mathrm{matrix}</math> <math>\mathit{NG}, \mathrm{Number}</math> <math>N_1, \mathrm{Number}</math> <math>N_2)</math>
which returns:
which returns:
: <math>\frac{a_{12}*N_1*N_2 + a_1*N_1 + a_2*N_2 + a}{b_{12}*N_1*N_2 + b_1*N_1 + a_2*N_2 + b}</math>
: <math>\frac{a_{12}\times N_1\times N_2 + a_1\times N_1 + a_2\times N_2 + a}{b_{12}\times N_1\times N_2 + b_1\times N_1 + a_2\times N_2 + b}</math>

: Convince yourself that NG = :
===Basic identities===
: <math>\begin{bmatrix}
: <math>\mathit{NG} = \begin{bmatrix}
0 & 1 & 1 & 0 \\
0 & 1 & 1 & 0 \\
0 & 0 & 0 & 1
0 & 0 & 0 & 1
\end{bmatrix}</math> adds N<sub>1</sub> to N<sub>2</sub>
\end{bmatrix}</math>
computes <math>N_1 + N_2</math>
:<math>\begin{bmatrix}
:<math>\mathit{NG} = \begin{bmatrix}
0 & 1 & -1 & 0 \\
0 & 1 & -1 & 0 \\
0 & 0 & 0 & 1
0 & 0 & 0 & 1
\end{bmatrix}</math> subtracts N<sub>2</sub> from N<sub>1</sub>
\end{bmatrix}</math>
: <math>\begin{bmatrix}
computes <math>N_1 - N_2</math>
: <math>\mathit{NG} = \begin{bmatrix}
1 & 0 & 0 & 0 \\
1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1
0 & 0 & 0 & 1
\end{bmatrix}</math> multiplies N<sub>1</sub> by N<sub>2</sub>
\end{bmatrix}</math>
: <math>\begin{bmatrix}
computes <math>N_1 \times N_2</math>
: <math>\mathit{NG} = \begin{bmatrix}
0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0
0 & 0 & 1 & 0
\end{bmatrix}</math> divides N<sub>1</sub> by N<sub>2</sub>
\end{bmatrix}</math>
: <math>\begin{bmatrix}
computes <math>N_1 / N_2</math>

===Compound operations===
: <math>\mathit{NG} = \begin{bmatrix}
21 & -15 & 28 & -20 \\
21 & -15 & 28 & -20 \\
0 & 0 & 0 & 1
0 & 0 & 0 & 1
\end{bmatrix}</math> calculates (3*N<sub>1</sub> + 4) * (7*N<sub>2</sub> - 5)
\end{bmatrix}</math>
calculates (<math>3\times N_1 + 4) \times (7\times N_2 - 5)</math>
:Note that with N<sub>1</sub> = 22, N<sub>2</sub> = 7, and NG = :

: <math>\begin{bmatrix}
Note that with <math>N_1 = 22</math>, <math>N_2 = 7</math>, and
: <math>\mathit{NG} = \begin{bmatrix}
0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0
0 & 0 & 1 & 0
\end{bmatrix}</math>
\end{bmatrix}</math>
:I could define the solution to be N<sub>1</sub> = 1, N<sub>2</sub> = 1 and NG = :
I could define the solution to be <math>N_1 = 1</math>, <math>N_2 = 1</math> and
: <math>\begin{bmatrix}
: <math>\mathit{NG} = \begin{bmatrix}
0 & 0 & 0 & 22 \\
0 & 0 & 0 & 22 \\
0 & 0 & 0 & 7
0 & 0 & 0 & 7
\end{bmatrix}</math>
\end{bmatrix}</math>
:So I can define arithmetic as operations on this matrix which make a<sub>12</sub>, a<sub>1</sub>, a<sub>2</sub>, b<sub>12</sub>, b<sub>1</sub>, b<sub>2</sub> zero and read the answer from a and b. This is more interesting when N<sub>1</sub> and N<sub>2</sub> are continued fractions, which is the subject of the following tasks.
So I can define arithmetic as operations on this matrix which make <math>a_{12}</math>, <math>a_1</math>, <math>a_2</math>, <math>b_{12}</math>, <math>b_1</math>, <math>b_2</math> zero and read the answer from <math>a</math> and <math>b</math>. This is more interesting when <math>N_1</math> and <math>N_2</math> are continued fractions, which is the subject of the following tasks.


==[[Continued fraction arithmetic/G(matrix NG, Contined Fraction N) | G(matrix NG, Contined Fraction N)]]==
==[[Continued fraction arithmetic/G(matrix NG, Contined Fraction N) | G(matrix NG, Contined Fraction N)]]==

Revision as of 17:11, 10 March 2013

By popular demand, see Talk:Continued fraction#creating_a_continued_fraction and Talk:Continued fraction#Arithmetics.3F.3F, or be careful what you ask for.

This page is a placeholder for several subtasks which will eventually implement a function:

G(matrix NG, Continued Fraction N1, Continued Fraction N2)

which will perform basic mathmatical operations on continued fractions.

{http://mathworld.wolfram.com/ContinuedFraction.html Mathworld] informs me:

Gosper has invented an algorithm for performing analytic addition, subtraction, multiplication, and division using continued fractions. It requires keeping track of eight integers which are conceptually arranged at the polyhedron vertices of a cube. Although this algorithm has not appeared in print, similar algorithms have been constructed by Vuillemin (1987) and Liardet and Stambul (1998).
Gosper's algorithm for computing the continued fraction for (ax+b)/(cx+d) from the continued fraction for x is described by Gosper (1972), Knuth (1998, Exercise 4.5.3.15, pp. 360 and 601), and Fowler (1999). (In line 9 of Knuth's solution, X_k<-|_A/C_| should be replaced by X_k<-min(|_A/C_|,|_(A+B)/(C+D)_|).) Gosper (1972) and Knuth (1981) also mention the bivariate case (axy+bx+cy+d)/(Axy+Bx+Cy+D).


My description follows part of Gosper reproduced on perl.plover.com. This document is text and unnumbered, you may wish to start by searching for "Addition, Multiplication, etc. of Two Continued Fractions" prior to reading the whole thing.

perl.plover.com also includes a series of slides as a class on continued fractions. The example [1;5,2] + 1/2 in G(matrix NG, Contined Fraction_N) is worked through in this class.


For these tasks continued fractions will be of the form:

so each may be described by the notation []

Continued fraction arithmetic/Continued fraction r2cf(Rational N)

During these tasks I shall use the function described in this task to create continued fractions from rational numbers.

Matrix NG

Consider a matrix NG:

and a function which returns:

Basic identities

computes

computes

computes

computes

Compound operations

calculates (

Note that with , , and

I could define the solution to be , and

So I can define arithmetic as operations on this matrix which make , , , , , zero and read the answer from and . This is more interesting when and are continued fractions, which is the subject of the following tasks.

G(matrix NG, Contined Fraction N)

Here we perform basic mathematical operations on a single continued fraction.

The bivarate solution G(matrix NG, Continued Fraction N1, Continued Fraction N2)

Here we perform basic mathematical operations on two continued fractions.

  • Compare two continued fractions
  • Sqrt of a continued fraction