John Gamble > Math-ContinuedFraction-0.11 > Math::ContinuedFraction

Download:
Math-ContinuedFraction-0.11.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.11   Source  

NAME ^

Math::ContinuedFraction - Create and Manipulate Continued Fractions.

SYNOPSIS ^

Quick summary of what the module does.

Perhaps a little code snippet.

    use Math::ContinuedFraction;

    #
    # Create new continued fraction objects.
    #
    my $cf = Math::ContinuedFraction->new([1, 4, 9, 25]);
    my $cf_phi = Math::ContinuedFraction->new([1, [1]]);
 
    my $cf_67div29 = Math::ContinuedFraction->from_ratio(67, 29);

DESCRIPTION ^

Continued fractions are expressions of the form

         b1
    a1 + -------
               b2
          a2 + -------
                     b3
                a3 + -------
                        ...

For most instances, the 'b' terms are 1, and the continued fraction can be written as [a1, a2, a3, ...], etc. If the sequence of 'a' terms ends at a certain point, the continued fraction is known as a finite continued fraction, and can be exactly represented as a fraction. If the sequence of 'a' terms has a repeating sequence, it is normally written as

                 ______
    [a1, a2, a3, a4, a5]

where the line over a4 and a5 indicates that they repeat forever. Since we can't use that method in perl code, we indicate the repeating portion by using an array within the array:

    [a1, a2, a3, [a4, a5]]

Note that in the examples in the "SYNOPSIS", $cf_phi is created using that notation.

Methods to Create Continued Fraction Objects

new()

Create a new continued fraction object from an array.

    my $cf = Math::ContinuedFraction([1, [2, 1]]);

Arrays are in the form [finite_sequence, [repeating_sequence]]. A continued fraction with no repeating part simply omits the embedded array reference:

    my $cf = Math::ContinuedFraction([1, 2, 1, 3, 1, 5]);

from_ratio()

Generate a continued fraction from a pair of relatively prime numbers.

Methods to Return Information

convergent()

Returns the fraction formed by calculating the rational approximation of the continued fraction at a stopping point, and returning the numerator and denominator.

Convergent term counts begin at 1. Continued fractions with a repeating component can effectively have a term count as high as you like. Finite continued fractions will stop at the end of the sequence without warning.

    #
    # Find the ratios that approximate pi.
    #
    # The array stops at seven elements for simplicity's sake,
    # the sequence actually does not end.
    #
    my $cfpi = Math::ContinuedFraction([3, 7, 15, 1, 292, 1, 1]);

    for my $j (1..4)
    {
        my($n, $d) = cfpi->convergent($j);
        print $n->bstr() . "/". $d->bstr() . "\n";
    }

The values returned are objects of type Math::BigInt.

brconvergent()

Behaves identically to convergent(), but returns a single Math::BigRat object instead of two Math::BigInt objects.

    #
    # Find the ratios that approximate pi.
    #
    # The array stops at seven elements for simplicity's sake,
    # the sequence actually does not end.
    #
    my $cfpi = Math::ContinuedFraction([3, 7, 15, 1, 292, 1, 1]);

    for my $j (1..4)
    {
        my $r = cfpi->convergent($j);
        print $r->bstr() . "\n";
    }

to_array()

Returns an array reference that can be used to create a continued fraction (see "new()").

    my $cf = Math::ContinuedFraction->from_ratio(0xfff1, 0x7fed);
    my $aref = $cf->to_array()
    my $cf2 = Math::ContinuedFraction->new($aref);

to_ascii()

Returns the string form of the array reference.

    my $cf = Math::ContinuedFraction->from_ratio(0xfff1, 0x7fed);
    print $cf->to_ascii(), "\n";

Returns [2, 1432, 1, 6, 1, 2].

AUTHOR ^

John Gamble, <jgamble at cpan.org>

ACKNOWLEDGEMENTS ^

Olds, C. D. Continued Fractions. New York: Random House, 1963.

COPYRIGHT & LICENSE ^

Copyright 2011 John Gamble.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

syntax highlighting: