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=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/test/org/apache/hama/TestVector.java (original) +++ incubator/hama/trunk/src/test/org/apache/hama/TestVector.java Mon Aug 25 06:27:04 2008 @@ -1,81 +1,82 @@ -/** - * 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; - -public class TestVector extends HamaTestCase { - private final double cosine = 0.6978227007909176; - private final double norm1 = 12.0; - private final double norm2 = 6.782329983125268; - private double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } }; - - /** - * Test vector - */ - public void testGetVector() { - Matrix m1 = new Matrix(conf, "dotTest"); - - for (int i = 0; i < 2; i++) { - for (int j = 0; j < 4; j++) { - m1.set(i, j, values[i][j]); - } - } - - Vector v1 = m1.getRow(0); - Vector v2 = m1.getRow(1); - - dotTest(v1, v2); - norm1Test(v1, v2); - norm2Test(v1, v2); - scalingTest(v2); - } - - /** - * Test |a| dot |b| - * - * @param v1 - * @param v2 - */ - private void dotTest(Vector v1, Vector v2) { - double cos = v1.dot(v2); - assertEquals(cos, cosine); - } - - private void norm1Test(Vector v1, Vector v2) { - assertEquals(norm1, v1.getNorm1()); - } - - private void norm2Test(Vector v1, Vector v2) { - assertEquals(norm2, v1.getNorm2()); - } - - private void scalingTest(Vector v2) { - v2.scale(0.5); - - for (int i = 0; i < v2.size(); i++) { - assertEquals(values[1][i] * 0.5, v2.get(i)); - } - } - - public void testGetSet() { - Vector v1 = new Vector(); - v1.set(0, 0.2); - assertEquals(v1.get(0), 0.2); - } -} +/** + * 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; + +public class TestVector extends HamaTestCase { + private final double cosine = 0.6978227007909176; + private final double norm1 = 12.0; + private final double norm2 = 6.782329983125268; + private double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } }; + private final String m = "dotTest"; + + /** + * Test vector + */ + public void testGetVector() { + Matrix m1 = new DenseMatrix(conf, m); + + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 4; j++) { + m1.set(i, j, values[i][j]); + } + } + + Vector v1 = m1.getRow(0); + Vector v2 = m1.getRow(1); + + dotTest(v1, v2); + norm1Test(v1, v2); + norm2Test(v1, v2); + scalingTest(v2); + } + + /** + * Test |a| dot |b| + * + * @param v1 + * @param v2 + */ + private void dotTest(Vector v1, Vector v2) { + double cos = v1.dot(v2); + assertEquals(cos, cosine); + } + + private void norm1Test(Vector v1, Vector v2) { + assertEquals(norm1, ((DenseVector) v1).getNorm1()); + } + + private void norm2Test(Vector v1, Vector v2) { + assertEquals(norm2, ((DenseVector) v1).getNorm2()); + } + + private void scalingTest(Vector v2) { + v2.scale(0.5); + + for (int i = 0; i < v2.size(); i++) { + assertEquals(values[1][i] * 0.5, v2.get(i)); + } + } + + public void testGetSet() { + Vector v1 = new DenseVector(); + v1.set(0, 0.2); + assertEquals(v1.get(0), 0.2); + } +}
Modified: incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java?rev=688720&r1=688719&r2=688720&view=diff ============================================================================== --- incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java (original) +++ incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java Mon Aug 25 06:27:04 2008 @@ -1,79 +1,80 @@ -/** - * 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.mapred; - -import java.io.IOException; - -import org.apache.hadoop.hbase.io.ImmutableBytesWritable; -import org.apache.hadoop.mapred.JobClient; -import org.apache.hadoop.mapred.JobConf; -import org.apache.hama.HamaTestCase; -import org.apache.hama.Matrix; -import org.apache.hama.Vector; -import org.apache.hama.algebra.AdditionMap; -import org.apache.hama.algebra.AdditionReduce; -import org.apache.log4j.Logger; - -/** - * Test Matrix Map/Reduce job - */ -public class TestMatrixMapReduce extends HamaTestCase { - static final Logger LOG = Logger.getLogger(TestMatrixMapReduce.class); - private String A = "matrixA"; - private String B = "matrixB"; - private String output = "output"; - - /** constructor */ - public TestMatrixMapReduce() { - super(); - } - - public void testMatrixMapReduce() throws IOException { - Matrix matrixA = new Matrix(conf, A); - matrixA.set(0, 0, 1); - matrixA.set(0, 1, 0); - - Matrix matrixB = new Matrix(conf, B); - matrixB.set(0, 0, 1); - matrixB.set(0, 1, 1); - - miniMRJob(); - } - - public void miniMRJob() throws IOException { - Matrix c = new Matrix(conf, output); - - JobConf jobConf = new JobConf(conf, TestMatrixMapReduce.class); - jobConf.setJobName("test MR job"); - - MatrixMap.initJob(A, B, AdditionMap.class, ImmutableBytesWritable.class, - Vector.class, jobConf); - MatrixReduce.initJob(output, AdditionReduce.class, jobConf); - - jobConf.setNumMapTasks(1); - jobConf.setNumReduceTasks(1); - - JobClient.runJob(jobConf); - - assertEquals(c.get(0, 0), 2.0); - assertEquals(c.get(0, 1), 1.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.mapred; + +import java.io.IOException; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.mapred.JobClient; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hama.DenseMatrix; +import org.apache.hama.DenseVector; +import org.apache.hama.HamaTestCase; +import org.apache.hama.Matrix; +import org.apache.hama.algebra.AdditionMap; +import org.apache.hama.algebra.AdditionReduce; +import org.apache.log4j.Logger; + +/** + * Test Matrix Map/Reduce job + */ +public class TestMatrixMapReduce extends HamaTestCase { + static final Logger LOG = Logger.getLogger(TestMatrixMapReduce.class); + private String A = "matrixA"; + private String B = "matrixB"; + private String output = "output"; + + /** constructor */ + public TestMatrixMapReduce() { + super(); + } + + public void testMatrixMapReduce() throws IOException { + Matrix matrixA = new DenseMatrix(conf, A); + matrixA.set(0, 0, 1); + matrixA.set(0, 1, 0); + + Matrix matrixB = new DenseMatrix(conf, B); + matrixB.set(0, 0, 1); + matrixB.set(0, 1, 1); + + miniMRJob(); + } + + public void miniMRJob() throws IOException { + Matrix c = new DenseMatrix(conf, output); + + JobConf jobConf = new JobConf(conf, TestMatrixMapReduce.class); + jobConf.setJobName("test MR job"); + + DenseMap.initJob(A, B, AdditionMap.class, ImmutableBytesWritable.class, + DenseVector.class, jobConf); + MatrixReduce.initJob(output, AdditionReduce.class, jobConf); + + jobConf.setNumMapTasks(1); + jobConf.setNumReduceTasks(1); + + JobClient.runJob(jobConf); + + assertEquals(c.get(0, 0), 2.0); + assertEquals(c.get(0, 1), 1.0); + } + +}
