org.apache.commons.math.MaxIterationsExceededException Java Examples

The following examples show how to use org.apache.commons.math.MaxIterationsExceededException. 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: RiddersSolver.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Find a root in the given interval with initial value.
 * <p>
 * Requires bracketing condition.</p>
 * 
 * @param min the lower bound for the interval
 * @param max the upper bound for the interval
 * @param initial the start value to use
 * @return the point at which the function value is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function
 * @throws IllegalArgumentException if any parameters are invalid
 */
public double solve(double min, double max, double initial) throws
    MaxIterationsExceededException, FunctionEvaluationException {

    // check for zeros before verifying bracketing
    if (f.value(min) == 0.0) { return min; }
    if (f.value(max) == 0.0) { return max; }
    if (f.value(initial) == 0.0) { return initial; }

    verifyBracketing(min, max, f);
    verifySequence(min, initial, max);
    if (isBracketing(min, initial, f)) {
        return solve(min, initial);
    } else {
        return solve(initial, max);
    }
}
 
Example #2
Source File: NPEfix_00155_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #3
Source File: Math_65_AbstractLeastSquaresOptimizer_s.java    From coming with MIT License 5 votes vote down vote up
/** Increment the iterations counter by 1.
 * @exception OptimizationException if the maximal number
 * of iterations is exceeded
 */
protected void incrementIterationsCounter()
    throws OptimizationException {
    if (++iterations > maxIterations) {
        throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
    }
}
 
Example #4
Source File: 1_BisectionSolver.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
    public double solve(final UnivariateRealFunction f, double min, double max, double initial)
        throws MaxIterationsExceededException, FunctionEvaluationException {
// start of generated patch
return solve(f,min,max);
// end of generated patch
/* start of original code
        return solve(min, max);
 end of original code*/
    }
 
Example #5
Source File: Cardumen_00187_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Find a zero in the given interval.
 * <p>
 * Requires that the values of the function at the endpoints have opposite
 * signs. An <code>IllegalArgumentException</code> is thrown if this is not
 * the case.</p>
 * 
 * @param min the lower bound for the interval.
 * @param max the upper bound for the interval.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function 
 * @throws IllegalArgumentException if min is not less than max or the
 * signs of the values of the function at the endpoints are not opposites
 */
public double solve(double min, double max) throws MaxIterationsExceededException, 
    FunctionEvaluationException {
    
    clearResult();
    verifyInterval(min, max);
    
    double ret = Double.NaN;
    
    double yMin = f.value(min);
    double yMax = f.value(max);
    
    // Verify bracketing
    double sign = yMin * yMax;
    if (sign >= 0) {
        // check if either value is close to a zero
            // neither value is close to zero and min and max do not bracket root.
            throw new IllegalArgumentException
            ("Function values at endpoints do not have different signs." +
                    "  Endpoints: [" + min + "," + max + "]" + 
                    "  Values: [" + yMin + "," + yMax + "]");
    } else {
        // solve using only the first endpoint as initial guess
        ret = solve(min, yMin, max, yMax, min, yMin);
        // either min or max is a root
    }

    return ret;
}
 
Example #6
Source File: Cardumen_0068_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Find a zero in the given interval.
 * <p>
 * Requires that the values of the function at the endpoints have opposite
 * signs. An <code>IllegalArgumentException</code> is thrown if this is not
 * the case.</p>
 * 
 * @param min the lower bound for the interval.
 * @param max the upper bound for the interval.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function 
 * @throws IllegalArgumentException if min is not less than max or the
 * signs of the values of the function at the endpoints are not opposites
 */
public double solve(double min, double max) throws MaxIterationsExceededException, 
    FunctionEvaluationException {
    
    clearResult();
    verifyInterval(min, max);
    
    double ret = Double.NaN;
    
    double yMin = f.value(min);
    double yMax = f.value(max);
    
    // Verify bracketing
    double sign = yMin * yMax;
    if (sign >= 0) {
        // check if either value is close to a zero
            // neither value is close to zero and min and max do not bracket root.
            throw new IllegalArgumentException
            ("Function values at endpoints do not have different signs." +
                    "  Endpoints: [" + min + "," + max + "]" + 
                    "  Values: [" + yMin + "," + yMax + "]");
    } else {
        // solve using only the first endpoint as initial guess
        ret = solve(min, yMin, max, yMax, min, yMin);
        // either min or max is a root
    }

    return ret;
}
 
