[ 
https://issues.apache.org/jira/browse/NUMBERS-176?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Alex Herbert resolved NUMBERS-176.
----------------------------------
    Fix Version/s: 1.1
       Resolution: Implemented

Added in commit

23ba4f8ea6109c9bac7b4ec43e17dcaaed358bd9

> 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
>            Priority: Trivial
>             Fix For: 1.1
>
>
> 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)

Reply via email to