incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r752381 - in /incubator/hama/trunk/src: java/org/apache/hama/ java/org/apache/hama/algebra/ test/org/apache/hama/
Date Wed, 11 Mar 2009 05:59:42 GMT
Author: edwardyoon
Date: Wed Mar 11 05:59:42 2009
New Revision: 752381

URL: http://svn.apache.org/viewvc?rev=752381&view=rev
Log:
Implemention of add(double alpha, Vector v)

Modified:
    incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java
    incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java
    incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java
    incubator/hama/trunk/src/java/org/apache/hama/Vector.java
    incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java
    incubator/hama/trunk/src/test/org/apache/hama/TestSparseVector.java

Modified: incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java?rev=752381&r1=752380&r2=752381&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java Wed Mar 11 05:59:42
2009
@@ -20,10 +20,16 @@
 package org.apache.hama;
 
 import java.util.Iterator;
+import java.util.Map;
 
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.hbase.io.RowResult;
+import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.MapWritable;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.Writable;
+import org.apache.hama.io.DoubleEntry;
+import org.apache.hama.util.BytesUtil;
 
 /**
  * Methods of the vector classes
@@ -31,6 +37,14 @@
 public abstract class AbstractVector {
   protected MapWritable entries;
 
+  public void initMap(RowResult row) {
+    this.entries = new MapWritable();
+    for (Map.Entry<byte[], Cell> f : row.entrySet()) {
+      this.entries.put(new IntWritable(BytesUtil.getColumnIndex(f.getKey())),
+          new DoubleEntry(f.getValue()));
+    }    
+  }
+  
   /**
    * Returns an Iterator.
    * 

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=752381&r1=752380&r2=752381&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Wed Mar 11 05:59:42 2009
@@ -368,7 +368,7 @@
       increaseRows();
 
     VectorUpdate update = new VectorUpdate(row);
-    update.putAll(((DenseVector) vector).getEntries());
+    update.putAll(vector.getEntries());
     table.commit(update.getBatchUpdate());
   }
 

Modified: incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java?rev=752381&r1=752380&r2=752381&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java Wed Mar 11 05:59:42 2009
@@ -24,14 +24,12 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.hadoop.hbase.io.Cell;
 import org.apache.hadoop.hbase.io.RowResult;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.MapWritable;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.Writable;
 import org.apache.hama.io.DoubleEntry;
-import org.apache.hama.util.BytesUtil;
 import org.apache.log4j.Logger;
 
 /**
@@ -49,11 +47,7 @@
   }
 
   public DenseVector(RowResult row) {
-    this.entries = new MapWritable();
-    for (Map.Entry<byte[], Cell> f : row.entrySet()) {
-      this.entries.put(new IntWritable(BytesUtil.getColumnIndex(f.getKey())),
-          new DoubleEntry(f.getValue()));
-    }
+    this.initMap(row);
   }
 
   public DenseVector(int row, MapWritable m) {
@@ -81,7 +75,7 @@
       return this;
 
     for (int i = 0; i < this.size(); i++) {
-      set(i, get(i) + alpha * v.get(i));
+      set(i, alpha * v.get(i) + get(i));
     }
     return this;
   }
@@ -164,7 +158,7 @@
    * @return x = v
    */
   public DenseVector set(Vector v) {
-    return new DenseVector(((DenseVector) v).getEntries());
+    return new DenseVector(v.getEntries());
   }
 
   public double getNorm1() {

Modified: incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java?rev=752381&r1=752380&r2=752381&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java Wed Mar 11 05:59:42 2009
@@ -22,15 +22,16 @@
 import java.io.IOException;
 import java.util.Map;
 
-import org.apache.hadoop.hbase.io.Cell;
 import org.apache.hadoop.hbase.io.RowResult;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.MapWritable;
 import org.apache.hadoop.io.Writable;
 import org.apache.hama.io.DoubleEntry;
-import org.apache.hama.util.BytesUtil;
 import org.apache.log4j.Logger;
 
+/**
+ * This class represents a sparse vector.
+ */
 public class SparseVector extends AbstractVector implements Vector {
   static final Logger LOG = Logger.getLogger(SparseVector.class);
 
@@ -43,17 +44,28 @@
   }
 
   public SparseVector(RowResult row) {
-    this.entries = new MapWritable();
-    for (Map.Entry<byte[], Cell> f : row.entrySet()) {
-      this.entries.put(new IntWritable(BytesUtil.getColumnIndex(f.getKey())),
-          new DoubleEntry(f.getValue()));
-    }
+    this.initMap(row);
   }
 
   @Override
   public Vector add(double alpha, Vector v) {
-    // TODO Auto-generated method stub
-    return null;
+    if (alpha == 0)
+      return this;
+
+    for (Map.Entry<Writable, Writable> e : v.getEntries().entrySet()) {
+      if (this.entries.containsKey(e.getKey())) {
+        // add
+        double value = alpha * ((DoubleEntry) e.getValue()).getValue()
+            + this.get(((IntWritable) e.getKey()).get());
+        this.entries.put(e.getKey(), new DoubleEntry(value));
+      } else {
+        // put
+        double value = alpha * ((DoubleEntry) e.getValue()).getValue();
+        this.entries.put(e.getKey(), new DoubleEntry(value));
+      }
+    }
+
+    return this;
   }
 
   /**
@@ -69,9 +81,7 @@
       return this;
     }
 
-    for (Map.Entry<Writable, Writable> e : ((SparseVector) v2).getEntries()
-        .entrySet()) {
-
+    for (Map.Entry<Writable, Writable> e : v2.getEntries().entrySet()) {
       if (this.entries.containsKey(e.getKey())) {
         // add
         double value = ((DoubleEntry) e.getValue()).getValue()
@@ -158,10 +168,14 @@
     set(index, get(index) + value);
   }
 
-  @Override
-  public Vector set(Vector v) {
-    // TODO Auto-generated method stub
-    return null;
+  /**
+   * Sets the vector
+   * 
+   * @param v
+   * @return x = v
+   */
+  public SparseVector set(Vector v) {
+    return new SparseVector(v.getEntries());
   }
 
   @Override

Modified: incubator/hama/trunk/src/java/org/apache/hama/Vector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Vector.java?rev=752381&r1=752380&r2=752381&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Vector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Vector.java Wed Mar 11 05:59:42 2009
@@ -21,6 +21,7 @@
 
 import java.util.Iterator;
 
+import org.apache.hadoop.io.MapWritable;
 import org.apache.hadoop.io.Writable;
 
 /**
@@ -144,4 +145,11 @@
    * @return iterator
    */
   public Iterator<Writable> iterator();
+  
+  /**
+   * Returns the {@link org.apache.hadoop.io.MapWritable}
+   * 
+   * @return the entries of vector
+   */
+  public MapWritable getEntries();
 }

Modified: incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java?rev=752381&r1=752380&r2=752381&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java Wed Mar 11
05:59:42 2009
@@ -95,7 +95,6 @@
             currVector.get(i)));
       }
       
-      
       output.collect(key, ((SparseVector) sum).getEntries());
     } else {
       ((DenseVector) sum).clear();

Modified: incubator/hama/trunk/src/test/org/apache/hama/TestSparseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestSparseVector.java?rev=752381&r1=752380&r2=752381&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestSparseVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestSparseVector.java Wed Mar 11 05:59:42
2009
@@ -75,4 +75,20 @@
     Cell c = table.get(BytesUtil.getRowIndex(0), BytesUtil.getColumnIndex(1));
     assertTrue(c == null);
   }
+  
+  /**
+   * Test add()
+   */
+  public void testAdd() {
+    v1.add(v2);
+    
+    for(int i = 0; i < values[0].length; i++) {
+      assertEquals(v1.get(i), values[0][i] + values[1][i]);
+    }
+
+    v1.add(0.5, v2);
+    for(int i = 0; i < values[0].length; i++) {
+      assertEquals(v1.get(i),  (values[0][i] + values[1][i]) + (0.5 * values[1][i]));
+    }
+  }
 }



Mime
View raw message