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 or the ratio of two numbers.

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:

$cf = Math::ContinuedFraction([1, 2, 1, 3, 1, 5]);
$cf = Math::ContinuedFraction->new([1, 71, 13, 8]);
$cf = Math::ContinuedFraction->new([1, 2, 1, 2, [3, 2, 3, 2]]);

A continued fraction may be created from a ratio between two numbers. Be sure not to put the numbers in an array, as

#
# Find the CF form of 121/23.
#
$cf  = Math::ContinuedFraction->new(121, 23);

is different from

#
# Find the CF of
#     121 + 1
#          -----
#           23
#
$cf  = Math::ContinuedFraction->new([121, 23]);

The ratio may consist of Math::BigInt objects.

$big_n = Math::BigInt->new("0xccc43c90d2c0");
$big_q = Math::BigInt->new("0xb2069d579ddb");
$cf = Math::ContinuedFraction->new($big_n, $big_q);

A Math::BigRat object will also work:

$bratio = Math::BigRat->new("0xccc43c90d2c0", "0xb2069d579ddb");
$cf = Math::ContinuedFraction->new($bratio);

from_ratio()

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

my $cf67_29 = Math::ContinuedFraction->from_ratio(67, 29);

Create a continued fraction from a simple ratio. These CFs will always be the simple types.

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. See sequence A001203
# at the Online Encyclopedia of Integer Sequences (http://www.oeis.org/)
#
my $cfpi = Math::ContinuedFraction([3, 7, 15, 1, 292, 1, 1]);

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

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.

($numerator, $denominator) = $cf->convergent($nth);

Get the fraction for the continued fraction at the nth term.

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].

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.

AUTHOR

John Gamble, <jgamble at cpan.org>