PolynomialFitter.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.math3.optimization.fitting;

  18. import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
  19. import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer;

  20. /**
  21.  * Polynomial fitting is a very simple case of {@link CurveFitter curve fitting}.
  22.  * The estimated coefficients are the polynomial coefficients (see the
  23.  * {@link #fit(double[]) fit} method).
  24.  *
  25.  * @deprecated As of 3.1 (to be removed in 4.0).
  26.  * @since 2.0
  27.  */
  28. @Deprecated
  29. public class PolynomialFitter extends CurveFitter<PolynomialFunction.Parametric> {
  30.     /** Polynomial degree.
  31.      * @deprecated
  32.      */
  33.     @Deprecated
  34.     private final int degree;

  35.     /**
  36.      * Simple constructor.
  37.      * <p>The polynomial fitter built this way are complete polynomials,
  38.      * ie. a n-degree polynomial has n+1 coefficients.</p>
  39.      *
  40.      * @param degree Maximal degree of the polynomial.
  41.      * @param optimizer Optimizer to use for the fitting.
  42.      * @deprecated Since 3.1 (to be removed in 4.0). Please use
  43.      * {@link #PolynomialFitter(DifferentiableMultivariateVectorOptimizer)} instead.
  44.      */
  45.     @Deprecated
  46.     public PolynomialFitter(int degree, final DifferentiableMultivariateVectorOptimizer optimizer) {
  47.         super(optimizer);
  48.         this.degree = degree;
  49.     }

  50.     /**
  51.      * Simple constructor.
  52.      *
  53.      * @param optimizer Optimizer to use for the fitting.
  54.      * @since 3.1
  55.      */
  56.     public PolynomialFitter(DifferentiableMultivariateVectorOptimizer optimizer) {
  57.         super(optimizer);
  58.         degree = -1; // To avoid compilation error until the instance variable is removed.
  59.     }

  60.     /**
  61.      * Get the polynomial fitting the weighted (x, y) points.
  62.      *
  63.      * @return the coefficients of the polynomial that best fits the observed points.
  64.      * @throws org.apache.commons.math3.exception.ConvergenceException
  65.      * if the algorithm failed to converge.
  66.      * @deprecated Since 3.1 (to be removed in 4.0). Please use {@link #fit(double[])} instead.
  67.      */
  68.     @Deprecated
  69.     public double[] fit() {
  70.         return fit(new PolynomialFunction.Parametric(), new double[degree + 1]);
  71.     }

  72.     /**
  73.      * Get the coefficients of the polynomial fitting the weighted data points.
  74.      * The degree of the fitting polynomial is {@code guess.length - 1}.
  75.      *
  76.      * @param guess First guess for the coefficients. They must be sorted in
  77.      * increasing order of the polynomial's degree.
  78.      * @param maxEval Maximum number of evaluations of the polynomial.
  79.      * @return the coefficients of the polynomial that best fits the observed points.
  80.      * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if
  81.      * the number of evaluations exceeds {@code maxEval}.
  82.      * @throws org.apache.commons.math3.exception.ConvergenceException
  83.      * if the algorithm failed to converge.
  84.      * @since 3.1
  85.      */
  86.     public double[] fit(int maxEval, double[] guess) {
  87.         return fit(maxEval, new PolynomialFunction.Parametric(), guess);
  88.     }

  89.     /**
  90.      * Get the coefficients of the polynomial fitting the weighted data points.
  91.      * The degree of the fitting polynomial is {@code guess.length - 1}.
  92.      *
  93.      * @param guess First guess for the coefficients. They must be sorted in
  94.      * increasing order of the polynomial's degree.
  95.      * @return the coefficients of the polynomial that best fits the observed points.
  96.      * @throws org.apache.commons.math3.exception.ConvergenceException
  97.      * if the algorithm failed to converge.
  98.      * @since 3.1
  99.      */
  100.     public double[] fit(double[] guess) {
  101.         return fit(new PolynomialFunction.Parametric(), guess);
  102.     }
  103. }