1Function: contfracpnqn
2Section: number_theoretical
3C-Name: contfracpnqn
4Prototype: GD-1,L,
5Help: contfracpnqn(x, {n=-1}): [p_n,p_{n-1}; q_n,q_{n-1}] corresponding to the
6 continued fraction x. If n >= 0 is present, returns all convergents from
7 p_0/q_0 up to p_n/q_n.
8Doc: when $x$ is a vector or a one-row matrix, $x$
9 is considered as the list of partial quotients $[a_0,a_1,\dots,a_n]$ of a
10 rational number, and the result is the 2 by 2 matrix
11 $[p_n,p_{n-1};q_n,q_{n-1}]$ in the standard notation of continued fractions,
12 so $p_n/q_n=a_0+1/(a_1+\dots+1/a_n)$. If $x$ is a matrix with two rows
13 $[b_0,b_1,\dots,b_n]$ and $[a_0,a_1,\dots,a_n]$, this is then considered as a
14 generalized continued fraction and we have similarly
15 $p_n/q_n=(1/b_0)(a_0+b_1/(a_1+\dots+b_n/a_n))$. Note that in this case one
16 usually has $b_0=1$.
17
18 If $n \geq 0$ is present, returns all convergents from $p_0/q_0$ up to
19 $p_n/q_n$. (All convergents if $x$ is too small to compute the $n+1$
20 requested convergents.)
21 \bprog
22 ? a = contfrac(Pi,10)
23 %1 = [3, 7, 15, 1, 292, 1, 1, 1, 3]
24 ? allpnqn(x) = contfracpnqn(x,#x) \\ all convergents
25 ? allpnqn(a)
26 %3 =
27 [3 22 333 355 103993 104348 208341 312689 1146408]
28
29 [1  7 106 113  33102  33215  66317  99532  364913]
30 ? contfracpnqn(a) \\ last two convergents
31 %4 =
32 [1146408 312689]
33
34 [ 364913  99532]
35
36 ? contfracpnqn(a,3) \\ first three convergents
37 %5 =
38 [3 22 333 355]
39
40 [1  7 106 113]
41 @eprog
42
43Variant: also available is \fun{GEN}{pnqn}{GEN x} for $n = -1$.
44