BracketedRealFieldUnivariateSolver.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.CalculusFieldElement;
  23. import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.exception.MathIllegalStateException;
  26. import org.hipparchus.exception.MathRuntimeException;

  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.  *
  47.  * @see AllowedSolution
  48.  * @param <T> the type of the field elements
  49.  */
  50. public interface BracketedRealFieldUnivariateSolver<T extends CalculusFieldElement<T>> {

  51.     /**
  52.      * Get the maximum number of function evaluations.
  53.      *
  54.      * @return the maximum number of function evaluations.
  55.      */
  56.     int getMaxEvaluations();

  57.     /**
  58.      * Get the number of evaluations of the objective function.
  59.      * The number of evaluations corresponds to the last call to the
  60.      * {@code optimize} method. It is 0 if the method has not been
  61.      * called yet.
  62.      *
  63.      * @return the number of evaluations of the objective function.
  64.      */
  65.     int getEvaluations();

  66.     /**
  67.      * Get the absolute accuracy of the solver.  Solutions returned by the
  68.      * solver should be accurate to this tolerance, i.e., if &epsilon; is the
  69.      * absolute accuracy of the solver and {@code v} is a value returned by
  70.      * one of the {@code solve} methods, then a root of the function should
  71.      * exist somewhere in the interval ({@code v} - &epsilon;, {@code v} + &epsilon;).
  72.      *
  73.      * @return the absolute accuracy.
  74.      */
  75.     T getAbsoluteAccuracy();

  76.     /**
  77.      * Get the relative accuracy of the solver.  The contract for relative
  78.      * accuracy is the same as {@link #getAbsoluteAccuracy()}, but using
  79.      * relative, rather than absolute error.  If &rho; is the relative accuracy
  80.      * configured for a solver and {@code v} is a value returned, then a root
  81.      * of the function should exist somewhere in the interval
  82.      * ({@code v} - &rho; {@code v}, {@code v} + &rho; {@code v}).
  83.      *
  84.      * @return the relative accuracy.
  85.      */
  86.     T getRelativeAccuracy();

  87.     /**
  88.      * Get the function value accuracy of the solver.  If {@code v} is
  89.      * a value returned by the solver for a function {@code f},
  90.      * then by contract, {@code |f(v)|} should be less than or equal to
  91.      * the function value accuracy configured for the solver.
  92.      *
  93.      * @return the function value accuracy.
  94.      */
  95.     T getFunctionValueAccuracy();

  96.     /**
  97.      * Solve for a zero in the given interval.
  98.      * A solver may require that the interval brackets a single zero root.
  99.      * Solvers that do require bracketing should be able to handle the case
  100.      * where one of the endpoints is itself a root.
  101.      *
  102.      * @param maxEval Maximum number of evaluations.
  103.      * @param f Function to solve.
  104.      * @param min Lower bound for the interval.
  105.      * @param max Upper bound for the interval.
  106.      * @param allowedSolution The kind of solutions that the root-finding algorithm may
  107.      * accept as solutions.
  108.      * @return A value where the function is zero.
  109.      * @throws org.hipparchus.exception.MathIllegalArgumentException
  110.      * if the arguments do not satisfy the requirements specified by the solver.
  111.      * @throws org.hipparchus.exception.MathIllegalStateException if
  112.      * the allowed number of evaluations is exceeded.
  113.      */
  114.     T solve(int maxEval, CalculusFieldUnivariateFunction<T> f, T min, T max,
  115.             AllowedSolution allowedSolution);

  116.     /**
  117.      * Solve for a zero in the given interval, start at {@code startValue}.
  118.      * A solver may require that the interval brackets a single zero root.
  119.      * Solvers that do require bracketing should be able to handle the case
  120.      * where one of the endpoints is itself a root.
  121.      *
  122.      * @param maxEval Maximum number of evaluations.
  123.      * @param f Function to solve.
  124.      * @param min Lower bound for the interval.
  125.      * @param max Upper bound for the interval.
  126.      * @param startValue Start value to use.
  127.      * @param allowedSolution The kind of solutions that the root-finding algorithm may
  128.      * accept as solutions.
  129.      * @return A value where the function is zero.
  130.      * @throws org.hipparchus.exception.MathIllegalArgumentException
  131.      * if the arguments do not satisfy the requirements specified by the solver.
  132.      * @throws org.hipparchus.exception.MathIllegalStateException if
  133.      * the allowed number of evaluations is exceeded.
  134.      */
  135.     T solve(int maxEval, CalculusFieldUnivariateFunction<T> f, T min, T max, T startValue,
  136.             AllowedSolution allowedSolution);

  137.     /**
  138.      * Solve for a zero in the given interval and return a tolerance interval surrounding
  139.      * the root.
  140.      *
  141.      * <p> It is required that the starting interval brackets a root.
  142.      *
  143.      * @param maxEval Maximum number of evaluations.
  144.      * @param f       Function to solve.
  145.      * @param min     Lower bound for the interval. f(min) != 0.0.
  146.      * @param max     Upper bound for the interval. f(max) != 0.0.
  147.      * @return an interval [ta, tb] such that for some t in [ta, tb] f(t) == 0.0 or has a
  148.      * step wise discontinuity that crosses zero. Both end points also satisfy the
  149.      * convergence criteria so either one could be used as the root. That is the interval
  150.      * satisfies the condition (| tb - ta | &lt;= {@link #getAbsoluteAccuracy() absolute}
  151.      * accuracy + max(ta, tb) * {@link #getRelativeAccuracy() relative} accuracy) or (
  152.      * max(|f(ta)|, |f(tb)|) &lt;= {@link #getFunctionValueAccuracy()}) or there are no
  153.      * numbers in the field between ta and tb. The width of the interval (tb - ta) may be
  154.      * zero.
  155.      * @throws MathIllegalArgumentException if the arguments do not satisfy the
  156.      *                                      requirements specified by the solver.
  157.      * @throws MathIllegalStateException    if the allowed number of evaluations is
  158.      *                                      exceeded.
  159.      */
  160.     default Interval<T> solveInterval(int maxEval,
  161.                                       CalculusFieldUnivariateFunction<T> f,
  162.                                       T min,
  163.                                       T max)
  164.             throws MathIllegalArgumentException, MathIllegalStateException {
  165.         return this.solveInterval(maxEval, f, min, max, min.add(max.subtract(min).multiply(0.5)));
  166.     }

  167.     /**
  168.      * Solve for a zero in the given interval and return a tolerance interval surrounding
  169.      * the root.
  170.      *
  171.      * <p> It is required that the starting interval brackets a root.
  172.      *
  173.      * @param maxEval    Maximum number of evaluations.
  174.      * @param startValue start value to use.
  175.      * @param f          Function to solve.
  176.      * @param min        Lower bound for the interval. f(min) != 0.0.
  177.      * @param max        Upper bound for the interval. f(max) != 0.0.
  178.      * @return an interval [ta, tb] such that for some t in [ta, tb] f(t) == 0.0 or has a
  179.      * step wise discontinuity that crosses zero. Both end points also satisfy the
  180.      * convergence criteria so either one could be used as the root. That is the interval
  181.      * satisfies the condition (| tb - ta | &lt;= {@link #getAbsoluteAccuracy() absolute}
  182.      * accuracy + max(ta, tb) * {@link #getRelativeAccuracy() relative} accuracy) or (
  183.      * max(|f(ta)|, |f(tb)|) &lt;= {@link #getFunctionValueAccuracy()}) or numbers in the
  184.      * field between ta and tb. The width of the interval (tb - ta) may be zero.
  185.      * @throws MathIllegalArgumentException if the arguments do not satisfy the
  186.      *                                      requirements specified by the solver.
  187.      * @throws MathIllegalStateException    if the allowed number of evaluations is
  188.      *                                      exceeded.
  189.      */
  190.     Interval<T> solveInterval(int maxEval,
  191.                               CalculusFieldUnivariateFunction<T> f,
  192.                               T min,
  193.                               T max,
  194.                               T startValue)
  195.             throws MathIllegalArgumentException, MathIllegalStateException;

  196.     /**
  197.      * An interval of a function that brackets a root.
  198.      * <p>
  199.      * Contains two end points and the value of the function at the two end points.
  200.      *
  201.      * @see #solveInterval(int, CalculusFieldUnivariateFunction, CalculusFieldElement,
  202.      * CalculusFieldElement)
  203.      * @param <T> the element type
  204.      */
  205.     class Interval<T extends CalculusFieldElement<T>> {

  206.         /** Abscissa on the left end of the interval. */
  207.         private final T leftAbscissa;
  208.         /** Function value at {@link #leftAbscissa}. */
  209.         private final T leftValue;
  210.         /** Abscissa on the right end of the interval, >= {@link #leftAbscissa}. */
  211.         private final T rightAbscissa;
  212.         /** Function value at {@link #rightAbscissa}. */
  213.         private final T rightValue;

  214.         /**
  215.          * Construct a new interval with the given end points.
  216.          *
  217.          * @param leftAbscissa  is the abscissa value at the left side of the interval.
  218.          * @param leftValue     is the function value at {@code leftAbscissa}.
  219.          * @param rightAbscissa is the abscissa value on the right side of the interval.
  220.          *                      Must be greater than or equal to {@code leftAbscissa}.
  221.          * @param rightValue    is the function value at {@code rightAbscissa}.
  222.          */
  223.         public Interval(final T leftAbscissa,
  224.                         final T leftValue,
  225.                         final T rightAbscissa,
  226.                         final T rightValue) {
  227.             this.leftAbscissa = leftAbscissa;
  228.             this.leftValue = leftValue;
  229.             this.rightAbscissa = rightAbscissa;
  230.             this.rightValue = rightValue;
  231.         }

  232.         /**
  233.          * Get the left abscissa.
  234.          *
  235.          * @return abscissa of the start of the interval.
  236.          */
  237.         public T getLeftAbscissa() {
  238.             return leftAbscissa;
  239.         }

  240.         /**
  241.          * Get the right abscissa.
  242.          *
  243.          * @return abscissa of the end of the interval.
  244.          */
  245.         public T getRightAbscissa() {
  246.             return rightAbscissa;
  247.         }

  248.         /**
  249.          * Get the function value at {@link #getLeftAbscissa()}.
  250.          *
  251.          * @return value of the function at the start of the interval.
  252.          */
  253.         public T getLeftValue() {
  254.             return leftValue;
  255.         }

  256.         /**
  257.          * Get the function value at {@link #getRightAbscissa()}.
  258.          *
  259.          * @return value of the function at the end of the interval.
  260.          */
  261.         public T getRightValue() {
  262.             return rightValue;
  263.         }

  264.         /**
  265.          * Get the abscissa corresponding to the allowed side.
  266.          *
  267.          * @param allowed side of the root.
  268.          * @return the abscissa on the selected side of the root.
  269.          */
  270.         public T getSide(final AllowedSolution allowed) {
  271.             final T xA = this.getLeftAbscissa();
  272.             final T yA = this.getLeftValue();
  273.             final T xB = this.getRightAbscissa();
  274.             switch (allowed) {
  275.                 case ANY_SIDE:
  276.                     final T absYA = this.getLeftValue().abs();
  277.                     final T absYB = this.getRightValue().abs();
  278.                     return absYA.subtract(absYB).getReal() < 0 ? xA : xB;
  279.                 case LEFT_SIDE:
  280.                     return xA;
  281.                 case RIGHT_SIDE:
  282.                     return xB;
  283.                 case BELOW_SIDE:
  284.                     return (yA.getReal() <= 0) ? xA : xB;
  285.                 case ABOVE_SIDE:
  286.                     return (yA.getReal() < 0) ? xB : xA;
  287.                 default:
  288.                     // this should never happen
  289.                     throw MathRuntimeException.createInternalError();
  290.             }
  291.         }

  292.     }

  293. }