incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r817156 [3/3] - in /incubator/hama/trunk: ./ src/examples/org/apache/hama/examples/ src/java/org/apache/hama/ src/java/org/apache/hama/algebra/ src/java/org/apache/hama/graph/ src/java/org/apache/hama/io/ src/java/org/apache/hama/mapred/ sr...
Date Mon, 21 Sep 2009 04:40:15 GMT
Added: incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/RowCyclicAdditionMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/RowCyclicAdditionMap.java?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/RowCyclicAdditionMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/RowCyclicAdditionMap.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,94 @@
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.mapred.VectorInputFormat;
+import org.apache.hama.matrix.DenseMatrix;
+import org.apache.hama.matrix.DenseVector;
+import org.apache.log4j.Logger;
+
+public class RowCyclicAdditionMap extends MapReduceBase implements
+Mapper<IntWritable, MapWritable, IntWritable, MapWritable> {
+  static final Logger LOG = Logger.getLogger(RowCyclicAdditionMap.class);
+  protected DenseMatrix[] matrix_summands;
+  protected double[] matrix_alphas;
+  public static final String MATRIX_SUMMANDS = "hama.addition.summands";
+  public static final String MATRIX_ALPHAS = "hama.addition.alphas";
+
+  public void configure(JobConf job) {
+    try {
+      String[] matrix_names = job.get(MATRIX_SUMMANDS, "").split(","); 
+      String[] matrix_alpha_strs = job.get(MATRIX_ALPHAS, "").split(",");
+      assert(matrix_names.length == matrix_alpha_strs.length && matrix_names.length >= 1);
+      
+      matrix_summands = new DenseMatrix[matrix_names.length];
+      matrix_alphas = new double[matrix_names.length];
+      for(int i=0; i<matrix_names.length; i++) {
+        matrix_summands[i] = new DenseMatrix(new HamaConfiguration(job), matrix_names[i]);
+        matrix_alphas[i] = Double.valueOf(matrix_alpha_strs[i]);
+      }
+    } catch (IOException e) {
+      LOG.warn("Load matrix_b failed : " + e.getMessage());
+    }
+  }
+
+  public static void initJob(String matrix_a, String matrix_summandlist, 
+      String matrix_alphalist, Class<RowCyclicAdditionMap> map, 
+      Class<IntWritable> outputKeyClass, Class<MapWritable> outputValueClass, 
+      JobConf jobConf) {
+
+    jobConf.setMapOutputValueClass(outputValueClass);
+    jobConf.setMapOutputKeyClass(outputKeyClass);
+    jobConf.setMapperClass(map);
+    jobConf.set(MATRIX_SUMMANDS, matrix_summandlist);
+    jobConf.set(MATRIX_ALPHAS, matrix_alphalist);
+
+    jobConf.setInputFormat(VectorInputFormat.class);
+    FileInputFormat.addInputPaths(jobConf, matrix_a);
+    jobConf.set(VectorInputFormat.COLUMN_LIST, Constants.COLUMN);
+  }
+
+  @Override
+  public void map(IntWritable key, MapWritable value,
+      OutputCollector<IntWritable, MapWritable> output, Reporter reporter)
+      throws IOException {
+    
+    DenseVector result = new DenseVector(value);
+    DenseVector summand;
+    for(int i=0; i<matrix_summands.length; i++) {
+      summand = matrix_summands[i].getRow(key.get());
+      result = result.add(matrix_alphas[i], summand);
+    }
+    output.collect(key, result.getEntries());
+
+  }
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/RowCyclicAdditionReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/RowCyclicAdditionReduce.java?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/RowCyclicAdditionReduce.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/RowCyclicAdditionReduce.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,67 @@
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.apache.hadoop.hbase.io.BatchUpdate;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reducer;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.io.VectorUpdate;
+import org.apache.hama.mapred.VectorOutputFormat;
+
+public class RowCyclicAdditionReduce extends MapReduceBase implements
+    Reducer<IntWritable, MapWritable, IntWritable, VectorUpdate> {
+
+  /**
+   * Use this before submitting a TableReduce job. It will appropriately set up
+   * the JobConf.
+   * 
+   * @param table
+   * @param reducer
+   * @param job
+   */
+  public static void initJob(String table, Class<RowCyclicAdditionReduce> reducer,
+      JobConf job) {
+    job.setOutputFormat(VectorOutputFormat.class);
+    job.setReducerClass(reducer);
+    job.set(VectorOutputFormat.OUTPUT_TABLE, table);
+    job.setOutputKeyClass(IntWritable.class);
+    job.setOutputValueClass(BatchUpdate.class);
+  }
+
+  @Override
+  public void reduce(IntWritable key, Iterator<MapWritable> values,
+      OutputCollector<IntWritable, VectorUpdate> output, Reporter reporter)
+      throws IOException {
+
+    VectorUpdate update = new VectorUpdate(key.get());
+    update.putAll(values.next());
+
+    output.collect(key, update);
+  }
+
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/SparseMatrixVectorMultMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/SparseMatrixVectorMultMap.java?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/SparseMatrixVectorMultMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/SparseMatrixVectorMultMap.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,88 @@
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.mapred.VectorInputFormat;
+import org.apache.hama.matrix.SparseMatrix;
+import org.apache.hama.matrix.SparseVector;
+import org.apache.log4j.Logger;
+
+public class SparseMatrixVectorMultMap extends MapReduceBase implements
+    Mapper<IntWritable, MapWritable, IntWritable, MapWritable> {
+  static final Logger LOG = Logger.getLogger(SparseMatrixVectorMultMap.class);
+  protected SparseVector currVector;
+  public static final String ITH_ROW = "ith.row";
+  public static final String MATRIX_A = "hama.multiplication.matrix.a";
+  public static final String MATRIX_B = "hama.multiplication.matrix.b";
+  private IntWritable nKey = new IntWritable();
+  
+  public void configure(JobConf job) {
+      SparseMatrix matrix_a;
+      try {
+        matrix_a = new SparseMatrix(new HamaConfiguration(job), job.get(MATRIX_A, ""));
+        int ithRow = job.getInt(ITH_ROW, 0);
+        nKey.set(ithRow);
+        currVector = matrix_a.getRow(ithRow);
+      } catch (IOException e) {
+        e.printStackTrace();
+      }
+  }
+
+  public static void initJob(int i, String matrix_a, String matrix_b,
+      Class<SparseMatrixVectorMultMap> map, Class<IntWritable> outputKeyClass,
+      Class<MapWritable> outputValueClass, JobConf jobConf) {
+
+    jobConf.setMapOutputValueClass(outputValueClass);
+    jobConf.setMapOutputKeyClass(outputKeyClass);
+    jobConf.setMapperClass(map);
+    jobConf.setInt(ITH_ROW, i);
+    jobConf.set(MATRIX_A, matrix_a);
+    jobConf.set(MATRIX_B, matrix_b);
+    
+    jobConf.setInputFormat(VectorInputFormat.class);
+    FileInputFormat.addInputPaths(jobConf, matrix_b);
+    jobConf.set(VectorInputFormat.COLUMN_LIST, Constants.COLUMN);
+  }
+
+  @Override
+  public void map(IntWritable key, MapWritable value,
+      OutputCollector<IntWritable, MapWritable> output, Reporter reporter)
+      throws IOException {
+
+    double ithjth = currVector.get(key.get());
+    if(ithjth != 0) {
+      SparseVector scaled = new SparseVector(value).scale(ithjth);
+      output.collect(nKey, scaled.getEntries());
+    }
+    
+  }
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/SparseMatrixVectorMultReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/SparseMatrixVectorMultReduce.java?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/SparseMatrixVectorMultReduce.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/SparseMatrixVectorMultReduce.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,76 @@
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.apache.hadoop.hbase.io.BatchUpdate;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reducer;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.io.VectorUpdate;
+import org.apache.hama.mapred.VectorOutputFormat;
+import org.apache.hama.matrix.SparseVector;
+import org.apache.log4j.Logger;
+
+public class SparseMatrixVectorMultReduce extends MapReduceBase implements
+    Reducer<IntWritable, MapWritable, IntWritable, VectorUpdate> {
+  static final Logger LOG = Logger
+      .getLogger(SparseMatrixVectorMultReduce.class);
+
+  /**
+   * Use this before submitting a TableReduce job. It will appropriately set up
+   * the JobConf.
+   * 
+   * @param table
+   * @param reducer
+   * @param job
+   */
+  public static void initJob(String table,
+      Class<SparseMatrixVectorMultReduce> reducer, JobConf job) {
+    job.setOutputFormat(VectorOutputFormat.class);
+    job.setReducerClass(reducer);
+    job.set(VectorOutputFormat.OUTPUT_TABLE, table);
+    job.setOutputKeyClass(IntWritable.class);
+    job.setOutputValueClass(BatchUpdate.class);
+  }
+
+  @Override
+  public void reduce(IntWritable key, Iterator<MapWritable> values,
+      OutputCollector<IntWritable, VectorUpdate> output, Reporter reporter)
+      throws IOException {
+    SparseVector sum = new SparseVector();
+
+    while (values.hasNext()) {
+      sum.add(new SparseVector(values.next()));
+    }
+    
+    VectorUpdate update = new VectorUpdate(key.get());
+    update.putAll(sum.getEntries());
+
+    output.collect(key, update);
+  }
+
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/TransposeMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/TransposeMap.java?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/TransposeMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/TransposeMap.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,68 @@
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.mapred.VectorInputFormat;
+
+public class TransposeMap extends MapReduceBase implements
+    Mapper<IntWritable, MapWritable, IntWritable, MapWritable> {
+  private IntWritable nKey = new IntWritable();
+  
+  public static void initJob(String path, Class<TransposeMap> map,
+      Class<IntWritable> outputKeyClass, Class<MapWritable> outputValueClass,
+      JobConf jobConf) {
+
+    jobConf.setMapOutputValueClass(outputValueClass);
+    jobConf.setMapOutputKeyClass(outputKeyClass);
+    jobConf.setMapperClass(map);
+
+    jobConf.setInputFormat(VectorInputFormat.class);
+    FileInputFormat.addInputPaths(jobConf, path);
+    jobConf.set(VectorInputFormat.COLUMN_LIST, Constants.COLUMN);
+  }
+
+  @Override
+  public void map(IntWritable key, MapWritable value,
+      OutputCollector<IntWritable, MapWritable> output, Reporter reporter)
+      throws IOException {
+    
+    for(Map.Entry<Writable, Writable> e : value.entrySet()) {
+      MapWritable val = new MapWritable();
+      nKey.set(((IntWritable) e.getKey()).get());
+      val.put(key, e.getValue());
+      output.collect(nKey, val);
+      val.clear();
+    }
+    
+  }
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/TransposeReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/TransposeReduce.java?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/TransposeReduce.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/TransposeReduce.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,66 @@
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.hadoop.hbase.io.BatchUpdate;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reducer;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.io.VectorUpdate;
+import org.apache.hama.mapred.VectorOutputFormat;
+
+public class TransposeReduce  extends MapReduceBase implements
+Reducer<IntWritable, MapWritable, IntWritable, VectorUpdate> {
+
+  public static void initJob(String path, Class<TransposeReduce> reducer,
+      JobConf jobConf) {
+    jobConf.setOutputFormat(VectorOutputFormat.class);
+    jobConf.setReducerClass(reducer);
+    jobConf.set(VectorOutputFormat.OUTPUT_TABLE, path);
+    jobConf.setOutputKeyClass(IntWritable.class);
+    jobConf.setOutputValueClass(BatchUpdate.class);
+  }
+
+  @Override
+  public void reduce(IntWritable key, Iterator<MapWritable> values,
+      OutputCollector<IntWritable, VectorUpdate> output, Reporter reporter)
+      throws IOException {
+
+    MapWritable sum = new MapWritable();
+    while (values.hasNext()) {
+      for(Map.Entry<Writable, Writable> e: values.next().entrySet()) {
+        sum.put(e.getKey(), e.getValue());  
+      }
+    }
+    
+    VectorUpdate update = new VectorUpdate(key.get());
+    update.putAll(sum);
+    output.collect(key, update);
+  }
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/package.html
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/package.html?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/package.html (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/matrix/algebra/package.html Mon Sep 21 04:40:10 2009
@@ -0,0 +1,23 @@
+<html>
+
+<!--
+   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.
+-->
+
+<body>
+Algebraic operations on map/reduce
+</body>
+</html>

Modified: incubator/hama/trunk/src/java/org/apache/hama/shell/HamaShellEnv.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/shell/HamaShellEnv.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/shell/HamaShellEnv.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/shell/HamaShellEnv.java Mon Sep 21 04:40:10 2009
@@ -21,7 +21,7 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.hama.Matrix;
+import org.apache.hama.matrix.Matrix;
 import org.apache.hama.util.RandomVariable;
 import org.apache.log4j.Logger;
 /**

Modified: incubator/hama/trunk/src/java/org/apache/hama/shell/execution/AlgebraOperator.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/shell/execution/AlgebraOperator.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/shell/execution/AlgebraOperator.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/shell/execution/AlgebraOperator.java Mon Sep 21 04:40:10 2009
@@ -21,7 +21,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.hama.Matrix;
+import org.apache.hama.matrix.Matrix;
 import org.apache.hama.shell.HamaShellEnv;
 
 public class AlgebraOperator {

Modified: incubator/hama/trunk/src/java/org/apache/hama/shell/execution/RandMatrixOperation.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/shell/execution/RandMatrixOperation.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/shell/execution/RandMatrixOperation.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/shell/execution/RandMatrixOperation.java Mon Sep 21 04:40:10 2009
@@ -19,8 +19,8 @@
 
 import java.io.IOException;
 
-import org.apache.hama.DenseMatrix;
 import org.apache.hama.HamaConfiguration;
+import org.apache.hama.matrix.DenseMatrix;
 
 /**
  * Generate a random matrix.

Modified: incubator/hama/trunk/src/java/org/apache/hama/shell/execution/SaveExpression.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/shell/execution/SaveExpression.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/shell/execution/SaveExpression.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/shell/execution/SaveExpression.java Mon Sep 21 04:40:10 2009
@@ -18,7 +18,7 @@
 package org.apache.hama.shell.execution;
 
 import org.apache.hama.HamaConfiguration;
-import org.apache.hama.Matrix;
+import org.apache.hama.matrix.Matrix;
 import org.apache.hama.shell.HamaShellEnv;
 
 public class SaveExpression extends HamaExpression {

Modified: incubator/hama/trunk/src/java/org/apache/hama/util/JobManager.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/util/JobManager.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/util/JobManager.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/util/JobManager.java Mon Sep 21 04:40:10 2009
@@ -23,7 +23,7 @@
 
 import org.apache.hadoop.mapred.JobClient;
 import org.apache.hadoop.mapred.JobConf;
-import org.apache.hama.Matrix;
+import org.apache.hama.matrix.Matrix;
 
 /**
  * A map/reduce job manager 

Modified: incubator/hama/trunk/src/test/org/apache/hama/examples/TestFileMatrixBlockMult.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/examples/TestFileMatrixBlockMult.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/examples/TestFileMatrixBlockMult.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/examples/TestFileMatrixBlockMult.java Mon Sep 21 04:40:10 2009
@@ -47,17 +47,17 @@
 import org.apache.hadoop.mapred.Reporter;
 import org.apache.hadoop.mapred.SequenceFileInputFormat;
 import org.apache.hama.Constants;
-import org.apache.hama.DenseMatrix;
-import org.apache.hama.DenseVector;
-import org.apache.hama.HCluster;
 import org.apache.hama.HamaConfiguration;
-import org.apache.hama.Matrix;
-import org.apache.hama.algebra.BlockMultiplyMap;
-import org.apache.hama.algebra.BlockMultiplyReduce;
 import org.apache.hama.io.BlockID;
 import org.apache.hama.io.BlockWritable;
 import org.apache.hama.mapred.CollectBlocksMap;
 import org.apache.hama.mapred.CollectBlocksMapReduceBase;
+import org.apache.hama.matrix.DenseMatrix;
+import org.apache.hama.matrix.DenseVector;
+import org.apache.hama.matrix.HCluster;
+import org.apache.hama.matrix.Matrix;
+import org.apache.hama.matrix.algebra.BlockMultiplyMap;
+import org.apache.hama.matrix.algebra.BlockMultiplyReduce;
 import org.apache.hama.util.JobManager;
 import org.apache.hama.util.RandomVariable;
 

Modified: incubator/hama/trunk/src/test/org/apache/hama/graph/TestGraph.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/graph/TestGraph.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/graph/TestGraph.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/graph/TestGraph.java Mon Sep 21 04:40:10 2009
@@ -28,8 +28,8 @@
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
-import org.apache.hama.HCluster;
 import org.apache.hama.HamaConfiguration;
+import org.apache.hama.matrix.HCluster;
 import org.apache.log4j.Logger;
 
 public class TestGraph extends TestCase {

Modified: incubator/hama/trunk/src/test/org/apache/hama/mapred/TestBlockMatrixMapReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/mapred/TestBlockMatrixMapReduce.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/mapred/TestBlockMatrixMapReduce.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/mapred/TestBlockMatrixMapReduce.java Mon Sep 21 04:40:10 2009
@@ -21,8 +21,8 @@
 
 import java.io.IOException;
 
-import org.apache.hama.DenseMatrix;
-import org.apache.hama.HCluster;
+import org.apache.hama.matrix.DenseMatrix;
+import org.apache.hama.matrix.HCluster;
 import org.apache.log4j.Logger;
 
 public class TestBlockMatrixMapReduce extends HCluster {

Modified: incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java Mon Sep 21 04:40:10 2009
@@ -21,9 +21,9 @@
 
 import java.io.IOException;
 
-import org.apache.hama.DenseMatrix;
-import org.apache.hama.HCluster;
-import org.apache.hama.SparseMatrix;
+import org.apache.hama.matrix.DenseMatrix;
+import org.apache.hama.matrix.HCluster;
+import org.apache.hama.matrix.SparseMatrix;
 import org.apache.log4j.Logger;
 
 public class TestRandomMatrixMapReduce extends HCluster {

Added: incubator/hama/trunk/src/test/org/apache/hama/matrix/HCluster.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/HCluster.java?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/HCluster.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/HCluster.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,38 @@
+/**
+ * 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 org.apache.hadoop.hbase.HBaseClusterTestCase;
+import org.apache.hama.HamaConfiguration;
+
+/**
+ * Forming up the miniDfs and miniHbase
+ */
+public class HCluster extends HBaseClusterTestCase {
+  protected final HamaConfiguration conf = new HamaConfiguration();
+
+  public void setUp() throws Exception {
+    super.setUp();
+  }
+  
+  public HamaConfiguration getConf() {
+    return conf;
+  }
+}

Added: incubator/hama/trunk/src/test/org/apache/hama/matrix/MatrixTestCommon.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/matrix/MatrixTestCommon.java?rev=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/MatrixTestCommon.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/MatrixTestCommon.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,74 @@
+/**
+ * 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;
+
+class MatrixTestCommon {
+
+  static double verifyNorm1(Matrix m1) throws IOException {
+    double[] colSum = new double[m1.getColumns()];
+    for (int j = 0; j < m1.getColumns(); j++) {
+      for (int i = 0; i < m1.getRows(); i++) {
+        colSum[j] += Math.abs(m1.get(i, j));
+      }
+    }
+    
+    double max = 0;
+    for (int i=0; i < colSum.length; i++) {
+      max = Math.max(colSum[i], max);
+    }
+    return max;
+  }
+  
+  static double verifyNormInfinity(Matrix m1) throws IOException {
+    double[] rowSum = new double[m1.getRows()];
+    for (int i = 0; i < m1.getRows(); i++) {
+      for (int j = 0; j < m1.getColumns(); j++) {
+        rowSum[i] += Math.abs(m1.get(i, j));
+      }
+    }
+
+    double max = 0;
+    for (int i = 0; i < rowSum.length; ++i)
+      max = Math.max(rowSum[i], max);
+    return max;
+  }
+  
+  static double verifyNormMaxValue(Matrix m1) throws IOException {
+    double max = 0;
+    for (int i = 0; i < m1.getRows(); i++) {
+      for (int j = 0; j < m1.getColumns(); j++) {
+        max = Math.max(Math.abs(m1.get(i, j)), max);
+      }
+    }
+    
+    return max;
+  }
+  
+  static double verifyNormFrobenius(Matrix m1) throws IOException {
+    double sqrtSum = 0;
+    for (int i = 0; i < m1.getRows(); i++) {
+      for (int j = 0; j < m1.getColumns(); j++) {
+        double cellValue = m1.get(i, j);
+        sqrtSum += ( cellValue * cellValue );
+      }
+    } 
+    return Math.sqrt(sqrtSum);
+  }
+  
+}

Added: 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=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseMatrix.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseMatrix.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,572 @@
+/**
+ * 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.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.HamaAdmin;
+import org.apache.hama.HamaAdminImpl;
+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 {
+  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));
+      }
+    }
+  }
+
+  /**
+   * Column vector test.
+   * 
+   * @param rand
+   * @throws IOException
+   */
+  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 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 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));
+      }
+    }
+  }
+
+  /**
+   * Test matrices addition
+   * 
+   * @throws IOException
+   */
+  public void testMatrixAdd() throws IOException {
+    Matrix result = m1.add(m2);
+
+    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));
+      }
+    }
+
+    Matrix subtract = result.add(-1.0, m2);
+
+    for (int i = 0; i < SIZE; i++) {
+      for (int j = 0; j < SIZE; j++) {
+        double gap = (subtract.get(i, j) - m1.get(i, j));
+        assertTrue(-0.00001 < gap && gap < 0.00001);
+      }
+    }
+  }
+
+  public void testMultiMatrixAdd() 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));
+      }
+    }
+  }
+
+  /**
+   * Test matrices multiplication
+   * 
+   * @throws IOException
+   */
+  public void testMatrixMult() throws IOException {
+    Matrix result = m1.mult(m2);
+
+    assertEquals(result.getRows(), SIZE);
+    assertEquals(result.getColumns(), SIZE);
+
+    verifyMultResult(m1, m2, result);
+  }
+
+  public void testSetMatrix() throws IOException {
+    Matrix a = new DenseMatrix(conf, m1.getRows(), m1.getColumns());
+    a.set(m1);
+
+    for (int i = 0; i < 5; i++) {
+      // between 0 ~ SIZE -1
+      int x = RandomVariable.randInt(0, SIZE - 1);
+      int y = RandomVariable.randInt(0, SIZE - 1);
+      assertEquals(a.get(x, y), m1.get(x, y));
+    }
+  }
+
+  public void testSetAlphaMatrix() 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);
+    
+    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 : " + normFrobenius + "(map/reduce) - " + verify_normFrobenius + " expected");
+    LOG.info("Norm Frobenius : gap " + gap);
+    assertTrue(gap < 0.000001 && gap > -0.000001);
+  }
+  
+  public void testSetRow() 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.setRow(SIZE, v);
+    Iterator<Writable> it = m1.getRow(SIZE).iterator();
+
+    int i = 0;
+    while (it.hasNext()) {
+      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());
+    }
+  }
+
+  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();
+    }
+  }
+  
+  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
+   * 
+   * @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));
+      }
+    }
+  }
+  
+  //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;
+  }
+}

