incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r705456 - in /incubator/hama/trunk: CHANGES.txt src/java/org/apache/hama/AbstractVector.java src/java/org/apache/hama/DenseVector.java src/test/org/apache/hama/TestDenseVector.java
Date Fri, 17 Oct 2008 04:00:19 GMT
Author: edwardyoon
Date: Thu Oct 16 21:00:19 2008
New Revision: 705456

URL: http://svn.apache.org/viewvc?rev=705456&view=rev
Log:
Implement Vector.add() method

Modified:
    incubator/hama/trunk/CHANGES.txt
    incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java
    incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java
    incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java

Modified: incubator/hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=705456&r1=705455&r2=705456&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Thu Oct 16 21:00:19 2008
@@ -27,6 +27,7 @@
     
   IMPROVEMENTS
     
+    HAMA-28: Implement Vector.add() method (edwardyoon)
     HAMA-78: Separate Interface and Implementation for HamaAdmin (edwardyoon)
     HAMA-76: Remove load() since it duplicated with constructor (edwardyoon)
     HAMA-74: set the number of map/reduce task on the shell (samuel)

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=705456&r1=705455&r2=705456&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java Thu Oct 16 21:00:19
2008
@@ -27,12 +27,16 @@
 public abstract class AbstractVector {
   public VectorMapWritable<Integer, VectorEntry> entries;
   
-  public double get(int index) {
+  public double get(int index) throws NullPointerException {
     return this.entries.get(index).getValue();
   }
   
+  public void set(int index, double value) {
+    entries.put(index, new VectorEntry(value));
+  }
+  
   public void add(int index, double value) {
-    // TODO Auto-generated method stub
+    set(index, get(index) + value);
   }
   
   /**

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=705456&r1=705455&r2=705456&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java Thu Oct 16 21:00:19 2008
@@ -28,7 +28,7 @@
 
 public class DenseVector extends AbstractVector implements Vector {
   static final Logger LOG = Logger.getLogger(DenseVector.class);
-  
+
   public DenseVector() {
     this(new VectorMapWritable<Integer, VectorEntry>());
   }
@@ -38,8 +38,13 @@
   }
 
   public Vector add(double alpha, Vector v) {
-    // TODO Auto-generated method stub
-    return null;
+    if (alpha == 0)
+      return this;
+
+    for (int i = 0; i < this.size(); i++) {
+      set(i, get(i) + alpha * v.get(i));
+    }
+    return this;
   }
 
   public Vector add(Vector v2) {
@@ -97,10 +102,6 @@
       return getNormInf();
   }
 
-  public void set(int index, double value) {
-    entries.put(index, new VectorEntry(value));
-  }
-
   public DenseVector set(Vector v) {
     return new DenseVector(((DenseVector) v).getEntries());
   }
@@ -125,7 +126,7 @@
     Iterator<Integer> it = keySet.iterator();
 
     while (it.hasNext()) {
-      double value = get(it.next()); //this.get(it.next()).getValue();
+      double value = get(it.next()); // this.get(it.next()).getValue();
       square_sum += value * value;
     }
 
@@ -144,10 +145,10 @@
 
   public Vector subVector(int i0, int i1) {
     Vector res = new DenseVector();
-    for(int i = i0; i <= i1; i++ ) {
+    for (int i = i0; i <= i1; i++) {
       res.set(i, get(i));
     }
-      
+
     return res;
   }
 }

Modified: incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java?rev=705456&r1=705455&r2=705456&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java Thu Oct 16 21:00:19
2008
@@ -32,6 +32,7 @@
 
 public class TestDenseVector extends TestCase {
   final static Log LOG = LogFactory.getLog(TestDenseVector.class.getName());
+  
   private static final double cosine = 0.6978227007909176;
   private static final double norm1 = 12.0;
   private static final double norm2 = 6.782329983125268;
@@ -118,15 +119,29 @@
   }
 
   /**
-   * Test iterator
+   * Test add()
    */
-  public void testIterator() {
+  public void testAdd() {
+    v1.add(v2);
     int i = 0;
     Iterator<VectorEntry> it = v1.iterator();
     while (it.hasNext()) {
       VectorEntry c = it.next();
-      assertEquals(c.getValue(), values[0][i]);
+      assertEquals(c.getValue(), values[0][i] + values[1][i]);
       i++;
     }
+
+    v1.add(0.5, v2);
+    int j = 0;
+    Iterator<VectorEntry> itt = v1.iterator();
+    while (itt.hasNext()) {
+      VectorEntry c = itt.next();
+      assertEquals(c.getValue(), (values[0][j] + values[1][j]) + (0.5 * values[1][j]));
+      j++;
+    }
+    
+    double old = v1.get(0);
+    v1.add(0, norm1);
+    assertEquals(v1.get(0), old + norm1);
   }
 }



Mime
View raw message