Java Code Examples for org.apache.commons.math.exception.util.LocalizedFormats#NON_CONVERGENT_CONTINUED_FRACTION

The following examples show how to use org.apache.commons.math.exception.util.LocalizedFormats#NON_CONVERGENT_CONTINUED_FRACTION . 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: MaxIterationsExceededExceptionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testComplexConstructor(){
    MaxIterationsExceededException ex =
        new MaxIterationsExceededException(1000000,
            LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION,
            1234567);
    assertNull(ex.getCause());
    assertNotNull(ex.getMessage());
    assertTrue(ex.getMessage().indexOf("1,000,000") < 0);
    assertTrue(ex.getMessage().indexOf("1,234,567") > 0);
    assertEquals(1000000, ex.getMaxIterations());
    assertFalse(ex.getMessage().equals(ex.getMessage(Locale.FRENCH)));
}
 
Example 2
Source File: Arja_0039_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 equations 14-17 of:
 * <ul>
 * <li>
 *   Eric W. Weisstein. "Continued Fraction." From MathWorld--A Wolfram Web
 *   Resource. <a target="_blank"
 *   href="http://mathworld.wolfram.com/ContinuedFraction.html">
 *   http://mathworld.wolfram.com/ContinuedFraction.html</a>
 * </li>
 * </ul>
 * The recurrence relationship defined in those equations can result in
 * very large intermediate results which can result in numerical overflow.
 * As a means to combat these overflow conditions, the intermediate results
 * are scaled whenever they threaten to become numerically unstable.</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 MathException if the algorithm fails to converge.
 */
public double evaluate(double x, double epsilon, int maxIterations)
    throws MathException
{
    double p0 = 1.0;
    double p1 = getA(0, x);
    double q0 = 0.0;
    double q1 = 1.0;
    double c = p1 / q1;
    int n = 0;
    double relativeError = Double.MAX_VALUE;
    while (n < maxIterations && relativeError > epsilon) {
        ++n;
        double a = getA(n, x);
        double b = getB(n, x);
        double p2 = a * p1 + b * p0;
        double q2 = a * q1 + b * q0;
        boolean infinite = false;
        if (Double.isInfinite(p2) || Double.isInfinite(q2)) {
            /*
             * Need to scale. Try successive powers of the larger of a or b
             * up to 5th power. Throw ConvergenceException if one or both
             * of p2, q2 still overflow.
             */
            double scaleFactor = 1d;
            double lastScaleFactor = 1d;
            final int maxPower = 5;
            final double scale = FastMath.max(a,b);
            if (scale <= 0) {  // Can't scale
                throw new ConvergenceException(
                        LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
                         x);
            }
            infinite = true;
            continue;
        }

        if (infinite) {
           // Scaling failed
           throw new ConvergenceException(
             LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE,
              x);
        }

        double r = p2 / q2;

        if (Double.isNaN(r)) {
        	return 0.0;
        }
        relativeError = FastMath.abs(r / c - 1.0);

        // prepare for next iteration
        c = p2 / q2;
        p0 = p1;
        p1 = p2;
        q0 = q1;
        q1 = q2;
    }

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

    return c;
}