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

svn commit: r1409686 - in /mahout/trunk/math/src: main/java/org/apache/mahout/math/ test/java/org/apache/mahout/math/

Author: tdunning
Date: Thu Nov 15 08:19:30 2012
New Revision: 1409686

URL: http://svn.apache.org/viewvc?rev=1409686&view=rev
Log:
MAHOUT-1114 - Fix with test case.

Modified:
    mahout/trunk/math/src/main/java/org/apache/mahout/math/Centroid.java
    mahout/trunk/math/src/main/java/org/apache/mahout/math/DelegatingVector.java
    mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractVectorTest.java
    mahout/trunk/math/src/test/java/org/apache/mahout/math/CentroidTest.java

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/Centroid.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/Centroid.java?rev=1409686&r1=1409685&r2=1409686&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/Centroid.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/Centroid.java Thu Nov 15 08:19:30 2012
@@ -24,48 +24,48 @@ import org.apache.mahout.math.function.D
  * to make it easy to use vector search classes and such.
  */
 public class Centroid extends WeightedVector {
-    public Centroid(WeightedVector original) {
-        super(original.getWeight(), original.getIndex());
-        delegate = original.getVector().like();
-        delegate.assign(original);
-    }
+  public Centroid(WeightedVector original) {
+    super(original.getWeight(), original.getIndex());
+    delegate = original.getVector().like();
+    delegate.assign(original);
+  }
 
-    public Centroid(int key, Vector initialValue) {
-        super(initialValue, 1, key);
-    }
+  public Centroid(int key, Vector initialValue) {
+    super(initialValue, 1, key);
+  }
 
-    public Centroid(int key, Vector initialValue, double weight) {
-        super(initialValue, weight, key);
-    }
+  public Centroid(int key, Vector initialValue, double weight) {
+    super(initialValue, weight, key);
+  }
 
-    public static Centroid create(int key, Vector initialValue) {
-        if (initialValue instanceof WeightedVector) {
-            return new Centroid(key, new DenseVector(initialValue), ((WeightedVector) initialValue).getWeight());
-        } else {
-            return new Centroid(key, new DenseVector(initialValue), 1);
-        }
+  public static Centroid create(int key, Vector initialValue) {
+    if (initialValue instanceof WeightedVector) {
+      return new Centroid(key, new DenseVector(initialValue), ((WeightedVector) initialValue).getWeight());
+    } else {
+      return new Centroid(key, new DenseVector(initialValue), 1);
     }
+  }
 
-    public void update(Vector v) {
-        if (v instanceof Centroid) {
-            Centroid c = (Centroid) v;
-            update(c.delegate, c.getWeight());
-        } else {
-            update(v, 1);
-        }
+  public void update(Vector v) {
+    if (v instanceof Centroid) {
+      Centroid c = (Centroid) v;
+      update(c.delegate, c.getWeight());
+    } else {
+      update(v, 1);
     }
+  }
 
-    public void update(Vector v, final double w) {
-        final double weight = getWeight();
-        final double totalWeight = weight + w;
-        delegate.assign(v, new DoubleDoubleFunction() {
-            @Override
-            public double apply(double v, double v1) {
-                return (weight * v + w * v1) / totalWeight;
-            }
-        });
-        setWeight(totalWeight);
-    }
+  public void update(Vector v, final double w) {
+    final double weight = getWeight();
+    final double totalWeight = weight + w;
+    delegate.assign(v, new DoubleDoubleFunction() {
+      @Override
+      public double apply(double v, double v1) {
+        return (weight * v + w * v1) / totalWeight;
+      }
+    });
+    setWeight(totalWeight);
+  }
 
   @Override
   public Vector like() {
@@ -73,20 +73,20 @@ public class Centroid extends WeightedVe
   }
 
   /**
-     * Gets the index of this centroid.  Use getIndex instead to maintain standard names.
-     */
-    @Deprecated
-    public int getKey() {
-        return getIndex();
-    }
+   * Gets the index of this centroid.  Use getIndex instead to maintain standard names.
+   */
+  @Deprecated
+  public int getKey() {
+    return getIndex();
+  }
 
-    public void addWeight() {
-        setWeight(getWeight() + 1);
-    }
+  public void addWeight() {
+    setWeight(getWeight() + 1);
+  }
 
-    @Override
-    public String toString() {
-        return String.format("key = %d, weight = %.2f, vector = %s", getIndex(), getWeight(), delegate);
-    }
+  @Override
+  public String toString() {
+    return String.format("key = %d, weight = %.2f, vector = %s", getIndex(), getWeight(), delegate);
+  }
 
 }

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/DelegatingVector.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/DelegatingVector.java?rev=1409686&r1=1409685&r2=1409686&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/DelegatingVector.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/DelegatingVector.java Thu Nov 15 08:19:30 2012
@@ -60,12 +60,13 @@ public class DelegatingVector implements
 
   @Override
   public Vector clone() {
-    WeightedVector r;
+    DelegatingVector r;
     try {
-      r = (WeightedVector) super.clone();
+      r = (DelegatingVector) super.clone();
     } catch (CloneNotSupportedException e) {
       throw new RuntimeException("Clone not supported for DelegatingVector, shouldn't be possible");
     }
+    // delegate points to original without this
     r.delegate = delegate.clone();
     return r;
   }

Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractVectorTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractVectorTest.java?rev=1409686&r1=1409685&r2=1409686&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractVectorTest.java (original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractVectorTest.java Thu Nov 15 08:19:30 2012
@@ -137,6 +137,11 @@ public abstract class AbstractVectorTest
     assertEquals(dv1.viewPart(5, 10).zSum(), v1.viewPart(5, 10).zSum(), FUZZ);
 
     Vector v3 = v1.clone();
+
+    // must be the right type ... tricky to tell that in the face of type erasure
+    assertTrue(v0.getClass().isAssignableFrom(v3.getClass()));
+    assertTrue(v3.getClass().isAssignableFrom(v0.getClass()));
+
     assertEquals(0, v1.getDistanceSquared(v3), FUZZ);
     assertNotSame(v1, v3);
     v3.assign(0);

Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/CentroidTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/CentroidTest.java?rev=1409686&r1=1409685&r2=1409686&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/CentroidTest.java (original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/CentroidTest.java Thu Nov 15 08:19:30 2012
@@ -21,7 +21,7 @@ import org.apache.mahout.math.function.F
 import org.apache.mahout.math.random.MultiNormal;
 import org.junit.Test;
 
-public class CentroidTest extends AbstractVectorTest {
+public class CentroidTest extends AbstractVectorTest<Centroid> {
   @Test
   public void testUpdate() {
     MultiNormal f = new MultiNormal(20);
@@ -56,7 +56,7 @@ public class CentroidTest extends Abstra
   }
 
   @Override
-  public Vector vectorToTest(int size) {
+  public Centroid vectorToTest(int size) {
     return new Centroid(new WeightedVector(new DenseVector(size), 3.15, 51));
   }