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