mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tdunn...@apache.org
Subject svn commit: r1380430 - in /mahout/trunk/math/src: main/java/org/apache/mahout/math/ test/java/org/apache/mahout/math/
Date Tue, 04 Sep 2012 02:18:52 GMT
Author: tdunning
Date: Tue Sep  4 02:18:51 2012
New Revision: 1380430

URL: http://svn.apache.org/viewvc?rev=1380430&view=rev
Log:
MAHOUT-1059 - Add generic vector test.

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

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=1380430&r1=1380429&r2=1380430&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 Tue Sep 
4 02:18:51 2012
@@ -180,33 +180,39 @@ public class DelegatingVector implements
 
     @Override
     public Vector assign(double value) {
-        return delegate.assign(value);
-    }
-
-    @Override
-    public Vector assign(double[] values) {
-        return delegate.assign(values);
-    }
-
-    @Override
-    public Vector assign(Vector other) {
-        return delegate.assign(other);
-    }
-
-    @Override
-    public Vector assign(DoubleDoubleFunction f, double y) {
-        return delegate.assign(f, y);
-    }
-
-    @Override
-    public Vector assign(DoubleFunction function) {
-        return delegate.assign(function);
-    }
-
-    @Override
-    public Vector assign(Vector other, DoubleDoubleFunction function) {
-        return delegate.assign(other, function);
-    }
+    delegate.assign(value);
+    return this;
+  }
+
+  @Override
+  public Vector assign(double[] values) {
+    delegate.assign(values);
+    return this;
+  }
+
+  @Override
+  public Vector assign(Vector other) {
+    delegate.assign(other);
+    return this;
+  }
+
+  @Override
+  public Vector assign(DoubleDoubleFunction f, double y) {
+    delegate.assign(f, y);
+    return this;
+  }
+
+  @Override
+  public Vector assign(DoubleFunction function) {
+    delegate.assign(function);
+    return this;
+  }
+
+  @Override
+  public Vector assign(Vector other, DoubleDoubleFunction function) {
+    delegate.assign(other, function);
+    return this;
+  }
 
     @Override
     public Matrix cross(Vector other) {

Added: 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=1380430&view=auto
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractVectorTest.java (added)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractVectorTest.java Tue Sep
 4 02:18:51 2012
@@ -0,0 +1,103 @@
+package org.apache.mahout.math;
+
+import org.apache.mahout.common.RandomUtils;
+import org.apache.mahout.math.jet.random.Normal;
+import org.junit.Test;
+
+import java.util.Random;
+
+/**
+ * Makes sure that a vector under test acts the same as a DenseVector or RandomAccessSparseVector
+ * (according to whether it is dense or sparse).  Most operations need to be done within
a reasonable
+ * tolerance.
+ *
+ * The idea is that a new vector implementation can extend AbstractVectorTest to get pretty
high
+ * confidence that it is working correctly.
+ */
+public abstract class AbstractVectorTest<T extends Vector> extends MahoutTestCase {
+  public abstract T vectorToTest(int size);
+
+  @Test
+  public void testSimpleOps() {
+
+    final T v0 = vectorToTest(20);
+    final Random gen = RandomUtils.getRandom();
+    Vector v1 = v0.assign(new Normal(0, 1, gen));
+    Vector v2 = vectorToTest(20).assign(new Normal(0, 1, gen));
+
+    assertEquals(v0.get(12), v1.get(12), 0);
+    v0.set(12, gen.nextDouble());
+    assertEquals(v0.get(12), v1.get(12), 0);
+    assertTrue(v0 == v1);
+
+    Vector dv1 = new DenseVector(v1);
+    Vector dv2 = new DenseVector(v2);
+    Vector sv1 = new RandomAccessSparseVector(v1);
+    Vector sv2 = new RandomAccessSparseVector(v2);
+
+    assertEquals(0, dv1.plus(dv2).getDistanceSquared(v1.plus(v2)), 1e-13);
+    assertEquals(0, dv1.plus(dv2).getDistanceSquared(v1.plus(dv2)), 1e-13);
+    assertEquals(0, dv1.plus(dv2).getDistanceSquared(v1.plus(sv2)), 1e-13);
+    assertEquals(0, dv1.plus(dv2).getDistanceSquared(sv1.plus(v2)), 1e-13);
+
+    assertEquals(0, dv1.minus(dv2).getDistanceSquared(v1.minus(v2)), 1e-13);
+    assertEquals(0, dv1.minus(dv2).getDistanceSquared(v1.minus(dv2)), 1e-13);
+    assertEquals(0, dv1.minus(dv2).getDistanceSquared(v1.minus(sv2)), 1e-13);
+    assertEquals(0, dv1.minus(dv2).getDistanceSquared(sv1.minus(v2)), 1e-13);
+
+    double z = gen.nextDouble();
+    assertEquals(0, dv1.divide(z).getDistanceSquared(v1.divide(z)), 1e-12);
+    assertEquals(0, dv1.times(z).getDistanceSquared(v1.times(z)), 1e-12);
+    assertEquals(0, dv1.plus(z).getDistanceSquared(v1.plus(z)), 1e-12);
+
+    assertEquals(dv1.dot(dv2), v1.dot(v2), 1e-13);
+    assertEquals(dv1.dot(dv2), v1.dot(dv2), 1e-13);
+    assertEquals(dv1.dot(dv2), v1.dot(sv2), 1e-13);
+    assertEquals(dv1.dot(dv2), sv1.dot(v2), 1e-13);
+    assertEquals(dv1.dot(dv2), dv1.dot(v2), 1e-13);
+
+    assertEquals(dv1.getLengthSquared(), v1.getLengthSquared(), 1e-13);
+    assertEquals(dv1.getDistanceSquared(dv2), v1.getDistanceSquared(v2), 1e-13);
+    assertEquals(dv1.getDistanceSquared(dv2), dv1.getDistanceSquared(v2), 1e-13);
+    assertEquals(dv1.getDistanceSquared(dv2), sv1.getDistanceSquared(v2), 1e-13);
+    assertEquals(dv1.getDistanceSquared(dv2), v1.getDistanceSquared(dv2), 1e-13);
+    assertEquals(dv1.getDistanceSquared(dv2), v1.getDistanceSquared(sv2), 1e-13);
+
+    assertEquals(dv1.minValue(), v1.minValue(), 1e-13);
+    assertEquals(dv1.minValueIndex(), v1.minValueIndex());
+
+    assertEquals(dv1.maxValue(), v1.maxValue(), 1e-13);
+    assertEquals(dv1.maxValueIndex(), v1.maxValueIndex());
+
+    Vector nv1 = v1.normalize();
+
+    assertEquals(0, dv1.getDistanceSquared(v1), 1e-13);
+    assertEquals(1, nv1.norm(2), 1e-13);
+    assertEquals(0, dv1.normalize().getDistanceSquared(nv1), 1e-13);
+
+    nv1 = v1.normalize(1);
+    assertEquals(0, dv1.getDistanceSquared(v1), 1e-13);
+    assertEquals(1, nv1.norm(1), 1e-13);
+    assertEquals(0, dv1.normalize(1).getDistanceSquared(nv1), 1e-13);
+
+    assertEquals(dv1.norm(0), v1.norm(0), 1e-13);
+    assertEquals(dv1.norm(1), v1.norm(1), 1e-13);
+    assertEquals(dv1.norm(1.5), v1.norm(1.5), 1e-13);
+    assertEquals(dv1.norm(2), v1.norm(2), 1e-13);
+
+    // assign double, function, vector x function
+
+
+    // aggregate
+
+    // cross,
+
+    // getNumNondefaultElements
+
+    for (Vector.Element element : v1) {
+      assertEquals(dv1.get(element.index()), element.get(), 0);
+      assertEquals(dv1.get(element.index()), v1.get(element.index()), 0);
+      assertEquals(dv1.get(element.index()), v1.getQuick(element.index()), 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=1380430&r1=1380429&r2=1380430&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 Tue Sep  4 02:18:51
2012
@@ -21,9 +21,7 @@ import org.apache.mahout.math.function.F
 import org.apache.mahout.math.random.MultiNormal;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
-
-public class CentroidTest {
+public class CentroidTest extends AbstractVectorTest {
   @Test
   public void testUpdate() {
     MultiNormal f = new MultiNormal(20);
@@ -56,4 +54,9 @@ public class CentroidTest {
     assertEquals(3, x1.getWeight(), 1e-8);
     assertEquals(1, x1.getIndex());
   }
+
+  @Override
+  public Vector vectorToTest(int size) {
+    return new Centroid(new WeightedVector(new DenseVector(size), 3.15, 51));
+  }
 }

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=1380430&r1=1380429&r2=1380430&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 Tue Sep
 4 02:18:51 2012
@@ -19,10 +19,8 @@ package org.apache.mahout.math;
 
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
 
-
-public class WeightedVectorTest {
+public class WeightedVectorTest extends AbstractVectorTest {
   @Test
   public void testLength() {
     Vector v = new DenseVector(new double[]{0.9921337470551008, 1.0031004325833064, 0.9963963182745947});
@@ -33,4 +31,9 @@ public class WeightedVectorTest {
     System.out.printf("c = %.9f\nv = %.9f\n", c.getLengthSquared(), c.getVector().getLengthSquared());
     assertEquals(c.getVector().getLengthSquared(), c.getLengthSquared(), 1e-17);
   }
+
+  @Override
+  public Vector vectorToTest(int size) {
+    return new WeightedVector(new DenseVector(size), 4.52, 345);
+  }
 }



Mime
View raw message