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    *      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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.analysis.solvers;
24  
25  import org.hipparchus.analysis.differentiation.DSFactory;
26  import org.hipparchus.analysis.differentiation.DerivativeStructure;
27  import org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction;
28  import org.hipparchus.exception.MathIllegalStateException;
29  
30  /**
31   * Provide a default implementation for several functions useful to generic
32   * solvers.
33   *
34   */
35  public abstract class AbstractUnivariateDifferentiableSolver
36      extends BaseAbstractUnivariateSolver<UnivariateDifferentiableFunction>
37      implements UnivariateDifferentiableSolver {
38  
39      /** Function to solve. */
40      private UnivariateDifferentiableFunction function;
41  
42      /** Factory for DerivativeStructure instances. */
43      private final DSFactory factory;
44  
45      /**
46       * Construct a solver with given absolute accuracy.
47       *
48       * @param absoluteAccuracy Maximum absolute error.
49       */
50      protected AbstractUnivariateDifferentiableSolver(final double absoluteAccuracy) {
51          super(absoluteAccuracy);
52          factory = new DSFactory(1, 1);
53      }
54  
55      /**
56       * Construct a solver with given accuracies.
57       *
58       * @param relativeAccuracy Maximum relative error.
59       * @param absoluteAccuracy Maximum absolute error.
60       * @param functionValueAccuracy Maximum function value error.
61       */
62      protected AbstractUnivariateDifferentiableSolver(final double relativeAccuracy,
63                                                       final double absoluteAccuracy,
64                                                       final double functionValueAccuracy) {
65          super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy);
66          factory = new DSFactory(1, 1);
67      }
68  
69      /**
70       * Compute the objective function value.
71       *
72       * @param point Point at which the objective function must be evaluated.
73       * @return the objective function value and derivative at specified point.
74       * @throws MathIllegalStateException
75       * if the maximal number of evaluations is exceeded.
76       */
77      protected DerivativeStructure computeObjectiveValueAndDerivative(double point)
78          throws MathIllegalStateException {
79          incrementEvaluationCount();
80          return function.value(factory.variable(0, point));
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      protected void setup(int maxEval, UnivariateDifferentiableFunction f,
88                           double min, double max, double startValue) {
89          super.setup(maxEval, f, min, max, startValue);
90          function = f;
91      }
92  }