commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r885027 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/util/MathUtils.java site/xdoc/changes.xml test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java
Date Fri, 27 Nov 2009 21:45:39 GMT
Author: psteitz
Date: Fri Nov 27 21:45:38 2009
New Revision: 885027

URL: http://svn.apache.org/viewvc?rev=885027&view=rev
Log:
Fixed an overflow error in MathUtils.distance that was causing KMeansPlusPlusClusterer to
fail with a NullPointerException when
component distances between points exceeded Integer.MAXVALUE.
JIRA: MATH-305
Reported by Erik van Ingen

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=885027&r1=885026&r2=885027&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Fri
Nov 27 21:45:38 2009
@@ -1621,9 +1621,9 @@
      * @return the L<sub>2</sub> distance between the two points
      */
     public static double distance(int[] p1, int[] p2) {
-      int sum = 0;
+      double sum = 0;
       for (int i = 0; i < p1.length; i++) {
-          final int dp = p1[i] - p2[i];
+          final double dp = p1[i] - p2[i];
           sum += dp * dp;
       }
       return Math.sqrt(sum);

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=885027&r1=885026&r2=885027&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Nov 27 21:45:38 2009
@@ -39,6 +39,11 @@
   </properties>
   <body>
     <release version="2.1" date="TBD" description="TBD">
+      <action dev="psteitz" type="fix" issue="MATH-305" due-to="Erik van Ingen">
+        Fixed an overflow error in MathUtils.distance that was causing KMeansPlusPlusClusterer
+        to fail with a NullPointerException when component distances between points
+        exceeded Integer.MAXVALUE.
+      </action>
       <action dev="psteitz" type="update" issue="MATH-315" due-to="Mikkel Meyer Andersen">
         Added generationsEvolved property to GeneticAlgorithm to track the number of generations
         evolved by the evolve() method before reaching the StoppingCondition.

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java?rev=885027&r1=885026&r2=885027&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/clustering/KMeansPlusPlusClustererTest.java
Fri Nov 27 21:45:38 2009
@@ -93,5 +93,27 @@
         assertTrue(cluster3Found);
 
     }
+    
+    /**
+     * JIRA: MATH-305
+     * 
+     * Two points, one cluster, one iteration
+     */
+    @Test
+    public void testPerformClusterAnalysisDegenerate() {
+        KMeansPlusPlusClusterer<EuclideanIntegerPoint> transformer = new KMeansPlusPlusClusterer<EuclideanIntegerPoint>(
+                new Random(1746432956321l));
+        EuclideanIntegerPoint[] points = new EuclideanIntegerPoint[] {
+                new EuclideanIntegerPoint(new int[] { 1959, 325100 }),
+                new EuclideanIntegerPoint(new int[] { 1960, 373200 }), };
+        List<Cluster<EuclideanIntegerPoint>> clusters = transformer.cluster(Arrays.asList(points),
1, 1);
+        assertEquals(1, clusters.size());
+        assertEquals(2, (clusters.get(0).getPoints().size()));
+        EuclideanIntegerPoint pt1 = new EuclideanIntegerPoint(new int[] { 1959, 325100 });
+        EuclideanIntegerPoint pt2 = new EuclideanIntegerPoint(new int[] { 1960, 373200 });
+        assertTrue(clusters.get(0).getPoints().contains(pt1));
+        assertTrue(clusters.get(0).getPoints().contains(pt2));
+
+    } 
 
 }



Mime
View raw message