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 */
017package org.apache.commons.math4.legacy.fitting;
018
019import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
020
021/**
022 * Fits points to a {@link
023 * org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction.Parametric polynomial}
024 * function.
025 * <br>
026 * The size of the {@link #withStartPoint(double[]) initial guess} array defines the
027 * degree of the polynomial to be fitted.
028 * They must be sorted in increasing order of the polynomial's degree.
029 * The optimal values of the coefficients will be returned in the same order.
030 *
031 * @since 3.3
032 */
033public final class PolynomialCurveFitter extends SimpleCurveFitter {
034    /** Parametric function to be fitted. */
035    private static final PolynomialFunction.Parametric FUNCTION = new PolynomialFunction.Parametric();
036
037    /**
038     * Constructor used by the factory methods.
039     *
040     * @param initialGuess Initial guess.
041     * @param maxIter Maximum number of iterations of the optimization algorithm.
042     */
043    private PolynomialCurveFitter(double[] initialGuess,
044                                  int maxIter) {
045        super(FUNCTION, initialGuess, null, maxIter);
046    }
047
048    /**
049     * Creates a default curve fitter.
050     * Zero will be used as initial guess for the coefficients, and the maximum
051     * number of iterations of the optimization algorithm is set to
052     * {@link Integer#MAX_VALUE}.
053     *
054     * @param degree Degree of the polynomial to be fitted.
055     * @return a curve fitter.
056     *
057     * @see #withStartPoint(double[])
058     * @see #withMaxIterations(int)
059     */
060    public static PolynomialCurveFitter create(int degree) {
061        return new PolynomialCurveFitter(new double[degree + 1], Integer.MAX_VALUE);
062    }
063}