Index: src/test/org/apache/hama/examples/TestBlockMatrixMapReduce.java =================================================================== --- src/test/org/apache/hama/examples/TestBlockMatrixMapReduce.java (리비전 0) +++ src/test/org/apache/hama/examples/TestBlockMatrixMapReduce.java (리비전 0) @@ -0,0 +1,61 @@ +/** + * 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.examples; + +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); + static final int SIZE = 32; + + /** constructor */ + public TestBlockMatrixMapReduce() { + super(); + } + + public void testBlockMatrixMapReduce() throws IOException, + ClassNotFoundException { + DenseMatrix m1 = DenseMatrix.random(conf, SIZE, SIZE); + DenseMatrix m2 = DenseMatrix.random(conf, SIZE, SIZE); + + DenseMatrix c = MatrixMultiplication.mult(m1, m2, 16); + + double[][] mem = 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++) { + mem[i][k] += m1.get(i, j) * m2.get(j, k); + } + } + } + + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + double gap = (mem[i][j] - c.get(i, j)); + assertTrue(gap < 0.000001 || gap < -0.000001); + } + } + } +} Index: src/test/org/apache/hama/examples/TestRandomMatrixMapReduce.java =================================================================== --- src/test/org/apache/hama/examples/TestRandomMatrixMapReduce.java (리비전 0) +++ src/test/org/apache/hama/examples/TestRandomMatrixMapReduce.java (리비전 0) @@ -0,0 +1,59 @@ +/** + * 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.examples; + +import java.io.IOException; + +import org.apache.hama.HamaCluster; +import org.apache.hama.examples.RandomMatrix; +import org.apache.hama.matrix.DenseMatrix; +import org.apache.hama.matrix.SparseMatrix; +import org.apache.log4j.Logger; + +public class TestRandomMatrixMapReduce extends HamaCluster { + static final Logger LOG = Logger.getLogger(TestRandomMatrixMapReduce.class); + + public void testRandomMatrixMapReduce() throws IOException { + DenseMatrix rand = RandomMatrix.random_mapred(conf, 20, 20); + assertEquals(20, rand.getRows()); + assertEquals(20, rand.getColumns()); + + for(int i = 0; i < 20; i++) { + for(int j = 0; j < 20; j++) { + assertTrue(rand.get(i, j) > 0); + } + } + + rand.close(); + + SparseMatrix rand2 = RandomMatrix.random_mapred(conf, 20, 20, 30); + assertEquals(20, rand2.getRows()); + assertEquals(20, rand2.getColumns()); + boolean zeroAppear = false; + for(int i = 0; i < 20; i++) { + for(int j = 0; j < 20; j++) { + if(rand2.get(i, j) == 0.0) + zeroAppear = true; + } + } + assertTrue(zeroAppear); + rand2.close(); + } +} Index: src/test/org/apache/hama/mapreduce/TestBlockMatrixMapReduce.java =================================================================== --- src/test/org/apache/hama/mapreduce/TestBlockMatrixMapReduce.java (리비전 943815) +++ src/test/org/apache/hama/mapreduce/TestBlockMatrixMapReduce.java (작업 사본) @@ -1,61 +0,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.mapreduce; - -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); - static final int SIZE = 32; - - /** constructor */ - public TestBlockMatrixMapReduce() { - super(); - } - - public void testBlockMatrixMapReduce() throws IOException, - ClassNotFoundException { - DenseMatrix m1 = DenseMatrix.random(conf, SIZE, SIZE); - DenseMatrix m2 = DenseMatrix.random(conf, SIZE, SIZE); - - DenseMatrix c = MatrixMultiplication.mult(m1, m2, 16); - - double[][] mem = 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++) { - mem[i][k] += m1.get(i, j) * m2.get(j, k); - } - } - } - - for (int i = 0; i < SIZE; i++) { - for (int j = 0; j < SIZE; j++) { - double gap = (mem[i][j] - c.get(i, j)); - assertTrue(gap < 0.000001 || gap < -0.000001); - } - } - } -} Index: src/test/org/apache/hama/mapreduce/TestRandomMatrixMapReduce.java =================================================================== --- src/test/org/apache/hama/mapreduce/TestRandomMatrixMapReduce.java (리비전 943815) +++ src/test/org/apache/hama/mapreduce/TestRandomMatrixMapReduce.java (작업 사본) @@ -1,59 +0,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.mapreduce; - -import java.io.IOException; - -import org.apache.hama.HamaCluster; -import org.apache.hama.examples.RandomMatrix; -import org.apache.hama.matrix.DenseMatrix; -import org.apache.hama.matrix.SparseMatrix; -import org.apache.log4j.Logger; - -public class TestRandomMatrixMapReduce extends HamaCluster { - static final Logger LOG = Logger.getLogger(TestRandomMatrixMapReduce.class); - - public void testRandomMatrixMapReduce() throws IOException { - DenseMatrix rand = RandomMatrix.random_mapred(conf, 20, 20); - assertEquals(20, rand.getRows()); - assertEquals(20, rand.getColumns()); - - for(int i = 0; i < 20; i++) { - for(int j = 0; j < 20; j++) { - assertTrue(rand.get(i, j) > 0); - } - } - - rand.close(); - - SparseMatrix rand2 = RandomMatrix.random_mapred(conf, 20, 20, 30); - assertEquals(20, rand2.getRows()); - assertEquals(20, rand2.getColumns()); - boolean zeroAppear = false; - for(int i = 0; i < 20; i++) { - for(int j = 0; j < 20; j++) { - if(rand2.get(i, j) == 0.0) - zeroAppear = true; - } - } - assertTrue(zeroAppear); - rand2.close(); - } -} Index: src/test/org/apache/hama/matrix/TestMatrixVectorMult.java =================================================================== --- src/test/org/apache/hama/matrix/TestMatrixVectorMult.java (리비전 943815) +++ src/test/org/apache/hama/matrix/TestMatrixVectorMult.java (작업 사본) @@ -1,70 +0,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 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); - private Matrix m1, m2; - private HamaConfiguration conf; - private double[][] result = { { 5 }, { 11 } }; - - /** - * @throws UnsupportedEncodingException - */ - public TestMatrixVectorMult() throws UnsupportedEncodingException { - super(); - } - - 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 { - DenseMatrix c = MatrixMultiplication.mult(m1, m2); - assertTrue(m1.getRows() == 2); - - for (int i = 0; i < c.getRows(); i++) { - for (int j = 0; j < c.getColumns(); j++) { - assertEquals(result[i][j], c.get(i, j)); - } - } - } -} Index: src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java =================================================================== --- src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java (리비전 943815) +++ src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java (작업 사본) @@ -1,100 +0,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 org.apache.hadoop.hbase.client.Get; -import org.apache.hadoop.hbase.client.HTable; -import org.apache.hadoop.hbase.client.Result; -import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hama.Constants; -import org.apache.hama.HamaCluster; -import org.apache.hama.HamaConfiguration; -import org.apache.hama.examples.JacobiEigen; -import org.apache.hama.examples.MatrixMultiplication; -import org.apache.hama.util.BytesUtil; -import org.apache.log4j.Logger; - -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 double[][] matrixA = { { 4, 0 }, { 3, -5 } }; - // A'A = [25 -15; -15 25] - 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 double[] singularvalues = { 3.1622776601683795, 6.324555320336759 }; - - public void testEigenSingularValues() throws IOException { - Matrix aT = m1.transpose(); - DenseMatrix aTa = MatrixMultiplication.mult(aT, m1); - - 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 - JacobiEigen.jacobiEigenValue(aTa, 100); - HTable table = aTa.getHTable(); - - for (int x = 0; x < 2; x++) { - Get get = new Get(BytesUtil.getRowIndex(x)); - get.addColumn(Bytes.toBytes(JacobiEigen.EI)); - Result r = table.get(get); - - double eigenvalue = Bytes.toDouble(r.getValue(Bytes - .toBytes(JacobiEigen.EI), Bytes.toBytes(JacobiEigen.EIVAL))); - 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) - - // Therefore, U= AVS'1=[-0.8944 -0.4472; 0.4472 -0.8944] - // A = USV'=[4 0; 3 -5] - } -}