Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#CONTINUED_FRACTION_NAN_DIVERGENCE

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#CONTINUED_FRACTION_NAN_DIVERGENCE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Math_31_ContinuedFraction_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * <p>
 * Evaluates the continued fraction at the value x.
 * </p>
 *
 * <p>
 * The implementation of this method is based on the modified Lentz algorithm as described
 * on page 18 ff. in:
 * <ul>
 * <li>
 *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
 *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
 *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
 * </li>
 * </ul>
 * Note: the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction / MathWorld</a>.
 * </p>
 *
 * @param x the evaluation point.
 * @param epsilon maximum error allowed.
 * @param maxIterations maximum number of convergents
 * @return the value of the continued fraction evaluated at x.
 * @throws ConvergenceException if the algorithm fails to converge.
 */
public double evaluate(double x, double epsilon, int maxIterations) {
    final double small = 1e-50;
    double hPrev = getA(0, x);

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

    int n = 1;
    double dPrev = 0.0;
    double cPrev = hPrev;
    double hN = hPrev;

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

        double dN = a + b * dPrev;
        if (Precision.equals(dN, 0.0, small)) {
            dN = small;
        }
        double cN = a + b / cPrev;
        if (Precision.equals(cN, 0.0, small)) {
            cN = small;
        }

        dN = 1 / dN;
        final double deltaN = cN * dN;
        hN = hPrev * deltaN;

        if (Double.isInfinite(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                                           x);
        }
        if (Double.isNaN(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
                                           x);
        }

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

        dPrev = dN;
        cPrev = cN;
        hPrev = hN;
        n++;
    }

    if (n >= maxIterations) {
        throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
                                            maxIterations, x);
    }

    return hN;
}
 
Example 2
Source File: ContinuedFraction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Evaluates the continued fraction at the value x.
 * <p>
 * The implementation of this method is based on the modified Lentz algorithm as described
 * on page 18 ff. in:
 * <ul>
 *   <li>
 *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
 *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
 *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
 *   </li>
 * </ul>
 * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
 * </p>
 *
 * @param x the evaluation point.
 * @param epsilon maximum error allowed.
 * @param maxIterations maximum number of convergents
 * @return the value of the continued fraction evaluated at x.
 * @throws ConvergenceException if the algorithm fails to converge.
 * @throws MaxCountExceededException if maximal number of iterations is reached
 */
public double evaluate(double x, double epsilon, int maxIterations)
    throws ConvergenceException, MaxCountExceededException {
    final double small = 1e-50;
    double hPrev = getA(0, x);

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

    int n = 1;
    double dPrev = 0.0;
    double cPrev = hPrev;
    double hN = hPrev;

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

        double dN = a + b * dPrev;
        if (Precision.equals(dN, 0.0, small)) {
            dN = small;
        }
        double cN = a + b / cPrev;
        if (Precision.equals(cN, 0.0, small)) {
            cN = small;
        }

        dN = 1 / dN;
        final double deltaN = cN * dN;
        hN = hPrev * deltaN;

        if (Double.isInfinite(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                                           x);
        }
        if (Double.isNaN(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
                                           x);
        }

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

        dPrev = dN;
        cPrev = cN;
        hPrev = hN;
        n++;
    }

    if (n >= maxIterations) {
        throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
                                            maxIterations, x);
    }

    return hN;
}
 
Example 3
Source File: ContinuedFraction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Evaluates the continued fraction at the value x.
 * <p>
 * The implementation of this method is based on the modified Lentz algorithm as described
 * on page 18 ff. in:
 * <ul>
 *   <li>
 *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
 *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
 *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
 *   </li>
 * </ul>
 * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
 * </p>
 *
 * @param x the evaluation point.
 * @param epsilon maximum error allowed.
 * @param maxIterations maximum number of convergents
 * @return the value of the continued fraction evaluated at x.
 * @throws ConvergenceException if the algorithm fails to converge.
 * @throws MaxCountExceededException if maximal number of iterations is reached
 */
