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.  *      http://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. package org.apache.commons.math4.legacy.analysis.solvers;

  18. import org.apache.commons.math4.legacy.analysis.differentiation.DerivativeStructure;
  19. import org.apache.commons.math4.legacy.analysis.differentiation.UnivariateDifferentiableFunction;
  20. import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;

  21. /**
  22.  * Provide a default implementation for several functions useful to generic
  23.  * solvers.
  24.  *
  25.  * @since 3.1
  26.  */
  27. public abstract class AbstractUnivariateDifferentiableSolver
  28.     extends BaseAbstractUnivariateSolver<UnivariateDifferentiableFunction>
  29.     implements UnivariateDifferentiableSolver {

  30.     /** Function to solve. */
  31.     private UnivariateDifferentiableFunction function;

  32.     /**
  33.      * Construct a solver with given absolute accuracy.
  34.      *
  35.      * @param absoluteAccuracy Maximum absolute error.
  36.      */
  37.     protected AbstractUnivariateDifferentiableSolver(final double absoluteAccuracy) {
  38.         super(absoluteAccuracy);
  39.     }

  40.     /**
  41.      * Construct a solver with given accuracies.
  42.      *
  43.      * @param relativeAccuracy Maximum relative error.
  44.      * @param absoluteAccuracy Maximum absolute error.
  45.      * @param functionValueAccuracy Maximum function value error.
  46.      */
  47.     protected AbstractUnivariateDifferentiableSolver(final double relativeAccuracy,
  48.                                                      final double absoluteAccuracy,
  49.                                                      final double functionValueAccuracy) {
  50.         super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy);
  51.     }

  52.     /**
  53.      * Compute the objective function value.
  54.      *
  55.      * @param point Point at which the objective function must be evaluated.
  56.      * @return the objective function value and derivative at specified point.
  57.      * @throws TooManyEvaluationsException
  58.      * if the maximal number of evaluations is exceeded.
  59.      */
  60.     protected DerivativeStructure computeObjectiveValueAndDerivative(double point)
  61.         throws TooManyEvaluationsException {
  62.         incrementEvaluationCount();
  63.         return function.value(new DerivativeStructure(1, 1, 0, point));
  64.     }

  65.     /**
  66.      * {@inheritDoc}
  67.      */
  68.     @Override
  69.     protected void setup(int maxEval, UnivariateDifferentiableFunction f,
  70.                          double min, double max, double startValue) {
  71.         super.setup(maxEval, f, min, max, startValue);
  72.         function = f;
  73.     }
  74. }