BisectionSolver.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.MathIllegalStateException;
  23. import org.hipparchus.util.FastMath;

  24. /**
  25.  * Implements the <a href="http://mathworld.wolfram.com/Bisection.html">
  26.  * bisection algorithm</a> for finding zeros of univariate real functions.
  27.  * <p>
  28.  * The function should be continuous but not necessarily smooth.</p>
  29.  *
  30.  */
  31. public class BisectionSolver extends AbstractUnivariateSolver {
  32.     /** Default absolute accuracy. */
  33.     private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6;

  34.     /**
  35.      * Construct a solver with default accuracy (1e-6).
  36.      */
  37.     public BisectionSolver() {
  38.         this(DEFAULT_ABSOLUTE_ACCURACY);
  39.     }
  40.     /**
  41.      * Construct a solver.
  42.      *
  43.      * @param absoluteAccuracy Absolute accuracy.
  44.      */
  45.     public BisectionSolver(double absoluteAccuracy) {
  46.         super(absoluteAccuracy);
  47.     }
  48.     /**
  49.      * Construct a solver.
  50.      *
  51.      * @param relativeAccuracy Relative accuracy.
  52.      * @param absoluteAccuracy Absolute accuracy.
  53.      */
  54.     public BisectionSolver(double relativeAccuracy,
  55.                            double absoluteAccuracy) {
  56.         super(relativeAccuracy, absoluteAccuracy);
  57.     }

  58.     /**
  59.      * {@inheritDoc}
  60.      */
  61.     @Override
  62.     protected double doSolve()
  63.         throws MathIllegalStateException {
  64.         double min = getMin();
  65.         double max = getMax();
  66.         verifyInterval(min, max);
  67.         verifyBracketing(min, max);
  68.         final double absoluteAccuracy = getAbsoluteAccuracy();
  69.         double m;
  70.         double fm;
  71.         double fmin;

  72.         while (true) {
  73.             m = UnivariateSolverUtils.midpoint(min, max);
  74.             fmin = computeObjectiveValue(min);
  75.             fm = computeObjectiveValue(m);

  76.             if (fm * fmin > 0) {
  77.                 // max and m bracket the root.
  78.                 min = m;
  79.             } else {
  80.                 // min and m bracket the root.
  81.                 max = m;
  82.             }

  83.             if (FastMath.abs(max - min) <= absoluteAccuracy) {
  84.                 m = UnivariateSolverUtils.midpoint(min, max);
  85.                 return m;
  86.             }
  87.         }
  88.     }
  89. }