Class BrentSolver


  • public class BrentSolver
    extends java.lang.Object
    This class implements the Brent algorithm for finding zeros of real univariate functions. The function should be continuous but not necessarily smooth. The solve method returns a zero x of the function f in the given interval [a, b] to within a tolerance 2 eps abs(x) + t where eps is the relative accuracy and t is the absolute accuracy.

    The given interval must bracket the root.

    The reference implementation is given in chapter 4 of

    Algorithms for Minimization Without Derivatives, Richard P. Brent, Dover, 2002
    • Constructor Summary

      Constructors 
      Constructor Description
      BrentSolver​(double relativeAccuracy, double absoluteAccuracy, double functionValueAccuracy)
      Construct a solver.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private double brent​(java.util.function.DoubleUnaryOperator func, double lo, double hi, double fLo, double fHi)
      Search for a zero inside the provided interval.
      private static boolean equalsZero​(double value)
      Return true if the value is within 1 ULP of zero.
      double findRoot​(java.util.function.DoubleUnaryOperator func, double min, double max)
      Search the function's zero within the given interval.
      double findRoot​(java.util.function.DoubleUnaryOperator func, double min, double initial, double max)
      Search the function's zero within the given interval, starting from the given estimate.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • relativeAccuracy

        private final double relativeAccuracy
        Relative accuracy.
      • absoluteAccuracy

        private final double absoluteAccuracy
        Absolute accuracy.
      • functionValueAccuracy

        private final double functionValueAccuracy
        Function accuracy.
    • Constructor Detail

      • BrentSolver

        public BrentSolver​(double relativeAccuracy,
                           double absoluteAccuracy,
                           double functionValueAccuracy)
        Construct a solver.
        Parameters:
        relativeAccuracy - Relative accuracy.
        absoluteAccuracy - Absolute accuracy.
        functionValueAccuracy - Function value accuracy.
    • Method Detail

      • findRoot

        public double findRoot​(java.util.function.DoubleUnaryOperator func,
                               double min,
                               double max)
        Search the function's zero within the given interval.
        Parameters:
        func - Function to solve.
        min - Lower bound.
        max - Upper bound.
        Returns:
        the root.
        Throws:
        java.lang.IllegalArgumentException - if min > max.
        java.lang.IllegalArgumentException - if the given interval does not bracket the root.
      • findRoot

        public double findRoot​(java.util.function.DoubleUnaryOperator func,
                               double min,
                               double initial,
                               double max)
        Search the function's zero within the given interval, starting from the given estimate.
        Parameters:
        func - Function to solve.
        min - Lower bound.
        initial - Initial guess.
        max - Upper bound.
        Returns:
        the root.
        Throws:
        java.lang.IllegalArgumentException - if min > max or initial is not in the [min, max] interval.
        java.lang.IllegalArgumentException - if the given interval does not bracket the root.
      • brent

        private double brent​(java.util.function.DoubleUnaryOperator func,
                             double lo,
                             double hi,
                             double fLo,
                             double fHi)
        Search for a zero inside the provided interval. This implementation is based on the algorithm described at page 58 of the book
        Algorithms for Minimization Without Derivatives, Richard P. Brent, Dover 0-486-41998-3
        Parameters:
        func - Function to solve.
        lo - Lower bound of the search interval.
        hi - Higher bound of the search interval.
        fLo - Function value at the lower bound of the search interval.
        fHi - Function value at the higher bound of the search interval.
        Returns:
        the value where the function is zero.
      • equalsZero

        private static boolean equalsZero​(double value)
        Return true if the value is within 1 ULP of zero.
        Parameters:
        value - Value
        Returns:
        true if zero within a 1 ULP tolerance