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
018package org.apache.commons.math3.analysis.solvers;
019
020import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
021import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction;
022import 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 */
030public 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 TooManyEvaluationsException
065     * if the maximal number of evaluations is exceeded.
066     */
067    protected DerivativeStructure computeObjectiveValueAndDerivative(double point)
068        throws TooManyEvaluationsException {
069        incrementEvaluationCount();
070        return function.value(new DerivativeStructure(1, 1, 0, point));
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    protected void setup(int maxEval, UnivariateDifferentiableFunction f,
078                         double min, double max, double startValue) {
079        super.setup(maxEval, f, min, max, startValue);
080        function = f;
081    }
082}