Example usage for org.apache.commons.math.analysis.solvers BrentSolver BrentSolver

List of usage examples for org.apache.commons.math.analysis.solvers BrentSolver BrentSolver

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis.solvers BrentSolver BrentSolver.

Prototype

public BrentSolver(int maximumIterations, double absoluteAccuracy) 

Source Link

Document

Contstruct a solver with the given maximum iterations and absolute accuracy.

Usage

From source file:org.renjin.primitives.optimize.Roots.java

/**
 * Searches the interval from lower to upper for a root (i.e., zero) of the function f with
 * respect to its first argument.//from  w w w. j  a  v a 2  s .c  o m
 *
 * <p>
 * This internal primitive is used by the uniroot() function in the stats package.
 *
 * <p>
 * This implementation wraps the Commons Math {@link BrentSolver}; there are however
 *
 */
public static DoubleVector zeroin2(@Current Context context, @Current Environment rho, Function fn,
        double lower, double upper, double fLower, double fUpper, double tol, int maximumIterations) {

    BrentSolver solver = new BrentSolver(maximumIterations, tol);

    double root;
    int iterations;
    double estimatedPrecision = DoubleVector.EPSILON; // not exposed by commons math impl

    try {
        root = solver.solve(new UnivariateRealClosure(context, rho, fn), lower, upper);
        iterations = 1; // the Commons math impl doesn't expose this
    } catch (MaxIterationsExceededException e) {
        root = DoubleVector.NA;
        iterations = -1;
    } catch (FunctionEvaluationException e) {
        throw new EvalException(e);
    }

    return new DoubleArrayVector(root, iterations, estimatedPrecision);
}

From source file:org.renjin.stats.internals.optimize.Roots.java

/**
 * Searches the interval from lower to upper for a root (i.e., zero) of the function f with
 * respect to its first argument./*from ww w . j  ava2 s. c o m*/
 *
 * <p>
 * This internal primitive is used by the uniroot() function in the stats package.
 *
 * <p>
 * This implementation wraps the Commons Math {@link BrentSolver}; there are however
 *
 */
@Internal
public static DoubleVector zeroin2(@Current Context context, @Current Environment rho, Function fn,
        double lower, double upper, double fLower, double fUpper, double tol, int maximumIterations) {

    BrentSolver solver = new BrentSolver(maximumIterations, tol);

    double root;
    int iterations;
    double estimatedPrecision = DoubleVector.EPSILON; // not exposed by commons math impl

    try {
        root = solver.solve(maximumIterations, new UnivariateRealClosure(context, rho, fn), lower, upper);
        iterations = 1; // the Commons math impl doesn't expose this
    } catch (MaxIterationsExceededException e) {
        root = DoubleVector.NA;
        iterations = -1;
    } catch (FunctionEvaluationException e) {
        throw new EvalException(e);
    }

    return new DoubleArrayVector(root, iterations, estimatedPrecision);
}

From source file:r.base.optimize.Roots.java

/**
 * Searches the interval from lower to upper for a root (i.e., zero) of the function f with
 * respect to its first argument.// w w  w  . j  a va 2 s  . c  om
 *
 * <p>
 * This internal primitive is used by the uniroot() function in the stats package.
 *
 * <p>
 * This implementation wraps the Commons Math {@link BrentSolver}; there are however
 *
 */
public static DoubleVector zeroin2(@Current Context context, @Current Environment rho, Function fn,
        double lower, double upper, double fLower, double fUpper, double tol, int maximumIterations) {

    BrentSolver solver = new BrentSolver(maximumIterations, tol);

    double root;
    int iterations;
    double estimatedPrecision = DoubleVector.EPSILON; // not exposed by commons math impl

    try {
        root = solver.solve(new UnivariateRealClosure(context, rho, fn), lower, upper);
        iterations = 1; // the Commons math impl doesn't expose this
    } catch (MaxIterationsExceededException e) {
        root = DoubleVector.NA;
        iterations = -1;
    } catch (FunctionEvaluationException e) {
        throw new EvalException(e);
    }

    return new DoubleVector(root, iterations, estimatedPrecision);
}