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
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
RuntimeException
thrown when the continued fraction fails to converge for a given epsilon before a certain number of iterations.static interface
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
ConstructorsConstructorDescriptionContinuedFraction
(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
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 Details
-
ContinuedFraction
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
Construct a continued fraction.- Parameters:
partials
- the definition in terms of partial numerators and partial denominatorsscale
- the accuracymaxIterations
- the maximum number of iterations
-
ContinuedFraction
Construct a continued fraction.- Parameters:
partials
- the definition in terms of partial numerators and partial denominators
-
-
Method Details
-
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
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
-