commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1039083 [4/4] - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/ main/java/org/apache/commons/math/analysis/polynomials/ main/java/org/apache/commons/math/analysis/solvers/ main/java/org/apache/commons/math/di...
Date Thu, 25 Nov 2010 16:22:01 GMT
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/ode/nonstiff/HighamHall54IntegratorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/ode/nonstiff/HighamHall54IntegratorTest.java?rev=1039083&r1=1039082&r2=1039083&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/ode/nonstiff/HighamHall54IntegratorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/ode/nonstiff/HighamHall54IntegratorTest.java
Thu Nov 25 16:22:00 2010
@@ -20,8 +20,9 @@ package org.apache.commons.math.ode.nons
 import junit.framework.TestCase;
 
 import org.apache.commons.math.ConvergenceException;
-import org.apache.commons.math.exception.MathUserException;
 import org.apache.commons.math.exception.util.LocalizedFormats;
+import org.apache.commons.math.exception.TooManyEvaluationsException;
+import org.apache.commons.math.exception.MathUserException;
 import org.apache.commons.math.ode.FirstOrderDifferentialEquations;
 import org.apache.commons.math.ode.FirstOrderIntegrator;
 import org.apache.commons.math.ode.IntegratorException;
@@ -268,9 +269,8 @@ public class HighamHall54IntegratorTest
                       pb.getInitialTime(), pb.getInitialState(),
                       pb.getFinalTime(), new double[pb.getDimension()]);
       fail("an exception should have been thrown");
