# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.function.rn2r1.univariate

## Class ContinuedFraction

• All Implemented Interfaces:
Function<Vector,Double>, RealScalarFunction, UnivariateRealFunction

public class ContinuedFraction
extends AbstractUnivariateRealFunction
A continued fraction representation of a number has this form: $z = b_0 + \cfrac{a_1}{b_1 + \cfrac{a_2}{b_2 + \cfrac{a_3}{b_3 + \cfrac{a_4}{b_4 + \ddots\,}}}}$ 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: $$z_n = \frac{A_n}{B_n}$$. 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.

• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
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
This interface defines 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

Constructors
Constructor and Description
ContinuedFraction(ContinuedFraction.Partials partials)
Construct a continued fraction.
ContinuedFraction(ContinuedFraction.Partials partials, double epsilon, int maxIterations)
Construct a continued fraction.
ContinuedFraction(ContinuedFraction.Partials partials, int scale, int maxIterations)
Construct a continued fraction.
• ### Method Summary

All Methods
Modifier and Type Method and Description
BigDecimal evaluate(BigDecimal x)
Evaluate z.
double evaluate(double x)
Evaluate y = f(x).
• ### Methods inherited from class com.numericalmethod.suanshu.analysis.function.rn2r1.univariate.AbstractUnivariateRealFunction

evaluate
• ### Methods inherited from class com.numericalmethod.suanshu.analysis.function.rn2r1.AbstractRealScalarFunction

dimensionOfDomain, dimensionOfRange
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Methods inherited from interface com.numericalmethod.suanshu.analysis.function.Function

dimensionOfDomain, dimensionOfRange
• ### Constructor Detail

• #### ContinuedFraction

public ContinuedFraction(ContinuedFraction.Partials partials,
double epsilon,
int maxIterations)
Construct a continued fraction.
Parameters:
partials - the definition in terms of partial numerators and partial denominators
epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0
maxIterations - the maximum number of iterations
• #### ContinuedFraction

public ContinuedFraction(ContinuedFraction.Partials partials,
int scale,
int maxIterations)
Construct a continued fraction.
Parameters:
partials - the definition in terms of partial numerators and partial denominators
scale - the accuracy
maxIterations - the maximum number of iterations
• #### ContinuedFraction

public ContinuedFraction(ContinuedFraction.Partials partials)
Construct a continued fraction.
Parameters:
partials - the definition in terms of partial numerators and partial denominators
• ### Method Detail

• #### evaluate

public double evaluate(double x)
Evaluate y = f(x). This implementation adopts the modified Lentz's method, using double arithmetics. It is quick. However, the precision is limited by the double precision of the intermediate results. This (and probably other implementations using double precision math) may give poor results for some continued fraction.
Parameters:
x - x
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 BigDecimal evaluate(BigDecimal x)
Evaluate z. This implementation adopts the modified Lentz's method using arbitrary precision arithmetics BigDecimal.
Parameters:
x - x
Returns:
an approximation of z
Throws:
ContinuedFraction.MaxIterationsExceededException - if it does not converge before the maximum number of iterations; repeat with a bigger epsilon