ContinuedFraction.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.util;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalStateException;

  24. /**
  25.  * Provides a generic means to evaluate continued fractions.  Subclasses simply
  26.  * provided the a and b coefficients to evaluate the continued fraction.
  27.  * <p>
  28.  * References:
  29.  * <ul>
  30.  * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
  31.  * Continued Fraction</a></li>
  32.  * </ul>
  33.  */
  34. public abstract class ContinuedFraction {
  35.     /** Maximum allowed numerical error. */
  36.     private static final double DEFAULT_EPSILON = 10e-9;

  37.     /**
  38.      * Default constructor.
  39.      */
  40.     protected ContinuedFraction() {
  41.         super();
  42.     }

  43.     /**
  44.      * Access the n-th a coefficient of the continued fraction.  Since a can be
  45.      * a function of the evaluation point, x, that is passed in as well.
  46.      * @param n the coefficient index to retrieve.
  47.      * @param x the evaluation point.
  48.      * @return the n-th a coefficient.
  49.      */
  50.     protected abstract double getA(int n, double x);

  51.     /**
  52.      * Access the n-th b coefficient of the continued fraction.  Since b can be
  53.      * a function of the evaluation point, x, that is passed in as well.
  54.      * @param n the coefficient index to retrieve.
  55.      * @param x the evaluation point.
  56.      * @return the n-th b coefficient.
  57.      */
  58.     protected abstract double getB(int n, double x);

  59.     /**
  60.      * Evaluates the continued fraction at the value x.
  61.      * @param x the evaluation point.
  62.      * @return the value of the continued fraction evaluated at x.
  63.      * @throws MathIllegalStateException if the algorithm fails to converge.
  64.      */
  65.     public double evaluate(double x) throws MathIllegalStateException {
  66.         return evaluate(x, DEFAULT_EPSILON, Integer.MAX_VALUE);
  67.     }

  68.     /**
  69.      * Evaluates the continued fraction at the value x.
  70.      * @param x the evaluation point.
  71.      * @param epsilon maximum error allowed.
  72.      * @return the value of the continued fraction evaluated at x.
  73.      * @throws MathIllegalStateException if the algorithm fails to converge.
  74.      */
  75.     public double evaluate(double x, double epsilon) throws MathIllegalStateException {
  76.         return evaluate(x, epsilon, Integer.MAX_VALUE);
  77.     }

  78.     /**
  79.      * Evaluates the continued fraction at the value x.
  80.      * @param x the evaluation point.
  81.      * @param maxIterations maximum number of convergents
  82.      * @return the value of the continued fraction evaluated at x.
  83.      * @throws MathIllegalStateException if the algorithm fails to converge.
  84.      * @throws MathIllegalStateException if maximal number of iterations is reached
  85.      */
  86.     public double evaluate(double x, int maxIterations)
  87.         throws MathIllegalStateException {
  88.         return evaluate(x, DEFAULT_EPSILON, maxIterations);
  89.     }

  90.     /**
  91.      * Evaluates the continued fraction at the value x.
  92.      * <p>
  93.      * The implementation of this method is based on the modified Lentz algorithm as described
  94.      * on page 18 ff. in:
  95.      * </p>
  96.      * <ul>
  97.      *   <li>
  98.      *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
  99.      *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
  100.      *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
  101.      *   </li>
  102.      * </ul>
  103.      * <p>
  104.      * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
  105.      * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
  106.      * </p>
  107.      *
  108.      * @param x the evaluation point.
  109.      * @param epsilon maximum error allowed.
  110.      * @param maxIterations maximum number of convergents
  111.      * @return the value of the continued fraction evaluated at x.
  112.      * @throws MathIllegalStateException if the algorithm fails to converge.
  113.      * @throws MathIllegalStateException if maximal number of iterations is reached
  114.      */
  115.     public double evaluate(double x, double epsilon, int maxIterations)
  116.         throws MathIllegalStateException {
  117.         final double small = 1e-50;
  118.         double hPrev = getA(0, x);

  119.         // use the value of small as epsilon criteria for zero checks
  120.         if (Precision.equals(hPrev, 0.0, small)) {
  121.             hPrev = small;
  122.         }

  123.         int n = 1;
  124.         double dPrev = 0.0;
  125.         double cPrev = hPrev;
  126.         double hN = hPrev;

  127.         while (n < maxIterations) {
  128.             final double a = getA(n, x);
  129.             final double b = getB(n, x);

  130.             double dN = a + b * dPrev;
  131.             if (Precision.equals(dN, 0.0, small)) {
  132.                 dN = small;
  133.             }
  134.             double cN = a + b / cPrev;
  135.             if (Precision.equals(cN, 0.0, small)) {
  136.                 cN = small;
  137.             }

  138.             dN = 1 / dN;
  139.             final double deltaN = cN * dN;
  140.             hN = hPrev * deltaN;

  141.             if (Double.isInfinite(hN)) {
  142.                 throw new MathIllegalStateException(LocalizedCoreFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE, x);
  143.             }
  144.             if (Double.isNaN(hN)) {
  145.                 throw new MathIllegalStateException(LocalizedCoreFormats.CONTINUED_FRACTION_NAN_DIVERGENCE, x);
  146.             }

  147.             if (FastMath.abs(deltaN - 1.0) < epsilon) {
  148.                 break;
  149.             }

  150.             dPrev = dN;
  151.             cPrev = cN;
  152.             hPrev = hN;
  153.             n++;
  154.         }

  155.         if (n >= maxIterations) {
  156.             throw new MathIllegalStateException(LocalizedCoreFormats.NON_CONVERGENT_CONTINUED_FRACTION,
  157.                                                 maxIterations, x);
  158.         }

  159.         return hN;
  160.     }

  161. }