commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1075054 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/interpolation/ main/java/org/apache/commons/math/analysis/polynomials/ main/java/org/apache/commons/math/optimization/fitting/ test/java/org/apache/com...
Date Sun, 27 Feb 2011 13:31:47 GMT
Author: erans
Date: Sun Feb 27 13:31:47 2011
New Revision: 1075054

URL: http://svn.apache.org/viewvc?rev=1075054&view=rev
Log:
MATH-534
Refactored "PolynomialFitter". Moved parametric function inner class to
"PolynomialFunction".
Adapted affected classes.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/fitting/PolynomialFitter.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java?rev=1075054&r1=1075053&r2=1075054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java
Sun Feb 27 13:31:47 2011
@@ -33,10 +33,8 @@ import org.apache.commons.math.analysis.
  */
 public class SmoothingPolynomialBicubicSplineInterpolator
     extends BicubicSplineInterpolator {
-
     /** Fitter for x. */
     private final PolynomialFitter xFitter;
-
     /** Fitter for y. */
     private final PolynomialFitter yFitter;
 
@@ -101,7 +99,7 @@ public class SmoothingPolynomialBicubicS
                 xFitter.addObservedPoint(1, xval[i], fval[i][j]);
             }
 
-            yPolyX[j] = xFitter.fit();
+            yPolyX[j] = new PolynomialFunction(xFitter.fit());
         }
 
         // For every knot (xval[i], yval[j]) of the grid, calculate corrected
@@ -123,7 +121,7 @@ public class SmoothingPolynomialBicubicS
                 yFitter.addObservedPoint(1, yval[j], fval_1[i][j]);
             }
 
-            xPolyY[i] = yFitter.fit();
+            xPolyY[i] = new PolynomialFunction(yFitter.fit());
         }
 
         // For every knot (xval[i], yval[j]) of the grid, calculate corrected

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java?rev=1075054&r1=1075053&r2=1075054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunction.java
Sun Feb 27 13:31:47 2011
@@ -23,6 +23,7 @@ import org.apache.commons.math.exception
 import org.apache.commons.math.exception.NoDataException;
 import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
 import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.analysis.ParametricUnivariateRealFunction;
 import org.apache.commons.math.util.FastMath;
 
 /**
@@ -124,7 +125,7 @@ public class PolynomialFunction implemen
             throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
         }
         double result = coefficients[n - 1];
-        for (int j = n -2; j >=0; j--) {
+        for (int j = n - 2; j >= 0; j--) {
             result = argument * result + coefficients[j];
         }
         return result;
@@ -281,7 +282,6 @@ public class PolynomialFunction implemen
                 return "0";
             }
         } else {
-            //         s.append(Double.toString(coefficients[0])); XXX
             s.append(toString(coefficients[0]));
         }
 
@@ -301,7 +301,6 @@ public class PolynomialFunction implemen
 
                 double absAi = FastMath.abs(coefficients[i]);
                 if ((absAi - 1) != 0) {
-                    //             s.append(Double.toString(absAi)); XXX
                     s.append(toString(absAi));
                     s.append(' ');
                 }
@@ -356,4 +355,25 @@ public class PolynomialFunction implemen
         }
         return true;
     }
+
+    /**
+     * Dedicated parametric polynomial class.
+     */
+    public static class Parametric implements ParametricUnivariateRealFunction {
+        /** {@inheritDoc} */
+        public double[] gradient(double x, double[] parameters) {
+            final double[] gradient = new double[parameters.length];
+            double xn = 1.0;
+            for (int i = 0; i < parameters.length; ++i) {
+                gradient[i] = xn;
+                xn *= x;
+            }
+            return gradient;
+        }
+
+        /** {@inheritDoc} */
+        public double value(final double x, final double[] parameters) {
+            return PolynomialFunction.evaluate(parameters, x);
+        }
+    }
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/fitting/PolynomialFitter.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/fitting/PolynomialFitter.java?rev=1075054&r1=1075053&r2=1075054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/fitting/PolynomialFitter.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/fitting/PolynomialFitter.java
Sun Feb 27 13:31:47 2011
@@ -18,7 +18,6 @@
 package org.apache.commons.math.optimization.fitting;
 
 import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
-import org.apache.commons.math.analysis.ParametricUnivariateRealFunction;
 import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
 
 /** This class implements a curve fitting specialized for polynomials.
@@ -29,79 +28,31 @@ import org.apache.commons.math.optimizat
  * @since 2.0
  */
 