Example #7
Source File: NPEfix_00145_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       if (f == null) {
           return min;
       }
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #8
Source File: Cardumen_00280_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Find a zero in the given interval.
 * <p>
 * Requires that the values of the function at the endpoints have opposite
 * signs. An <code>IllegalArgumentException</code> is thrown if this is not
 * the case.</p>
 * 
 * @param min the lower bound for the interval.
 * @param max the upper bound for the interval.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function 
 * @throws IllegalArgumentException if min is not less than max or the
 * signs of the values of the function at the endpoints are not opposites
 */
public double solve(double min, double max) throws MaxIterationsExceededException, 
    FunctionEvaluationException {
    
    clearResult();
    verifyInterval(min, max);
    
    double ret = Double.NaN;
    
    double yMin = f.value(min);
    double yMax = f.value(max);
    
    // Verify bracketing
    double sign = yMin * yMax;
    if (sign >= 0) {
        // check if either value is close to a zero
            // neither value is close to zero and min and max do not bracket root.
            throw new IllegalArgumentException
            ("Function values at endpoints do not have different signs." +
                    "  Endpoints: [" + min + "," + max + "]" + 
                    "  Values: [" + yMin + "," + yMax + "]");
    } else {
        // solve using only the first endpoint as initial guess
        ret = solve(min, yMin, max, yMax, min, yMin);
        // either min or max is a root
    }

    return ret;
}
 
Example #9
Source File: NormalDistributionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * For this disbution, X, this method returns P(X &lt; <code>x</code>).
 * @param x the value at which the CDF is evaluated.
 * @return CDF evaluted at <code>x</code>. 
 * @throws MathException if the algorithm fails to converge; unless
 * x is more than 20 standard deviations from the mean, in which case the
 * convergence exception is caught and 0 or 1 is returned.
 */
public double cumulativeProbability(double x) throws MathException {
    try {
        return 0.5 * (1.0 + Erf.erf((x - mean) /
                (standardDeviation * Math.sqrt(2.0))));
    } catch (MaxIterationsExceededException ex) {
        if (x < (mean - 20 * standardDeviation)) { // JDK 1.5 blows at 38
            return 0.0d;
        } else if (x > (mean + 20 * standardDeviation)) {
            return 1.0d;
        } else {
            throw ex;
        }
    }
}
 
Example #10
Source File: NPEfix_00154_t.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       if (f == null) {
           return functionValue;
       }
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #11
Source File: jMutRepair_007_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns the regularized gamma function P(a, x).
 * 
 * The implementation of this method is based on:
 * <ul>
 * <li>
 * <a href="http://mathworld.wolfram.com/RegularizedGammaFunction.html">
 * Regularized Gamma Function</a>, equation (1).</li>
 * <li>
 * <a href="http://mathworld.wolfram.com/IncompleteGammaFunction.html">
 * Incomplete Gamma Function</a>, equation (4).</li>
 * <li>
 * <a href="http://mathworld.wolfram.com/ConfluentHypergeometricFunctionoftheFirstKind.html">
 * Confluent Hypergeometric Function of the First Kind</a>, equation (1).
 * </li>
 * </ul>
 * 
 * @param a the a parameter.
 * @param x the value.
 * @param epsilon When the absolute value of the nth item in the
 *                series is less than epsilon the approximation ceases
 *                to calculate further elements in the series.
 * @param maxIterations Maximum number of "iterations" to complete. 
 * @return the regularized gamma function P(a, x)
 * @throws MathException if the algorithm fails to converge.
 */
public static double regularizedGammaP(double a, 
                                       double x, 
                                       double epsilon, 
                                       int maxIterations) 
    throws MathException
{
    double ret;

    if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) {
        ret = Double.NaN;
    } else if (x == 0.0) {
        ret = 0.0;
    } else if (a >= 1.0 && x >= a) {
        // use regularizedGammaQ because it should converge faster in this
        // case.
        ret = 1.0 - regularizedGammaQ(a, x, epsilon, maxIterations);
    } else {
        // calculate series
        double n = 0.0; // current element index
        double an = 1.0 / a; // n-th element in the series
        double sum = an; // partial sum
        while (Math.abs(an) > epsilon && n < maxIterations) {
            // compute next element in the series
            n = n + 1.0;
            an = an * (x / (a + n));

            // update partial sum
            sum = sum + an;
        }
        if (n >= maxIterations) {
            throw new MaxIterationsExceededException(maxIterations);
        } else {
            ret = Math.exp(-x + (a * Math.log(x)) - logGamma(a)) * sum;
        }
    }

    return ret;
}
 
