FieldContinuedFraction.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.util;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.exception.LocalizedCoreFormats;
  20. import org.hipparchus.exception.MathIllegalStateException;

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

  35.     /**
  36.      * Default constructor.
  37.      */
  38.     protected FieldContinuedFraction() {
  39.         super();
  40.     }

  41.     /**
  42.      * Access the n-th a coefficient of the continued fraction.  Since a can be
  43.      * a function of the evaluation point, x, that is passed in as well.
  44.      * @param n the coefficient index to retrieve.
  45.      * @param x the evaluation point.
  46.      * @param <T> type of the field elements.
  47.      * @return the n-th a coefficient.
  48.      */
  49.     public abstract <T extends CalculusFieldElement<T>> T getA(int n, T x);

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

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

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

  80.     /**
  81.      * Evaluates the continued fraction at the value x.
  82.      * @param x the evaluation point.
  83.      * @param maxIterations maximum number of convergents
  84.      * @param <T> type of the field elements.
  85.      * @return the value of the continued fraction evaluated at x.
  86.      * @throws MathIllegalStateException if the algorithm fails to converge.
  87.      * @throws MathIllegalStateException if maximal number of iterations is reached
  88.      */
  89.     public <T extends CalculusFieldElement<T>> T evaluate(T x, int maxIterations)
  90.         throws MathIllegalStateException {
  91.         return evaluate(x, DEFAULT_EPSILON, maxIterations);
  92.     }

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

  123.         final double small      = 1e-50;
  124.         final T      smallField = one.multiply(small);

  125.         T hPrev = getA(0, x);

  126.         // use the value of small as epsilon criteria for zero checks
  127.         if (Precision.equals(hPrev.getReal(), 0.0, small)) {
  128.             hPrev = one.multiply(small);
  129.         }

  130.         int n     = 1;
  131.         T   dPrev = zero;
  132.         T   cPrev = hPrev;
  133.         T   hN    = hPrev;

  134.         while (n < maxIterations) {
  135.             final T a = getA(n, x);
  136.             final T b = getB(n, x);

  137.             T dN = a.add(b.multiply(dPrev));
  138.             if (Precision.equals(dN.getReal(), 0.0, small)) {
  139.                 dN = smallField;
  140.             }
  141.             T cN = a.add(b.divide(cPrev));
  142.             if (Precision.equals(cN.getReal(), 0.0, small)) {
  143.                 cN = smallField;
  144.             }

  145.             dN = dN.reciprocal();
  146.             final T deltaN = cN.multiply(dN);
  147.             hN = hPrev.multiply(deltaN);

  148.             if (hN.isInfinite()) {
  149.                 throw new MathIllegalStateException(LocalizedCoreFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE, x);
  150.             }
  151.             if (hN.isNaN()) {
  152.                 throw new MathIllegalStateException(LocalizedCoreFormats.CONTINUED_FRACTION_NAN_DIVERGENCE, x);
  153.             }

  154.             if (deltaN.subtract(1.0).abs().getReal() < epsilon) {
  155.                 break;
  156.             }

  157.             dPrev = dN;
  158.             cPrev = cN;
  159.             hPrev = hN;
  160.             n++;
  161.         }

  162.         if (n >= maxIterations) {
  163.             throw new MathIllegalStateException(LocalizedCoreFormats.NON_CONVERGENT_CONTINUED_FRACTION,
  164.                                                 maxIterations, x);
  165.         }

  166.         return hN;
  167.     }

  168. }