001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math3.analysis.solvers;
019    
020    import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
021    import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction;
022    
023    /**
024     * Provide a default implementation for several functions useful to generic
025     * solvers.
026     *
027     * @since 3.1
028     * @version $Id: AbstractUnivariateDifferentiableSolver.java 1383441 2012-09-11 14:56:39Z luc $
029     */
030    public abstract class AbstractUnivariateDifferentiableSolver
031        extends BaseAbstractUnivariateSolver<UnivariateDifferentiableFunction>
032        implements UnivariateDifferentiableSolver {
033    
034        /** Function to solve. */
035        private UnivariateDifferentiableFunction function;
036    
037        /**
038         * Construct a solver with given absolute accuracy.
039         *
040         * @param absoluteAccuracy Maximum absolute error.
041         */
042        protected AbstractUnivariateDifferentiableSolver(final double absoluteAccuracy) {
043            super(absoluteAccuracy);
044        }
045    
046        /**
047         * Construct a solver with given accuracies.
048         *
049         * @param relativeAccuracy Maximum relative error.
050         * @param absoluteAccuracy Maximum absolute error.
051         * @param functionValueAccuracy Maximum function value error.
052         */
053        protected AbstractUnivariateDifferentiableSolver(final double relativeAccuracy,
054                                                         final double absoluteAccuracy,
055                                                         final double functionValueAccuracy) {
056            super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy);
057        }
058    
059        /**
060         * Compute the objective function value.
061         *
062         * @param point Point at which the objective function must be evaluated.
063         * @return the objective function value and derivative at specified point.
064         * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
065         * if the maximal number of evaluations is exceeded.
066         */
067        protected DerivativeStructure computeObjectiveValueAndDerivative(double point) {
068            incrementEvaluationCount();
069            return function.value(new DerivativeStructure(1, 1, 0, point));
070        }
071    
072        /**
073         * {@inheritDoc}
074         */
075        @Override
076        protected void setup(int maxEval, UnivariateDifferentiableFunction f,
077                             double min, double max, double startValue) {
078            super.setup(maxEval, f, min, max, startValue);
079            function = f;
080        }
081    }