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.optimization.fitting;
019    
020    import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
021    import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer;
022    
023    /**
024     * Polynomial fitting is a very simple case of {@link CurveFitter curve fitting}.
025     * The estimated coefficients are the polynomial coefficients (see the
026     * {@link #fit(double[]) fit} method).
027     *
028     * @version $Id: PolynomialFitter.java 1422313 2012-12-15 18:53:41Z psteitz $
029     * @deprecated As of 3.1 (to be removed in 4.0).
030     * @since 2.0
031     */
032    @Deprecated
033    public class PolynomialFitter extends CurveFitter<PolynomialFunction.Parametric> {
034        /** Polynomial degree.
035         * @deprecated
036         */
037        @Deprecated
038        private final int degree;
039    
040        /**
041         * Simple constructor.
042         * <p>The polynomial fitter built this way are complete polynomials,
043         * ie. a n-degree polynomial has n+1 coefficients.</p>
044         *
045         * @param degree Maximal degree of the polynomial.
046         * @param optimizer Optimizer to use for the fitting.
047         * @deprecated Since 3.1 (to be removed in 4.0). Please use
048         * {@link #PolynomialFitter(DifferentiableMultivariateVectorOptimizer)} instead.
049         */
050        @Deprecated
051        public PolynomialFitter(int degree, final DifferentiableMultivariateVectorOptimizer optimizer) {
052            super(optimizer);
053            this.degree = degree;
054        }
055    
056        /**
057         * Simple constructor.
058         *
059         * @param optimizer Optimizer to use for the fitting.
060         * @since 3.1
061         */
062        public PolynomialFitter(DifferentiableMultivariateVectorOptimizer optimizer) {
063            super(optimizer);
064            degree = -1; // To avoid compilation error until the instance variable is removed.
065        }
066    
067        /**
068         * Get the polynomial fitting the weighted (x, y) points.
069         *
070         * @return the coefficients of the polynomial that best fits the observed points.
071         * @throws org.apache.commons.math3.exception.ConvergenceException
072         * if the algorithm failed to converge.
073         * @deprecated Since 3.1 (to be removed in 4.0). Please use {@link #fit(double[])} instead.
074         */
075        @Deprecated
076        public double[] fit() {
077            return fit(new PolynomialFunction.Parametric(), new double[degree + 1]);
078        }
079    
080        /**
081         * Get the coefficients of the polynomial fitting the weighted data points.
082         * The degree of the fitting polynomial is {@code guess.length - 1}.
083         *
084         * @param guess First guess for the coefficients. They must be sorted in
085         * increasing order of the polynomial's degree.
086         * @param maxEval Maximum number of evaluations of the polynomial.
087         * @return the coefficients of the polynomial that best fits the observed points.
088         * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if
089         * the number of evaluations exceeds {@code maxEval}.
090         * @throws org.apache.commons.math3.exception.ConvergenceException
091         * if the algorithm failed to converge.
092         * @since 3.1
093         */
094        public double[] fit(int maxEval, double[] guess) {
095            return fit(maxEval, new PolynomialFunction.Parametric(), guess);
096        }
097    
098        /**
099         * Get the coefficients of the polynomial fitting the weighted data points.
100         * The degree of the fitting polynomial is {@code guess.length - 1}.
101         *
102         * @param guess First guess for the coefficients. They must be sorted in
103         * increasing order of the polynomial's degree.
104         * @return the coefficients of the polynomial that best fits the observed points.
105         * @throws org.apache.commons.math3.exception.ConvergenceException
106         * if the algorithm failed to converge.
107         * @since 3.1
108         */
109        public double[] fit(double[] guess) {
110            return fit(new PolynomialFunction.Parametric(), guess);
111        }
112    }