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 2010/05/10 07:01:40 UTC
svn commit: r942658 [2/2] - in /incubator/hama/trunk: ./
src/examples/org/apache/hama/examples/
src/examples/org/apache/hama/examples/mapreduce/
src/java/org/apache/hama/mapreduce/ src/java/org/apache/hama/matrix/
src/java/org/apache/hama/matrix/algebr...
Added: incubator/hama/trunk/src/test/org/apache/hama/examples/TestMatrixMult.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/examples/TestMatrixMult.java?rev=942658&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/examples/TestMatrixMult.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/examples/TestMatrixMult.java Mon May 10 05:01:39 2010
@@ -0,0 +1,75 @@
+package org.apache.hama.examples;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.hama.HamaCluster;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.matrix.Matrix;
+
+public class TestMatrixMult extends HamaCluster {
+ private int SIZE = 8;
+ private Matrix m1;
+ private Matrix m2;
+ private HamaConfiguration conf;
+
+ /**
+ * @throws UnsupportedEncodingException
+ */
+ public TestMatrixMult() throws UnsupportedEncodingException {
+ super();
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+
+ conf = getConf();
+
+ m1 = RandomMatrix.random_mapred(conf, SIZE, SIZE);
+ m2 = RandomMatrix.random_mapred(conf, SIZE, SIZE);
+ }
+
+ /**
+ * Test matrices multiplication
+ *
+ * @throws IOException
+ */
+ public void testMult() throws IOException {
+ Matrix result = MatrixMultiplication.mult(m1, m2);
+
+ assertEquals(result.getRows(), SIZE);
+ assertEquals(result.getColumns(), SIZE);
+
+ Matrix result2 = MatrixMultiplication.mult(m1, m2, 4);
+
+ verifyMultResult(m1, m2, result);
+ verifyMultResult(m1, m2, result2);
+ }
+
+ /**
+ * Verifying multiplication result
+ *
+ * @param m1
+ * @param m2
+ * @param result
+ * @throws IOException
+ */
+ private void verifyMultResult(Matrix m1, Matrix m2, Matrix result)
+ throws IOException {
+ double[][] c = new double[SIZE][SIZE];
+
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ for (int k = 0; k < SIZE; k++) {
+ c[i][k] += m1.get(i, j) * m2.get(j, k);
+ }
+ }
+ }
+
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ assertTrue((Math.abs(c[i][j] - result.get(i, j)) < .0000001));
+ }
+ }
+ }
+}
Modified: incubator/hama/trunk/src/test/org/apache/hama/mapreduce/TestBlockMatrixMapReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/mapreduce/TestBlockMatrixMapReduce.java?rev=942658&r1=942657&r2=942658&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/mapreduce/TestBlockMatrixMapReduce.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/mapreduce/TestBlockMatrixMapReduce.java Mon May 10 05:01:39 2010
@@ -24,6 +24,7 @@ import java.io.IOException;
import org.apache.hama.HamaCluster;
import org.apache.hama.matrix.DenseMatrix;
import org.apache.log4j.Logger;
+import org.apache.hama.examples.MatrixMultiplication;
public class TestBlockMatrixMapReduce extends HamaCluster {
static final Logger LOG = Logger.getLogger(TestBlockMatrixMapReduce.class);
@@ -39,7 +40,7 @@ public class TestBlockMatrixMapReduce ex
DenseMatrix m1 = DenseMatrix.random(conf, SIZE, SIZE);
DenseMatrix m2 = DenseMatrix.random(conf, SIZE, SIZE);
- DenseMatrix c = (DenseMatrix) m1.mult(m2, 16);
+ DenseMatrix c = MatrixMultiplication.mult(m1, m2, 16);
double[][] mem = new double[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
Modified: incubator/hama/trunk/src/test/org/apache/hama/mapreduce/TestRandomMatrixMapReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/mapreduce/TestRandomMatrixMapReduce.java?rev=942658&r1=942657&r2=942658&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/mapreduce/TestRandomMatrixMapReduce.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/mapreduce/TestRandomMatrixMapReduce.java Mon May 10 05:01:39 2010
@@ -25,12 +25,14 @@ import org.apache.hama.HamaCluster;
import org.apache.hama.matrix.DenseMatrix;
import org.apache.hama.matrix.SparseMatrix;
import org.apache.log4j.Logger;
+import org.apache.hama.examples.mapreduce.*;
+import org.apache.hama.examples.*;
public class TestRandomMatrixMapReduce extends HamaCluster {
static final Logger LOG = Logger.getLogger(TestRandomMatrixMapReduce.class);
public void testRandomMatrixMapReduce() throws IOException {
- DenseMatrix rand = DenseMatrix.random_mapred(conf, 20, 20);
+ DenseMatrix rand = RandomMatrix.random_mapred(conf, 20, 20);
assertEquals(20, rand.getRows());
assertEquals(20, rand.getColumns());
@@ -42,7 +44,7 @@ public class TestRandomMatrixMapReduce e
rand.close();
- SparseMatrix rand2 = SparseMatrix.random_mapred(conf, 20, 20, 30);
+ SparseMatrix rand2 = RandomMatrix.random_mapred(conf, 20, 20, 30);
assertEquals(20, rand2.getRows());
assertEquals(20, rand2.getColumns());
boolean zeroAppear = false;
Modified: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestAbstractMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestAbstractMatrix.java?rev=942658&r1=942657&r2=942658&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestAbstractMatrix.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestAbstractMatrix.java Mon May 10 05:01:39 2010
@@ -5,6 +5,7 @@ import java.io.UnsupportedEncodingExcept
import org.apache.hama.HamaCluster;
import org.apache.hama.HamaConfiguration;
+import org.apache.hama.examples.MatrixNorm;
import org.apache.hama.matrix.Matrix.Norm;
import org.apache.log4j.Logger;
@@ -48,22 +49,22 @@ public class TestAbstractMatrix extends
}
public void normTest(Matrix matrix) throws IOException {
- double norm1 = matrix.norm(Norm.One);
+ double norm1 = MatrixNorm.norm(matrix, Norm.One);
double verify_norm1 = MatrixTestCommon.verifyNorm1(matrix);
gap = norm1 - verify_norm1;
assertTrue(gap < 0.000001 && gap > -0.000001);
- double normInfinity = matrix.norm(Norm.Infinity);
+ double normInfinity = MatrixNorm.norm(matrix, Norm.Infinity);
double verify_normInf = MatrixTestCommon.verifyNormInfinity(matrix);
gap = normInfinity - verify_normInf;
assertTrue(gap < 0.000001 && gap > -0.000001);
- double normFrobenius = matrix.norm(Norm.Frobenius);
+ double normFrobenius = MatrixNorm.norm(matrix, Norm.Frobenius);
double verify_normFrobenius = MatrixTestCommon.verifyNormFrobenius(matrix);
gap = normFrobenius - verify_normFrobenius;
assertTrue(gap < 0.000001 && gap > -0.000001);
- double normMaxValue = matrix.norm(Norm.Maxvalue);
+ double normMaxValue = MatrixNorm.norm(matrix, Norm.Maxvalue);
double verify_normMV = MatrixTestCommon.verifyNormMaxValue(matrix);
gap = normMaxValue - verify_normMV;
assertTrue(gap < 0.000001 && gap > -0.000001);
Modified: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseMatrix.java?rev=942658&r1=942657&r2=942658&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseMatrix.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseMatrix.java Mon May 10 05:01:39 2010
@@ -66,13 +66,6 @@ public class TestDenseMatrix extends Ham
} catch (IOException e) {
LOG.info(e.toString());
}
-
- try {
- m1.mult(m4);
- fail("Matrix-Mult should be failed while A.columns!=B.rows.");
- } catch (IOException e) {
- LOG.info(e.toString());
- }
double origin = m1.get(1, 1);
m1.add(1, 1, 0.5);
@@ -80,7 +73,6 @@ public class TestDenseMatrix extends Ham
matrixAdd(m1, m2);
multMatrixAdd(m1, m2, m3);
- matrixMult(m1, m2);
addAlphaMatrix(m1, m2);
getRowColumnVector();
@@ -131,20 +123,6 @@ public class TestDenseMatrix extends Ham
}
}
- /**
- * Test matrices multiplication
- *
- * @throws IOException
- */
- public void matrixMult(Matrix m1, Matrix m2) throws IOException {
- Matrix result = m1.mult(m2);
-
- assertEquals(result.getRows(), SIZE);
- assertEquals(result.getColumns(), SIZE);
-
- verifyMultResult(m1, m2, result);
- }
-
public void addAlphaMatrix(Matrix m1, Matrix m2) throws IOException {
double value = m1.get(0, 0) + (m2.get(0, 0) * 0.1);
Matrix result = m1.add(0.1, m2);
@@ -227,31 +205,4 @@ public class TestDenseMatrix extends Ham
x++;
}
}
-
- /**
- * Verifying multiplication result
- *
- * @param m1
- * @param m2
- * @param result
- * @throws IOException
- */
- private void verifyMultResult(Matrix m1, Matrix m2, Matrix result)
- throws IOException {
- double[][] c = new double[SIZE][SIZE];
-
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- for (int k = 0; k < SIZE; k++) {
- c[i][k] += m1.get(i, j) * m2.get(j, k);
- }
- }
- }
-
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- assertTrue((Math.abs(c[i][j] - result.get(i, j)) < .0000001));
- }
- }
- }
}
Modified: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestMatrixVectorMult.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestMatrixVectorMult.java?rev=942658&r1=942657&r2=942658&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestMatrixVectorMult.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestMatrixVectorMult.java Mon May 10 05:01:39 2010
@@ -25,6 +25,7 @@ import java.io.UnsupportedEncodingExcept
import org.apache.hama.HamaCluster;
import org.apache.hama.HamaConfiguration;
import org.apache.log4j.Logger;
+import org.apache.hama.examples.MatrixMultiplication;
public class TestMatrixVectorMult extends HamaCluster {
static final Logger LOG = Logger.getLogger(TestMatrixVectorMult.class);
@@ -57,7 +58,7 @@ public class TestMatrixVectorMult extend
}
public void testMatVectorMult() throws IOException {
- DenseMatrix c = (DenseMatrix) m1.mult(m2);
+ DenseMatrix c = MatrixMultiplication.mult(m1, m2);
assertTrue(m1.getRows() == 2);
for (int i = 0; i < c.getRows(); i++) {
Modified: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java?rev=942658&r1=942657&r2=942658&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java Mon May 10 05:01:39 2010
@@ -29,6 +29,7 @@ import org.apache.hadoop.hbase.util.Byte
import org.apache.hama.Constants;
import org.apache.hama.HamaCluster;
import org.apache.hama.HamaConfiguration;
+import org.apache.hama.examples.MatrixMultiplication;
import org.apache.hama.util.BytesUtil;
import org.apache.log4j.Logger;
@@ -66,7 +67,7 @@ public class TestSingularValueDecomposit
public void testEigenSingularValues() throws IOException {
Matrix aT = m1.transpose();
- DenseMatrix aTa = (DenseMatrix) aT.mult(m1);
+ DenseMatrix aTa = MatrixMultiplication.mult(aT, m1);
for (int i = 0; i < m1.getRows(); i++) {
for (int j = 0; j < m1.getRows(); j++) {