View Javadoc
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  
19  import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
20  
21  /**
22   * Fits points to a {@link
23   * org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction.Parametric polynomial}
24   * function.
25   * <br>
26   * The size of the {@link #withStartPoint(double[]) initial guess} array defines the
27   * degree of the polynomial to be fitted.
28   * They must be sorted in increasing order of the polynomial's degree.
29   * The optimal values of the coefficients will be returned in the same order.
30   *
31   * @since 3.3
32   */
33  public final class PolynomialCurveFitter extends SimpleCurveFitter {
34      /** Parametric function to be fitted. */
35      private static final PolynomialFunction.Parametric FUNCTION = new PolynomialFunction.Parametric();
36  
37      /**
38       * Constructor used by the factory methods.
39       *
40       * @param initialGuess Initial guess.
41       * @param maxIter Maximum number of iterations of the optimization algorithm.
42       */
43      private PolynomialCurveFitter(double[] initialGuess,
44                                    int maxIter) {
45          super(FUNCTION, initialGuess, null, maxIter);
46      }
47  
48      /**
49       * Creates a default curve fitter.
50       * Zero will be used as initial guess for the coefficients, and the maximum
51       * number of iterations of the optimization algorithm is set to
52       * {@link Integer#MAX_VALUE}.
53       *
54       * @param degree Degree of the polynomial to be fitted.
55       * @return a curve fitter.
56       *
57       * @see #withStartPoint(double[])
58       * @see #withMaxIterations(int)
59       */
60      public static PolynomialCurveFitter create(int degree) {
61          return new PolynomialCurveFitter(new double[degree + 1], Integer.MAX_VALUE);
62      }
63  }