Example #12
Source File: NPEfix_00149_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #13
Source File: Cardumen_00103_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns the regularized gamma function P(a, x).
 * 
 * The implementation of this method is based on:
 * <ul>
 * <li>
 * <a href="http://mathworld.wolfram.com/RegularizedGammaFunction.html">
 * Regularized Gamma Function</a>, equation (1).</li>
 * <li>
 * <a href="http://mathworld.wolfram.com/IncompleteGammaFunction.html">
 * Incomplete Gamma Function</a>, equation (4).</li>
 * <li>
 * <a href="http://mathworld.wolfram.com/ConfluentHypergeometricFunctionoftheFirstKind.html">
 * Confluent Hypergeometric Function of the First Kind</a>, equation (1).
 * </li>
 * </ul>
 * 
 * @param a the a parameter.
 * @param x the value.
 * @param epsilon When the absolute value of the nth item in the
 *                series is less than epsilon the approximation ceases
 *                to calculate further elements in the series.
 * @param maxIterations Maximum number of "iterations" to complete. 
 * @return the regularized gamma function P(a, x)
 * @throws MathException if the algorithm fails to converge.
 */
public static double regularizedGammaP(double a, 
                                       double x, 
                                       double epsilon, 
                                       int maxIterations) 
    throws MathException
{
    double ret;

    if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) {
        ret = Double.NaN;
    } else if (x == 0.0) {
        ret = 0.0;
    } else if (a >= 1.0 && x > a) {
        // use regularizedGammaQ because it should converge faster in this
        // case.
        ret = 1.0 - regularizedGammaQ(a, x, epsilon, maxIterations);
    } else {
        // calculate series
        double n = 0.0; // current element index
        double an = 1.0 / a; // n-th element in the series
        double sum = an; // partial sum
        while (Math.abs(an) > epsilon && n < maxIterations) {
            // compute next element in the series
            n = n + 1.0;
            an = an * (x / (a + n));

            // update partial sum
            sum = sum + an;
        }
        if (n >= maxIterations) {
            throw new MaxIterationsExceededException(maxIterations);
        } else {
            ret = Math.exp(-x + (a * Math.log(x)) - logGamma(a)) * sum;
        }
    }

    return ret;
}
 
Example #14
Source File: 1_BisectionSolver.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #15
Source File: NPEfix_00144_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #16
Source File: Cardumen_00187_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Find a zero in the given interval.
 * <p>
 * Requires that the values of the function at the endpoints have opposite
 * signs. An <code>IllegalArgumentException</code> is thrown if this is not
 * the case.</p>
 * 
 * @param min the lower bound for the interval.
 * @param max the upper bound for the interval.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function 
 * @throws IllegalArgumentException if min is not less than max or the
 * signs of the values of the function at the endpoints are not opposites
 */
public double solve(double min, double max) throws MaxIterationsExceededException, 
    FunctionEvaluationException {
    
    clearResult();
    verifyInterval(min, max);
    
    double ret = Double.NaN;
    
    double yMin = f.value(min);
    double yMax = f.value(max);
    
    // Verify bracketing
    double sign = ((1.5 * yMin) * yMax) - (java.lang.Math.abs((min * yMax)));
    if (sign >= 0) {
        // check if either value is close to a zero
            // neither value is close to zero and min and max do not bracket root.
            throw new IllegalArgumentException
            ("Function values at endpoints do not have different signs." +
                    "  Endpoints: [" + min + "," + max + "]" + 
                    "  Values: [" + yMin + "," + yMax + "]");
    } else {
        // solve using only the first endpoint as initial guess
        ret = solve(min, yMin, max, yMax, min, yMin);
        // either min or max is a root
    }

    return ret;
}
 
Example #17
Source File: Cardumen_00225_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #18
Source File: NPEfix_00151_s.java    From coming with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #19
Source File: 1_BisectionSolver.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifyInterval(min,max);
    double m;
    double fm;
    double fmin;

    int i = 0;
    while (i < maximalIterationCount) {
        m = UnivariateRealSolverUtils.midpoint(min, max);
       fmin = f.value(min);
       fm = f.value(m);

        if (fm * fmin > 0.0) {
            // max and m bracket the root.
            min = m;
        } else {
            // min and m bracket the root.
            max = m;
        }

        if (Math.abs(max - min) <= absoluteAccuracy) {
            m = UnivariateRealSolverUtils.midpoint(min, max);
            setResult(m, i);
            return m;
        }
        ++i;
    }

    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #20