-    } catch (IntegratorException ie) {
-       assertTrue(ie.getCause() != null);
-       assertTrue(ie.getCause() instanceof ConvergenceException);
+    } catch (TooManyEvaluationsException tmee) {
+        // Expected.
     }
 
 }

Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleScalar.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleScalar.java?rev=1039083&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleScalar.java
(added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleScalar.java
Thu Nov 25 16:22:00 2010
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.optimization.general;
+
+import java.awt.geom.Point2D;
+import java.util.ArrayList;
+import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
+import org.apache.commons.math.analysis.MultivariateRealFunction;
+import org.apache.commons.math.analysis.MultivariateVectorialFunction;
+
+/**
+ * Class used in the tests.
+ */
+class CircleScalar implements DifferentiableMultivariateRealFunction {
+    private ArrayList<Point2D.Double> points;
+
+    public CircleScalar() {
+        points  = new ArrayList<Point2D.Double>();
+    }
+
+    public void addPoint(double px, double py) {
+        points.add(new Point2D.Double(px, py));
+    }
+
+    public double getRadius(Point2D.Double center) {
+        double r = 0;
+        for (Point2D.Double point : points) {
+            r += point.distance(center);
+        }
+        return r / points.size();
+    }
+
+    private double[] gradient(double[] point) {
+        // optimal radius
+        Point2D.Double center = new Point2D.Double(point[0], point[1]);
+        double radius = getRadius(center);
+
+        // gradient of the sum of squared residuals
+        double dJdX = 0;
+        double dJdY = 0;
+        for (Point2D.Double pk : points) {
+            double dk = pk.distance(center);
+            dJdX += (center.x - pk.x) * (dk - radius) / dk;
+            dJdY += (center.y - pk.y) * (dk - radius) / dk;
+        }
+        dJdX *= 2;
+        dJdY *= 2;
+
+        return new double[] { dJdX, dJdY };
+    }
+
+    public double value(double[] variables)  {
+        Point2D.Double center = new Point2D.Double(variables[0], variables[1]);
+        double radius = getRadius(center);
+
+        double sum = 0;
+        for (Point2D.Double point : points) {
+            double di = point.distance(center) - radius;
+            sum += di * di;
+        }
+
+        return sum;
+    }
+
+    public MultivariateVectorialFunction gradient() {
+        return new MultivariateVectorialFunction() {
+            private static final long serialVersionUID = 3174909643301201710L;
+            public double[] value(double[] point) {
+                return gradient(point);
+            }
+        };
+    }
+
+    public MultivariateRealFunction partialDerivative(final int k) {
+        return new MultivariateRealFunction() {
+            private static final long serialVersionUID = 3073956364104833888L;
+            public double value(double[] point) {
+                return gradient(point)[k];
+            }
+        };
+    }
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleScalar.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleVectorial.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleVectorial.java?rev=1039083&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleVectorial.java
(added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleVectorial.java
Thu Nov 25 16:22:00 2010
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.optimization.general;
+
+import java.awt.geom.Point2D;
+import java.util.ArrayList;
+import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
+import org.apache.commons.math.analysis.MultivariateMatrixFunction;
+
+/**
+ * Class used in the tests.
+ */
+class CircleVectorial implements DifferentiableMultivariateVectorialFunction {
+    private ArrayList<Point2D.Double> points;
+
+    public CircleVectorial() {
+        points  = new ArrayList<Point2D.Double>();
+    }
+
+    public void addPoint(double px, double py) {
+        points.add(new Point2D.Double(px, py));
+    }
+
+    public int getN() {
+        return points.size();
+    }
+
+    public double getRadius(Point2D.Double center) {
+        double r = 0;
+        for (Point2D.Double point : points) {
+            r += point.distance(center);
+        }
+        return r / points.size();
+    }
+
+    private double[][] jacobian(double[] point) {
+        int n = points.size();
+        Point2D.Double center = new Point2D.Double(point[0], point[1]);
+
+        // gradient of the optimal radius
+        double dRdX = 0;
+        double dRdY = 0;
+        for (Point2D.Double pk : points) {
+            double dk = pk.distance(center);
+            dRdX += (center.x - pk.x) / dk;
+            dRdY += (center.y - pk.y) / dk;
+        }
+        dRdX /= n;
+        dRdY /= n;
+
+        // jacobian of the radius residuals
+        double[][] jacobian = new double[n][2];
+        for (int i = 0; i < n; ++i) {
+            Point2D.Double pi = points.get(i);
+            double di   = pi.distance(center);
+            jacobian[i][0] = (center.x - pi.x) / di - dRdX;
+            jacobian[i][1] = (center.y - pi.y) / di - dRdY;
+        }
+
+        return jacobian;
+    }
+
+    public double[] value(double[] variables) {
+        Point2D.Double center = new Point2D.Double(variables[0], variables[1]);
+        double radius = getRadius(center);
+
+        double[] residuals = new double[points.size()];
+        for (int i = 0; i < residuals.length; ++i) {
+            residuals[i] = points.get(i).distance(center) - radius;
+        }
+
+        return residuals;
+    }
+
+    public MultivariateMatrixFunction jacobian() {
+        return new MultivariateMatrixFunction() {
+            private static final long serialVersionUID = -4340046230875165095L;
+            public double[][] value(double[] point) {
+                return jacobian(point);
+            }
+        };
+    }
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/CircleVectorial.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java?rev=1039083&r1=1039082&r2=1039083&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizerTest.java
Thu Nov 25 16:22:00 2010
@@ -368,7 +368,7 @@ extends TestCase {
     }
 
     public void testMaxEvaluations() throws Exception {
-        Circle circle = new Circle();
+        CircleVectorial circle = new CircleVectorial();
         circle.addPoint( 30.0,  68.0);
         circle.addPoint( 50.0,  -6.0);
         circle.addPoint(110.0, -20.0);
@@ -388,7 +388,7 @@ extends TestCase {
     }
 
     public void testCircleFitting() throws MathUserException {
-        Circle circle = new Circle();
+        CircleVectorial circle = new CircleVectorial();
         circle.addPoint( 30.0,  68.0);
         circle.addPoint( 50.0,  -6.0);
         circle.addPoint(110.0, -20.0);
@@ -409,7 +409,7 @@ extends TestCase {
     }
 
     public void testCircleFittingBadInit() throws MathUserException {
-        Circle circle = new Circle();
+        CircleVectorial circle = new CircleVectorial();
         double[][] points = new double[][] {
                 {-0.312967,  0.072366}, {-0.339248,  0.132965}, {-0.379780,  0.202724},
                 {-0.390426,  0.260487}, {-0.361212,  0.328325}, {-0.346039,  0.392619},
@@ -488,86 +488,5 @@ extends TestCase {
                 }
             };
         }
-
     }
-
-    private static class Circle implements DifferentiableMultivariateVectorialFunction, Serializable
{
-
-        private static final long serialVersionUID = -7165774454925027042L;
-        private ArrayList<Point2D.Double> points;
-
-        public Circle() {
-            points  = new ArrayList<Point2D.Double>();
-        }
-
-        public void addPoint(double px, double py) {
-            points.add(new Point2D.Double(px, py));
-        }
-
-        public int getN() {
-            return points.size();
-        }
-
-        public double getRadius(Point2D.Double center) {
-            double r = 0;
-            for (Point2D.Double point : points) {
-                r += point.distance(center);
-            }
-            return r / points.size();
-        }
-
-        private double[][] jacobian(double[] variables) {
-
-            int n = points.size();
-            Point2D.Double center = new Point2D.Double(variables[0], variables[1]);
-
-            // gradient of the optimal radius
-            double dRdX = 0;
-            double dRdY = 0;
-            for (Point2D.Double pk : points) {
-                double dk = pk.distance(center);
-                dRdX += (center.x - pk.x) / dk;
-                dRdY += (center.y - pk.y) / dk;
-            }
-            dRdX /= n;
-            dRdY /= n;
-
-            // jacobian of the radius residuals
-            double[][] jacobian = new double[n][2];
-            for (int i = 0; i < n; ++i) {
-                Point2D.Double pi = points.get(i);
-                double di   = pi.distance(center);
-                jacobian[i][0] = (center.x - pi.x) / di - dRdX;
-                jacobian[i][1] = (center.y - pi.y) / di - dRdY;
-           }
-
-            return jacobian;
-
-        }
-
-        public double[] value(double[] variables) {
-
-            Point2D.Double center = new Point2D.Double(variables[0], variables[1]);
-            double radius = getRadius(center);
-
-            double[] residuals = new double[points.size()];
-            for (int i = 0; i < residuals.length; ++i) {
-                residuals[i] = points.get(i).distance(center) - radius;
-            }
-
-            return residuals;
-
-        }
-
-        public MultivariateMatrixFunction jacobian() {
-            return new MultivariateMatrixFunction() {
-                private static final long serialVersionUID = -4340046230875165095L;
-                public double[][] value(double[] point) {
-                    return jacobian(point);
-                }
-            };
-        }
-
-    }
-
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java?rev=1039083&r1=1039082&r2=1039083&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java
Thu Nov 25 16:22:00 2010
@@ -25,20 +25,19 @@ import java.util.List;
 
 import junit.framework.TestCase;
 
-import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
-import org.apache.commons.math.analysis.MultivariateMatrixFunction;
+import org.apache.commons.math.exception.SingularMatrixException;
 import org.apache.commons.math.exception.ConvergenceException;
 import org.apache.commons.math.exception.DimensionMismatchException;
-import org.apache.commons.math.exception.MathUserException;
-import org.apache.commons.math.exception.NumberIsTooSmallException;
-import org.apache.commons.math.exception.SingularMatrixException;
 import org.apache.commons.math.exception.TooManyEvaluationsException;
+import org.apache.commons.math.exception.NumberIsTooSmallException;
+import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
+import org.apache.commons.math.analysis.MultivariateMatrixFunction;
 import org.apache.commons.math.linear.BlockRealMatrix;
 import org.apache.commons.math.linear.RealMatrix;
 import org.apache.commons.math.optimization.SimpleVectorialValueChecker;
 import org.apache.commons.math.optimization.VectorialPointValuePair;
-import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.FastMath;
 
 /**
  * <p>Some of the unit tests are re-implementations of the MINPACK <a
@@ -109,7 +108,7 @@ public class LevenbergMarquardtOptimizer
         super(name);
     }
 
-    public void testTrivial() throws MathUserException {
+    public void testTrivial() {
         LinearProblem problem =
             new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
         LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
@@ -126,7 +125,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals(3.0, optimum.getValue()[0], 1.0e-10);
     }
 
-    public void testQRColumnsPermutation() throws MathUserException {
+    public void testQRColumnsPermutation() {
 
         LinearProblem problem =
             new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 }
},
@@ -143,7 +142,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals(1.0, optimum.getValue()[2], 1.0e-10);
     }
 
-    public void testNoDependency() throws MathUserException {
+    public void testNoDependency() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 2, 0, 0, 0, 0, 0 },
                 { 0, 2, 0, 0, 0, 0 },
@@ -162,7 +161,7 @@ public class LevenbergMarquardtOptimizer
         }
     }
 
-    public void testOneSet() throws MathUserException {
+    public void testOneSet() {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  1,  0, 0 },
@@ -178,7 +177,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals(3.0, optimum.getPoint()[2], 1.0e-10);
     }
 
-    public void testTwoSets() throws MathUserException {
+    public void testTwoSets() {
         double epsilon = 1.0e-7;
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  2,  1,   0,  4,       0, 0 },
@@ -202,7 +201,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10);
     }
 
-    public void testNonInvertible() throws MathUserException {
+    public void testNonInvertible() {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  1, 2, -3 },
@@ -221,7 +220,7 @@ public class LevenbergMarquardtOptimizer
         }
     }
 
-    public void testIllConditioned() throws MathUserException {
+    public void testIllConditioned() {
         LinearProblem problem1 = new LinearProblem(new double[][] {
                 { 10.0, 7.0,  8.0,  7.0 },
                 {  7.0, 5.0,  6.0,  5.0 },
@@ -254,7 +253,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals( 22.0, optimum2.getPoint()[3], 1.0e-8);
     }
 
-    public void testMoreEstimatedParametersSimple() throws MathUserException {
+    public void testMoreEstimatedParametersSimple() {
 
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 3.0, 2.0,  0.0, 0.0 },
@@ -268,7 +267,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals(0, optimizer.getRMS(), 1.0e-10);
     }
 
-    public void testMoreEstimatedParametersUnsorted() throws MathUserException {
+    public void testMoreEstimatedParametersUnsorted() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0, 1.0,  0.0,  0.0, 0.0,  0.0 },
                 { 0.0, 0.0,  1.0,  1.0, 1.0,  0.0 },
@@ -288,7 +287,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals(6.0, optimum.getPointRef()[5], 1.0e-10);
     }
 
-    public void testRedundantEquations() throws MathUserException {
+    public void testRedundantEquations() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0,  1.0 },
                 { 1.0, -1.0 },
@@ -304,7 +303,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals(1.0, optimum.getPointRef()[1], 1.0e-10);
     }
 
-    public void testInconsistentEquations() throws MathUserException {
+    public void testInconsistentEquations() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0,  1.0 },
                 { 1.0, -1.0 },
@@ -316,7 +315,7 @@ public class LevenbergMarquardtOptimizer
         assertTrue(optimizer.getRMS() > 0.1);
     }
 
-    public void testInconsistentSizes() throws MathUserException {
+    public void testInconsistentSizes() {
         LinearProblem problem =
             new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1
});
         LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
@@ -346,8 +345,8 @@ public class LevenbergMarquardtOptimizer
         }
     }
 
-    public void testControlParameters() throws MathUserException {
-        Circle circle = new Circle();
+    public void testControlParameters() {
+        CircleVectorial circle = new CircleVectorial();
         circle.addPoint( 30.0,  68.0);
         circle.addPoint( 50.0,  -6.0);
         circle.addPoint(110.0, -20.0);
@@ -363,7 +362,7 @@ public class LevenbergMarquardtOptimizer
     private void checkEstimate(DifferentiableMultivariateVectorialFunction problem,
                                double initialStepBoundFactor, int maxCostEval,
                                double costRelativeTolerance, double parRelativeTolerance,
-                               double orthoTolerance, boolean shouldFail) throws MathUserException
{
+                               double orthoTolerance, boolean shouldFail) {
         try {
             LevenbergMarquardtOptimizer optimizer
                 = new LevenbergMarquardtOptimizer(initialStepBoundFactor,
@@ -382,8 +381,8 @@ public class LevenbergMarquardtOptimizer
         }
     }
 
-    public void testCircleFitting() throws MathUserException {
-        Circle circle = new Circle();
+    public void testCircleFitting() {
+        CircleVectorial circle = new CircleVectorial();
         circle.addPoint( 30.0,  68.0);
         circle.addPoint( 50.0,  -6.0);
         circle.addPoint(110.0, -20.0);
@@ -430,8 +429,8 @@ public class LevenbergMarquardtOptimizer
         assertEquals(0.004, errors[1], 0.001);
     }
 
-    public void testCircleFittingBadInit() throws MathUserException {
-        Circle circle = new Circle();
+    public void testCircleFittingBadInit() {
+        CircleVectorial circle = new CircleVectorial();
         double[][] points = new double[][] {
                 {-0.312967,  0.072366}, {-0.339248,  0.132965}, {-0.379780,  0.202724},
                 {-0.390426,  0.260487}, {-0.361212,  0.328325}, {-0.346039,  0.392619},
@@ -483,7 +482,7 @@ public class LevenbergMarquardtOptimizer
         assertEquals( 0.2075001, center.y,      1.0e-6);
     }
 
-    public void testMath199() throws MathUserException {
+    public void testMath199() {
         try {
             QuadraticProblem problem = new QuadraticProblem();
             problem.addPoint (0, -3.182591015485607);
@@ -527,83 +526,6 @@ public class LevenbergMarquardtOptimizer
         }
     }
 
-    private static class Circle implements DifferentiableMultivariateVectorialFunction, Serializable
{
-
-        private static final long serialVersionUID = -4711170319243817874L;
-
-        private ArrayList<Point2D.Double> points;
-
-        public Circle() {
-            points  = new ArrayList<Point2D.Double>();
-        }
-
-        public void addPoint(double px, double py) {
-            points.add(new Point2D.Double(px, py));
-        }
-
-        public int getN() {
-            return points.size();
-        }
-
-        public double getRadius(Point2D.Double center) {
-            double r = 0;
-            for (Point2D.Double point : points) {
-                r += point.distance(center);
-            }
-            return r / points.size();
-        }
-
-        private double[][] jacobian(double[] point) {
-
-            int n = points.size();
-            Point2D.Double center = new Point2D.Double(point[0], point[1]);
-
-            // gradient of the optimal radius
-            double dRdX = 0;
-            double dRdY = 0;
-            for (Point2D.Double pk : points) {
-                double dk = pk.distance(center);
-                dRdX += (center.x - pk.x) / dk;
-                dRdY += (center.y - pk.y) / dk;
-            }
-            dRdX /= n;
-            dRdY /= n;
-
-            // jacobian of the radius residuals
-            double[][] jacobian = new double[n][2];
-            for (int i = 0; i < n; ++i) {
-                Point2D.Double pi = points.get(i);
-                double di   = pi.distance(center);
-                jacobian[i][0] = (center.x - pi.x) / di - dRdX;
-                jacobian[i][1] = (center.y - pi.y) / di - dRdY;
-            }
-
-            return jacobian;
-        }
-
-        public double[] value(double[] variables) {
-
-            Point2D.Double center = new Point2D.Double(variables[0], variables[1]);
-            double radius = getRadius(center);
-
-            double[] residuals = new double[points.size()];
-            for (int i = 0; i < residuals.length; ++i) {
-                residuals[i] = points.get(i).distance(center) - radius;
-            }
-
-            return residuals;
-        }
-
-        public MultivariateMatrixFunction jacobian() {
-            return new MultivariateMatrixFunction() {
-                private static final long serialVersionUID = -4340046230875165095L;
-                public double[][] value(double[] point) {
-                    return jacobian(point);
-                }
-            };
-        }
-    }
-
     private static class QuadraticProblem implements DifferentiableMultivariateVectorialFunction,
Serializable {
 
         private static final long serialVersionUID = 7072187082052755854L;

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java?rev=1039083&r1=1039082&r2=1039083&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizerTest.java
Thu Nov 25 16:22:00 2010
@@ -21,18 +21,18 @@ import java.awt.geom.Point2D;
 import java.io.Serializable;
 import java.util.ArrayList;
 
-import junit.framework.TestCase;
-
 import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
 import org.apache.commons.math.analysis.MultivariateRealFunction;
 import org.apache.commons.math.analysis.MultivariateVectorialFunction;
+import org.apache.commons.math.analysis.solvers.UnivariateRealSolver;
 import org.apache.commons.math.analysis.solvers.BrentSolver;
-import org.apache.commons.math.exception.MathUserException;
 import org.apache.commons.math.linear.BlockRealMatrix;
 import org.apache.commons.math.linear.RealMatrix;
 import org.apache.commons.math.optimization.GoalType;
 import org.apache.commons.math.optimization.RealPointValuePair;
 import org.apache.commons.math.optimization.SimpleScalarValueChecker;
+import org.junit.Assert;
+import org.junit.Test;
 
 /**
  * <p>Some of the unit tests are re-implementations of the MINPACK <a
@@ -96,14 +96,9 @@ import org.apache.commons.math.optimizat
  * @author Jorge J. More (original fortran minpack tests)
  * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation)
  */
-public class NonLinearConjugateGradientOptimizerTest
-extends TestCase {
-
-    public NonLinearConjugateGradientOptimizerTest(String name) {
-        super(name);
-    }
-
-    public void testTrivial() throws MathUserException {
+public class NonLinearConjugateGradientOptimizerTest {
+    @Test
+    public void testTrivial() {
         LinearProblem problem =
             new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
         NonLinearConjugateGradientOptimizer optimizer =
@@ -112,12 +107,12 @@ extends TestCase {
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0 });
-        assertEquals(1.5, optimum.getPoint()[0], 1.0e-10);
-        assertEquals(0.0, optimum.getValue(), 1.0e-10);
+        Assert.assertEquals(1.5, optimum.getPoint()[0], 1.0e-10);
+        Assert.assertEquals(0.0, optimum.getValue(), 1.0e-10);
     }
 
-    public void testColumnsPermutation() throws MathUserException {
-
+    @Test
+    public void testColumnsPermutation() {
         LinearProblem problem =
             new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 }
},
                               new double[] { 4.0, 6.0, 1.0 });
@@ -128,13 +123,14 @@ extends TestCase {
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0 });
-        assertEquals(7.0, optimum.getPoint()[0], 1.0e-10);
-        assertEquals(3.0, optimum.getPoint()[1], 1.0e-10);
-        assertEquals(0.0, optimum.getValue(), 1.0e-10);
+        Assert.assertEquals(7.0, optimum.getPoint()[0], 1.0e-10);
+        Assert.assertEquals(3.0, optimum.getPoint()[1], 1.0e-10);
+        Assert.assertEquals(0.0, optimum.getValue(), 1.0e-10);
 
     }
 
-    public void testNoDependency() throws MathUserException {
+    @Test
+    public void testNoDependency() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 2, 0, 0, 0, 0, 0 },
                 { 0, 2, 0, 0, 0, 0 },
@@ -150,12 +146,12 @@ extends TestCase {
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0,
0 });
         for (int i = 0; i < problem.target.length; ++i) {
-            assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10);
+            Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10);
         }
     }
 
