Author: edwardyoon
Date: Thu Aug 7 22:51:36 2008
New Revision: 683859
URL: http://svn.apache.org/viewvc?rev=683859&view=rev
Log:
Remove vector-norms enum to interface from implementation
Modified:
incubator/hama/trunk/CHANGES.txt
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/CHANGES.txt
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=683859&r1=683858&r2=683859&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Thu Aug 7 22:51:36 2008
@@ -2,6 +2,7 @@
Trunk (unreleased changes)
+ HAMA-19: Remove vector-norms enum to interface from implementation
(edwardyoon)
HAMA-17: Vector to Writable conversion (edwardyoon)
HAMA-15: Logo Alignment (edwardyoon)
HAMA-10: Refactor the mapred package
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=683859&r1=683858&r2=683859&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Vector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Vector.java Thu Aug 7
22:51:36 2008
@@ -23,49 +23,31 @@
import org.apache.hadoop.hbase.io.HbaseMapWritable;
import org.apache.hadoop.hbase.io.RowResult;
import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hama.VectorInterface.Norm;
import org.apache.hama.io.VectorWritable;
import org.apache.log4j.Logger;
public class Vector extends VectorWritable implements VectorInterface {
static final Logger LOG = Logger.getLogger(Vector.class);
- public enum Norm {
- // TODO : Add types
- }
-
public Vector() {
this(null, new HbaseMapWritable<byte[], Cell>());
}
- /**
- * Constructor a Vector
- */
public Vector(final byte[] row, final HbaseMapWritable<byte[], Cell> m) {
this.row = row;
this.cells = m;
}
- public Vector(RowResult r) {
- parse(r.entrySet());
- }
-
- public Vector(VectorWritable r) {
- parse(r.entrySet());
- }
-
/**
- * @param rowKey
- * @param b
+ * @param row
+ * @param matrix
*/
- public Vector(byte[] rowKey, Matrix b) {
- this.row = rowKey;
- parse(b.getRowResult(this.row).entrySet());
+ public Vector(byte[] row, Matrix matrix) {
+ this.row = row;
+ parse(matrix.getRowResult(this.row).entrySet());
}
- public byte[] getRow() {
- return row;
- }
-
public void add(int index, double value) {
// TODO Auto-generated method stub
@@ -106,8 +88,14 @@
}
public double norm(Norm type) {
- // TODO Auto-generated method stub
- return 0;
+ if (type == Norm.One)
+ return getL1Norm();
+ else if (type == Norm.Two)
+ return getL2Norm();
+ else if (type == Norm.TwoRobust)
+ return getL2NormRobust();
+ else
+ return getNormInf();
}
public void set(int index, double value) {
@@ -120,10 +108,6 @@
return null;
}
- /**
- * Returns the linear norm factor of this vector's values (i.e., the sum of
- * it's values).
- */
public double getL1Norm() {
double sum = 0.0;
for (int i = 0; i < m_vals.length; i++) {
@@ -132,9 +116,6 @@
return sum;
}
- /**
- * Returns the L2 norm factor of this vector's values.
- */
public double getL2Norm() {
double square_sum = 0.0;
for (int i = 0; i < m_vals.length; i++) {
@@ -143,6 +124,16 @@
return Math.sqrt(square_sum);
}
+ public double getL2NormRobust() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public double getNormInf() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
public int getDimAt(int index) {
return m_dims[index];
}
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=683859&r1=683858&r2=683859&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java
(original)
+++ incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java Thu Aug
7 22:51:36 2008
@@ -4,7 +4,7 @@
* Basic vector interface.
*/
public interface VectorInterface {
-
+
/**
* @return size of the vector
*/
@@ -14,34 +14,34 @@
* @param index
* @return v(index)
*/
- public double get(int index);
-
+ public double get(int index);
+
/**
* v(index) = value
- *
+ *
* @param index
* @param value
*/
public void set(int index, double value);
-
+
/**
* @param v
- * @return x = v
+ * @return x = v
*/
public Vector set(Vector v);
/**
* v(index) += value
- *
+ *
* @param index
* @param value
*/
public void add(int index, double value);
-
+
/**
* @param alpha
* @param v
- * @return x = alpha*v + x
+ * @return x = alpha*v + x
*/
public boolean add(double alpha, Vector v);
@@ -50,7 +50,7 @@
* @return x = v + x
*/
public Vector add(Vector v);
-
+
/**
* @param v
* @return x dot v
@@ -63,17 +63,26 @@
* @param type
* @return norm of the vector
*/
- public double norm(Vector.Norm type);
+ public double norm(Norm type);
+
+ /**
+ * Supported vector-norms.
+ */
+ enum Norm {
+
+ /** Sum of the absolute values of the entries */
+ One,
+
+ /** The root of sum of squares */
+ Two,
+
+ /**
+ * As the 2 norm may overflow, an overflow resistant version is also
+ * available. Note that it may be slower.
+ */
+ TwoRobust,
- @Deprecated
- public double getValueAt(int index);
-
- @Deprecated
- public int getDimAt(int index);
-
- @Deprecated
- public double getL1Norm();
-
- @Deprecated
- public double getL2Norm();
+ /** Largest entry in absolute value */
+ Infinity
+ }
}
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=683859&r1=683858&r2=683859&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestVector.java Thu Aug 7
22:51:36 2008
@@ -19,6 +19,7 @@
*/
package org.apache.hama;
+import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;
public class TestVector extends HamaTestCase {
@@ -47,8 +48,8 @@
LOG.info("get test : " + m1.get(0, 0));
LOG.info("get test : " + m1.get(0, 1));
- Vector v1 = new Vector(m1.getRowResult(0));
- Vector v2 = new Vector(m1.getRowResult(1));
+ Vector v1 = new Vector(Bytes.toBytes(String.valueOf(0)), m1);
+ Vector v2 = new Vector(Bytes.toBytes(String.valueOf(1)), m1);
double cos = v1.dot(v2);
assertEquals(cos, result);