Source File: SimpsonIntegrator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Integrate the function in the given interval.
 * 
 * @param min the lower bound for the interval
 * @param max the upper bound for the interval
 * @return the value of integral
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * or the integrator detects convergence problems otherwise
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function
 * @throws IllegalArgumentException if any parameters are invalid
 */
public double integrate(double min, double max) throws MaxIterationsExceededException,
    FunctionEvaluationException, IllegalArgumentException {
    
    int i = 1;
    double s, olds, t, oldt;
    
    clearResult();
    verifyInterval(min, max);
    verifyIterationCount();

    TrapezoidIntegrator qtrap = new TrapezoidIntegrator(this.f);
    if (minimalIterationCount == 1) {
        s = (4 * qtrap.stage(min, max, 1) - qtrap.stage(min, max, 0)) / 3.0;
        setResult(s, 1);
        return result;
    }
    // Simpson's rule requires at least two trapezoid stages.
    olds = 0;
    oldt = qtrap.stage(min, max, 0);
    while (i <= maximalIterationCount) {
        t = qtrap.stage(min, max, i);
        s = (4 * t - oldt) / 3.0;
        if (i >= minimalIterationCount) {
            if (Math.abs(s - olds) <= Math.abs(relativeAccuracy * olds)) {
                setResult(s, i);
                return result;
            }
        }
        olds = s;
        oldt = t;
        i++;
    }
    throw new MaxIterationsExceededException(maximalIterationCount);
}
 
Example #21
Source File: 1_BrentOptimizer.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public double optimize(final UnivariateRealFunction f, final GoalType goalType,
                       final double min, final double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    clearResult();
    return localMin(f, goalType, min, max, relativeAccuracy, absoluteAccuracy);
}
 
Example #22
Source File: 1_BrentOptimizer.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public double optimize(final UnivariateRealFunction f, final GoalType goalType, final double min, final double max, final double startValue) throws MaxIterationsExceededException, FunctionEvaluationException {
// start of generated patch
if(f.value(max)==0.0){
return max;
}
clearResult();
return localMin(getGoalType()==GoalType.MINIMIZE,f,goalType,min,startValue,max,getRelativeAccuracy(),getAbsoluteAccuracy());
// end of generated patch
/* start of original code
        clearResult();
        return localMin(getGoalType() == GoalType.MINIMIZE,
                        f, goalType, min, startValue, max,
                        getRelativeAccuracy(), getAbsoluteAccuracy());
 end of original code*/
    }
 
Example #23
Source File: NPEfix_00142_t.java    From coming with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
public double solve(double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    return solve(f, min, max);
}
 
Example #24
Source File: Nopol2017_0079_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Find a zero in the given interval with an initial guess.
 * <p>Throws <code>IllegalArgumentException</code> if the values of the
 * function at the three points have the same sign (note that it is
 * allowed to have endpoints with the same sign if the initial point has
 * opposite sign function-wise).</p>
 *
 * @param f function to solve.
 * @param min the lower bound for the interval.
 * @param max the upper bound for the interval.
 * @param initial the start value to use (must be set to min if no
 * initial point is known).
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException the maximum iteration count
 * is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating
 *  the function
 * @throws IllegalArgumentException if initial is not between min and max
 * (even if it <em>is</em> a root)
 */
public double solve(final UnivariateRealFunction f,
                    final double min, final double max, final double initial)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    clearResult();
    verifySequence(min, initial, max);

    // return the initial guess if it is good enough
    double yInitial = f.value(initial);
    if (Math.abs(yInitial) <= functionValueAccuracy) {
        setResult(initial, 0);
        return result;
    }

    // return the first endpoint if it is good enough
    double yMin = f.value(min);
    if (Math.abs(yMin) <= functionValueAccuracy) {
        setResult(yMin, 0);
        return result;
    }

    // reduce interval if min and initial bracket the root
    if (yInitial * yMin < 0) {
        return solve(f, min, yMin, initial, yInitial, min, yMin);
    }

    // return the second endpoint if it is good enough
    double yMax = f.value(max);
    if (Math.abs(yMax) <= functionValueAccuracy) {
        setResult(yMax, 0);
        return result;
    }

    // reduce interval if initial and max bracket the root
    if (yInitial * yMax < 0) {
        return solve(f, initial, yInitial, max, yMax, initial, yInitial);
    }

    // full Brent algorithm starting with provided initial guess
    return solve(f, min, yMin, max, yMax, initial, yInitial);

}
 
