Package sage :: Package rings :: Module contfrac
[hide private]
[frames] | no frames]

Module contfrac

source code


Continued Fractions

\sage implements the field code{ContinuedFractionField} (or \code{CFF}
for short) of finite simple continued fractions.  This is really
isomorphic to the field $\QQ$ of rational numbers, but with different
printing and semantics.  It should be possible to use this field in
most cases where one could use $\QQ$, except arithmetic is slower.

The \code{continued\_fraction(x)} command returns an element of
\code{CFF} that defines a continued fraction expansion to $x$.  The
command \code{continued\_fraction(x,bits)} computes the continued
fraction expansion of an approximation to $x$ with given bits of
precision.  Use \code{show(c)} to see a continued fraction nicely
typeset, and \code{latex(c)} to obtain the typeset version, e.g., for
inclusion in a paper.

EXAMPLES:
We create some example elements of the continued fraction field.

    sage: c = continued_fraction([1,2]); c
    [1, 2]
    sage: c = continued_fraction([3,7,15,1,292]); c
    [3, 7, 15, 1, 292]
    sage: c = continued_fraction(pi); c
    [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 3]
    sage: c.value()
    245850922/78256779
    sage: QQ(c) 
    245850922/78256779
    sage: RealField(200)(QQ(c) - pi)
    -7.8179366199075435400152113059910891481153981448107195930950e-17

We can also create matrices, polynomials, vectors, etc., over the continued
fraction field.
    sage: a = random_matrix(CFF, 4)
    sage: a
    [[0, 2]    [1]    [1] [0, 2]]
    [  [-1]    [1]   [-1]   [-2]]
    [   [2]    [1]   [-1]    [1]]
    [  [-1]    [1]   [-2] [0, 2]]
    sage: f = a.charpoly()
    sage: f
    [1]*x^4 + ([-1])*x^3 + [3, 1, 3]*x^2 + [3]*x + [-18]
    sage: f(a)
    [[0] [0] [0] [0]]
    [[0] [0] [0] [0]]
    [[0] [0] [0] [0]]
    [[0] [0] [0] [0]]
    sage: vector(CFF, [1/2, 2/3, 3/4, 4/5])
    ([0, 2], [0, 1, 2], [0, 1, 3], [0, 1, 4])



Classes [hide private]
  ContinuedFractionField_class
The field of all finite continued fraction of real numbers.
  ContinuedFraction
Functions [hide private]
 
ContinuedFractionField() source code
 
continued_fraction(x, bits=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a...) source code
Variables [hide private]
  CFF = Field of all continued fractions