Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-95

[math] BrentSolver and SecantSolver request boundary values from function twice

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0
    • None
    • None
    • None
    • Operating System: Windows XP
      Platform: PC

    • 35042

    Description

      I'm using the BrentSolver class and I noticed that, when I call the solve()
      method, it requests the boundary values of the initial interval twice from my
      function. The function is quite expensive so it would be good to avoid these
      two duplicate computations.

      I had a look at the source code (see below) and I can see that
      the 'verifyBracketing' check requests the boundary values from the function and
      then the 'solve' method itself also requests those values. It looks like the
      SecantSolver will exhibit this behaviour too. Would it be possible to change
      the code so that the values are requested just once?

      BrentSolver.class

      public double solve(double min, double max) throws ConvergenceException,
      FunctionEvaluationException {

      clearResult();
      verifyBracketing(min, max, f);

      // Index 0 is the old approximation for the root.
      // Index 1 is the last calculated approximation for the root.
      // Index 2 is a bracket for the root with respect to x1.
      double x0 = min;
      double x1 = max;
      double y0;
      double y1;
      y0 = f.value(x0); // ******* REQUESTS BOUNDARY (MIN) VALUE HERE ****
      y1 = f.value(x1); // ******* REQUESTS BOUNDARY (MAX) VALUE HERE ****

      UnivariateRealSolverImpl.class:

      protected void verifyBracketing(double lower, double upper,
      UnivariateRealFunction f) throws FunctionEvaluationException {
      verifyInterval(lower, upper);
      if (!isBracketing(lower, upper, f))

      { throw new IllegalArgumentException ("Function values at endpoints do not have different signs." + " Endpoints: [" + lower + "," + upper + "]" + " Values: [" + f.value(lower) + "," + f.value(upper) + "]"); }

      }

      protected boolean isBracketing(double lower, double upper,
      UnivariateRealFunction f) throws FunctionEvaluationException

      { // ******* REQUESTS BOTH BOUNDARY VALUES HERE **** return (f.value(lower) * f.value(upper) < 0); }

      Attachments

        Activity

          People

            Unassigned Unassigned
            paulf Paul Field
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: