You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ss...@apache.org on 2014/02/11 15:56:37 UTC

svn commit: r1567165 - in /mahout/trunk: CHANGELOG math/src/main/java/org/apache/mahout/math/SparseMatrix.java math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java

Author: ssc
Date: Tue Feb 11 14:56:37 2014
New Revision: 1567165

URL: http://svn.apache.org/r1567165
Log:
MAHOUT-1415: Clone method on sparse matrices fails if there is an empty row which has not been set explicitly

Modified:
    mahout/trunk/CHANGELOG
    mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java
    mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java

Modified: mahout/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/mahout/trunk/CHANGELOG?rev=1567165&r1=1567164&r2=1567165&view=diff
==============================================================================
--- mahout/trunk/CHANGELOG (original)
+++ mahout/trunk/CHANGELOG Tue Feb 11 14:56:37 2014
@@ -1,6 +1,8 @@
 Mahout Change Log
 
 Release 1.0 - unreleased
+  
+  MAHOUT-1415: Clone method on sparse matrices fails if there is an empty row which has not been set explicitly (till.rohrmann via ssc)
 
 Release 0.9 - 2014-02-01
 

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java?rev=1567165&r1=1567164&r2=1567165&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseMatrix.java Tue Feb 11 14:56:37 2014
@@ -55,10 +55,9 @@ public class SparseMatrix extends Abstra
 
   @Override
   public Matrix clone() {
-    SparseMatrix clone = (SparseMatrix) super.clone();
-    clone.rowVectors = rowVectors.clone();
-    for (int i = 0; i < numRows(); i++) {
-      clone.rowVectors.put(i, rowVectors.get(i).clone());
+    SparseMatrix clone = new SparseMatrix(numRows(), numCols());
+    for (MatrixSlice slice : this) {
+      clone.rowVectors.put(slice.index(), slice.clone());
     }
     return clone;
   }

Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java?rev=1567165&r1=1567164&r2=1567165&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java (original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseMatrix.java Tue Feb 11 14:56:37 2014
@@ -17,6 +17,10 @@
 
 package org.apache.mahout.math;
 
+import java.util.Iterator;
+
+import org.junit.Test;
+
 public final class TestSparseMatrix extends MatrixTest {
 
   @Override
@@ -29,4 +33,39 @@ public final class TestSparseMatrix exte
     }
     return matrix;
   }
+
+  /** Test copy method of sparse matrices which have empty non-initialized rows */
+  @Test
+  public void testSparseCopy() {
+    SparseMatrix matrix = createSparseMatrixWithEmptyRow();
+    Matrix copy = matrix.clone();
+
+    assertSame("wrong class", copy.getClass(), matrix.getClass());
+
+    SparseMatrix castedCopy = (SparseMatrix) copy;
+        
+    Iterator<MatrixSlice> originalSlices = matrix.iterator();
+    Iterator<MatrixSlice> copySlices = castedCopy.iterator();
+    
+    while (originalSlices.hasNext() && copySlices.hasNext()) {
+      MatrixSlice originalSlice = originalSlices.next();
+      MatrixSlice copySlice = copySlices.next();
+      assertEquals("Wrong row indices.", originalSlice.index(), copySlice.index());
+      assertEquals("Slices are not equal.", originalSlice, copySlice);
+    }
+    
+    assertSame("Number of rows of original and copy are not equal.", originalSlices.hasNext(), copySlices.hasNext());
+  }
+  
+  /**
+   * @return Sparse matrix whose last row is empty. Since no entry is set in the last row, there is no vector instance
+   * initialized in the underlying map.
+   */
+  private SparseMatrix createSparseMatrixWithEmptyRow() {
+    SparseMatrix result = new SparseMatrix(3, 3);
+    result.setQuick(0, 0, 1);
+    result.setQuick(1, 1 ,1);
+    result.setQuick(1, 2, 1);
+    return result;
+  }
 }