BracketedUnivariateSolver.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.analysis.UnivariateFunction;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.MathIllegalStateException;
  25. import org.hipparchus.exception.MathRuntimeException;
  26. import org.hipparchus.util.FastMath;

  27. /** Interface for {@link UnivariateSolver (univariate real) root-finding
  28.  * algorithms} that maintain a bracketed solution. There are several advantages
  29.  * to having such root-finding algorithms:
  30.  * <ul>
  31.  *  <li>The bracketed solution guarantees that the root is kept within the
  32.  *      interval. As such, these algorithms generally also guarantee
  33.  *      convergence.</li>
  34.  *  <li>The bracketed solution means that we have the opportunity to only
  35.  *      return roots that are greater than or equal to the actual root, or
  36.  *      are less than or equal to the actual root. That is, we can control
  37.  *      whether under-approximations and over-approximations are
  38.  *      {@link AllowedSolution allowed solutions}. Other root-finding
  39.  *      algorithms can usually only guarantee that the solution (the root that
  40.  *      was found) is around the actual root.</li>
  41.  * </ul>
  42.  *
  43.  * <p>For backwards compatibility, all root-finding algorithms must have
  44.  * {@link AllowedSolution#ANY_SIDE ANY_SIDE} as default for the allowed
  45.  * solutions.</p>
  46.  * @param <F> Type of function to solve.
  47.  *
  48.  * @see AllowedSolution
  49.  */
  50. public interface BracketedUnivariateSolver<F extends UnivariateFunction>
  51.     extends BaseUnivariateSolver<F> {

  52.     /**
  53.      * Solve for a zero in the given interval.
  54.      * A solver may require that the interval brackets a single zero root.
  55.      * Solvers that do require bracketing should be able to handle the case
  56.      * where one of the endpoints is itself a root.
  57.      *
  58.      * @param maxEval Maximum number of evaluations.
  59.      * @param f Function to solve.
  60.      * @param min Lower bound for the interval.
  61.      * @param max Upper bound for the interval.
  62.      * @param allowedSolution The kind of solutions that the root-finding algorithm may
  63.      * accept as solutions.
  64.      * @return A value where the function is zero.
  65.      * @throws org.hipparchus.exception.MathIllegalArgumentException
  66.      * if the arguments do not satisfy the requirements specified by the solver.
  67.      * @throws org.hipparchus.exception.MathIllegalStateException if
  68.      * the allowed number of evaluations is exceeded.
  69.      */
  70.     double solve(int maxEval, F f, double min, double max,
  71.                  AllowedSolution allowedSolution);

  72.     /**
  73.      * Solve for a zero in the given interval, start at {@code startValue}.
  74.      * A solver may require that the interval brackets a single zero root.
  75.      * Solvers that do require bracketing should be able to handle the case
  76.      * where one of the endpoints is itself a root.
  77.      *
  78.      * @param maxEval Maximum number of evaluations.
  79.      * @param f Function to solve.
  80.      * @param min Lower bound for the interval.
  81.      * @param max Upper bound for the interval.
  82.      * @param startValue Start value to use.
  83.      * @param allowedSolution The kind of solutions that the root-finding algorithm may
  84.      * accept as solutions.
  85.      * @return A value where the function is zero.
  86.      * @throws org.hipparchus.exception.MathIllegalArgumentException
  87.      * if the arguments do not satisfy the requirements specified by the solver.
  88.      * @throws org.hipparchus.exception.MathIllegalStateException if
  89.      * the allowed number of evaluations is exceeded.
  90.      */
  91.     double solve(int maxEval, F f, double min, double max, double startValue,
  92.                  AllowedSolution allowedSolution);

  93.     /**
  94.      * Solve for a zero in the given interval and return a tolerance interval surrounding
  95.      * the root.
  96.      *
  97.      * <p> It is required that the starting interval brackets a root or that the function
  98.      * value at either end point is 0.0.
  99.      *
  100.      * @param maxEval Maximum number of evaluations.
  101.      * @param f       Function to solve.
  102.      * @param min     Lower bound for the interval.
  103.      * @param max     Upper bound for the interval. Must be greater than {@code min}.
  104.      * @return an interval [ta, tb] such that for some t in [ta, tb] f(t) == 0.0 or has a
  105.      * step wise discontinuity that crosses zero. Both end points also satisfy the
  106.      * convergence criteria so either one could be used as the root. That is the interval
  107.      * satisfies the condition (| tb - ta | &lt;= {@link #getAbsoluteAccuracy() absolute}
  108.      * accuracy + max(ta, tb) * {@link #getRelativeAccuracy() relative} accuracy) or (
  109.      * max(|f(ta)|, |f(tb)|) &lt;= {@link #getFunctionValueAccuracy()}) or there are no
  110.      * floating point numbers between ta and tb. The width of the interval (tb - ta) may
  111.      * be zero.
  112.      * @throws MathIllegalArgumentException if the arguments do not satisfy the
  113.      *                                      requirements specified by the solver.
  114.      * @throws MathIllegalStateException    if the allowed number of evaluations is
  115.      *                                      exceeded.
  116.      */
  117.     default Interval solveInterval(int maxEval, F f, double min, double max)
  118.             throws MathIllegalArgumentException, MathIllegalStateException {
  119.         return this.solveInterval(maxEval, f, min, max, min + 0.5 * (max - min));
  120.     }

  121.     /**
  122.      * Solve for a zero in the given interval and return a tolerance interval surrounding
  123.      * the root.
  124.      *
  125.      * <p> It is required that the starting interval brackets a root or that the function
  126.      * value at either end point is 0.0.
  127.      *
  128.      * @param maxEval    Maximum number of evaluations.
  129.      * @param startValue start value to use. Must be in the interval [min, max].
  130.      * @param f          Function to solve.
  131.      * @param min        Lower bound for the interval.
  132.      * @param max     Upper bound for the interval. Must be greater than {@code min}.
  133.      * @return an interval [ta, tb] such that for some t in [ta, tb] f(t) == 0.0 or has a
  134.      * step wise discontinuity that crosses zero. Both end points also satisfy the
  135.      * convergence criteria so either one could be used as the root. That is the interval
  136.      * satisfies the condition (| tb - ta | &lt;= {@link #getAbsoluteAccuracy() absolute}
  137.      * accuracy + max(ta, tb) * {@link #getRelativeAccuracy() relative} accuracy) or (
  138.      * max(|f(ta)|, |f(tb)|) &lt;= {@link #getFunctionValueAccuracy()}) or there are no
  139.      * floating point numbers between ta and tb. The width of the interval (tb - ta) may
  140.      * be zero.
  141.      * @throws MathIllegalArgumentException if the arguments do not satisfy the
  142.      *                                      requirements specified by the solver.
  143.      * @throws MathIllegalStateException    if the allowed number of evaluations is
  144.      *                                      exceeded.
  145.      */
  146.     Interval solveInterval(int maxEval, F f, double min, double max, double startValue)
  147.             throws MathIllegalArgumentException, MathIllegalStateException;

  148.     /**
  149.      * An interval of a function that brackets a root.
  150.      *
  151.      * <p> Contains two end points and the value of the function at the two end points.
  152.      *
  153.      * @see #solveInterval(int, UnivariateFunction, double, double, double)
  154.      */
  155.     class Interval {

  156.         /** Abscissa on the left end of the interval. */
  157.         private final double leftAbscissa;
  158.         /** Function value at {@link #leftAbscissa}. */
  159.         private final double leftValue;
  160.         /** Abscissa on the right end of the interval, >= {@link #leftAbscissa}. */
  161.         private final double rightAbscissa;
  162.         /** Function value at {@link #rightAbscissa}. */
  163.         private final double rightValue;

  164.         /**
  165.          * Construct a new interval with the given end points.
  166.          *
  167.          * @param leftAbscissa  is the abscissa value at the left side of the interval.
  168.          * @param leftValue     is the function value at {@code leftAbscissa}.
  169.          * @param rightAbscissa is the abscissa value on the right side of the interval.
  170.          *                      Must be greater than or equal to {@code leftAbscissa}.
  171.          * @param rightValue    is the function value at {@code rightAbscissa}.
  172.          */
  173.         public Interval(final double leftAbscissa,
  174.                         final double leftValue,
  175.                         final double rightAbscissa,
  176.                         final double rightValue) {
  177.             this.leftAbscissa = leftAbscissa;
  178.             this.leftValue = leftValue;
  179.             this.rightAbscissa = rightAbscissa;
  180.             this.rightValue = rightValue;
  181.         }

  182.         /**
  183.          * Get the left abscissa.
  184.          *
  185.          * @return abscissa of the start of the interval.
  186.          */
  187.         public double getLeftAbscissa() {
  188.             return leftAbscissa;
  189.         }

  190.         /**
  191.          * Get the right abscissa.
  192.          *
  193.          * @return abscissa of the end of the interval.
  194.          */
  195.         public double getRightAbscissa() {
  196.             return rightAbscissa;
  197.         }

  198.         /**
  199.          * Get the function value at {@link #getLeftAbscissa()}.
  200.          *
  201.          * @return value of the function at the start of the interval.
  202.          */
  203.         public double getLeftValue() {
  204.             return leftValue;
  205.         }

  206.         /**
  207.          * Get the function value at {@link #getRightAbscissa()}.
  208.          *
  209.          * @return value of the function at the end of the interval.
  210.          */
  211.         public double getRightValue() {
  212.             return rightValue;
  213.         }

  214.         /**
  215.          * Get the abscissa corresponding to the allowed side.
  216.          *
  217.          * @param allowed side of the root.
  218.          * @return the abscissa on the selected side of the root.
  219.          */
  220.         public double getSide(final AllowedSolution allowed) {
  221.             final double xA = this.getLeftAbscissa();
  222.             final double yA = this.getLeftValue();
  223.             final double xB = this.getRightAbscissa();
  224.             switch (allowed) {
  225.                 case ANY_SIDE:
  226.                     final double absYA = FastMath.abs(this.getLeftValue());
  227.                     final double absYB = FastMath.abs(this.getRightValue());
  228.                     return absYA < absYB ? xA : xB;
  229.                 case LEFT_SIDE:
  230.                     return xA;
  231.                 case RIGHT_SIDE:
  232.                     return xB;
  233.                 case BELOW_SIDE:
  234.                     return (yA <= 0) ? xA : xB;
  235.                 case ABOVE_SIDE:
  236.                     return (yA < 0) ? xB : xA;
  237.                 default:
  238.                     // this should never happen
  239.                     throw MathRuntimeException.createInternalError();
  240.             }
  241.         }

  242.     }

  243. }