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.

    See Also:
    • 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