incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r708442 - in /incubator/hama/trunk: CHANGES.txt src/java/org/apache/hama/DenseMatrix.java src/java/org/apache/hama/Matrix.java src/java/org/apache/hama/SubMatrix.java src/test/org/apache/hama/TestDenseMatrix.java
Date Tue, 28 Oct 2008 05:36:10 GMT
Author: edwardyoon
Date: Mon Oct 27 22:36:09 2008
New Revision: 708442

URL: http://svn.apache.org/viewvc?rev=708442&view=rev
Log:
Add subMatrix(int i0, int i1, int j0, int j1) to Matrix

Added:
    incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java
Modified:
    incubator/hama/trunk/CHANGES.txt
    incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
    incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java

Modified: incubator/hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=708442&r1=708441&r2=708442&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Mon Oct 27 22:36:09 2008
@@ -4,8 +4,9 @@
 
   NEW FEATURES
     
+    HAMA-92: Add subMatrix() to Matrix (edwardyoon)
     HAMA-83: Add a writable comparable for BlockID (edwardyoon) 
-    HAMA-81: Add subVector(int i0, int i1) to Vector interface (edwardyoon)
+    HAMA-81: Add subVector(int i0, int i1) to Vector (edwardyoon)
     Hama-80: Add identity(int m, int n) which returns identity matrix (edwardyoon) 
     HAMA-62: Hama Shell Implementation (samuel via edwardyoon)
     HAMA-61: Load / save matrices from HTable (edwardyoon)

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=708442&r1=708441&r2=708442&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Mon Oct 27 22:36:09 2008
@@ -267,4 +267,28 @@
   public String getType() {
     return this.getClass().getSimpleName();
   }
+
+  public SubMatrix subMatrix(int i0, int i1, int j0, int j1) throws IOException {
+    int columnSize = (j1 - j0) + 1;
+    SubMatrix result = new SubMatrix((i1-i0) + 1, columnSize);
+    byte[][] c = new byte[columnSize][];
+    for (int i = 0; i < columnSize; i++) {
+      c[i] = Numeric.getColumnIndex(j0 + i);
+    }
+
+    Scanner scan = table.getScanner(c, Numeric.intToBytes(i0), Numeric
+        .intToBytes(i1 + 1));
+
+    int rKey = 0, cKey = 0;
+    for (RowResult row : scan) {
+      for (Map.Entry<byte[], Cell> e : row.entrySet()) {
+        result.set(rKey, cKey, Numeric.bytesToDouble(e.getValue().getValue()));
+        cKey++;
+      }
+      rKey++;
+      cKey = 0;
+    }
+
+    return result;
+  }
 }

Modified: incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Matrix.java?rev=708442&r1=708441&r2=708442&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Matrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Matrix.java Mon Oct 27 22:36:09 2008
@@ -241,9 +241,25 @@
   public boolean save(String path) throws IOException;
 
   /**
+   * Returns the matrix type
+   * 
    * @return the matrix type
    */
   public String getType();
+
+  /**
+   * Returns the sub matrix formed by selecting certain rows and
+   * columns from a bigger matrix. The sub matrix is a in-memory operation only.
+   * 
+   * @param i0 the start index of row
+   * @param i1 the end index of row
+   * @param j0 the start index of column
+   * @param j1 the end index of column
+   * @return the sub matrix of matrix
+   * @throws IOException
+   */
+  public SubMatrix subMatrix(int i0, int i1, int j0, int j1) throws IOException;
+
   /**
    * Close current matrix.
    * 

Added: incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java?rev=708442&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java Mon Oct 27 22:36:09 2008
@@ -0,0 +1,65 @@
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama;
+
+import org.apache.log4j.Logger;
+
+public class SubMatrix {
+  static final Logger LOG = Logger.getLogger(SubMatrix.class);
+  private double[][] matrix;
+  
+  public SubMatrix(int i, int j) {
+    matrix = new double[i][j];
+  }
+
+  public SubMatrix(double[][] c) {
+    double[][] matrix = c;
+    this.matrix = matrix;
+  }
+
+  public void set(int row, int column, double value) {
+    matrix[row][column] = value;
+  }
+
+  public double get(int i, int j) {
+    return matrix[i][j];
+  }
+
+  public SubMatrix mult(SubMatrix b) {
+    double[][] C = new double[size()][size()];
+    for (int i = 0; i < size(); i++) {
+      for (int j = 0; j < size(); j++) {
+        for (int k = 0; k < size(); k++) {
+          C[i][k] += this.get(i, j) * b.get(j, k);
+        }
+      }
+    }
+
+    return new SubMatrix(C);
+  }
+  
+  public int size() {
+    return matrix.length;
+  }
+
+  public void close() {
+    matrix = null;
+  }
+}

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=708442&r1=708441&r2=708442&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java Mon Oct 27 22:36:09
2008
@@ -92,6 +92,33 @@
     assertEquals(m1.getColumnAttribute(1), null);
   }
 
+  public void testSubMatrix() throws IOException {
+    SubMatrix a = m1.subMatrix(2, 4, 2, 4);
+    for (int i = 0; i < a.size(); i++) {
+      for (int j = 0; j < a.size(); j++) {
+        assertEquals(a.get(i, j), m1.get(i + 2, j + 2));
+      }
+    }
+    
+    SubMatrix b = m2.subMatrix(0, 2, 0, 2);
+    SubMatrix c = a.mult(b);
+    
+    double[][] C = new double[3][3];
+    for (int i = 0; i < 3; i++) {
+      for (int j = 0; j < 3; j++) {
+        for (int k = 0; k < 3; k++) {
+          C[i][k] += m1.get(i + 2, j + 2) * m2.get(j, k);
+        }
+      }
+    }
+    
+    for (int i = 0; i < 3; i++) {
+      for (int j = 0; j < 3; j++) {
+        assertEquals(C[i][j], c.get(i, j));
+      }
+    }
+  }
+
   /**
    * Test matrices addition
    * 



Mime
View raw message