You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2012/11/16 19:06:02 UTC

svn commit: r1410518 - in /mahout/trunk/math/src: main/java/org/apache/mahout/math/WeightedVector.java main/java/org/apache/mahout/math/WeightedVectorComparator.java test/java/org/apache/mahout/math/WeightedVectorTest.java

Author: srowen
Date: Fri Nov 16 18:06:01 2012
New Revision: 1410518

URL: http://svn.apache.org/viewvc?rev=1410518&view=rev
Log:
MAHOUT-1116 move WeightedVector comparison to separate class for better consistency with equals

Added:
    mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java
Modified:
    mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVector.java
    mahout/trunk/math/src/test/java/org/apache/mahout/math/WeightedVectorTest.java

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVector.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVector.java?rev=1410518&r1=1410517&r2=1410518&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVector.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVector.java Fri Nov 16 18:06:01 2012
@@ -20,7 +20,7 @@ package org.apache.mahout.math;
 /**
  * Decorates a vector with a floating point weight and an index.
  */
-public class WeightedVector extends DelegatingVector implements Comparable<WeightedVector> {
+public class WeightedVector extends DelegatingVector {
   private static final int INVALID_INDEX = -1;
   private double weight;
   private int index;
@@ -55,31 +55,6 @@ public class WeightedVector extends Dele
     return weight;
   }
 
-
-  @Override
-  public int compareTo(WeightedVector other) {
-    if (this == other) {
-      return 0;
-    }
-    int r = Double.compare(weight, other.getWeight());
-    if (r == 0 || Math.abs(weight - other.getWeight()) < 1e-8) {
-      double diff = this.minus(other).norm(1);
-      if (diff < 1e-12) {
-        return 0;
-      } else {
-        for (Vector.Element element : this) {
-          r = Double.compare(element.get(), other.get(element.index()));
-          if (r != 0) {
-            return r;
-          }
-        }
-        return 0;
-      }
-    } else {
-      return r;
-    }
-  }
-
   public int getIndex() {
     return index;
   }

Added: mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java?rev=1410518&view=auto
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java (added)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java Fri Nov 16 18:06:01 2012
@@ -0,0 +1,53 @@
+/*
+ * 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.mahout.math;
+
+import java.util.Comparator;
+
+/**
+ * Orders {@link WeightedVector} by {@link WeightedVector#getWeight()}.
+ */
+public final class WeightedVectorComparator implements Comparator<WeightedVector> {
+
+  private static final double DOUBLE_EQUALITY_ERROR = 1.0e-8;
+
+  @Override
+  public int compare(WeightedVector a, WeightedVector b) {
+    if (a == b) {
+      return 0;
+    }
+    double aWeight = a.getWeight();
+    double bWeight = b.getWeight();
+    int r = Double.compare(aWeight, bWeight);
+    if (r != 0 && Math.abs(aWeight - bWeight) >= DOUBLE_EQUALITY_ERROR) {
+      return r;
+    }
+    double diff = a.minus(b).norm(1);
+    if (diff < 1.0e-12) {
+      return 0;
+    }
+    for (Vector.Element element : a) {
+      r = Double.compare(element.get(), b.get(element.index()));
+      if (r != 0) {
+        return r;
+      }
+    }
+    return 0;
+  }
+
+}

Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/WeightedVectorTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/WeightedVectorTest.java?rev=1410518&r1=1410517&r2=1410518&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/WeightedVectorTest.java (original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/WeightedVectorTest.java Fri Nov 16 18:06:01 2012
@@ -44,12 +44,13 @@ public class WeightedVectorTest extends 
     WeightedVector v2 = new WeightedVector(new DenseVector(new double[]{1, 2, 3}), 5.00, 31);
     WeightedVector v3 = new WeightedVector(new DenseVector(new double[]{1, 3, 3}), 5.00, 31);
     WeightedVector v4 = (WeightedVector) v1.clone();
+    WeightedVectorComparator comparator = new WeightedVectorComparator();
 
-    assertTrue(v1.compareTo(v2) > 0);
-    assertTrue(v3.compareTo(v1) < 0);
-    assertTrue(v3.compareTo(v2) > 0);
-    assertEquals(0, v4.compareTo(v1));
-    assertEquals(0, v1.compareTo(v1));
+    assertTrue(comparator.compare(v1, v2) > 0);
+    assertTrue(comparator.compare(v3, v1) < 0);
+    assertTrue(comparator.compare(v3, v2) > 0);
+    assertEquals(0, comparator.compare(v4, v1));
+    assertEquals(0, comparator.compare(v1, v1));
   }
 
   @Test