You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by ed...@apache.org on 2008/08/12 11:38:28 UTC

svn commit: r685106 - in /incubator/hama/trunk/src: java/org/apache/hama/Vector.java java/org/apache/hama/VectorInterface.java test/org/apache/hama/TestVector.java

Author: edwardyoon
Date: Tue Aug 12 02:38:27 2008
New Revision: 685106

URL: http://svn.apache.org/viewvc?rev=685106&view=rev
Log:
Add scaling method to Vector

Modified:
    incubator/hama/trunk/src/java/org/apache/hama/Vector.java
    incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java
    incubator/hama/trunk/src/test/org/apache/hama/TestVector.java

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=685106&r1=685105&r2=685106&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Vector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Vector.java Tue Aug 12 02:38:27 2008
@@ -64,7 +64,7 @@
     HbaseMapWritable<byte[], Cell> trunk = new HbaseMapWritable<byte[], Cell>();
     for (int i = 0; i < this.size(); i++) {
       double value = (this.get(i) + v2.get(i));
-      Cell cValue = new Cell(String.valueOf(value), 0);
+      Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis());
       trunk.put(Bytes.toBytes("column:" + i), cValue);
     }
 
@@ -82,6 +82,21 @@
     return cosine / (this.getNorm2() * v.getNorm2());
   }
 
+  public Vector scale(double alpha) {
+    Set<byte[]> keySet = cells.keySet();
+    Iterator<byte[]> it = keySet.iterator();
+
+    while (it.hasNext()) {
+      byte[] key = it.next();
+      double oValue = bytesToDouble(get(key).getValue());
+      double nValue = oValue * alpha;
+      Cell cValue = new Cell(String.valueOf(nValue), System.currentTimeMillis());
+      cells.put(key, cValue);
+    }
+
+    return this;
+  }
+
   public double get(int index) {
     return bytesToDouble(this.cells.get(getColumnIndex(index)).getValue());
   }

Modified: incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java?rev=685106&r1=685105&r2=685106&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java Tue Aug 12 02:38:27 2008
@@ -1,3 +1,22 @@
+/**
+ * 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;
 
 /**
@@ -70,6 +89,15 @@
   public double dot(Vector v);
 
   /**
+   * v = alpha*v 
+   * 
+   * @param alpha
+   * @return v = alpha*v
+   */
+  public Vector scale(double alpha);
+  
+  
+  /**
    * Computes the given norm of the vector
    * 
    * @param type

Modified: incubator/hama/trunk/src/test/org/apache/hama/TestVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestVector.java?rev=685106&r1=685105&r2=685106&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestVector.java Tue Aug 12 02:38:27 2008
@@ -43,6 +43,7 @@
     dotTest(v1, v2);
     norm1Test(v1, v2);
     norm2Test(v1, v2);
+    scalingTest(v2);
   }
 
   public void dotTest(Vector v1, Vector v2) {
@@ -58,4 +59,11 @@
     assertEquals(norm2, v1.getNorm2());
   }
 
+  private void scalingTest(Vector v2) {
+    v2.scale(0.5);
+    
+    for (int i = 0; i < v2.size(); i++) {
+      assertEquals(values[1][i] * 0.5, v2.get(i));
+    }
+  }
 }