-    public void testOneSet() throws MathUserException {
-
+    @Test
+    public void testOneSet() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  1,  0, 0 },
                 { -1,  1, 0 },
@@ -167,13 +163,14 @@ extends TestCase {
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 });
-        assertEquals(1.0, optimum.getPoint()[0], 1.0e-10);
-        assertEquals(2.0, optimum.getPoint()[1], 1.0e-10);
-        assertEquals(3.0, optimum.getPoint()[2], 1.0e-10);
+        Assert.assertEquals(1.0, optimum.getPoint()[0], 1.0e-10);
+        Assert.assertEquals(2.0, optimum.getPoint()[1], 1.0e-10);
+        Assert.assertEquals(3.0, optimum.getPoint()[2], 1.0e-10);
 
     }
 
-    public void testTwoSets() throws MathUserException {
+    @Test
+    public void testTwoSets() {
         final double epsilon = 1.0e-7;
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  2,  1,   0,  4,       0, 0 },
@@ -203,17 +200,17 @@ extends TestCase {
 
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0,
0 });
-        assertEquals( 3.0, optimum.getPoint()[0], 1.0e-10);
-        assertEquals( 4.0, optimum.getPoint()[1], 1.0e-10);
-        assertEquals(-1.0, optimum.getPoint()[2], 1.0e-10);
-        assertEquals(-2.0, optimum.getPoint()[3], 1.0e-10);
-        assertEquals( 1.0 + epsilon, optimum.getPoint()[4], 1.0e-10);
-        assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10);
+        Assert.assertEquals( 3.0, optimum.getPoint()[0], 1.0e-10);
+        Assert.assertEquals( 4.0, optimum.getPoint()[1], 1.0e-10);
+        Assert.assertEquals(-1.0, optimum.getPoint()[2], 1.0e-10);
+        Assert.assertEquals(-2.0, optimum.getPoint()[3], 1.0e-10);
+        Assert.assertEquals( 1.0 + epsilon, optimum.getPoint()[4], 1.0e-10);
+        Assert.assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10);
 
     }
 
