Return-Path: Delivered-To: apmail-incubator-hama-commits-archive@locus.apache.org Received: (qmail 18042 invoked from network); 17 Oct 2008 04:01:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 17 Oct 2008 04:01:19 -0000 Received: (qmail 56725 invoked by uid 500); 17 Oct 2008 04:01:14 -0000 Delivered-To: apmail-incubator-hama-commits-archive@incubator.apache.org Received: (qmail 56704 invoked by uid 500); 17 Oct 2008 04:01:14 -0000 Mailing-List: contact hama-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: hama-dev@ Delivered-To: mailing list hama-commits@incubator.apache.org Received: (qmail 56576 invoked by uid 99); 17 Oct 2008 04:01:13 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Oct 2008 21:01:13 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Oct 2008 04:00:12 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 261E02388920; Thu, 16 Oct 2008 21:00:20 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: hama-commits@incubator.apache.org From: edwardyoon@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081017040020.261E02388920@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 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()); } @@ -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 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 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 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); } }