Example #25
Source File: NPEfix_00148_s.java    From coming with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
public double solve(final UnivariateRealFunction f, double min, double max, double initial)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    return solve(min, max);
}
 
Example #26
Source File: Cardumen_00175_t.java    From coming with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
public double solve(double min, double max, double initial)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    return solve(f, min, max);
}
 
Example #27
Source File: NPEfix_00154_s.java    From coming with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
public double solve(double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    return solve(f, min, max);
}
 
Example #28
Source File: NPEfix_00150_t.java    From coming with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
public double solve(double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    return solve(f, min, max);
}
 
Example #29
Source File: Cardumen_00175_s.java    From coming with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Deprecated
public double solve(double min, double max)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    return solve(f, min, max);
}
 
Example #30
Source File: MullerSolver.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Find a real root in the given interval.
 * <p>
 * solve2() differs from solve() in the way it avoids complex operations.
 * Except for the initial [min, max], solve2() does not require bracketing
 * condition, e.g. f(x0), f(x1), f(x2) can have the same sign. If complex
 * number arises in the computation, we simply use its modulus as real
 * approximation.</p>
 * <p>
 * Because the interval may not be bracketing, bisection alternative is
 * not applicable here. However in practice our treatment usually works
 * well, especially near real zeros where the imaginary part of complex
 * approximation is often negligible.</p>
 * <p>
 * The formulas here do not use divided differences directly.</p>
 * 
 * @param min the lower bound for the interval
 * @param max the upper bound for the interval
 * @return the point at which the function value is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * or the solver detects convergence problems otherwise
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function 
 * @throws IllegalArgumentException if any parameters are invalid
 */
public double solve2(double min, double max) throws MaxIterationsExceededException, 
    FunctionEvaluationException {

    // x2 is the last root approximation
    // x is the new approximation and new x2 for next round
    // x0 < x1 < x2 does not hold here
    double x0, x1, x2, x, oldx, y0, y1, y2, y;
    double q, A, B, C, delta, denominator, tolerance;

    x0 = min; y0 = f.value(x0);
    x1 = max; y1 = f.value(x1);
    x2 = 0.5 * (x0 + x1); y2 = f.value(x2);

    // check for zeros before verifying bracketing
    if (y0 == 0.0) { return min; }
    if (y1 == 0.0) { return max; }
    verifyBracketing(min, max, f);

    int i = 1;
    oldx = Double.POSITIVE_INFINITY;
    while (i <= maximalIterationCount) {
        // quadratic interpolation through x0, x1, x2
        q = (x2 - x1) / (x1 - x0);
        A = q * (y2 - (1 + q) * y1 + q * y0);
        B = (2*q + 1) * y2 - (1 + q) * (1 + q) * y1 + q * q * y0;
        C = (1 + q) * y2;
        delta = B * B - 4 * A * C;
        if (delta >= 0.0) {
            // choose a denominator larger in magnitude
            double dplus = B + Math.sqrt(delta);
            double dminus = B - Math.sqrt(delta);
            denominator = Math.abs(dplus) > Math.abs(dminus) ? dplus : dminus;
        } else {
            // take the modulus of (B +/- Math.sqrt(delta))
            denominator = Math.sqrt(B * B - delta);
        }
        if (denominator != 0) {
            x = x2 - 2.0 * C * (x2 - x1) / denominator;
            // perturb x if it exactly coincides with x1 or x2
            // the equality tests here are intentional
            while (x == x1 || x == x2) {
                x += absoluteAccuracy;
            }
        } else {
            // extremely rare case, get a random number to skip it
            x = min + Math.random() * (max - min);
            oldx = Double.POSITIVE_INFINITY;
        }
        y = f.value(x);

        // check for convergence
        tolerance = Math.max(relativeAccuracy * Math.abs(x), absoluteAccuracy);
        if (Math.abs(x - oldx) <= tolerance) {
            setResult(x, i);
            return result;
        }
        if (Math.abs(y) <= functionValueAccuracy) {
            setResult(x, i);
            return result;
        }

        // prepare the next iteration
        x0 = x1; y0 = y1;
        x1 = x2; y1 = y2;
        x2 = x; y2 = y;
        oldx = x;
        i++;
    }
    throw new MaxIterationsExceededException(maximalIterationCount);
}