PolynomialCurveFitter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.fitting;

  18. import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;

  19. /**
  20.  * Fits points to a {@link
  21.  * org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction.Parametric polynomial}
  22.  * function.
  23.  * <br>
  24.  * The size of the {@link #withStartPoint(double[]) initial guess} array defines the
  25.  * degree of the polynomial to be fitted.
  26.  * They must be sorted in increasing order of the polynomial's degree.
  27.  * The optimal values of the coefficients will be returned in the same order.
  28.  *
  29.  * @since 3.3
  30.  */
  31. public final class PolynomialCurveFitter extends SimpleCurveFitter {
  32.     /** Parametric function to be fitted. */
  33.     private static final PolynomialFunction.Parametric FUNCTION = new PolynomialFunction.Parametric();

  34.     /**
  35.      * Constructor used by the factory methods.
  36.      *
  37.      * @param initialGuess Initial guess.
  38.      * @param maxIter Maximum number of iterations of the optimization algorithm.
  39.      */
  40.     private PolynomialCurveFitter(double[] initialGuess,
  41.                                   int maxIter) {
  42.         super(FUNCTION, initialGuess, null, maxIter);
  43.     }

  44.     /**
  45.      * Creates a default curve fitter.
  46.      * Zero will be used as initial guess for the coefficients, and the maximum
  47.      * number of iterations of the optimization algorithm is set to
  48.      * {@link Integer#MAX_VALUE}.
  49.      *
  50.      * @param degree Degree of the polynomial to be fitted.
  51.      * @return a curve fitter.
  52.      *
  53.      * @see #withStartPoint(double[])
  54.      * @see #withMaxIterations(int)
  55.      */
  56.     public static PolynomialCurveFitter create(int degree) {
  57.         return new PolynomialCurveFitter(new double[degree + 1], Integer.MAX_VALUE);
  58.     }
  59. }