public double evaluate(double x, double epsilon, int maxIterations)
    throws ConvergenceException, MaxCountExceededException {
    final double small = 1e-50;
    double hPrev = getA(0, x);

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

    int n = 1;
    double dPrev = 0.0;
    double cPrev = hPrev;
    double hN = hPrev;

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

        double dN = a + b * dPrev;
        if (Precision.equals(dN, 0.0, small)) {
            dN = small;
        }
        double cN = a + b / cPrev;
        if (Precision.equals(cN, 0.0, small)) {
            cN = small;
        }

        dN = 1 / dN;
        final double deltaN = cN * dN;
        hN = hPrev * deltaN;

        if (Double.isInfinite(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                                           x);
        }
        if (Double.isNaN(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
                                           x);
        }

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

        dPrev = dN;
        cPrev = cN;
        hPrev = hN;
        n++;
    }

    if (n >= maxIterations) {
        throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
                                            maxIterations, x);
    }

    return hN;
}
 
Example 4
Source File: ContinuedFraction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>
 * Evaluates the continued fraction at the value x.
 * </p>
 *
 * <p>
 * The implementation of this method is based on the modified Lentz algorithm as described
 * on page 18 ff. in:
 * <ul>
 * <li>
 *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
 *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
 *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
 * </li>
 * </ul>
 * Note: the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction / MathWorld</a>.
 * </p>
 *
 * @param x the evaluation point.
 * @param epsilon maximum error allowed.
 * @param maxIterations maximum number of convergents
 * @return the value of the continued fraction evaluated at x.
 * @throws ConvergenceException if the algorithm fails to converge.
 */
public double evaluate(double x, double epsilon, int maxIterations) {
    final double small = 1e-50;
    double hPrev = getA(0, x);

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

    int n = 1;
    double dPrev = 0.0;
    double cPrev = hPrev;
    double hN = hPrev;

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

        double dN = a + b * dPrev;
        if (Precision.equals(dN, 0.0, small)) {
            dN = small;
        }
        double cN = a + b / cPrev;
        if (Precision.equals(cN, 0.0, small)) {
            cN = small;
        }

        dN = 1 / dN;
        final double deltaN = cN * dN;
        hN = hPrev * deltaN;

        if (Double.isInfinite(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                                           x);
        }
        if (Double.isNaN(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
                                           x);
        }

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

        dPrev = dN;
        cPrev = cN;
        hPrev = hN;
        n++;
    }

    if (n >= maxIterations) {
        throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
                                            maxIterations, x);
    }

    return hN;
}
 
Example 5
Source File: ContinuedFraction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Evaluates the continued fraction at the value x.
 * <p>
 * The implementation of this method is based on the modified Lentz algorithm as described
 * on page 18 ff. in:
 * <ul>
 *   <li>
 *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
 *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
 *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
 *   </li>
 * </ul>
 * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
 * </p>
 *
 * @param x the evaluation point.
 * @param epsilon maximum error allowed.
 * @param maxIterations maximum number of convergents
 * @return the value of the continued fraction evaluated at x.
 * @throws ConvergenceException if the algorithm fails to converge.
 * @throws MaxCountExceededException if maximal number of iterations is reached
 */
public double evaluate(double x, double epsilon, int maxIterations)
    throws ConvergenceException, MaxCountExceededException {
    final double small = 1e-50;
    double hPrev = getA(0, x);

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

    int n = 1;
    double dPrev = 0.0;
    double cPrev = hPrev;
    double hN = hPrev;

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

        double dN = a + b * dPrev;
        if (Precision.equals(dN, 0.0, small)) {
            dN = small;
        }
        double cN = a + b / cPrev;
        if (Precision.equals(cN, 0.0, small)) {
            cN = small;
        }

        dN = 1 / dN;
        final double deltaN = cN * dN;
        hN = hPrev * deltaN;

        if (Double.isInfinite(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                                           x);
        }
        if (Double.isNaN(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
                                           x);
        }

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

        dPrev = dN;
        cPrev = cN;
        hPrev = hN;
        n++;
    }

    if (n >= maxIterations) {
        throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
                                            maxIterations, x);
    }

    return hN;
}
 
Example 6
Source File: ContinuedFraction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Evaluates the continued fraction at the value x.
 * <p>
 * The implementation of this method is based on the modified Lentz algorithm as described
 * on page 18 ff. in:
 * <ul>
 *   <li>
 *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
 *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
 *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
 *   </li>
 * </ul>
 * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
 * </p>
 *
 * @param x the evaluation point.
 * @param epsilon maximum error allowed.
 * @param maxIterations maximum number of convergents
 * @return the value of the continued fraction evaluated at x.
 * @throws ConvergenceException if the algorithm fails to converge.
 * @throws MaxCountExceededException if maximal number of iterations is reached
 */
