View Javadoc
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  
18  package org.apache.commons.math4.legacy.analysis.solvers;
19  
20  import org.apache.commons.math4.legacy.analysis.differentiation.DerivativeStructure;
21  import org.apache.commons.math4.legacy.analysis.differentiation.UnivariateDifferentiableFunction;
22  import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
23  
24  /**
25   * Provide a default implementation for several functions useful to generic
26   * solvers.
27   *
28   * @since 3.1
29   */
30  public abstract class AbstractUnivariateDifferentiableSolver
31      extends BaseAbstractUnivariateSolver<UnivariateDifferentiableFunction>
32      implements UnivariateDifferentiableSolver {
33  
34      /** Function to solve. */
35      private UnivariateDifferentiableFunction function;
36  
37      /**
38       * Construct a solver with given absolute accuracy.
39       *
40       * @param absoluteAccuracy Maximum absolute error.
41       */
42      protected AbstractUnivariateDifferentiableSolver(final double absoluteAccuracy) {
43          super(absoluteAccuracy);
44      }
45  
46      /**
47       * Construct a solver with given accuracies.
48       *
49       * @param relativeAccuracy Maximum relative error.
50       * @param absoluteAccuracy Maximum absolute error.
51       * @param functionValueAccuracy Maximum function value error.
52       */
53      protected AbstractUnivariateDifferentiableSolver(final double relativeAccuracy,
54                                                       final double absoluteAccuracy,
55                                                       final double functionValueAccuracy) {
56          super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy);
57      }
58  
59      /**
60       * Compute the objective function value.
61       *
62       * @param point Point at which the objective function must be evaluated.
63       * @return the objective function value and derivative at specified point.
64       * @throws TooManyEvaluationsException
65       * if the maximal number of evaluations is exceeded.
66       */
67      protected DerivativeStructure computeObjectiveValueAndDerivative(double point)
68          throws TooManyEvaluationsException {
69          incrementEvaluationCount();
70          return function.value(new DerivativeStructure(1, 1, 0, point));
71      }
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  }