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    import org.apache.commons.math3.exception.TooManyEvaluationsException;
023    
024    /**
025     * Provide a default implementation for several functions useful to generic
026     * solvers.
027     *
028     * @since 3.1
029     * @version $Id: AbstractUnivariateDifferentiableSolver.java 1455194 2013-03-11 15:45:54Z luc $
030     */
031    public abstract class AbstractUnivariateDifferentiableSolver
032        extends BaseAbstractUnivariateSolver<UnivariateDifferentiableFunction>
033        implements UnivariateDifferentiableSolver {
034    
035        /** Function to solve. */
036        private UnivariateDifferentiableFunction function;
037    
038        /**
039         * Construct a solver with given absolute accuracy.
040         *
041         * @param absoluteAccuracy Maximum absolute error.
042         */
043        protected AbstractUnivariateDifferentiableSolver(final double absoluteAccuracy) {
044            super(absoluteAccuracy);
045        }
046    
047        /**
048         * Construct a solver with given accuracies.
049         *
050         * @param relativeAccuracy Maximum relative error.
051         * @param absoluteAccuracy Maximum absolute error.
052         * @param functionValueAccuracy Maximum function value error.
053         */
054        protected AbstractUnivariateDifferentiableSolver(final double relativeAccuracy,
055                                                         final double absoluteAccuracy,
056                                                         final double functionValueAccuracy) {
057            super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy);
058        }
059    
060        /**
061         * Compute the objective function value.
062         *
063         * @param point Point at which the objective function must be evaluated.
064         * @return the objective function value and derivative at specified point.
065         * @throws TooManyEvaluationsException
066         * if the maximal number of evaluations is exceeded.
067         */
068        protected DerivativeStructure computeObjectiveValueAndDerivative(double point)
069            throws TooManyEvaluationsException {
070            incrementEvaluationCount();
071            return function.value(new DerivativeStructure(1, 1, 0, point));
072        }
073    
074        /**
075         * {@inheritDoc}
076         */
077        @Override
078        protected void setup(int maxEval, UnivariateDifferentiableFunction f,
079                             double min, double max, double startValue) {
080            super.setup(maxEval, f, min, max, startValue);
081            function = f;
082        }
083    }