You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by sa...@apache.org on 2009/02/23 02:26:35 UTC

svn commit: r746854 - in /incubator/hama/trunk/src: java/org/apache/hama/AbstractMatrix.java java/org/apache/hama/DenseMatrix.java test/org/apache/hama/TestDenseMatrix.java

Author: samuel
Date: Mon Feb 23 01:26:34 2009
New Revision: 746854

URL: http://svn.apache.org/viewvc?rev=746854&view=rev
Log:
HAMA-153: Need some condition-check before matrix algebra

Modified:
    incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
    incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java

Modified: incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java?rev=746854&r1=746853&r2=746854&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java Mon Feb 23 01:26:34 2009
@@ -172,7 +172,25 @@
 
     table.commit(update.getBatchUpdate());
   }
-
+  
+  public void increaseRows() throws IOException {
+    int nValue = this.getRows() + 1;
+    try {
+      this.setDimension(nValue, this.getColumns());
+    } catch (NullPointerException ne) {
+      // If there is no metadata of dimension, nothing to do.
+    }
+  }
+  
+  public void increaseColumns() throws IOException {
+    int nValue = this.getColumns() + 1;
+    try {
+      this.setDimension(this.getRows(), nValue);
+    } catch (NullPointerException ne) {
+      // If there is no metadata of dimension, nothing to do.
+    }
+  }
+  
   /** {@inheritDoc} */
   public void add(int i, int j, double value) throws IOException {
     VectorUpdate update = new VectorUpdate(i);

Modified: incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java?rev=746854&r1=746853&r2=746854&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Mon Feb 23 01:26:34 2009
@@ -382,6 +382,9 @@
    * @throws IOException
    */
   public void setRow(int row, Vector vector) throws IOException {
+    if(this.getRows() < row)
+      increaseRows();
+    
     VectorUpdate update = new VectorUpdate(row);
     update.putAll(((DenseVector) vector).getEntries());
     table.commit(update.getBatchUpdate());
@@ -395,21 +398,26 @@
    * @throws IOException
    */
   public void setColumn(int column, Vector vector) throws IOException {
+    if (this.getColumns() < column)
+      increaseColumns();
+      
     for (int i = 0; i < vector.size(); i++) {
       VectorUpdate update = new VectorUpdate(i);
       update.put(column, vector.get(i));
       table.commit(update.getBatchUpdate());
     }
   }
-  
+
   /**
-   * A = B + A
+   * C = B + A
    * 
    * @param B
-   * @return A
+   * @return C
    * @throws IOException
    */
   public Matrix add(Matrix B) throws IOException {
+    ensureForAddition(B);
+    
     Matrix result = new DenseMatrix(config);
     JobConf jobConf = new JobConf(config);
     jobConf.setJobName("addition MR job" + result.getPath());
@@ -428,21 +436,28 @@
   }
 
   /**
-   * A = alpha*B + A
+   * C = alpha*B + A
    * 
    * @param alpha
    * @param B
-   * @return A
+   * @return C
    * @throws IOException
    */
   public Matrix add(double alpha, Matrix B) throws IOException {
+    ensureForAddition(B);
+    
     Matrix temp = new DenseMatrix(config);
     temp.set(alpha, B);
-    
     Matrix result = this.add(temp);
     return result;
   }
   
+  private void ensureForAddition(Matrix m) throws IOException {
+    if(getRows()!= m.getRows() || getColumns() != m.getColumns()) {
+      throw new IOException("Matrices' rows and columns should be same while A+B.");
+    }
+  }
+  
   /**
    * C = A*B using SIMD algorithm
    * 
@@ -451,6 +466,8 @@
    * @throws IOException
    */
   public Matrix mult(Matrix B) throws IOException {
+    ensureForMultiplication(B);
+    
     Matrix result = new DenseMatrix(config);
 
     JobConf jobConf = new JobConf(config);
@@ -476,6 +493,8 @@
    * @throws IOException
    */
   public Matrix mult(Matrix B, int blocks) throws IOException {
+    ensureForMultiplication(B);
+    
     Matrix collectionTable = new DenseMatrix(config);
     LOG.info("Collect Blocks");
 
@@ -499,6 +518,12 @@
     // Should be collectionTable removed?
     return result;
   }
+  
+  private void ensureForMultiplication(Matrix m) throws IOException {
+    if(getColumns() != m.getRows()) {
+      throw new IOException("A's columns should equal with B's rows while A*B.");
+    }
+  }
 
   /**
    * C = alpha*A*B + C

Modified: incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java?rev=746854&r1=746853&r2=746854&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java Mon Feb 23 01:26:34 2009
@@ -213,6 +213,8 @@
       assertEquals(entries[i], ((DoubleEntry) it.next()).getValue());
       i++;
     }
+    
+    assertEquals(m1.getRows(), SIZE + 1);
   }
 
   public void testSetColumn() throws IOException {
@@ -231,8 +233,29 @@
       assertEquals(entries[i], ((DoubleEntry) it.next()).getValue());
       i++;
     }
+    
+    assertEquals(m1.getColumns(), SIZE + 1);
   }
 
+  public void testEnsureForAddition() {
+    try {
+      m1.add(m2);
+      fail("Matrix-Addition should be failed while rows and columns aren't same.");
+    } catch (IOException e) {
+      LOG.info(e.toString());
+    }
+  }
+  
+
+  public void testEnsureForMultiplication() {
+    try {
+      m1.mult(m2);
+      fail("Matrix-Mult should be failed while A.columns!=B.rows.");
+    } catch (IOException e) {
+      LOG.info(e.toString());
+    }
+  }
+  
   public void testLoadSave() throws IOException {
     String path1 = m1.getPath();
     // save m1 to aliase1