Class ContinuedFraction
- java.lang.Object
-
- dev.nm.analysis.function.rn2r1.AbstractRealScalarFunction
-
- dev.nm.analysis.function.rn2r1.univariate.AbstractUnivariateRealFunction
-
- dev.nm.analysis.function.rn2r1.univariate.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.- See Also:
- "I. J. Thompson, A. R. Barnett, "Coulomb and Bessel functions of complex arguments and order," J. Comput. Phys, 1986."
- "W. J. Lentz, "Generating Bessel Functions In Mie Scattering Calculations Using Continued Fractions," Applied Optics, Vol. 15, Issue 3, p. 668-671."
- "W. Gautschi, Math. Comput, 31, 994(1997)."
- Wikipedia: Generalized continued fraction
- Wikipedia: Fundamental recurrence formulas
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 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 dev.nm.analysis.function.Function
Function.EvaluationException
-
-
Constructor Summary
Constructors Constructor 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 Instance Methods Concrete Methods Modifier and Type Method Description double
evaluate(double x)
Evaluate y = f(x).BigDecimal
evaluate(BigDecimal x)
Evaluate z.-
Methods inherited from class dev.nm.analysis.function.rn2r1.univariate.AbstractUnivariateRealFunction
evaluate
-
Methods inherited from class dev.nm.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 dev.nm.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 denominatorsepsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0maxIterations
- 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 denominatorsscale
- the accuracymaxIterations
- 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, usingdouble
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 arithmeticsBigDecimal
.- 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
-
-