Description
The ContinuedFraction class allows computation of:
b0 + a1 ------------------ b1 + a2 ------------- b2 + a3 -------- b3 + ...
This is done using an abstract class that is extended to implement the methods to get the terms:
double getA(int n, double x); double getB(int n, double x);
This allows the fraction to be reused to generate results for different points to evaluate. However:
- It does not lend itself to fractions where the terms can be computed using recursion:
b(n+1) = f( b(n) )
- It requires two method calls to generate terms a_n and b_n for each iteration thus preventing optimisation of the computation using the input n for values shared between computation of a and b.
An alternative method is to support a generator of the paired terms a_n and b_n:
static double continuedFraction(Supplier<double[]> gen);
To be used in a single evaluation as:
Supplier<double[]> goldenRatio = () -> return new double[] {1, 1}; double gr = continuedFraction(goldenRatio); // gr = 1.61803398874...
An additional feature is to support a simple continued fraction where all partial numerators are 1:
b0 + 1 ------------------ b1 + 1 ------------- b2 + 1 -------- b3 + ...
E.g. using:
static double simpleContinuedFraction(DoubleSupplier gen);
Addition
In some situations it is an advantage to not evaluate the leading term b0. The term may not be part of a regular series, or may be zero:
a1 ------------------ b1 + a2 ------------- b2 + a3 -------- b3 + ...
API
Using the nomeclature from Wikipedia and Wolfram suggests the following:
public static final class GeneralizedContinuedFraction { /** * Evaluate the continued fraction from the generator for partial numerator * a and partial denominator b. * <pre> * b0 + a1 * ------------------ * b1 + a2 * ------------- * b2 + a3 * -------- * b3 + ... * </pre> * * <p>Note: The first generated partial numerator a0 is discarded. * * @param gen Generator * @return the continued fraction value */ public static double value(Supplier<double[]> gen); /** * Evaluate the continued fraction from the generator for partial numerator * a and partial denominator b. * <pre> * a1 * ------------------ * b1 + a2 * ------------- * b2 + a3 * -------- * b3 + ... * </pre> * * <p>Note: Both of the first terms a and b are used. * * @param gen Generator * @return the continued fraction value */ public static double valueA(Supplier<double[]> gen); } public static final class SimpleContinuedFraction { public static double value(DoubleSupplier gen); public static double valueA(DoubleSupplier gen); }
The API should support the optional parameters to control the convergence tolerance and the maximum number of iterations.
The variant to evaluate without the leading b0 term is not essential. It can be evaluated by starting the generator at the next iteration using:
Supplier<double[]> gen = ...; // Start at terms (a1,b1) // Evaluation will discard a1; // this value (separately computed) is then used to compute the result double value = a1 / GeneralizedContinuedFraction.value(gen);
This variant is present in the Boost c++ library and used to evaluate terms for the gamma function.
See:
https://en.wikipedia.org/wiki/Continued_fraction
https://mathworld.wolfram.com/SimpleContinuedFraction.html
https://mathworld.wolfram.com/GeneralizedContinuedFraction.html
Boost Continued Fraction (v 1_77_0)
Attachments
Issue Links
- is related to
-
NUMBERS-174 Update Gamma functions using the Boost implementation
- Closed