-public class PolynomialFitter {
-    /** Fitter for the coefficients. */
-    private final CurveFitter fitter;
+public class PolynomialFitter extends CurveFitter {
     /** Polynomial degree. */
     private final int degree;
 
     /**
      * Simple constructor.
-     *
      * <p>The polynomial fitter built this way are complete polynomials,
      * ie. a n-degree polynomial has n+1 coefficients.</p>
-     * @param degree maximal degree of the polynomial
-     * @param optimizer optimizer to use for the fitting
+     *
+     * @param degree Maximal degree of the polynomial.
+     * @param optimizer Optimizer to use for the fitting.
      */
     public PolynomialFitter(int degree, final DifferentiableMultivariateVectorialOptimizer
optimizer) {
-        this.fitter = new CurveFitter(optimizer);
+        super(optimizer);
         this.degree = degree;
     }
 
     /**
-     * Add an observed weighted (x,y) point to the sample.
-     *
-     * @param weight weight of the observed point in the fit
-     * @param x abscissa of the point
-     * @param y observed value of the point at x, after fitting we should
-     * have P(x) as close as possible to this value
-     */
-    public void addObservedPoint(double weight, double x, double y) {
-        fitter.addObservedPoint(weight, x, y);
-    }
-
-    /**
-     * Remove all observations.
-     * @since 2.2
-     */
-    public void clearObservations() {
-        fitter.clearObservations();
-    }
-
-    /**
      * Get the polynomial fitting the weighted (x, y) points.
      *
-     * @return polynomial function best fitting the observed points
+     * @return the coefficients of the polynomial that best fits the observed points.
      * @throws org.apache.commons.math.exception.ConvergenceException
      * if the algorithm failed to converge.
      */
-    public PolynomialFunction fit() {
-        return new PolynomialFunction(fitter.fit(new ParametricPolynomial(), new double[degree
+ 1]));
-    }
-
-    /**
-     * Dedicated parametric polynomial class.
-     */
-    private static class ParametricPolynomial implements ParametricUnivariateRealFunction
{
-
-        /** {@inheritDoc} */
-        public double[] gradient(double x, double[] parameters) {
-            final double[] gradient = new double[parameters.length];
-            double xn = 1.0;
-            for (int i = 0; i < parameters.length; ++i) {
-                gradient[i] = xn;
-                xn *= x;
-            }
-            return gradient;
-        }
-
-        /** {@inheritDoc} */
-        public double value(final double x, final double[] parameters) {
-            double y = 0;
-            for (int i = parameters.length - 1; i >= 0; --i) {
-                y = y * x + parameters[i];
-            }
-            return y;
-        }
+    public double[] fit() {
+        return fit(new PolynomialFunction.Parametric(), new double[degree + 1]);
     }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java?rev=1075054&r1=1075053&r2=1075054&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/fitting/PolynomialFitterTest.java
Sun Feb 27 13:31:47 2011
@@ -17,9 +17,6 @@
 
 package org.apache.commons.math.optimization.fitting;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
 import java.util.Random;
 
 import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
@@ -28,7 +25,9 @@ import org.apache.commons.math.optimizat
 import org.apache.commons.math.optimization.general.GaussNewtonOptimizer;
 import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;
 import org.apache.commons.math.util.FastMath;
+
 import org.junit.Test;
+import org.junit.Assert;
 
 public class PolynomialFitterTest {
 
@@ -44,16 +43,14 @@ public class PolynomialFitterTest {
                 fitter.addObservedPoint(1.0, i, p.value(i));
             }
 
-            PolynomialFunction fitted = fitter.fit();
+            PolynomialFunction fitted = new PolynomialFunction(fitter.fit());
 
             for (double x = -1.0; x < 1.0; x += 0.01) {
                 double error = FastMath.abs(p.value(x) - fitted.value(x)) /
                                (1.0 + FastMath.abs(p.value(x)));
-                assertEquals(0.0, error, 1.0e-6);
+                Assert.assertEquals(0.0, error, 1.0e-6);
             }
-
         }
-
     }
 
     @Test
@@ -70,16 +67,16 @@ public class PolynomialFitterTest {
                                         p.value(x) + 0.1 * randomizer.nextGaussian());
             }
 
-            PolynomialFunction fitted = fitter.fit();
+            PolynomialFunction fitted = new PolynomialFunction(fitter.fit());
 
             for (double x = -1.0; x < 1.0; x += 0.01) {
                 double error = FastMath.abs(p.value(x) - fitted.value(x)) /
                               (1.0 + FastMath.abs(p.value(x)));
                 maxError = FastMath.max(maxError, error);
-                assertTrue(FastMath.abs(error) < 0.1);
+                Assert.assertTrue(FastMath.abs(error) < 0.1);
             }
         }
-        assertTrue(maxError > 0.01);
+        Assert.assertTrue(maxError > 0.01);
 
     }
 
@@ -115,13 +112,11 @@ public class PolynomialFitterTest {
 
             try {
                 fitter.fit();
-                assertTrue(solvable || (degree == 0));
+                Assert.assertTrue(solvable || (degree == 0));
             } catch(ConvergenceException e) {
-                assertTrue((! solvable) && (degree > 0));
+                Assert.assertTrue((! solvable) && (degree > 0));
             }
-
         }
-
     }
 
     private PolynomialFunction buildRandomPolynomial(int degree, Random randomizer) {
@@ -131,5 +126,4 @@ public class PolynomialFitterTest {
         }
         return new PolynomialFunction(coefficients);
     }
-
 }



Mime
View raw message