From 6525baf1941d083b931f9f31f4bde8daffeaea5c Mon Sep 17 00:00:00 2001
From: Gilles Sadowski
Date: Wed, 6 Jun 2012 22:33:42 +0000
Subject: [PATCH] MATH-800 Deprecated "PolynomialFitter" and adapted unit test
to use "CurveFitter" directly.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1347184 13f79535-47bb-0310-9956-ffa450edef68
---
.../fitting/PolynomialFitter.java | 8 ++++-
.../fitting/PolynomialFitterTest.java | 33 ++++++++++++-------
2 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/src/main/java/org/apache/commons/math3/optimization/fitting/PolynomialFitter.java b/src/main/java/org/apache/commons/math3/optimization/fitting/PolynomialFitter.java
index f5fd4451c..20b339619 100644
--- a/src/main/java/org/apache/commons/math3/optimization/fitting/PolynomialFitter.java
+++ b/src/main/java/org/apache/commons/math3/optimization/fitting/PolynomialFitter.java
@@ -26,8 +26,14 @@ import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOpt
* searched by a least square estimator.
* @version $Id$
* @since 2.0
+ *
+ * @deprecated Since 3.1 (to be removed in 4.0, see MATH-800).
+ * Please use {@link CurveFitter} directly, by passing an instance of
+ * {@link org.apache.commons.math3.analysis.polynomials.PolynomialFunction.Parametric PolynomialFunction.Parametric}
+ * as an argument to the
+ * {@link CurveFitter#fit(int,org.apache.commons.math3.analysis.ParametricUnivariateFunction,double[]) fit}
+ * method.
*/
-
public class PolynomialFitter extends CurveFitter {
/** Polynomial degree. */
private final int degree;
diff --git a/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java b/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java
index 97566672d..d7f8f6c69 100644
--- a/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java
+++ b/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java
@@ -24,11 +24,16 @@ import org.apache.commons.math3.exception.ConvergenceException;
import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer;
import org.apache.commons.math3.optimization.general.GaussNewtonOptimizer;
import org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer;
+import org.apache.commons.math3.optimization.SimpleVectorValueChecker;
import org.apache.commons.math3.util.FastMath;
import org.junit.Test;
import org.junit.Assert;
+/**
+ * Test for class {@link CurveFitter} where the function to fit is a
+ * polynomial.
+ */
public class PolynomialFitterTest {
@Test
@@ -37,13 +42,15 @@ public class PolynomialFitterTest {
for (int degree = 1; degree < 10; ++degree) {
PolynomialFunction p = buildRandomPolynomial(degree, randomizer);
- PolynomialFitter fitter =
- new PolynomialFitter(degree, new LevenbergMarquardtOptimizer());
+ CurveFitter fitter = new CurveFitter(new LevenbergMarquardtOptimizer());
for (int i = 0; i <= degree; ++i) {
fitter.addObservedPoint(1.0, i, p.value(i));
}
- PolynomialFunction fitted = new PolynomialFunction(fitter.fit());
+ final double[] init = new double[degree + 1];
+ PolynomialFunction fitted = new PolynomialFunction(fitter.fit(Integer.MAX_VALUE,
+ new PolynomialFunction.Parametric(),
+ init));
for (double x = -1.0; x < 1.0; x += 0.01) {
double error = FastMath.abs(p.value(x) - fitted.value(x)) /
@@ -60,14 +67,16 @@ public class PolynomialFitterTest {
for (int degree = 0; degree < 10; ++degree) {
PolynomialFunction p = buildRandomPolynomial(degree, randomizer);
- PolynomialFitter fitter =
- new PolynomialFitter(degree, new LevenbergMarquardtOptimizer());
+ CurveFitter fitter = new CurveFitter(new LevenbergMarquardtOptimizer());
for (double x = -1.0; x < 1.0; x += 0.01) {
fitter.addObservedPoint(1.0, x,
p.value(x) + 0.1 * randomizer.nextGaussian());
}
- PolynomialFunction fitted = new PolynomialFunction(fitter.fit());
+ final double[] init = new double[degree + 1];
+ PolynomialFunction fitted = new PolynomialFunction(fitter.fit(Integer.MAX_VALUE,
+ new PolynomialFunction.Parametric(),
+ init));
for (double x = -1.0; x < 1.0; x += 0.01) {
double error = FastMath.abs(p.value(x) - fitted.value(x)) /
@@ -77,7 +86,6 @@ public class PolynomialFitterTest {
}
}
Assert.assertTrue(maxError > 0.01);
-
}
@Test
@@ -89,9 +97,7 @@ public class PolynomialFitterTest {
@Test
public void testRedundantUnsolvable() {
// Gauss-Newton should not be able to solve redundant information
- DifferentiableMultivariateVectorOptimizer optimizer =
- new GaussNewtonOptimizer(true);
- checkUnsolvableProblem(optimizer, false);
+ checkUnsolvableProblem(new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-15, 1e-15)), false);
}
private void checkUnsolvableProblem(DifferentiableMultivariateVectorOptimizer optimizer,
@@ -100,7 +106,7 @@ public class PolynomialFitterTest {
for (int degree = 0; degree < 10; ++degree) {
PolynomialFunction p = buildRandomPolynomial(degree, randomizer);
- PolynomialFitter fitter = new PolynomialFitter(degree, optimizer);
+ CurveFitter fitter = new CurveFitter(optimizer);
// reusing the same point over and over again does not bring
// information, the problem cannot be solved in this case for
@@ -111,7 +117,10 @@ public class PolynomialFitterTest {
}
try {
- fitter.fit();
+ final double[] init = new double[degree + 1];
+ fitter.fit(Integer.MAX_VALUE,
+ new PolynomialFunction.Parametric(),
+ init);
Assert.assertTrue(solvable || (degree == 0));
} catch(ConvergenceException e) {
Assert.assertTrue((! solvable) && (degree > 0));