Example usage for org.apache.commons.math.analysis BisectionSolver BisectionSolver

List of usage examples for org.apache.commons.math.analysis BisectionSolver BisectionSolver

Introduction

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

Prototype

public BisectionSolver(UnivariateRealFunction f) 

Source Link

Document

Construct a solver for the given function.

Usage

From source file:desmoj.core.dist.ContDistCustom.java

/**
 * Returns the next sample from this distribution. The value depends upon
 * the seed, the number of values taken from the stream by using this method
 * before and the alpha and beta parameters specified for this distribution.
 * /*from w w  w. j  ava  2  s  . c o m*/
 * @return Double : The next gamma distributed sample from this
 *         distribution.
 */
public Double sample() {

    double newSample; //

    incrementObservations(); // increase count of samples

    randomNumber = randomGenerator.nextDouble();
    if (isAntithetic()) {
        randomNumber = 1 - randomNumber;

    }

    Function functiontosolve = new Function() {

        public double value(double x) {

            return distFunction.value(x) - randomNumber;
            // Decrease input function by randomNumber to make the desired
            // sample be at a zero root.
        }

        public String getDescription() {
            return null;
        }
    };

    UnivariateRealSolver solver = new BisectionSolver(functiontosolve);
    try {
        newSample = solver.solve(lowerBound, upperBound); // Finding zero
        // root

    } catch (Exception e) {
        sendWarning("Failed to find sample, returning -1",
                "CustomContDist : " + getName() + " Method: sample()",
                "The solver could not deal with the distribution function specified.",
                "Make sure the CustomFunction this Distribution is using is a proper "
                        + "distribution function and the upper and lower bounds are set accordingly");
        newSample = -1;
    }
    if (this.currentlySendTraceNotes())
        this.traceLastSample(Double.toString(newSample));
    return newSample;

}