public double evaluate(double x, double epsilon, int maxIterations)
    throws ConvergenceException, MaxCountExceededException {
    final double small = 1e-50;
    double hPrev = getA(0, x);

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

    int n = 1;
    double dPrev = 0.0;
    double cPrev = hPrev;
    double hN = hPrev;

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

        double dN = a + b * dPrev;
        if (Precision.equals(dN, 0.0, small)) {
            dN = small;
        }
        double cN = a + b / cPrev;
        if (Precision.equals(cN, 0.0, small)) {
            cN = small;
        }

        dN = 1 / dN;
        final double deltaN = cN * dN;
        hN = hPrev * deltaN;

        if (Double.isInfinite(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                                           x);
        }
        if (Double.isNaN(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
                                           x);
        }

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

        dPrev = dN;
        cPrev = cN;
        hPrev = hN;
        n++;
    }

    if (n >= maxIterations) {
        throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
                                            maxIterations, x);
    }

    return hN;
}
 
Example 7
Source File: ContinuedFraction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Evaluates the continued fraction at the value x.
 * <p>
 * The implementation of this method is based on the modified Lentz algorithm as described
 * on page 18 ff. in:
 * <ul>
 *   <li>
 *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
 *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
 *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
 *   </li>
 * </ul>
 * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
 * </p>
 *
 * @param x the evaluation point.
 * @param epsilon maximum error allowed.
 * @param maxIterations maximum number of convergents
 * @return the value of the continued fraction evaluated at x.
 * @throws ConvergenceException if the algorithm fails to converge.
 * @throws MaxCountExceededException if maximal number of iterations is reached
 */
public double evaluate(double x, double epsilon, int maxIterations)
    throws ConvergenceException, MaxCountExceededException {
    final double small = 1e-50;
    double hPrev = getA(0, x);

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

    int n = 1;
    double dPrev = 0.0;
    double cPrev = hPrev;
    double hN = hPrev;

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

        double dN = a + b * dPrev;
        if (Precision.equals(dN, 0.0, small)) {
            dN = small;
        }
        double cN = a + b / cPrev;
        if (Precision.equals(cN, 0.0, small)) {
            cN = small;
        }

        dN = 1 / dN;
        final double deltaN = cN * dN;
        hN = hPrev * deltaN;

        if (Double.isInfinite(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                                           x);
        }
        if (Double.isNaN(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
                                           x);
        }

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

        dPrev = dN;
        cPrev = cN;
        hPrev = hN;
        n++;
    }

    if (n >= maxIterations) {
        throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
                                            maxIterations, x);
    }

    return hN;
}
 
Example 8
Source File: ContinuedFraction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Evaluates the continued fraction at the value x.
 * <p>
 * The implementation of this method is based on the modified Lentz algorithm as described
 * on page 18 ff. in:
 * <ul>
 *   <li>
 *   I. J. Thompson,  A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order."
 *   <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf">
 *   http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a>
 *   </li>
 * </ul>
 * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in
 * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>.
 * </p>
 *
 * @param x the evaluation point.
 * @param epsilon maximum error allowed.
 * @param maxIterations maximum number of convergents
 * @return the value of the continued fraction evaluated at x.
 * @throws ConvergenceException if the algorithm fails to converge.
 * @throws MaxCountExceededException if maximal number of iterations is reached
 */
public double evaluate(double x, double epsilon, int maxIterations)
    throws ConvergenceException, MaxCountExceededException {
    final double small = 1e-50;
    double hPrev = getA(0, x);

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

    int n = 1;
    double dPrev = 0.0;
    double cPrev = hPrev;
    double hN = hPrev;

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

        double dN = a + b * dPrev;
        if (Precision.equals(dN, 0.0, small)) {
            dN = small;
        }
        double cN = a + b / cPrev;
        if (Precision.equals(cN, 0.0, small)) {
            cN = small;
        }

        dN = 1 / dN;
        final double deltaN = cN * dN;
        hN = hPrev * deltaN;

        if (Double.isInfinite(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                                           x);
        }
        if (Double.isNaN(hN)) {
            throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE,
                                           x);
        }

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

        dPrev = dN;
        cPrev = cN;
        hPrev = hN;
        n++;
    }

    if (n >= maxIterations) {
        throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
                                            maxIterations, x);
    }

    return hN;
}