Class GeneralizedContinuedFraction

java.lang.Object
org.apache.commons.numbers.fraction.GeneralizedContinuedFraction

public final class GeneralizedContinuedFraction extends Object
Provides a means to evaluate generalized continued fractions.

The continued fraction uses the following form for the numerator (a) and denominator (b) coefficients:

              a1
 b0 + ------------------
      b1 +      a2
           -------------
           b2 +    a3
                --------
                b3 + ...
 

A generator of the coefficients must be provided to evaluate the continued fraction.

The implementation of the fraction evaluation is based on the modified Lentz algorithm as described on page 508 in:

Since:
1.1
See Also:
  • Method Details

    • value

      Evaluates the continued fraction.

      Note: The first generated partial numerator a0 is discarded.

      Parameters:
      gen - Generator of coefficients.
      Returns:
      the value of the continued fraction.
      Throws:
      ArithmeticException - if the algorithm fails to converge or if the maximal number of iterations is reached before the expected convergence is achieved.
      See Also:
    • value

      public static double value(Supplier<GeneralizedContinuedFraction.Coefficient> gen, double epsilon)
      Evaluates the continued fraction.

      Note: The first generated partial numerator a0 is discarded.

      Parameters:
      gen - Generator of coefficients.
      epsilon - Maximum relative error allowed.
      Returns:
      the value of the continued fraction.
      Throws:
      ArithmeticException - if the algorithm fails to converge or if the maximal number of iterations is reached before the expected convergence is achieved.
      See Also:
    • value

      public static double value(Supplier<GeneralizedContinuedFraction.Coefficient> gen, double epsilon, int maxIterations)
      Evaluates the continued fraction.
                    a1
       b0 + ------------------
            b1 +      a2
                 -------------
                 b2 +    a3
                      --------
                      b3 + ...
       

      Setting coefficient an to zero will signal the end of the recursive evaluation.

      Note: The first generated partial numerator a0 is discarded.

      Usage Note

      This method is not functionally identical to calling value(double, Supplier, double, int) with the generator configured to provide coefficients from n=1 and supplying b0 separately. In some cases the computed result from the two variations may be different by more than the provided epsilon. The other method should be used if b0 is zero or very small. See the corresponding javadoc for details.

      Parameters:
      gen - Generator of coefficients.
      epsilon - Maximum relative error allowed.
      maxIterations - Maximum number of iterations.
      Returns:
      the value of the continued fraction.
      Throws:
      ArithmeticException - if the algorithm fails to converge or if the maximal number of iterations is reached before the expected convergence is achieved.
      See Also:
    • value

      public static double value(double b0, Supplier<GeneralizedContinuedFraction.Coefficient> gen)
      Evaluates the continued fraction.

      Note: The initial term b0 is supplied as an argument. Both of the first generated terms a and b are used. This fraction evaluation can be used when:

      • b0 is not part of a regular series
      • b0 is zero and the result will evaluate only the continued fraction component
      • b0 is very small and the result is expected to approach zero
      Parameters:
      b0 - Coefficient b0.
      gen - Generator of coefficients.
      Returns:
      the value of the continued fraction.
      Throws:
      ArithmeticException - if the algorithm fails to converge or if the maximal number of iterations is reached before the expected convergence is achieved.
      See Also:
    • value

      public static double value(double b0, Supplier<GeneralizedContinuedFraction.Coefficient> gen, double epsilon)
      Evaluates the continued fraction.

      Note: The initial term b0 is supplied as an argument. Both of the first generated terms a and b are used. This fraction evaluation can be used when:

      • b0 is not part of a regular series
      • b0 is zero and the result will evaluate only the continued fraction component
      • b0 is very small and the result is expected to approach zero
      Parameters:
      b0 - Coefficient b0.
      gen - Generator of coefficients.
      epsilon - Maximum relative error allowed.
      Returns:
      the value of the continued fraction.
      Throws:
      ArithmeticException - if the algorithm fails to converge or if the maximal number of iterations is reached before the expected convergence is achieved.
      See Also:
    • value

      public static double value(double b0, Supplier<GeneralizedContinuedFraction.Coefficient> gen, double epsilon, int maxIterations)
      Evaluates the continued fraction.
                    a1
       b0 + ------------------
            b1 +      a2
                 -------------
                 b2 +    a3
                      --------
                      b3 + ...
       

      Setting coefficient an to zero will signal the end of the recursive evaluation.

      Note: The initial term b0 is supplied as an argument. Both of the first generated terms a and b are used. This fraction evaluation can be used when:

      • b0 is not part of a regular series
      • b0 is zero and the result will evaluate only the continued fraction component
      • b0 is very small and the result is expected to approach zero

      Usage Note

      This method is not functionally identical to calling value(Supplier, double, int) with the generator configured to provide term "b0" in the first coefficient. In some cases the computed result from the two variations may be different by more than the provided epsilon. The convergence of the continued fraction algorithm relies on computing an update multiplier applied to the current value. Convergence is faster if the initial value is close to the final value. The value(Supplier, double, int) method will initialise the current value using b0 and evaluate the continued fraction using updates computed from the generated coefficients. This method initialises the algorithm using b1 to evaluate part of the continued fraction and computes the result as:

              a1
       b0 + ------
             part
       

      This is preferred if b0 is smaller in magnitude than the continued fraction component. In particular the evaluation algorithm sets a bound on the minimum initial value as 1e-50. If b0 is smaller than this value then using this method is the preferred evaluation.

      Parameters:
      b0 - Coefficient b0.
      gen - Generator of coefficients.
      epsilon - Maximum relative error allowed.
      maxIterations - Maximum number of iterations.
      Returns:
      the value of the continued fraction.
      Throws:
      ArithmeticException - if the algorithm fails to converge or if the maximal number of iterations is reached before the expected convergence is achieved.
      See Also: