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    package org.apache.commons.math3.fitting;
018    
019    import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
020    import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer;
021    
022    /**
023     * Polynomial fitting is a very simple case of {@link CurveFitter curve fitting}.
024     * The estimated coefficients are the polynomial coefficients (see the
025     * {@link #fit(double[]) fit} method).
026     *
027     * @version $Id: PolynomialFitter.java 1416643 2012-12-03 19:37:14Z tn $
028     * @since 2.0
029     */
030    public class PolynomialFitter extends CurveFitter<PolynomialFunction.Parametric> {
031        /**
032         * Simple constructor.
033         *
034         * @param optimizer Optimizer to use for the fitting.
035         */
036        public PolynomialFitter(MultivariateVectorOptimizer optimizer) {
037            super(optimizer);
038        }
039    
040        /**
041         * Get the coefficients of the polynomial fitting the weighted data points.
042         * The degree of the fitting polynomial is {@code guess.length - 1}.
043         *
044         * @param guess First guess for the coefficients. They must be sorted in
045         * increasing order of the polynomial's degree.
046         * @param maxEval Maximum number of evaluations of the polynomial.
047         * @return the coefficients of the polynomial that best fits the observed points.
048         * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if
049         * the number of evaluations exceeds {@code maxEval}.
050         * @throws org.apache.commons.math3.exception.ConvergenceException
051         * if the algorithm failed to converge.
052         */
053        public double[] fit(int maxEval, double[] guess) {
054            return fit(maxEval, new PolynomialFunction.Parametric(), guess);
055        }
056    
057        /**
058         * Get the coefficients of the polynomial fitting the weighted data points.
059         * The degree of the fitting polynomial is {@code guess.length - 1}.
060         *
061         * @param guess First guess for the coefficients. They must be sorted in
062         * increasing order of the polynomial's degree.
063         * @return the coefficients of the polynomial that best fits the observed points.
064         * @throws org.apache.commons.math3.exception.ConvergenceException
065         * if the algorithm failed to converge.
066         */
067        public double[] fit(double[] guess) {
068            return fit(new PolynomialFunction.Parametric(), guess);
069        }
070    }