MullerSolver.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.analysis.solvers;

  22. import org.hipparchus.exception.MathIllegalArgumentException;
  23. import org.hipparchus.exception.MathIllegalStateException;
  24. import org.hipparchus.util.FastMath;

  25. /**
  26.  * This class implements the <a href="http://mathworld.wolfram.com/MullersMethod.html">
  27.  * Muller's Method</a> for root finding of real univariate functions. For
  28.  * reference, see <b>Elementary Numerical Analysis</b>, ISBN 0070124477,
  29.  * chapter 3.
  30.  * <p>
  31.  * Muller's method applies to both real and complex functions, but here we
  32.  * restrict ourselves to real functions.
  33.  * This class differs from {@link MullerSolver} in the way it avoids complex
  34.  * operations.</p><p>
  35.  * Muller's original method would have function evaluation at complex point.
  36.  * Since our f(x) is real, we have to find ways to avoid that. Bracketing
  37.  * condition is one way to go: by requiring bracketing in every iteration,
  38.  * the newly computed approximation is guaranteed to be real.</p>
  39.  * <p>
  40.  * Normally Muller's method converges quadratically in the vicinity of a
  41.  * zero, however it may be very slow in regions far away from zeros. For
  42.  * example, f(x) = exp(x) - 1, min = -50, max = 100. In such case we use
  43.  * bisection as a safety backup if it performs very poorly.</p>
  44.  * <p>
  45.  * The formulas here use divided differences directly.</p>
  46.  *
  47.  * @see MullerSolver2
  48.  */
  49. public class MullerSolver extends AbstractUnivariateSolver {

  50.     /** Default absolute accuracy. */
  51.     private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6;

  52.     /**
  53.      * Construct a solver with default accuracy (1e-6).
  54.      */
  55.     public MullerSolver() {
  56.         this(DEFAULT_ABSOLUTE_ACCURACY);
  57.     }
  58.     /**
  59.      * Construct a solver.
  60.      *
  61.      * @param absoluteAccuracy Absolute accuracy.
  62.      */
  63.     public MullerSolver(double absoluteAccuracy) {
  64.         super(absoluteAccuracy);
  65.     }
  66.     /**
  67.      * Construct a solver.
  68.      *
  69.      * @param relativeAccuracy Relative accuracy.
  70.      * @param absoluteAccuracy Absolute accuracy.
  71.      */
  72.     public MullerSolver(double relativeAccuracy,
  73.                         double absoluteAccuracy) {
  74.         super(relativeAccuracy, absoluteAccuracy);
  75.     }

  76.     /**
  77.      * {@inheritDoc}
  78.      */
  79.     @Override
  80.     protected double doSolve()
  81.         throws MathIllegalArgumentException, MathIllegalStateException {
  82.         final double min = getMin();
  83.         final double max = getMax();
  84.         final double initial = getStartValue();

  85.         final double functionValueAccuracy = getFunctionValueAccuracy();

  86.         verifySequence(min, initial, max);

  87.         // check for zeros before verifying bracketing
  88.         final double fMin = computeObjectiveValue(min);
  89.         if (FastMath.abs(fMin) < functionValueAccuracy) {
  90.             return min;
  91.         }
  92.         final double fMax = computeObjectiveValue(max);
  93.         if (FastMath.abs(fMax) < functionValueAccuracy) {
  94.             return max;
  95.         }
  96.         final double fInitial = computeObjectiveValue(initial);
  97.         if (FastMath.abs(fInitial) <  functionValueAccuracy) {
  98.             return initial;
  99.         }

  100.         verifyBracketing(min, max);

  101.         if (isBracketing(min, initial)) {
  102.             return solve(min, initial, fMin, fInitial);
  103.         } else {
  104.             return solve(initial, max, fInitial, fMax);
  105.         }
  106.     }

  107.     /**
  108.      * Find a real root in the given interval.
  109.      *
  110.      * @param min Lower bound for the interval.
  111.      * @param max Upper bound for the interval.
  112.      * @param fMin function value at the lower bound.
  113.      * @param fMax function value at the upper bound.
  114.      * @return the point at which the function value is zero.
  115.      * @throws MathIllegalStateException if the allowed number of calls to
  116.      * the function to be solved has been exhausted.
  117.      */
  118.     private double solve(double min, double max,
  119.                          double fMin, double fMax)
  120.         throws MathIllegalStateException {
  121.         final double relativeAccuracy = getRelativeAccuracy();
  122.         final double absoluteAccuracy = getAbsoluteAccuracy();
  123.         final double functionValueAccuracy = getFunctionValueAccuracy();

  124.         // [x0, x2] is the bracketing interval in each iteration
  125.         // x1 is the last approximation and an interpolation point in (x0, x2)
  126.         // x is the new root approximation and new x1 for next round
  127.         // d01, d12, d012 are divided differences

  128.         double x0 = min;
  129.         double y0 = fMin;
  130.         double x2 = max;
  131.         double y2 = fMax;
  132.         double x1 = 0.5 * (x0 + x2);
  133.         double y1 = computeObjectiveValue(x1);

  134.         double oldx = Double.POSITIVE_INFINITY;
  135.         while (true) {
  136.             // Muller's method employs quadratic interpolation through
  137.             // x0, x1, x2 and x is the zero of the interpolating parabola.
  138.             // Due to bracketing condition, this parabola must have two
  139.             // real roots and we choose one in [x0, x2] to be x.
  140.             final double d01 = (y1 - y0) / (x1 - x0);
  141.             final double d12 = (y2 - y1) / (x2 - x1);
  142.             final double d012 = (d12 - d01) / (x2 - x0);
  143.             final double c1 = d01 + (x1 - x0) * d012;
  144.             final double delta = c1 * c1 - 4 * y1 * d012;
  145.             final double xplus = x1 + (-2.0 * y1) / (c1 + FastMath.sqrt(delta));
  146.             final double xminus = x1 + (-2.0 * y1) / (c1 - FastMath.sqrt(delta));
  147.             // xplus and xminus are two roots of parabola and at least
  148.             // one of them should lie in (x0, x2)
  149.             final double x = isSequence(x0, xplus, x2) ? xplus : xminus;
  150.             final double y = computeObjectiveValue(x);

  151.             // check for convergence
  152.             final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy);
  153.             if (FastMath.abs(x - oldx) <= tolerance ||
  154.                 FastMath.abs(y) <= functionValueAccuracy) {
  155.                 return x;
  156.             }

  157.             // Bisect if convergence is too slow. Bisection would waste
  158.             // our calculation of x, hopefully it won't happen often.
  159.             // the real number equality test x == x1 is intentional and
  160.             // completes the proximity tests above it
  161.             boolean bisect = (x < x1 && (x1 - x0) > 0.95 * (x2 - x0)) ||
  162.                              (x > x1 && (x2 - x1) > 0.95 * (x2 - x0)) ||
  163.                              (x == x1);
  164.             // prepare the new bracketing interval for next iteration
  165.             if (!bisect) {
  166.                 x0 = x < x1 ? x0 : x1;
  167.                 y0 = x < x1 ? y0 : y1;
  168.                 x2 = x > x1 ? x2 : x1;
  169.                 y2 = x > x1 ? y2 : y1;
  170.                 x1 = x; y1 = y;
  171.                 oldx = x;
  172.             } else {
  173.                 double xm = 0.5 * (x0 + x2);
  174.                 double ym = computeObjectiveValue(xm);
  175.                 if (FastMath.signum(y0) + FastMath.signum(ym) == 0.0) {
  176.                     x2 = xm; y2 = ym;
  177.                 } else {
  178.                     x0 = xm; y0 = ym;
  179.                 }
  180.                 x1 = 0.5 * (x0 + x2);
  181.                 y1 = computeObjectiveValue(x1);
  182.                 oldx = Double.POSITIVE_INFINITY;
  183.             }
  184.         }
  185.     }
  186. }