AbstractUnivariateDifferentiableSolver.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.differentiation.DSFactory;
  23. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  24. import org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction;
  25. import org.hipparchus.exception.MathIllegalStateException;

  26. /**
  27.  * Provide a default implementation for several functions useful to generic
  28.  * solvers.
  29.  *
  30.  */
  31. public abstract class AbstractUnivariateDifferentiableSolver
  32.     extends BaseAbstractUnivariateSolver<UnivariateDifferentiableFunction>
  33.     implements UnivariateDifferentiableSolver {

  34.     /** Function to solve. */
  35.     private UnivariateDifferentiableFunction function;

  36.     /** Factory for DerivativeStructure instances. */
  37.     private final DSFactory factory;

  38.     /**
  39.      * Construct a solver with given absolute accuracy.
  40.      *
  41.      * @param absoluteAccuracy Maximum absolute error.
  42.      */
  43.     protected AbstractUnivariateDifferentiableSolver(final double absoluteAccuracy) {
  44.         super(absoluteAccuracy);
  45.         factory = new DSFactory(1, 1);
  46.     }

  47.     /**
  48.      * Construct a solver with given accuracies.
  49.      *
  50.      * @param relativeAccuracy Maximum relative error.
  51.      * @param absoluteAccuracy Maximum absolute error.
  52.      * @param functionValueAccuracy Maximum function value error.
  53.      */
  54.     protected AbstractUnivariateDifferentiableSolver(final double relativeAccuracy,
  55.                                                      final double absoluteAccuracy,
  56.                                                      final double functionValueAccuracy) {
  57.         super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy);
  58.         factory = new DSFactory(1, 1);
  59.     }

  60.     /**
  61.      * Compute the objective function value.
  62.      *
  63.      * @param point Point at which the objective function must be evaluated.
  64.      * @return the objective function value and derivative at specified point.
  65.      * @throws MathIllegalStateException
  66.      * if the maximal number of evaluations is exceeded.
  67.      */
  68.     protected DerivativeStructure computeObjectiveValueAndDerivative(double point)
  69.         throws MathIllegalStateException {
  70.         incrementEvaluationCount();
  71.         return function.value(factory.variable(0, point));
  72.     }

  73.     /**
  74.      * {@inheritDoc}
  75.      */
  76.     @Override
  77.     protected void setup(int maxEval, UnivariateDifferentiableFunction f,
  78.                          double min, double max, double startValue) {
  79.         super.setup(maxEval, f, min, max, startValue);
  80.         function = f;
  81.     }
  82. }