SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.function.rn2r1
Class ContinuedFraction

java.lang.Object
  extended by com.numericalmethod.suanshu.analysis.function.rn2r1.UnivariateRealFunction
      extended by com.numericalmethod.suanshu.analysis.function.rn2r1.ContinuedFraction
All Implemented Interfaces:
Function, RealScalarFunction

public class ContinuedFraction
extends UnivariateRealFunction

A continued fraction representation of a number.

 z = b0 +  a1
         -------
         b1 + a2 
            -------
            b2 + a3
               -------
               b3 + a4
                  -------
                  b4 + ...
 
ai and bi can be functions of x, which in turn makes z a function of x.

The sequence zn may or may not converge. In theory, zn can be written as a fraction.

        An
 zn = ------
        Bn
 
An and Bn can be computed by the fundamental recurrence formulas.

In practice, we compute zn using the modified Lentz's method from Thompson and Barnett. This method may suffer from the "false convergence" problem. That is, differences between successive convergents become small, seeming to indicate convergence, but then increase again by many orders of magnitude before finally converging.

See Also:

Nested Class Summary
static class ContinuedFraction.MaxIterationsExceededException
          RuntimeException thrown when the continued fraction fails to converge for a given epsilon before a certain number of iterations.
static interface ContinuedFraction.Partials
          Definition of a continued fraction in terms of the partial numerators an, and the partial denominators bn.
 
Nested classes/interfaces inherited from interface com.numericalmethod.suanshu.analysis.function.Function
Function.EvaluationException
 
Constructor Summary
ContinuedFraction(ContinuedFraction.Partials partials)
          Construct an instance of a continued fraction.
 
Method Summary
 java.math.BigDecimal evaluate(java.math.BigDecimal x, int scale, int nIterations)
          This implements the modified Lentz's method using arbitrary precision arithmetics BigDecimal.
 double evaluate(double x)
          Compute f(x).
 double evaluate(double x, double threshold, int nIterations)
          This implements the modified Lentz's method.
 
Methods inherited from class com.numericalmethod.suanshu.analysis.function.rn2r1.UnivariateRealFunction
dimension4Domain, dimension4Range, evaluate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ContinuedFraction

public ContinuedFraction(ContinuedFraction.Partials partials)
Construct an instance of a continued fraction.

Parameters:
partials - the defining partial numerator and partial denominator functions
Method Detail

evaluate

public double evaluate(double x,
                       double threshold,
                       int nIterations)
This implements the modified Lentz's method.

This implementation is done using double arithmetics, so it is quick. However, the precision is limited by the double precision of the intermediate results. This (and probably others implemented using double precision math) may give poor results for some continued fraction.

Parameters:
x - x
threshold - the threshold if the difference between two successive zn is below it, the fraction is deemed converged
nIterations - the maximum number of iterations
Returns:
an approximation of z
Throws:
ContinuedFraction.MaxIterationsExceededException - if it does not converge before the maximum number of iterations; repeat with a bigger epsilon, or use the BigDecimal version of the algorithm

evaluate

public java.math.BigDecimal evaluate(java.math.BigDecimal x,
                                     int scale,
                                     int nIterations)
This implements the modified Lentz's method using arbitrary precision arithmetics BigDecimal.

Parameters:
x - x
scale - the precision parameter
nIterations - the maximum number of iterations
Returns:
an approximation of z
Throws:
ContinuedFraction.MaxIterationsExceededException - if it does not converge before the maximum number of iterations; repeat with a bigger epsilon

evaluate

public double evaluate(double x)
Description copied from class: UnivariateRealFunction
Compute f(x).

Specified by:
evaluate in class UnivariateRealFunction
Parameters:
x - x
Returns:
f(x)

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.