Added: 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=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseVector.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestDenseVector.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,229 @@
+/**
+ * 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);
+  }
+}

Added: 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=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestMatrixVectorMult.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestMatrixVectorMult.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,85 @@
+/**
+ * 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 junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.hama.HamaConfiguration;
+import org.apache.log4j.Logger;
+
+public class TestMatrixVectorMult extends TestCase {
+  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;
+  }
+
+  public static void closeTest() throws IOException {
+    m1.close();
+    m2.close();
+  }
+
+  public void testMatVectorMult() throws IOException {
+    DenseMatrix c = (DenseMatrix) m1.mult(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));
+      }
+    }
+  }
+}

Added: 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=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSingularValueDecomposition.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,84 @@
+/**
+ * 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 junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.util.Bytes;
+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;
+
+  // Let's assume the A = [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};
+  // 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, values[i][j]);
+      }
+
+      protected void tearDown() {
+        // do nothing
+      }
+    };
+    return setup;
+  }
+
+  public void testLog() throws IOException {
+    // Find the eigen/singular values and vectors of A'A    
+    m1.jacobiEigenValue(1);
+    HTable table = m1.getHTable();
+    
+    for(int x=0; x<2; x++) {
+      double eigenvalue = BytesUtil.bytesToDouble(table.get(BytesUtil.getRowIndex(x), 
+          Bytes.toBytes(JacobiEigenValue.EIVAL)).getValue());
+      assertTrue(Math.abs(eigenvalues[x] - eigenvalue) < .0000001);
+      assertTrue(Math.abs(Math.pow(eigenvalue, 0.5) - singularvalues[x]) < .0000001);
+    }
+    
+    // Therefore, U= AVS'1=[-0.8944 -0.4472; 0.4472 -0.8944]
+    // A = USV'=[4 0; 3 -5]
+  }
+}

Added: 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=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseMatrix.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseMatrix.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,151 @@
+/**
+ * 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 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.log4j.Logger;
+
+public class TestSparseMatrix extends TestCase {
+  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);
+      }
+
+      protected void tearDown() {
+        try {
+          closeTest();
+        } catch (IOException e) {
+          e.printStackTrace();
+        }
+      }
+    };
+    return setup;
+  }
+
+  public static void closeTest() throws IOException {
+    m1.close();
+    m2.close();
+  }
+
+  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 testSparsity() throws IOException {
+    boolean appeared = false;
+    for (int i = 0; i < m1.getRows(); i++) {
+      for (int j = 0; j < m1.getColumns(); j++) {
+        if (m1.get(i, j) == 0)
+          appeared = true;
+      }
+    }
+
+    assertTrue(appeared);
+  }
+
+  /**
+   * 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
+   * @param m2
+   * @param result
+   * @throws IOException
+   */
+  private void verifyMultResult(SparseMatrix m1, SparseMatrix m2,
+      SparseMatrix 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++) {
+        double gap = (c[i][j] - result.get(i, j));
+        assertTrue(gap < 0.000001 && gap > -0.000001);
+      }
+    }
+  }
+ 
+}

