You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2021/11/24 13:55:00 UTC

[jira] [Created] (NUMBERS-176) ContinuedFraction to delegate to GeneralizedContinuedFraction

Alex Herbert created NUMBERS-176:
------------------------------------

             Summary: ContinuedFraction to delegate to GeneralizedContinuedFraction
                 Key: NUMBERS-176
                 URL: https://issues.apache.org/jira/browse/NUMBERS-176
             Project: Commons Numbers
          Issue Type: Improvement
          Components: fraction
    Affects Versions: 1.1
            Reporter: Alex Herbert


The ContinuedFraction class evaluates using the same algorithm as the GeneralizedContinuedFraction. These should use the same code to eliminate duplication.

The GeneralizedContinuedFraction has the latest version of the algorithm with a bug fix for detecting invalid update steps and increased support for very small convergence epsilon (see NUMBERS-175).

The simple approach is to call the GeneralizedContinuedFraction by creating a generator from the ContinuedFraction and the argument x:
{code:java}
public double evaluate(double x, double epsilon, int maxIterations) {
    // Delegate to GeneralizedContinuedFraction

    // Get the first coefficient
    final double b0 = getB(0, x);

    // Generate coefficients from (a1,b1)
    final Supplier<Coefficient> gen = new Supplier<Coefficient>() {
        private int n;
        @Override
        public Coefficient get() {
            n++;
            final double a = getA(n, x);
            final double b = getB(n, x);
            return Coefficient.of(a, b);
        }
    };

    // Invoke appropriate method based on magnitude of first term
    if (Math.abs(b0) < SMALL) {
        return GeneralizedContinuedFraction.value(b0, gen, epsilon, maxIterations);
    }
    return GeneralizedContinuedFraction.evaluate(b0, gen, epsilon, maxIterations);
}
{code}
This will only evaluate each used coefficient once. It requires exposure of the GeneralizedContinuedFraction.evaluate method at the package level.

The threshold for small values and default for iterations can be exposed as package private from GeneralizedContinuedFraction. This ensures the method calls the appropriate evaluation method based on the magnitude of the first term b0 with the same defaults.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)