mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sro...@apache.org
Subject svn commit: r953661 - in /mahout/trunk/math/src: main/java/org/apache/mahout/math/SequentialAccessSparseVector.java test/java/org/apache/mahout/math/AbstractTestVector.java
Date Fri, 11 Jun 2010 11:17:09 GMT
Author: srowen
Date: Fri Jun 11 11:17:09 2010
New Revision: 953661

URL: http://svn.apache.org/viewvc?rev=953661&view=rev
Log:
Fix dot() bug from mailing list

Modified:
    mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java
    mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java?rev=953661&r1=953660&r2=953661&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java
(original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java
Fri Jun 11 11:17:09 2010
@@ -152,7 +152,7 @@ public class SequentialAccessSparseVecto
   public Iterator<Element> iterator() {
     return new AllIterator();
   }
-    
+
   @Override
   public double dot(Vector x) {
     if (size() != x.size()) {
@@ -162,37 +162,38 @@ public class SequentialAccessSparseVecto
       return dotSelf();
     }
     
-    double result = 0;
     if (x instanceof SequentialAccessSparseVector) {
       // For sparse SeqAccVectors. do dot product without lookup in a linear fashion
       Iterator<Element> myIter = iterateNonZero();
       Iterator<Element> otherIter = x.iterateNonZero();
-      Element myCurrent = null;
-      Element otherCurrent = null;
-      while (myIter.hasNext() && otherIter.hasNext()) {
-        if (myCurrent == null) {
+      if (!myIter.hasNext() || !otherIter.hasNext()) {
+        return 0.0;
+      }
+      Element myCurrent = myIter.next();
+      Element otherCurrent = otherIter.next();
+      double result = 0.0;
+      while (true) {
+        int myIndex = myCurrent.index();
+        int otherIndex = otherCurrent.index();
+        if (myIndex == otherIndex) {
+          result += myCurrent.get() * otherCurrent.get();
+        }
+        if (myIndex <= otherIndex) {
+          if (!myIter.hasNext()) {
+            break;
+          }
           myCurrent = myIter.next();
         }
-        if (otherCurrent == null) {
+        if (myIndex >= otherIndex) {
+          if (!otherIter.hasNext()) {
+            break;
+          }
           otherCurrent = otherIter.next();
         }
-
-        int myIndex = myCurrent.index();
-        int otherIndex = otherCurrent.index();
-        
-        if (myIndex < otherIndex) {
-          // due to the sparseness skipping occurs more hence checked before equality
-          myCurrent = null;
-        } else if (myIndex > otherIndex){
-          otherCurrent = null;
-        } else { // both are equal 
-          result += myCurrent.get() * otherCurrent.get();
-          myCurrent = null;
-          otherCurrent = null;
-        } 
       }
       return result;
     } else { // seq.rand. seq.dense
+      double result = 0.0;      
       Iterator<Element> iter = iterateNonZero();
       while (iter.hasNext()) {
         Element element = iter.next();

Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java?rev=953661&r1=953660&r2=953661&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java (original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java Fri Jun
11 11:17:09 2010
@@ -219,6 +219,13 @@ public abstract class AbstractTestVector
     assertEquals("dot", expected, res, EPSILON);
   }
 
+  public void testDot2() {
+    Vector test2 = test.clone();
+    test2.set(1, 0.0);
+    test2.set(3, 0.0);
+    assertEquals(3.3 * 3.3, test2.dot(test), EPSILON);
+  }
+
   public void testDotCardinality() {
     try {
       test.dot(new DenseVector(test.size() + 1));



Mime
View raw message