Added: 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=817156&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseVector.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/matrix/TestSparseVector.java Mon Sep 21 04:40:10 2009
@@ -0,0 +1,94 @@
+/**
+ * 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 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.hbase.client.HTable;
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hama.util.BytesUtil;
+
+public class TestSparseVector extends TestCase {
+  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;
+  }
+
+  /**
+   * Test get/set methods
+   * 
+   * @throws IOException
+   */
+  public void testGetSet() throws IOException {
+    assertEquals(v1.get(1), 0.0);
+    assertEquals(v2.get(1), 0.0);
+
+    HTable table = m1.getHTable();
+    Cell c = table.get(BytesUtil.getRowIndex(0), BytesUtil.getColumnIndex(1));
+    assertTrue(c == null);
+  }
+  
+  /**
+   * Test add()
+   */
+  public void testAdd() {
+    v1.add(v2);
+    
+    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]));
+    }
+  }
+}

Modified: incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java?rev=817156&r1=817155&r2=817156&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java Mon Sep 21 04:40:10 2009
@@ -6,10 +6,10 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.hama.DenseMatrix;
-import org.apache.hama.HCluster;
 import org.apache.hama.HamaConfiguration;
-import org.apache.hama.Matrix;
+import org.apache.hama.matrix.DenseMatrix;
+import org.apache.hama.matrix.HCluster;
+import org.apache.hama.matrix.Matrix;
 import org.apache.hama.shell.HamaShellEnv;
 import org.apache.hama.shell.execution.HamaExpression;
 



Mime
View raw message