in commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java [98:139]
public double findRoot(DoubleUnaryOperator func,
double min,
double initial,
double max) {
if (min > max) {
throw new SolverException(SolverException.TOO_LARGE, min, max);
}
if (initial < min ||
initial > max) {
throw new SolverException(SolverException.OUT_OF_RANGE, initial, min, max);
}
// Return the initial guess if it is good enough.
final double yInitial = func.applyAsDouble(initial);
if (Math.abs(yInitial) <= functionValueAccuracy) {
return initial;
}
// Return the first endpoint if it is good enough.
final double yMin = func.applyAsDouble(min);
if (Math.abs(yMin) <= functionValueAccuracy) {
return min;
}
// Reduce interval if min and initial bracket the root.
if (Double.compare(yInitial * yMin, 0.0) < 0) {
return brent(func, min, initial, yMin, yInitial);
}
// Return the second endpoint if it is good enough.
final double yMax = func.applyAsDouble(max);
if (Math.abs(yMax) <= functionValueAccuracy) {
return max;
}
// Reduce interval if initial and max bracket the root.
if (Double.compare(yInitial * yMax, 0.0) < 0) {
return brent(func, initial, max, yInitial, yMax);
}
throw new SolverException(SolverException.BRACKETING, min, yMin, max, yMax);
}