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 2009/03/12 10:56:47 UTC
svn commit: r752818 - in /incubator/hama/trunk: CHANGES.txt
src/java/org/apache/hama/DenseMatrix.java
src/java/org/apache/hama/DenseVector.java
src/java/org/apache/hama/Matrix.java
src/test/org/apache/hama/TestDenseVector.java
Author: edwardyoon
Date: Thu Mar 12 09:56:35 2009
New Revision: 752818
URL: http://svn.apache.org/viewvc?rev=752818&view=rev
Log:
Vector's first location bug fixed and getNormInf() and getNorm2Robust() are implemented
Modified:
incubator/hama/trunk/CHANGES.txt
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/Matrix.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=752818&r1=752817&r2=752818&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Thu Mar 12 09:56:35 2009
@@ -105,6 +105,8 @@
BUG FIXES
+ HAMA-169: Vector's first location bug fixed,
+ getNormInf() and getNorm2Robust() are implemented (edwardyoon)
HAMA-155: When out of index, should throw exception (edwardyoon)
HAMA-147: Fix typos (edwardyoon)
HAMA-140: In subMatrix(), Scanner should be closed (edwardyoon)
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=752818&r1=752817&r2=752818&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Thu Mar 12 09:56:35 2009
@@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.Iterator;
+import java.util.Map;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
@@ -32,6 +33,7 @@
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.SequenceFile;
+import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.JobClient;
@@ -383,9 +385,11 @@
if (this.getColumns() < column)
increaseColumns();
- for (int i = 0; i < vector.size(); i++) {
- VectorUpdate update = new VectorUpdate(i);
- update.put(column, vector.get(i));
+ for(Map.Entry<Writable, Writable> e : vector.getEntries().entrySet()) {
+ int key = ((IntWritable) e.getKey()).get();
+ double value = ((DoubleEntry) e.getValue()).getValue();
+ VectorUpdate update = new VectorUpdate(key);
+ update.put(column, value);
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=752818&r1=752817&r2=752818&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java Thu Mar 12 09:56:35 2009
@@ -55,15 +55,69 @@
this.entries.put(new Text("row"), new IntWritable(row));
}
+ /**
+ * Sets the vector
+ *
+ * @param v
+ * @return x = v
+ */
+ public DenseVector set(Vector v) {
+ return new DenseVector(v.getEntries());
+ }
+
+ /**
+ * Sets the value of index
+ *
+ * @param index
+ * @param value
+ */
+ public void set(int index, double value) {
+ // If entries are null, create new object
+ if (this.entries == null) {
+ this.entries = new MapWritable();
+ }
+
+ this.entries.put(new IntWritable(index), new DoubleEntry(value));
+ }
+
public void setRow(int row) {
this.entries.put(new Text("row"), new IntWritable(row));
}
+ /**
+ * Gets the value of index
+ *
+ * @param index
+ * @return the value of v(index)
+ * @throws IOException
+ */
+ public double get(int index) {
+ double value;
+ try {
+ value = ((DoubleEntry) this.entries.get(new IntWritable(index)))
+ .getValue();
+ } catch (NullPointerException e) {
+ throw new NullPointerException("Unexpected null value : " + e.toString());
+ }
+
+ return value;
+ }
+
public int getRow() {
return ((IntWritable) this.entries.get(new Text("row"))).get();
}
/**
+ * Adds the value to v(index)
+ *
+ * @param index
+ * @param value
+ */
+ public void add(int index, double value) {
+ set(index, get(index) + value);
+ }
+
+ /**
* x = alpha*v + x
*
* @param alpha
@@ -74,9 +128,11 @@
if (alpha == 0)
return this;
- for (int i = 0; i < this.size(); i++) {
- set(i, alpha * v.get(i) + get(i));
+ for (Map.Entry<Writable, Writable> e : this.getEntries().entrySet()) {
+ int key = ((IntWritable) e.getKey()).get();
+ this.add(key, alpha * v.get(key));
}
+
return this;
}
@@ -94,9 +150,8 @@
}
for (Map.Entry<Writable, Writable> e : this.getEntries().entrySet()) {
- double value = ((DoubleEntry) e.getValue()).getValue()
- + v2.get(((IntWritable) e.getKey()).get());
- this.entries.put(e.getKey(), new DoubleEntry(value));
+ int key = ((IntWritable) e.getKey()).get();
+ this.add(key, v2.get(key));
}
return this;
@@ -151,17 +206,7 @@
return getNormInf();
}
- /**
- * Sets the vector
- *
- * @param v
- * @return x = v
- */
- public DenseVector set(Vector v) {
- return new DenseVector(v.getEntries());
- }
-
- public double getNorm1() {
+ protected double getNorm1() {
double sum = 0.0;
Set<Writable> keySet = this.entries.keySet();
@@ -174,7 +219,7 @@
return sum;
}
- public double getNorm2() {
+ protected double getNorm2() {
double square_sum = 0.0;
Set<Writable> keySet = entries.keySet();
@@ -189,57 +234,37 @@
}
/**
- * Gets the value of index
+ * Returns the robust norm of the vector
*
- * @param index
- * @return the value of v(index)
- * @throws IOException
+ * @return the robust norm of the vector
*/
- public double get(int index) {
- double value;
- try {
- value = ((DoubleEntry) this.entries.get(new IntWritable(index)))
- .getValue();
- } catch (NullPointerException e) {
- throw new NullPointerException("Unexpected null value : " + e.toString());
+ protected double getNorm2Robust() {
+ double scale = 0, ssq = 1;
+ for (int i = 0; i < this.size(); i++) {
+ double val = get(i);
+ if (val != 0) {
+ double absxi = Math.abs(val);
+ if (scale < absxi) {
+ ssq = 1 + ssq * Math.pow(scale / absxi, 2);
+ scale = absxi;
+ } else
+ ssq = ssq + Math.pow(absxi / scale, 2);
+ }
}
-
- return value;
+ return scale * Math.sqrt(ssq);
}
/**
- * Sets the value of index
+ * Returns the infinity norm of the vector
*
- * @param index
- * @param value
+ * @return the infinity norm of the vector
*/
- public void set(int index, double value) {
- // If entries are null, create new object
- if (this.entries == null) {
- this.entries = new MapWritable();
+ protected double getNormInf() {
+ double max = 0.0;
+ for (int i = 0; i < this.size(); i++) {
+ max = Math.max(max, Math.abs(get(i)));
}
-
- this.entries.put(new IntWritable(index), new DoubleEntry(value));
- }
-
- /**
- * Adds the value to v(index)
- *
- * @param index
- * @param value
- */
- public void add(int index, double value) {
- set(index, get(index) + value);
- }
-
- public double getNorm2Robust() {
- // TODO Auto-generated method stub
- return 0;
- }
-
- public double getNormInf() {
- // TODO Auto-generated method stub
- return 0;
+ return max;
}
/**
Modified: incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Matrix.java?rev=752818&r1=752817&r2=752818&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Matrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Matrix.java Thu Mar 12 09:56:35 2009
@@ -228,8 +228,17 @@
* Supported matrix-norms.
*/
enum Norm {
+ /** Maximum absolute row sum */
+ One,
+
+ /** The root of sum of the sum of squares */
+ Frobenius,
+
/** Largest entry in absolute value */
- Infinity
+ Infinity,
+
+ /** Largest entry in absolute value. Not a proper matrix norm */
+ Maxvalue
}
/**
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=752818&r1=752817&r2=752818&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java Thu Mar 12 09:56:35 2009
@@ -38,6 +38,8 @@
private static final double cosine = 0.6978227007909176;
private static final double norm1 = 12.0;
private static final double norm2 = 6.782329983125268;
+ private static final double normInf = 5.0;
+ private static final double norm2Robust = 6.782329983125269;
private static double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } };
private static DenseMatrix m1;
private static DenseVector v1;
@@ -90,19 +92,31 @@
* Test norm one
*/
public void testNom1() {
- double result = ((DenseVector) v1).getNorm1();
- assertEquals(norm1, result);
+ assertEquals(norm1, v1.norm(Vector.Norm.One));
}
/**
* Test norm two
*/
public void testNom2() {
- double result = ((DenseVector) v1).getNorm2();
- assertEquals(norm2, result);
+ assertEquals(norm2, v1.norm(Vector.Norm.Two));
}
/**
+ * Test infinity norm
+ */
+ public void testNormInf() {
+ assertEquals(normInf, v1.norm(Vector.Norm.Infinity));
+ }
+
+ /**
+ * Test infinity norm
+ */
+ public void testNorm2Robust() {
+ assertEquals(norm2Robust, v1.norm(Vector.Norm.TwoRobust));
+ }
+
+ /**
* Test scaling
*/
public void scalingTest() {