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/10/07 14:04:05 UTC
svn commit: r822682 [2/2] - in /incubator/hama/trunk: ./
src/java/org/apache/hama/ src/java/org/apache/hama/matrix/ src/test/
src/test/org/apache/hama/ src/test/org/apache/hama/examples/
src/test/org/apache/hama/graph/ src/test/org/apache/hama/mapred/ ...
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=822682&r1=822681&r2=822682&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 Wed Oct 7 12:04:03 2009
@@ -20,150 +20,72 @@
package org.apache.hama.matrix;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.util.Iterator;
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.io.Writable;
-import org.apache.hama.HamaAdmin;
-import org.apache.hama.HamaAdminImpl;
+import org.apache.hama.HamaCluster;
import org.apache.hama.HamaConfiguration;
import org.apache.hama.io.DoubleEntry;
-import org.apache.hama.matrix.Matrix.Norm;
import org.apache.hama.util.RandomVariable;
import org.apache.log4j.Logger;
/**
* Matrix test
*/
-public class TestDenseMatrix extends TestCase {
+public class TestDenseMatrix extends HamaCluster {
static final Logger LOG = Logger.getLogger(TestDenseMatrix.class);
- private static int SIZE = 10;
- private static Matrix m1;
- private static Matrix m2;
- private static Matrix m3;
- private static Matrix m4, m5;
- private final static String aliase1 = "matrix_aliase_A";
- private final static String aliase2 = "matrix_aliase_B";
- private static HamaConfiguration conf;
- private static HBaseAdmin admin;
- private static HamaAdmin hamaAdmin;
-
- public static Test suite() {
- TestSetup setup = new TestSetup(new TestSuite(TestDenseMatrix.class)) {
- protected void setUp() throws Exception {
- HCluster hCluster = new HCluster();
- hCluster.setUp();
-
- conf = hCluster.getConf();
- admin = new HBaseAdmin(conf);
- hamaAdmin = new HamaAdminImpl(conf, admin);
-
- m1 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE);
- m2 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE);
- m3 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE);
- m4 = DenseMatrix.random(hCluster.getConf(), SIZE-2, SIZE-2);
- m5 = DenseMatrix.random(hCluster.getConf(), SIZE, SIZE);
- }
-
- protected void tearDown() {
- try {
- closeTest();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- };
- return setup;
- }
-
- public static void closeTest() throws IOException {
- m1.close();
- m2.close();
- }
-
- public void testEntryAdd() throws IOException {
- double origin = m1.get(1, 1);
- m1.add(1, 1, 0.5);
-
- assertEquals(m1.get(1, 1), origin + 0.5);
- }
-
- public void testGet() throws IOException {
- boolean ex = false;
- try {
- m1.get(SIZE + 1, SIZE + 1);
- } catch (ArrayIndexOutOfBoundsException e) {
- ex = true;
- }
- assertTrue(ex);
- assertTrue(m1.get(0, 0) > 0);
- }
-
- public void testTranspose() throws IOException {
- DenseMatrix trans = (DenseMatrix) m1.transpose();
- for(int i = 0; i < trans.getRows(); i++) {
- for(int j = 0; j < trans.getColumns(); j++) {
- assertEquals(trans.get(i, j), m1.get(j, i));
- }
- }
- }
+ private int SIZE = 10;
+ private Matrix m1;
+ private Matrix m2;
+ private HamaConfiguration conf;
/**
- * Column vector test.
- *
- * @param rand
- * @throws IOException
+ * @throws UnsupportedEncodingException
*/
- public void testGetColumn() throws IOException {
- Vector v = m1.getColumn(0);
- Iterator<Writable> it = v.iterator();
- int x = 0;
- while (it.hasNext()) {
- assertEquals(m1.get(x, 0), ((DoubleEntry) it.next()).getValue());
- x++;
- }
+ public TestDenseMatrix() throws UnsupportedEncodingException {
+ super();
}
- public void testGetSetAttribute() throws IOException {
- m1.setRowLabel(0, "row1");
- assertEquals(m1.getRowLabel(0), "row1");
- assertEquals(m1.getRowLabel(1), null);
-
- m1.setColumnLabel(0, "column1");
- assertEquals(m1.getColumnLabel(0), "column1");
- assertEquals(m1.getColumnLabel(1), null);
- }
+ public void setUp() throws Exception {
+ super.setUp();
- public void testSubMatrix() throws IOException {
- SubMatrix a = m1.subMatrix(2, 4, 2, 5); // A : 3 * 4
- for (int i = 0; i < a.getRows(); i++) {
- for (int j = 0; j < a.getColumns(); j++) {
- assertEquals(a.get(i, j), m1.get(i + 2, j + 2));
- }
- }
+ conf = getConf();
- SubMatrix b = m2.subMatrix(0, 3, 0, 2); // B : 4 * 3
- SubMatrix c = a.mult(b);
+ m1 = DenseMatrix.random(conf, SIZE, SIZE);
+ m2 = DenseMatrix.random(conf, SIZE, SIZE);
+ }
- double[][] C = new double[3][3]; // A * B
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- for (int k = 0; k < 4; k++) {
- C[i][j] += m1.get(i + 2, k + 2) * m2.get(k, j);
- }
- }
+ public void testAddMult() throws IOException {
+ Matrix m3 = DenseMatrix.random(conf, SIZE, SIZE);
+ Matrix m4 = DenseMatrix.random(conf, SIZE - 2, SIZE - 2);
+ try {
+ m1.add(m4);
+ fail("Matrix-Addition should be failed while rows and columns aren't same.");
+ } catch (IOException e) {
+ LOG.info(e.toString());
}
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- assertEquals(C[i][j], c.get(i, j));
- }
+ 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);
+ assertEquals(m1.get(1, 1), origin + 0.5);
+
+ matrixAdd(m1, m2);
+ multMatrixAdd(m1, m2, m3);
+ matrixMult(m1, m2);
+ addAlphaMatrix(m1, m2);
+
+ getRowColumn();
+ setRowColumn();
+ setMatrix(m1);
+ setAlphaMatrix(m1);
}
/**
@@ -171,7 +93,7 @@
*
* @throws IOException
*/
- public void testMatrixAdd() throws IOException {
+ public void matrixAdd(Matrix m1, Matrix m2) throws IOException {
Matrix result = m1.add(m2);
assertEquals(result.getRows(), SIZE);
@@ -193,15 +115,16 @@
}
}
- public void testMultiMatrixAdd() throws IOException {
- Matrix result = ((DenseMatrix)m1).add(m2, m3);
-
+ public void multMatrixAdd(Matrix m1, Matrix m2, Matrix m3) throws IOException {
+ Matrix result = ((DenseMatrix) m1).add(m2, m3);
+
assertEquals(result.getRows(), SIZE);
assertEquals(result.getColumns(), SIZE);
-
+
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
- assertEquals(result.get(i, j), m1.get(i, j) + m2.get(i, j) + m3.get(i, j));
+ assertEquals(result.get(i, j), m1.get(i, j) + m2.get(i, j)
+ + m3.get(i, j));
}
}
}
@@ -211,7 +134,7 @@
*
* @throws IOException
*/
- public void testMatrixMult() throws IOException {
+ public void matrixMult(Matrix m1, Matrix m2) throws IOException {
Matrix result = m1.mult(m2);
assertEquals(result.getRows(), SIZE);
@@ -220,7 +143,13 @@
verifyMultResult(m1, m2, result);
}
- public void testSetMatrix() throws IOException {
+ 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);
+ assertEquals(value, result.get(0, 0));
+ }
+
+ public void setMatrix(Matrix m1) throws IOException {
Matrix a = new DenseMatrix(conf, m1.getRows(), m1.getColumns());
a.set(m1);
@@ -232,53 +161,45 @@
}
}
- public void testSetAlphaMatrix() throws IOException {
+ public void setAlphaMatrix(Matrix m1) throws IOException {
Matrix a = new DenseMatrix(conf, m1.getRows(), m1.getColumns());
a.set(0.5, m1);
-
+
for (int i = 0; i < 5; i++) {
int x = RandomVariable.randInt(0, SIZE - 1);
int y = RandomVariable.randInt(0, SIZE - 1);
assertEquals(a.get(x, y), (m1.get(x, y) * 0.5));
}
}
-
- public void testAddAlphaMatrix() throws IOException {
- double value = m1.get(0, 0) + (m2.get(0, 0) * 0.1);
- Matrix result = m1.add(0.1, m2);
- assertEquals(value, result.get(0, 0));
- }
-
- public void testNorm() throws IOException {
- double gap = 0.000001;
-
- double norm1 = m1.norm(Norm.One);
- double verify_norm1 = MatrixTestCommon.verifyNorm1(m1);
- gap = norm1 - verify_norm1;
- LOG.info("Norm One : gap " + gap);
- assertTrue(gap < 0.000001 && gap > -0.000001);
-
- double normInfinity = m1.norm(Norm.Infinity);
- double verify_normInf = MatrixTestCommon.verifyNormInfinity(m1);
- gap = normInfinity - verify_normInf;
- LOG.info("Norm Infinity : gap " + gap);
- assertTrue(gap < 0.000001 && gap > -0.000001);
+
+ public void getRowColumn() throws IOException {
+ boolean ex = false;
+ try {
+ m1.get(SIZE + 1, SIZE + 1);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ ex = true;
+ }
+ assertTrue(ex);
+ assertTrue(m1.get(0, 0) > 0);
- double normMaxValue = m1.norm(Norm.Maxvalue);
- double verify_normMV = MatrixTestCommon.verifyNormMaxValue(m1);
- gap = normMaxValue - verify_normMV;
- LOG.info("Norm MaxValue : gap " + gap);
- assertTrue(gap < 0.000001 && gap > -0.000001);
+ Vector v = m1.getColumn(0);
+ Iterator<Writable> it = v.iterator();
+ int x = 0;
+ while (it.hasNext()) {
+ assertEquals(m1.get(x, 0), ((DoubleEntry) it.next()).getValue());
+ x++;
+ }
- double normFrobenius = m1.norm(Norm.Frobenius);
- double verify_normFrobenius = MatrixTestCommon.verifyNormFrobenius(m1);
- gap = normFrobenius - verify_normFrobenius;
- LOG.info("Norm Frobenius : " + normFrobenius + "(map/reduce) - " + verify_normFrobenius + " expected");
- LOG.info("Norm Frobenius : gap " + gap);
- assertTrue(gap < 0.000001 && gap > -0.000001);
+ m1.setRowLabel(0, "row1");
+ assertEquals(m1.getRowLabel(0), "row1");
+ assertEquals(m1.getRowLabel(1), null);
+
+ m1.setColumnLabel(0, "column1");
+ assertEquals(m1.getColumnLabel(0), "column1");
+ assertEquals(m1.getColumnLabel(1), null);
}
- public void testSetRow() throws IOException {
+ public void setRowColumn() throws IOException {
Vector v = new DenseVector();
double[] entries = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
@@ -294,224 +215,16 @@
assertEquals(entries[i], ((DoubleEntry) it.next()).getValue());
i++;
}
- }
-
- public void testSetColumn() throws IOException {
- Vector v = new DenseVector();
- double[] entries = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
-
- for (int i = 0; i < SIZE; i++) {
- v.set(i, entries[i]);
- }
m1.setColumn(SIZE, v);
- Iterator<Writable> it = m1.getColumn(SIZE).iterator();
-
- int i = 0;
- while (it.hasNext()) {
- assertEquals(entries[i], ((DoubleEntry) it.next()).getValue());
- i++;
- }
- }
-
- public void testJacobiEigenValue() throws IOException {
- // copy Matrix m5 to the array
- double[][] S = new double[SIZE][SIZE];
-
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- S[i][j] = m5.get(i, j);
- }
- }
-
- // do m/r jacobi eigen value computation
- DenseMatrix dm = (DenseMatrix)m5;
- dm.jacobiEigenValue(3);
-
- // do jacobi egien value over S array
- int i, j, k, l, m, state;
- double s, c, t, p, y;
- double e1, e2;
- // index array
- int[] ind = new int[SIZE];
- boolean[] changed = new boolean[SIZE];
-
- // output
- double[] e = new double[SIZE];
- double[][] E = new double[SIZE][SIZE];
-
- // init e & E; ind & changed
- for(i=0; i<SIZE; i++) {
- for(j=0; j<SIZE; j++) {
- E[i][j] = 0;
- }
- E[i][i] = 1;
- }
-
- state = SIZE;
-
- for(i=0; i<SIZE; i++) {
- ind[i] = maxind(S, i, SIZE);
- e[i] = S[i][i];
- changed[i] = true;
- }
-
- int loops = 3;
- // next rotation
- while(state != 0 && loops > 0) {
- // find index(k, l) for pivot p
- m = 0;
- for(k = 1; k <= SIZE-2; k++) {
- if(Math.abs(S[m][ind[m]]) < Math.abs(S[k][ind[k]])) {
- m = k;
- }
- }
-
- k = m; l = ind[m]; p = S[k][l];
-
- // calculate c = cos, s = sin
- y = (e[l] - e[k]) / 2;
- t = Math.abs(y) + Math.sqrt(p * p + y * y);
- s = Math.sqrt(p * p + t * t);
- c = t / s;
- s = p / s;
- t = (p * p) / t;
- if(y < 0) {
- s = -s;
- t = -t;
- }
-
- S[k][l] = 0.0;
- state = update(e, changed, k, -t, state);
- state = update(e, changed, l, t, state);
-
- for(i = 0; i <= k-1; i++)
- rotate(S, i, k, i, l, c, s);
-
- for(i = l+1; i < SIZE; i++)
- rotate(S, k, i, l, i, c, s);
-
- for(i = k+1; i <= l-1; i++)
- rotate(S, k, i, i, l, c, s);
-
- // rotate eigenvectors
- for(i = 0; i < SIZE; i++) {
- e1 = E[k][i];
- e2 = E[l][i];
-
- E[k][i] = c * e1 - s * e2;
- E[l][i] = s * e1 + c * e2;
- }
-
- ind[k] = maxind(S, k, SIZE);
- ind[l] = maxind(S, l, SIZE);
-
- loops --;
- }
-
- // verify the results
- assertTrue(dm.verifyEigenValue(e, E));
- }
-
- public void testEnsureForAddition() {
- try {
- m1.add(m4);
- fail("Matrix-Addition should be failed while rows and columns aren't same.");
- } catch (IOException e) {
- LOG.info(e.toString());
- }
- }
+ Iterator<Writable> it2 = m1.getColumn(SIZE).iterator();
- public void testEnsureForMultiplication() {
- try {
- m1.mult(m4);
- fail("Matrix-Mult should be failed while A.columns!=B.rows.");
- } catch (IOException e) {
- LOG.info(e.toString());
- }
- }
-
- public void testLoadSave() throws IOException {
- String path1 = m1.getPath();
- // save m1 to aliase1
- m1.save(aliase1);
- // load matrix m1 using aliase1
- DenseMatrix loadTest = new DenseMatrix(conf, aliase1, false);
-
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- assertEquals(m1.get(i, j), loadTest.get(i, j));
- }
- }
-
- assertEquals(path1, loadTest.getPath());
- // close loadTest, it just disconnect to the table but didn't delete it.
- loadTest.close();
-
- // try to close m1 & load matrix m1 using aliase1 again.
- m1.close();
- DenseMatrix loadTest2 = new DenseMatrix(conf, aliase1, false);
- assertEquals(path1, loadTest2.getPath());
- // remove aliase1
- // because loadTest2 connect the aliase1, so we just remove aliase entry
- // but didn't delete the table.
- hamaAdmin.delete(aliase1);
- assertEquals(true, admin.tableExists(path1));
- // close loadTest2, because it is the last one who reference table 'path1'
- // it will do the gc!
- loadTest2.close();
- assertEquals(false, admin.tableExists(path1));
-
- // if we try to load non-existed matrix using aliase name, it should fail.
- DenseMatrix loadTest3 = null;
- try {
- loadTest3 = new DenseMatrix(conf, aliase1, false);
- fail("Try to load a non-existed matrix should fail!");
- } catch (IOException e) {
-
- } finally {
- if (loadTest3 != null)
- loadTest3.close();
+ int x = 0;
+ while (it2.hasNext()) {
+ assertEquals(entries[x], ((DoubleEntry) it2.next()).getValue());
+ x++;
}
}
-
- public void testForceCreate() throws IOException {
- String path2 = m2.getPath();
- // save m2 to aliase2
- m2.save(aliase2);
- // load matrix m2 using aliase2
- DenseMatrix loadTest = new DenseMatrix(conf, aliase2, false);
-
- for (int i = 0; i < loadTest.getRows(); i++) {
- for (int j = 0; j < loadTest.getColumns(); j++) {
- assertEquals(m2.get(i, j), loadTest.get(i, j));
- }
- }
-
- assertEquals(path2, loadTest.getPath());
-
- Matrix test = hamaAdmin.getMatrix(aliase2);
- assertEquals(test.getType(), "DenseMatrix");
-
- // force to create matrix loadTest2 using aliasename 'aliase2'
- DenseMatrix loadTest2 = new DenseMatrix(conf, aliase2, true);
- String loadPath2 = loadTest2.getPath();
- assertFalse(path2.equals(loadPath2));
- assertEquals(loadPath2, hamaAdmin.getPath(aliase2));
- assertFalse(path2.equals(hamaAdmin.getPath(aliase2)));
-
- // try to close m2 & loadTest, it table will be deleted finally
- m2.close();
- assertEquals(true, admin.tableExists(path2));
- loadTest.close();
- assertEquals(false, admin.tableExists(path2));
-
- // remove 'aliase2' & close loadTest2
- loadTest2.close();
- assertEquals(true, admin.tableExists(loadPath2));
- hamaAdmin.delete(aliase2);
- assertEquals(false, admin.tableExists(loadPath2));
- }
/**
* Verifying multiplication result
@@ -539,34 +252,4 @@
}
}
}
-
- //index of largest off-diagonal element in row k
- int maxind(double[][] S, int row, int size) {
- int m = row + 1;
- for(int i=row + 2; i<size; i++) {
- if(Math.abs(S[row][i]) > Math.abs(S[row][m]))
- m = i;
- }
- return m;
- }
-
- int update(double[] e, boolean[] changed, int row, double value, int state) {
- double y = e[row];
- e[row] += value;
-
- if(changed[row] && y == e[row]) {
- changed[row] = false;
- return state - 1;
- } else if(!changed[row] && y != e[row]) {
- changed[row] = true;
- return state + 1;
- } else
- return state;
- }
-
- void rotate(double[][] S, int k, int l, int i, int j, double c, double s) {
- double s1 = S[k][l], s2 = S[i][j];
- S[k][l] = c * s1 - s * s2;
- S[i][j] = s * s1 + c * s2;
- }
}
Modified: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseVector.java?rev=822682&r1=822681&r2=822682&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseVector.java Wed Oct 7 12:04:03 2009
@@ -1,229 +1,204 @@
-/**
- * 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.matrix;
-
-import java.io.IOException;
-import java.util.Iterator;
-
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.io.Writable;
-import org.apache.hama.io.DoubleEntry;
-import org.apache.hama.matrix.DenseMatrix;
-import org.apache.hama.matrix.DenseVector;
-import org.apache.hama.matrix.Vector;
-
-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;
- 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;
- private static DenseVector v2;
- private static DenseVector smallSize = new DenseVector();
-
- public static Test suite() {
- TestSetup setup = new TestSetup(new TestSuite(TestDenseVector.class)) {
- protected void setUp() throws Exception {
- HCluster hCluster = new HCluster();
- hCluster.setUp();
-
- m1 = new DenseMatrix(hCluster.getConf(), 2, 4);
-
- for (int i = 0; i < 2; i++)
- for (int j = 0; j < 4; j++)
- m1.set(i, j, values[i][j]);
-
- v1 = m1.getRow(0);
- v2 = m1.getRow(1);
- smallSize.set(0, 0.5);
- }
-
- protected void tearDown() {
- LOG.info("tearDown()");
- }
- };
- return setup;
- }
-
- /**
- * Test |a| dot |b|
- */
- public void testDot() {
- double cos = v1.dot(v2);
- assertEquals(cos, cosine);
-
- boolean except = false;
- try {
- v1.dot(smallSize);
- } catch (IndexOutOfBoundsException e) {
- except = true;
- }
-
- assertTrue(except);
- }
-
- public void testSubVector() {
- int start = 2;
- Vector subVector = v1.subVector(start, v1.size() - 1);
- Iterator<Writable> it = subVector.iterator();
-
- int i = start;
- while (it.hasNext()) {
- assertEquals(v1.get(i), ((DoubleEntry) it.next()).getValue());
- i++;
- }
- }
-
- /**
- * Test norm one
- */
- public void testNom1() {
- assertEquals(norm1, v1.norm(Vector.Norm.One));
- }
-
- /**
- * Test norm two
- */
- public void testNom2() {
- 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() {
- v2.scale(0.5);
-
- for (int i = 0; i < v2.size(); i++) {
- assertEquals(values[1][i] * 0.5, v2.get(i));
- }
- }
-
- /**
- * Test get/set methods
- * @throws IOException
- */
- public void testGetSet() throws IOException {
- assertEquals(v1.get(0), values[0][0]);
- boolean ex = false;
- try {
- v1.get(5);
- } catch (NullPointerException e) {
- ex = true;
- }
- assertTrue(ex);
- assertEquals(m1.getColumn(0).size(), 2);
- }
-
- /**
- * Test add()
- */
- public void testAdd() {
- v1.add(v2);
- int i = 0;
- Iterator<Writable> it = v1.iterator();
- while (it.hasNext()) {
- DoubleEntry c = (DoubleEntry) it.next();
- assertEquals(c.getValue(), values[0][i] + values[1][i]);
- i++;
- }
-
- v1.add(0.5, v2);
- int j = 0;
- Iterator<Writable> itt = v1.iterator();
- while (itt.hasNext()) {
- DoubleEntry c = (DoubleEntry) 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);
-
- boolean except = false;
- try {
- v1.add(smallSize);
- } catch (IndexOutOfBoundsException e) {
- except = true;
- }
-
- assertTrue(except);
-
- except = false;
- try {
- v1.add(0.6, smallSize);
- } catch (IndexOutOfBoundsException e) {
- except = true;
- }
-
- assertTrue(except);
- }
-
- public void testSet() {
- v1.set(v2);
-
- for(int i = 0; i < v1.size(); i ++) {
- assertEquals(v2.get(i), v1.get(i));
- }
-
- boolean except = false;
- try {
- v1.set(0.6, smallSize);
- } catch (IndexOutOfBoundsException e) {
- except = true;
- }
-
- assertTrue(except);
- }
-
- /**
- * Clear test
- */
- public void testClear() {
- ((DenseVector) v1).clear();
- assertEquals(v1.size(), 0);
- }
-}
+/**
+ * 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.matrix;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.Iterator;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.HamaCluster;
+import org.apache.hama.io.DoubleEntry;
+
+public class TestDenseVector extends HamaCluster {
+ final static Log LOG = LogFactory.getLog(TestDenseVector.class.getName());
+
+ private final double cosine = 0.6978227007909176;
+ private final double norm1 = 12.0;
+ private final double norm2 = 6.782329983125268;
+ private final double normInf = 5.0;
+ private final double norm2Robust = 6.782329983125269;
+ private double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } };
+ private DenseMatrix m1;
+ private DenseVector v1;
+ private DenseVector v2;
+ private DenseVector smallSize = new DenseVector();
+
+ /**
+ * @throws UnsupportedEncodingException
+ */
+ public TestDenseVector() throws UnsupportedEncodingException {
+ super();
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+
+ m1 = new DenseMatrix(getConf(), 2, 4);
+
+ for (int i = 0; i < 2; i++)
+ for (int j = 0; j < 4; j++)
+ m1.set(i, j, values[i][j]);
+
+ v1 = m1.getRow(0);
+ v2 = m1.getRow(1);
+ smallSize.set(0, 0.5);
+ }
+
+ /**
+ * @throws IOException
+ */
+ public void testDenseVector() throws IOException {
+ double cos = v1.dot(v2);
+ assertEquals(cos, cosine);
+
+ boolean except = false;
+ try {
+ v1.dot(smallSize);
+ } catch (IndexOutOfBoundsException e) {
+ except = true;
+ }
+
+ assertTrue(except);
+ subVector();
+
+ assertEquals(norm1, v1.norm(Vector.Norm.One));
+ assertEquals(norm2, v1.norm(Vector.Norm.Two));
+ assertEquals(normInf, v1.norm(Vector.Norm.Infinity));
+ assertEquals(norm2Robust, v1.norm(Vector.Norm.TwoRobust));
+
+ getSetTest();
+ add();
+ scalingTest();
+ setTest();
+ clear();
+ }
+
+ /**
+ * Test scaling
+ */
+ public void scalingTest() {
+ v2.scale(0.5);
+
+ for (int i = 0; i < v2.size(); i++) {
+ assertEquals(values[1][i] * 0.5, v2.get(i));
+ }
+ }
+
+ /**
+ * Test get/set methods
+ * @throws IOException
+ */
+ public void getSetTest() throws IOException {
+ assertEquals(v1.get(0), values[0][0]);
+ boolean ex = false;
+ try {
+ v1.get(5);
+ } catch (NullPointerException e) {
+ ex = true;
+ }
+ assertTrue(ex);
+ assertEquals(m1.getColumn(0).size(), 2);
+ }
+
+ /**
+ * Test add()
+ */
+ public void add() {
+ v1.add(v2);
+ int i = 0;
+ Iterator<Writable> it = v1.iterator();
+ while (it.hasNext()) {
+ DoubleEntry c = (DoubleEntry) it.next();
+ assertEquals(c.getValue(), values[0][i] + values[1][i]);
+ i++;
+ }
+
+ v1.add(0.5, v2);
+ int j = 0;
+ Iterator<Writable> itt = v1.iterator();
+ while (itt.hasNext()) {
+ DoubleEntry c = (DoubleEntry) 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);
+
+ boolean except = false;
+ try {
+ v1.add(smallSize);
+ } catch (IndexOutOfBoundsException e) {
+ except = true;
+ }
+
+ assertTrue(except);
+
+ except = false;
+ try {
+ v1.add(0.6, smallSize);
+ } catch (IndexOutOfBoundsException e) {
+ except = true;
+ }
+
+ assertTrue(except);
+ }
+
+ public void setTest() {
+ v1.set(v2);
+
+ for(int i = 0; i < v1.size(); i ++) {
+ assertEquals(v2.get(i), v1.get(i));
+ }
+
+ boolean except = false;
+ try {
+ v1.set(0.6, smallSize);
+ } catch (IndexOutOfBoundsException e) {
+ except = true;
+ }
+
+ assertTrue(except);
+ }
+
+ public void subVector() {
+ int start = 2;
+ Vector subVector = v1.subVector(start, v1.size() - 1);
+ Iterator<Writable> it = subVector.iterator();
+
+ int i = start;
+ while (it.hasNext()) {
+ assertEquals(v1.get(i), ((DoubleEntry) it.next()).getValue());
+ i++;
+ }
+ }
+
+ /**
+ * Clear test
+ */
+ public void clear() {
+ ((DenseVector) v1).clear();
+ assertEquals(v1.size(), 0);
+ }
+}
Added: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestJacobiEigenValue.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestJacobiEigenValue.java?rev=822682&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestJacobiEigenValue.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestJacobiEigenValue.java Wed Oct 7 12:04:03 2009
@@ -0,0 +1,158 @@
+package org.apache.hama.matrix;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.hama.HamaCluster;
+import org.apache.hama.HamaConfiguration;
+import org.apache.log4j.Logger;
+
+public class TestJacobiEigenValue extends HamaCluster {
+ static final Logger LOG = Logger.getLogger(TestDenseMatrix.class);
+ private int SIZE = 10;
+ private Matrix m5;
+ private HamaConfiguration conf;
+
+ /**
+ * @throws UnsupportedEncodingException
+ */
+ public TestJacobiEigenValue() throws UnsupportedEncodingException {
+ super();
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+
+ conf = getConf();
+ m5 = DenseMatrix.random(conf, SIZE, SIZE);
+ }
+
+ public void testJacobiEigenValue() throws IOException {
+ // copy Matrix m5 to the array
+ double[][] S = new double[SIZE][SIZE];
+
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++) {
+ S[i][j] = m5.get(i, j);
+ }
+ }
+
+ // do m/r jacobi eigen value computation
+ DenseMatrix dm = (DenseMatrix) m5;
+ dm.jacobiEigenValue(3);
+
+ // do jacobi egien value over S array
+ int i, j, k, l, m, state;
+ double s, c, t, p, y;
+ double e1, e2;
+ // index array
+ int[] ind = new int[SIZE];
+ boolean[] changed = new boolean[SIZE];
+
+ // output
+ double[] e = new double[SIZE];
+ double[][] E = new double[SIZE][SIZE];
+
+ // init e & E; ind & changed
+ for (i = 0; i < SIZE; i++) {
+ for (j = 0; j < SIZE; j++) {
+ E[i][j] = 0;
+ }
+ E[i][i] = 1;
+ }
+
+ state = SIZE;
+
+ for (i = 0; i < SIZE; i++) {
+ ind[i] = maxind(S, i, SIZE);
+ e[i] = S[i][i];
+ changed[i] = true;
+ }
+
+ int loops = 3;
+ // next rotation
+ while (state != 0 && loops > 0) {
+ // find index(k, l) for pivot p
+ m = 0;
+ for (k = 1; k <= SIZE - 2; k++) {
+ if (Math.abs(S[m][ind[m]]) < Math.abs(S[k][ind[k]])) {
+ m = k;
+ }
+ }
+
+ k = m;
+ l = ind[m];
+ p = S[k][l];
+
+ // calculate c = cos, s = sin
+ y = (e[l] - e[k]) / 2;
+ t = Math.abs(y) + Math.sqrt(p * p + y * y);
+ s = Math.sqrt(p * p + t * t);
+ c = t / s;
+ s = p / s;
+ t = (p * p) / t;
+ if (y < 0) {
+ s = -s;
+ t = -t;
+ }
+
+ S[k][l] = 0.0;
+ state = update(e, changed, k, -t, state);
+ state = update(e, changed, l, t, state);
+
+ for (i = 0; i <= k - 1; i++)
+ rotate(S, i, k, i, l, c, s);
+ for (i = l + 1; i < SIZE; i++)
+ rotate(S, k, i, l, i, c, s);
+ for (i = k + 1; i <= l - 1; i++)
+ rotate(S, k, i, i, l, c, s);
+
+ // rotate eigenvectors
+ for (i = 0; i < SIZE; i++) {
+ e1 = E[k][i];
+ e2 = E[l][i];
+
+ E[k][i] = c * e1 - s * e2;
+ E[l][i] = s * e1 + c * e2;
+ }
+
+ ind[k] = maxind(S, k, SIZE);
+ ind[l] = maxind(S, l, SIZE);
+
+ loops--;
+ }
+
+ // verify the results
+ assertTrue(dm.verifyEigenValue(e, E));
+ }
+
+ // index of largest off-diagonal element in row k
+ int maxind(double[][] S, int row, int size) {
+ int m = row + 1;
+ for (int i = row + 2; i < size; i++) {
+ if (Math.abs(S[row][i]) > Math.abs(S[row][m]))
+ m = i;
+ }
+ return m;
+ }
+
+ int update(double[] e, boolean[] changed, int row, double value, int state) {
+ double y = e[row];
+ e[row] += value;
+
+ if (changed[row] && y == e[row]) {
+ changed[row] = false;
+ return state - 1;
+ } else if (!changed[row] && y != e[row]) {
+ changed[row] = true;
+ return state + 1;
+ } else
+ return state;
+ }
+
+ void rotate(double[][] S, int k, int l, int i, int j, double c, double s) {
+ double s1 = S[k][l], s2 = S[i][j];
+ S[k][l] = c * s1 - s * s2;
+ S[i][j] = s * s1 + c * s2;
+ }
+}
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=822682&r1=822681&r2=822682&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 Wed Oct 7 12:04:03 2009
@@ -20,56 +20,40 @@
package org.apache.hama.matrix;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
+import org.apache.hama.HamaCluster;
import org.apache.hama.HamaConfiguration;
import org.apache.log4j.Logger;
-public class TestMatrixVectorMult extends TestCase {
+public class TestMatrixVectorMult extends HamaCluster {
static final Logger LOG = Logger.getLogger(TestMatrixVectorMult.class);
- private static Matrix m1;
- private static Matrix m2;
- private static HamaConfiguration conf;
- private static double[][] result = { { 5 }, { 11 } };
-
- public static Test suite() {
- TestSetup setup = new TestSetup(new TestSuite(TestMatrixVectorMult.class)) {
- protected void setUp() throws Exception {
- HCluster hCluster = new HCluster();
- hCluster.setUp();
-
- conf = hCluster.getConf();
-
- m1 = new DenseMatrix(conf, "A", true);
- m1.setDimension(2, 2);
- m1.set(0, 0, 1);
- m1.set(0, 1, 2);
- m1.set(1, 0, 3);
- m1.set(1, 1, 4);
- m2 = new DenseMatrix(conf, "B", true);
- m2.setDimension(2, 1);
- m2.set(0, 0, 1);
- m2.set(1, 0, 2);
- }
-
- protected void tearDown() {
- try {
- closeTest();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- };
- return setup;
+ private Matrix m1, m2;
+ private HamaConfiguration conf;
+ private double[][] result = { { 5 }, { 11 } };
+
+ /**
+ * @throws UnsupportedEncodingException
+ */
+ public TestMatrixVectorMult() throws UnsupportedEncodingException {
+ super();
}
- public static void closeTest() throws IOException {
- m1.close();
- m2.close();
+ public void setUp() throws Exception {
+ super.setUp();
+
+ conf = getConf();
+
+ m1 = new DenseMatrix(conf, "A", true);
+ m1.setDimension(2, 2);
+ m1.set(0, 0, 1);
+ m1.set(0, 1, 2);
+ m1.set(1, 0, 3);
+ m1.set(1, 1, 4);
+ m2 = new DenseMatrix(conf, "B", true);
+ m2.setDimension(2, 1);
+ m2.set(0, 0, 1);
+ m2.set(1, 0, 2);
}
public void testMatVectorMult() throws IOException {
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=822682&r1=822681&r2=822682&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 Wed Oct 7 12:04:03 2009
@@ -20,77 +20,75 @@
package org.apache.hama.matrix;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
+import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hama.HamaCluster;
import org.apache.hama.HamaConfiguration;
import org.apache.hama.matrix.algebra.JacobiEigenValue;
import org.apache.hama.util.BytesUtil;
import org.apache.log4j.Logger;
-public class TestSingularValueDecomposition extends TestCase {
- static final Logger LOG = Logger.getLogger(TestSingularValueDecomposition.class);
- private static DenseMatrix m1;
- private static HamaConfiguration conf;
+public class TestSingularValueDecomposition extends HamaCluster {
+ static final Logger LOG = Logger
+ .getLogger(TestSingularValueDecomposition.class);
+ private DenseMatrix m1;
+ private HamaConfiguration conf;
+
+ /**
+ * @throws UnsupportedEncodingException
+ */
+ public TestSingularValueDecomposition() throws UnsupportedEncodingException {
+ super();
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+
+ conf = getConf();
+ m1 = new DenseMatrix(conf, 2, 2);
+ for (int i = 0; i < 2; i++)
+ for (int j = 0; j < 2; j++)
+ m1.set(i, j, matrixA[i][j]);
+ }
// Let's assume the A = [4 0; 3-5]
- private static double[][] matrixA = {{4, 0}, {3, -5}};
+ private double[][] matrixA = { { 4, 0 }, { 3, -5 } };
// A'A = [25 -15; -15 25]
- private static double[][] values = { { 25, -15 }, { -15, 25 } };
- // Then, eigenvalues of A'A are 10, 40
- private static double[] eigenvalues = { 10, 40};
+ private double[][] values = { { 25, -15 }, { -15, 25 } };
+ // Then, eigenvalues of A'A are 10, 40
+ private double[] eigenvalues = { 10, 40 };
// And, Singular values are 3.1623, 6.3246
- private static double[] singularvalues = { 3.1622776601683795, 6.324555320336759 };
-
- public static Test suite() {
- TestSetup setup = new TestSetup(new TestSuite(TestSingularValueDecomposition.class)) {
- protected void setUp() throws Exception {
- HCluster hCluster = new HCluster();
- hCluster.setUp();
-
- conf = hCluster.getConf();
- m1 = new DenseMatrix(conf, 2, 2);
- for (int i = 0; i < 2; i++)
- for (int j = 0; j < 2; j++)
- m1.set(i, j, matrixA[i][j]);
- }
-
- protected void tearDown() {
- // do nothing
- }
- };
- return setup;
- }
+ private double[] singularvalues = { 3.1622776601683795, 6.324555320336759 };
public void testEigenSingularValues() throws IOException {
Matrix aT = m1.transpose();
DenseMatrix aTa = (DenseMatrix) aT.mult(m1);
-
- for(int i = 0; i < m1.getRows(); i++) {
- for(int j = 0; j < m1.getRows(); j++) {
+
+ for (int i = 0; i < m1.getRows(); i++) {
+ for (int j = 0; j < m1.getRows(); j++) {
assertEquals(aTa.get(i, j), values[i][j]);
}
}
-
+
// Find the eigen/singular values and vectors of A'A
aTa.jacobiEigenValue(1);
HTable table = aTa.getHTable();
- for(int x=0; x<2; x++) {
- double eigenvalue = BytesUtil.bytesToDouble(table.get(BytesUtil.getRowIndex(x),
- Bytes.toBytes(JacobiEigenValue.EIVAL)).getValue());
+ for (int x = 0; x < 2; x++) {
+ Get get = new Get(BytesUtil.getRowIndex(x));
+ get.addColumn(Bytes.toBytes(JacobiEigenValue.EIVAL));
+ double eigenvalue = BytesUtil.bytesToDouble(table.get(get).getCellValue()
+ .getValue());
assertTrue(Math.abs(eigenvalues[x] - eigenvalue) < .0000001);
assertTrue(Math.abs(Math.pow(eigenvalue, 0.5) - singularvalues[x]) < .0000001);
}
-
- //TODO: need to compute the inverse of S, S(-1)
- //TODO: need to find out the V(T)
-
+
+ // TODO: need to compute the inverse of S, S(-1)
+ // TODO: need to find out the V(T)
+
// Therefore, U= AVS'1=[-0.8944 -0.4472; 0.4472 -0.8944]
// A = USV'=[4 0; 3 -5]
}
Modified: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseMatrix.java?rev=822682&r1=822681&r2=822682&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseMatrix.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseMatrix.java Wed Oct 7 12:04:03 2009
@@ -20,57 +20,39 @@
package org.apache.hama.matrix;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import org.apache.hama.matrix.Matrix.Norm;
+import org.apache.hama.HamaCluster;
import org.apache.log4j.Logger;
-public class TestSparseMatrix extends TestCase {
+public class TestSparseMatrix extends HamaCluster {
static final Logger LOG = Logger.getLogger(TestSparseMatrix.class);
- private static int SIZE = 10;
- private static SparseMatrix m1;
- private static SparseMatrix m2;
-
- public static Test suite() {
- TestSetup setup = new TestSetup(new TestSuite(TestSparseMatrix.class)) {
- protected void setUp() throws Exception {
- HCluster hCluster = new HCluster();
- hCluster.setUp();
-
- m1 = SparseMatrix.random(hCluster.getConf(), SIZE, SIZE);
- m2 = SparseMatrix.random(hCluster.getConf(), SIZE, SIZE);
- }
+ private int SIZE = 10;
+ private SparseMatrix m1;
+ private SparseMatrix m2;
- protected void tearDown() {
- try {
- closeTest();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- };
- return setup;
+ /**
+ * @throws UnsupportedEncodingException
+ */
+ public TestSparseMatrix() throws UnsupportedEncodingException {
+ super();
}
- public static void closeTest() throws IOException {
- m1.close();
- m2.close();
+ public void setUp() throws Exception {
+ super.setUp();
+ m1 = SparseMatrix.random(getConf(), SIZE, SIZE);
+ m2 = SparseMatrix.random(getConf(), SIZE, SIZE);
}
- public void testTranspose() throws IOException {
- SparseMatrix trans = (SparseMatrix) m1.transpose();
- for (int i = 0; i < trans.getRows(); i++) {
- for (int j = 0; j < trans.getColumns(); j++) {
- assertEquals(trans.get(i, j), m1.get(j, i));
- }
- }
+ public void testMult() throws IOException {
+ assertTrue(m1.getRows() > 0);
+ sparsity();
+
+ SparseMatrix result = m1.mult(m2);
+ verifyMultResult(m1, m2, result);
}
-
- public void testSparsity() throws IOException {
+
+ public void sparsity() throws IOException {
boolean appeared = false;
for (int i = 0; i < m1.getRows(); i++) {
for (int j = 0; j < m1.getColumns(); j++) {
@@ -83,44 +65,6 @@
}
/**
- * Test matrices multiplication
- *
- * @throws IOException
- */
- public void testMatrixMult() throws IOException {
- SparseMatrix result = m1.mult(m2);
- verifyMultResult(m1, m2, result);
- }
-
- public void testNorm1() throws IOException {
- double gap = 0.000001;
-
- double norm1 = m1.norm(Norm.One);
- double verify_norm1 = MatrixTestCommon.verifyNorm1(m1);
- gap = norm1 - verify_norm1;
- LOG.info("Norm One : gap " + gap);
- assertTrue(gap < 0.000001 && gap > -0.000001);
-
- double normInfinity = m1.norm(Norm.Infinity);
- double verify_normInf = MatrixTestCommon.verifyNormInfinity(m1);
- gap = normInfinity - verify_normInf;
- LOG.info("Norm Infinity : gap " + gap);
- assertTrue(gap < 0.000001 && gap > -0.000001);
-
- double normMaxValue = m1.norm(Norm.Maxvalue);
- double verify_normMV = MatrixTestCommon.verifyNormMaxValue(m1);
- gap = normMaxValue - verify_normMV;
- LOG.info("Norm MaxValue : gap " + gap);
- assertTrue(gap < 0.000001 && gap > -0.000001);
-
- double normFrobenius = m1.norm(Norm.Frobenius);
- double verify_normFrobenius = MatrixTestCommon.verifyNormFrobenius(m1);
- gap = normFrobenius - verify_normFrobenius;
- LOG.info("Norm Frobenius : gap " + gap);
- assertTrue(gap < 0.000001 && gap > -0.000001);
- }
-
- /**
* Verifying multiplication result
*
* @param m1
@@ -147,5 +91,4 @@
}
}
}
-
}
Modified: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseVector.java?rev=822682&r1=822681&r2=822682&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseVector.java Wed Oct 7 12:04:03 2009
@@ -20,46 +20,40 @@
package org.apache.hama.matrix;
import java.io.IOException;
-
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
+import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
-import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hama.HamaCluster;
import org.apache.hama.util.BytesUtil;
-public class TestSparseVector extends TestCase {
+public class TestSparseVector extends HamaCluster {
final static Log LOG = LogFactory.getLog(TestSparseVector.class.getName());
- private static SparseMatrix m1;
- private static SparseVector v1;
- private static SparseVector v2;
- private static double[][] values = { { 2, 0, 0, 4 }, { 0, 0, 3, 3 } };
-
- public static Test suite() {
- TestSetup setup = new TestSetup(new TestSuite(TestSparseVector.class)) {
- protected void setUp() throws Exception {
- HCluster hCluster = new HCluster();
- hCluster.setUp();
-
- m1 = new SparseMatrix(hCluster.getConf(), 2, 4);
-
- for (int i = 0; i < 2; i++)
- for (int j = 0; j < 4; j++)
- m1.set(i, j, values[i][j]);
-
- v1 = m1.getRow(0);
- v2 = m1.getRow(1);
- }
-
- protected void tearDown() {
- LOG.info("tearDown()");
- }
- };
- return setup;
+ private SparseMatrix m1;
+ private SparseVector v1;
+ private SparseVector v2;
+ private double[][] values = { { 2, 0, 0, 4 }, { 0, 0, 3, 3 } };
+
+ /**
+ * @throws UnsupportedEncodingException
+ */
+ public TestSparseVector() throws UnsupportedEncodingException {
+ super();
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+ m1 = new SparseMatrix(getConf(), 2, 4);
+
+ for (int i = 0; i < 2; i++)
+ for (int j = 0; j < 4; j++)
+ m1.set(i, j, values[i][j]);
+
+ v1 = m1.getRow(0);
+ v2 = m1.getRow(1);
}
/**
@@ -72,23 +66,28 @@
assertEquals(v2.get(1), 0.0);
HTable table = m1.getHTable();
- Cell c = table.get(BytesUtil.getRowIndex(0), BytesUtil.getColumnIndex(1));
- assertTrue(c == null);
+ Get get = new Get(BytesUtil.getRowIndex(0));
+ get.addColumn(BytesUtil.getColumnIndex(1));
+ Result r = table.get(get);
+ assertTrue(r.getCellValue() == null);
+
+ addTest();
}
-
+
/**
* Test add()
*/
- public void testAdd() {
+ public void addTest() {
v1.add(v2);
-
- for(int i = 0; i < values[0].length; i++) {
+
+ for (int i = 0; i < values[0].length; i++) {
assertEquals(v1.get(i), values[0][i] + values[1][i]);
}
v1.add(0.5, v2);
- for(int i = 0; i < values[0].length; i++) {
- assertEquals(v1.get(i), (values[0][i] + values[1][i]) + (0.5 * values[1][i]));
+ for (int i = 0; i < values[0].length; i++) {
+ assertEquals(v1.get(i), (values[0][i] + values[1][i])
+ + (0.5 * values[1][i]));
}
}
}
Added: incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSubMatirx.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSubMatirx.java?rev=822682&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSubMatirx.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSubMatirx.java Wed Oct 7 12:04:03 2009
@@ -0,0 +1,59 @@
+package org.apache.hama.matrix;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.hama.HamaCluster;
+import org.apache.hama.HamaConfiguration;
+import org.apache.log4j.Logger;
+
+public class TestSubMatirx extends HamaCluster {
+ static final Logger LOG = Logger.getLogger(TestSubMatirx.class);
+ private int SIZE = 10;
+ private Matrix m1;
+ private Matrix m2;
+ private HamaConfiguration conf;
+
+ /**
+ * @throws UnsupportedEncodingException
+ */
+ public TestSubMatirx() throws UnsupportedEncodingException {
+ super();
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+
+ conf = getConf();
+
+ m1 = DenseMatrix.random(conf, SIZE, SIZE);
+ m2 = DenseMatrix.random(conf, SIZE, SIZE);
+ }
+
+ public void testSubMatrix() throws IOException {
+ SubMatrix a = m1.subMatrix(2, 4, 2, 5); // A : 3 * 4
+ for (int i = 0; i < a.getRows(); i++) {
+ for (int j = 0; j < a.getColumns(); j++) {
+ assertEquals(a.get(i, j), m1.get(i + 2, j + 2));
+ }
+ }
+
+ SubMatrix b = m2.subMatrix(0, 3, 0, 2); // B : 4 * 3
+ SubMatrix c = a.mult(b);
+
+ double[][] C = new double[3][3]; // A * B
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
+ for (int k = 0; k < 4; k++) {
+ C[i][j] += m1.get(i + 2, k + 2) * m2.get(k, j);
+ }
+ }
+ }
+
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
+ assertEquals(C[i][j], c.get(i, j));
+ }
+ }
+ }
+}