-    public void testNonInversible() throws MathUserException {
-
+    @Test
+    public void testNonInversible() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 {  1, 2, -3 },
                 {  2, 1,  3 },
@@ -225,10 +222,11 @@ extends TestCase {
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
                 optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 });
-        assertTrue(optimum.getValue() > 0.5);
+        Assert.assertTrue(optimum.getValue() > 0.5);
     }
 
-    public void testIllConditioned() throws MathUserException {
+    @Test
+    public void testIllConditioned() {
         LinearProblem problem1 = new LinearProblem(new double[][] {
                 { 10.0, 7.0,  8.0,  7.0 },
                 {  7.0, 5.0,  6.0,  5.0 },
@@ -239,16 +237,14 @@ extends TestCase {
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
         optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-13, 1.0e-13));
-        BrentSolver solver = new BrentSolver();
-        solver.setAbsoluteAccuracy(1.0e-15);
-        solver.setRelativeAccuracy(1.0e-15);
+        BrentSolver solver = new BrentSolver(1e-15, 1e-15);
         optimizer.setLineSearchSolver(solver);
         RealPointValuePair optimum1 =
             optimizer.optimize(problem1, GoalType.MINIMIZE, new double[] { 0, 1, 2, 3 });
-        assertEquals(1.0, optimum1.getPoint()[0], 1.0e-5);
-        assertEquals(1.0, optimum1.getPoint()[1], 1.0e-5);
-        assertEquals(1.0, optimum1.getPoint()[2], 1.0e-5);
-        assertEquals(1.0, optimum1.getPoint()[3], 1.0e-5);
+        Assert.assertEquals(1.0, optimum1.getPoint()[0], 1.0e-4);
+        Assert.assertEquals(1.0, optimum1.getPoint()[1], 1.0e-4);
+        Assert.assertEquals(1.0, optimum1.getPoint()[2], 1.0e-4);
+        Assert.assertEquals(1.0, optimum1.getPoint()[3], 1.0e-4);
 
         LinearProblem problem2 = new LinearProblem(new double[][] {
                 { 10.00, 7.00, 8.10, 7.20 },
@@ -258,16 +254,15 @@ extends TestCase {
         }, new double[] { 32, 23, 33, 31 });
         RealPointValuePair optimum2 =
             optimizer.optimize(problem2, GoalType.MINIMIZE, new double[] { 0, 1, 2, 3 });
-        assertEquals(-81.0, optimum2.getPoint()[0], 1.0e-1);
-        assertEquals(137.0, optimum2.getPoint()[1], 1.0e-1);
-        assertEquals(-34.0, optimum2.getPoint()[2], 1.0e-1);
-        assertEquals( 22.0, optimum2.getPoint()[3], 1.0e-1);
+        Assert.assertEquals(-81.0, optimum2.getPoint()[0], 1.0e-1);
+        Assert.assertEquals(137.0, optimum2.getPoint()[1], 1.0e-1);
+        Assert.assertEquals(-34.0, optimum2.getPoint()[2], 1.0e-1);
+        Assert.assertEquals( 22.0, optimum2.getPoint()[3], 1.0e-1);
 
     }
 
-    public void testMoreEstimatedParametersSimple()
-        throws MathUserException {
-
+    @Test
+    public void testMoreEstimatedParametersSimple() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 3.0, 2.0,  0.0, 0.0 },
                 { 0.0, 1.0, -1.0, 1.0 },
@@ -280,12 +275,12 @@ extends TestCase {
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 7, 6, 5, 4 });
-        assertEquals(0, optimum.getValue(), 1.0e-10);
+        Assert.assertEquals(0, optimum.getValue(), 1.0e-10);
 
     }
 
-    public void testMoreEstimatedParametersUnsorted()
-        throws MathUserException {
+    @Test
+    public void testMoreEstimatedParametersUnsorted() {
         LinearProblem problem = new LinearProblem(new double[][] {
                  { 1.0, 1.0,  0.0,  0.0, 0.0,  0.0 },
                  { 0.0, 0.0,  1.0,  1.0, 1.0,  0.0 },
@@ -299,10 +294,11 @@ extends TestCase {
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 2, 2, 2, 2, 2,
2 });
-        assertEquals(0, optimum.getValue(), 1.0e-10);
+        Assert.assertEquals(0, optimum.getValue(), 1.0e-10);
     }
 
-    public void testRedundantEquations() throws MathUserException {
+    @Test
+    public void testRedundantEquations() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0,  1.0 },
                 { 1.0, -1.0 },
@@ -315,12 +311,13 @@ extends TestCase {
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 1, 1 });
-        assertEquals(2.0, optimum.getPoint()[0], 1.0e-8);
-        assertEquals(1.0, optimum.getPoint()[1], 1.0e-8);
+        Assert.assertEquals(2.0, optimum.getPoint()[0], 1.0e-8);
+        Assert.assertEquals(1.0, optimum.getPoint()[1], 1.0e-8);
 
     }
 
-    public void testInconsistentEquations() throws MathUserException {
+    @Test
+    public void testInconsistentEquations() {
         LinearProblem problem = new LinearProblem(new double[][] {
                 { 1.0,  1.0 },
                 { 1.0, -1.0 },
@@ -333,12 +330,13 @@ extends TestCase {
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-6, 1.0e-6));
         RealPointValuePair optimum =
             optimizer.optimize(problem, GoalType.MINIMIZE, new double[] { 1, 1 });
-        assertTrue(optimum.getValue() > 0.1);
+        Assert.assertTrue(optimum.getValue() > 0.1);
 
     }
 
-    public void testCircleFitting() throws MathUserException {
-        Circle circle = new Circle();
+    @Test
+    public void testCircleFitting() {
+        CircleScalar circle = new CircleScalar();
         circle.addPoint( 30.0,  68.0);
         circle.addPoint( 50.0,  -6.0);
         circle.addPoint(110.0, -20.0);
@@ -348,16 +346,14 @@ extends TestCase {
             new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE);
         optimizer.setMaxEvaluations(100);
         optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-30, 1.0e-30));
-        BrentSolver solver = new BrentSolver();
-        solver.setAbsoluteAccuracy(1.0e-13);
-        solver.setRelativeAccuracy(1.0e-15);
+        UnivariateRealSolver solver = new BrentSolver(1e-15, 1e-13);
         optimizer.setLineSearchSolver(solver);
         RealPointValuePair optimum =
             optimizer.optimize(circle, GoalType.MINIMIZE, new double[] { 98.680, 47.345 });
         Point2D.Double center = new Point2D.Double(optimum.getPointRef()[0], optimum.getPointRef()[1]);
-        assertEquals(69.960161753, circle.getRadius(center), 1.0e-8);
-        assertEquals(96.075902096, center.x, 1.0e-8);
-        assertEquals(48.135167894, center.y, 1.0e-8);
+        Assert.assertEquals(69.960161753, circle.getRadius(center), 1.0e-8);
+        Assert.assertEquals(96.075902096, center.x, 1.0e-8);
+        Assert.assertEquals(48.135167894, center.y, 1.0e-8);
     }
 
     private static class LinearProblem implements DifferentiableMultivariateRealFunction,
Serializable {
@@ -382,7 +378,7 @@ extends TestCase {
             return p;
         }
 
-        public double value(double[] variables) throws MathUserException {
+        public double value(double[] variables) {
             double[] y = factors.operate(variables);
             double sum = 0;
             for (int i = 0; i < y.length; ++i) {
@@ -409,86 +405,5 @@ extends TestCase {
                 }
             };
         }
-
-    }
-
-    private static class Circle implements DifferentiableMultivariateRealFunction, Serializable
{
-
-        private static final long serialVersionUID = -4711170319243817874L;
-
-        private ArrayList<Point2D.Double> points;
-
-        public Circle() {
-            points  = new ArrayList<Point2D.Double>();
-        }
-
-        public void addPoint(double px, double py) {
-            points.add(new Point2D.Double(px, py));
-        }
-
-        public double getRadius(Point2D.Double center) {
-            double r = 0;
-            for (Point2D.Double point : points) {
-                r += point.distance(center);
-            }
-            return r / points.size();
-        }
-
-        private double[] gradient(double[] point) {
-
-            // optimal radius
-            Point2D.Double center = new Point2D.Double(point[0], point[1]);
-            double radius = getRadius(center);
-
-            // gradient of the sum of squared residuals
-            double dJdX = 0;
-            double dJdY = 0;
-            for (Point2D.Double pk : points) {
-                double dk = pk.distance(center);
-                dJdX += (center.x - pk.x) * (dk - radius) / dk;
-                dJdY += (center.y - pk.y) * (dk - radius) / dk;
-            }
-            dJdX *= 2;
-            dJdY *= 2;
-
-            return new double[] { dJdX, dJdY };
-
-        }
-
-        public double value(double[] variables)
-                throws IllegalArgumentException, MathUserException {
-
-            Point2D.Double center = new Point2D.Double(variables[0], variables[1]);
-            double radius = getRadius(center);
-
-            double sum = 0;
-            for (Point2D.Double point : points) {
-                double di = point.distance(center) - radius;
-                sum += di * di;
-            }
-
-            return sum;
-
-        }
-
-        public MultivariateVectorialFunction gradient() {
-            return new MultivariateVectorialFunction() {
-                private static final long serialVersionUID = 3174909643301201710L;
-                public double[] value(double[] point) {
-                    return gradient(point);
-                }
-            };
-        }
-
-        public MultivariateRealFunction partialDerivative(final int k) {
-            return new MultivariateRealFunction() {
-                private static final long serialVersionUID = 3073956364104833888L;
-                public double value(double[] point) {
-                    return gradient(point)[k];
-                }
-            };
-        }
-
     }
-
 }



Mime
View raw message