incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r813233 [7/7] - in /incubator/hama/branches: ./ hama-0.19/ hama-0.19/bin/ hama-0.19/conf/ hama-0.19/lib/ hama-0.19/lib/findbugs/ hama-0.19/lib/findbugs/plugin/ hama-0.19/lib/jetty-ext/ hama-0.19/src/ hama-0.19/src/docs/ hama-0.19/src/docs/s...
Date Thu, 10 Sep 2009 05:32:59 GMT
Added: incubator/hama/branches/hama-0.19/src/java/org/apache/hama/util/RandomVariable.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/java/org/apache/hama/util/RandomVariable.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/java/org/apache/hama/util/RandomVariable.java (added)
+++ incubator/hama/branches/hama-0.19/src/java/org/apache/hama/util/RandomVariable.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,255 @@
+/**
+ * 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.util;
+
+import org.apache.hama.Constants;
+
+/**
+ * The RandomVaraibale Class provides static methods for generating random
+ * numbers.
+ */
+public class RandomVariable {
+  
+  /**
+   * Generate a random number between 0 and 1.
+   * 
+   * @return a double between 0 and 1.
+   */
+  public static double rand() {
+    double x = Math.random();
+    return x;
+  }
+
+  /**
+   * Generate a random integer.
+   * 
+   * @param i0 min of the random variable.
+   * @param i1 max of the random variable.
+   * @return an int between i0 and i1.
+   */
+  public static int randInt(int i0, int i1) {
+    double x = rand();
+    int i = i0 + (int) Math.floor((i1 - i0 + 1) * x);
+    return i;
+  }
+
+  /**
+   * Generate a random matrix Path(tablename) with a default length.
+   * @return random matrix name
+   */
+  public static String randMatrixPath() {
+    return randString(Constants.RANDOM, 5);
+  }
+  
+  /**
+   * Generate a random matrix Path(tablename) with a given length.
+   * @param length the matrix path's length
+   * @return random matrix name
+   */
+  public static String randMatrixPath(int length) {
+    return randString(Constants.RANDOM, length);
+  }
+  
+  /**
+   * Generate a random aliase name.
+   * 
+   * @return random aliase name
+   */
+  public static String randAliaseName() {
+    return randString(Constants.RANDOMALIASE, 5);
+  }
+  
+  /**
+    * Generate a random string using the specified prefix and a fixed length. 
+    * @param prefix
+    *        the specified string prefix.
+    * @param length
+    *        the length of the string to be appended.
+    * @return random string.
+    */
+  public static String randString(String prefix, int length) {
+    StringBuilder result = new StringBuilder(prefix);
+    for (int i = 0; i < length; i++) {
+      char ch = (char) ((Math.random() * 26) + 97);
+      result.append(ch);
+    }
+      
+    return result.toString();
+  }
+
+  /**
+   * Generate a random number from a uniform random variable.
+   * 
+   * @param min min of the random variable.
+   * @param max max of the random variable.
+   * @return a double.
+   */
+  public static double uniform(double min, double max) {
+    double x = min + (max - min) * rand();
+    return x;
+  }
+
+  /**
+   * Generate a random number from a discrete random variable.
+   * 
+   * @param values discrete values.
+   * @param prob probability of each value.
+   * @return a double.
+   */
+  public static double dirac(double[] values, double[] prob) {
+    double[] prob_cumul = new double[values.length];
+    prob_cumul[0] = prob[0];
+    for (int i = 1; i < values.length; i++) {
+      prob_cumul[i] = prob_cumul[i - 1] + prob[i];
+    }
+    double y = rand();
+    double x = 0;
+    for (int i = 0; i < values.length - 1; i++) {
+      if ((y > prob_cumul[i]) && (y < prob_cumul[i + 1])) {
+        x = values[i];
+      }
+    }
+    return x;
+  }
+
+  /**
+   * Generate a random number from a Gaussian (Normal) random variable.
+   * 
+   * @param mu mean of the random variable.
+   * @param sigma standard deviation of the random variable.
+   * @return a double.
+   */
+  public static double normal(double mu, double sigma) {
+    double x = mu + sigma * Math.cos(2 * Math.PI * rand())
+        * Math.sqrt(-2 * Math.log(rand()));
+    return x;
+  }
+
+  /**
+   * Generate a random number from a Chi-2 random variable.
+   * 
+   * @param n degrees of freedom of the chi2 random variable.
+   * @return a double.
+   */
+  public static double chi2(int n) {
+    double x = 0;
+    for (int i = 0; i < n; i++) {
+      double norm = normal(0, 1);
+      x += norm * norm;
+    }
+    return x;
+  }
+
+  /**
+   * Generate a random number from a LogNormal random variable.
+   * 
+   * @param mu mean of the Normal random variable.
+   * @param sigma standard deviation of the Normal random variable.
+   * @return a double.
+   */
+  public static double logNormal(double mu, double sigma) {
+    double x = mu + sigma * Math.cos(2 * Math.PI * rand())
+        * Math.sqrt(-2 * Math.log(rand()));
+    return x;
+  }
+
+  /**
+   * Generate a random number from an exponantial random variable (Mean =
+   * 1/lambda, variance = 1/lambda^2).
+   * 
+   * @param lambda parmaeter of the exponential random variable.
+   * @return a double.
+   */
+  public static double exponential(double lambda) {
+    double x = -1 / lambda * Math.log(rand());
+    return x;
+  }
+
+  /**
+   * Generate a random number from a symetric triangular random variable.
+   * 
+   * @param min min of the random variable.
+   * @param max max of the random variable.
+   * @return a double.
+   */
+  public static double triangular(double min, double max) {
+    double x = min / 2 + (max - min) * rand() / 2 + min / 2 + (max - min)
+        * rand() / 2;
+    return x;
+  }
+
+  /**
+   * Generate a random number from a non-symetric triangular random variable.
+   * 
+   * @param min min of the random variable.
+   * @param med value of the random variable with max density.
+   * @param max max of the random variable.
+   * @return a double.
+   */
+  public static double triangular(double min, double med, double max) {
+    double y = rand();
+    double x = (y < ((med - min) / (max - min))) ? (min + Math.sqrt(y
+        * (max - min) * (med - min))) : (max - Math.sqrt((1 - y) * (max - min)
+        * (max - med)));
+    return x;
+  }
+
+  /**
+   * Generate a random number from a beta random variable.
+   * 
+   * @param a first parameter of the Beta random variable.
+   * @param b second parameter of the Beta random variable.
+   * @return a double.
+   */
+  public static double beta(double a, double b) {
+    double try_x;
+    double try_y;
+    do {
+      try_x = Math.pow(rand(), 1 / a);
+      try_y = Math.pow(rand(), 1 / b);
+    } while ((try_x + try_y) > 1);
+    return try_x / (try_x + try_y);
+  }
+
+  /**
+   * Generate a random number from a Cauchy random variable (Mean = Inf, and
+   * Variance = Inf).
+   * 
+   * @param mu median of the Weibull random variable
+   * @param sigma second parameter of the Cauchy random variable.
+   * @return a double.
+   */
+  public static double cauchy(double mu, double sigma) {
+    double x = sigma * Math.tan(Math.PI * (rand() - 0.5)) + mu;
+    return x;
+  }
+
+  /**
+   * Generate a random number from a Weibull random variable.
+   * 
+   * @param lambda first parameter of the Weibull random variable.
+   * @param c second parameter of the Weibull random variable.
+   * @return a double.
+   */
+  public static double weibull(double lambda, double c) {
+    double x = Math.pow(-Math.log(1 - rand()), 1 / c) / lambda;
+    return x;
+  }
+}

Added: incubator/hama/branches/hama-0.19/src/java/org/apache/hama/util/package.html
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/java/org/apache/hama/util/package.html?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/java/org/apache/hama/util/package.html (added)
+++ incubator/hama/branches/hama-0.19/src/java/org/apache/hama/util/package.html Thu Sep 10 05:32:52 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>
+Numerical and variable utilities
+</body>
+</html>

Added: incubator/hama/branches/hama-0.19/src/java/overview.html
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/java/overview.html?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/java/overview.html (added)
+++ incubator/hama/branches/hama-0.19/src/java/overview.html Thu Sep 10 05:32:52 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>
+The Hama Overview
+</body>
+</html>

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/HCluster.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/HCluster.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/HCluster.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/HCluster.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,37 @@
+/**
+ * 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;
+
+import org.apache.hadoop.hbase.HBaseClusterTestCase;
+
+/**
+ * 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/branches/hama-0.19/src/test/org/apache/hama/MatrixTestCommon.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/MatrixTestCommon.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/MatrixTestCommon.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/MatrixTestCommon.java Thu Sep 10 05:32:52 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;
+
+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/branches/hama-0.19/src/test/org/apache/hama/TestDenseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestDenseMatrix.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestDenseMatrix.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestDenseMatrix.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,569 @@
+/**
+ * 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;
+
+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.Matrix.Norm;
+import org.apache.hama.io.DoubleEntry;
+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/branches/hama-0.19/src/test/org/apache/hama/TestDenseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestDenseVector.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestDenseVector.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestDenseVector.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,226 @@
+/**
+ * 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;
+
+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;
+
+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/branches/hama-0.19/src/test/org/apache/hama/TestMatrixVectorMult.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestMatrixVectorMult.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestMatrixVectorMult.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestMatrixVectorMult.java Thu Sep 10 05:32:52 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;
+
+import java.io.IOException;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+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/branches/hama-0.19/src/test/org/apache/hama/TestSingularValueDecomposition.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSingularValueDecomposition.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSingularValueDecomposition.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSingularValueDecomposition.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,83 @@
+/**
+ * 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;
+
+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.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/branches/hama-0.19/src/test/org/apache/hama/TestSparseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSparseMatrix.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSparseMatrix.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSparseMatrix.java Thu Sep 10 05:32:52 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;
+
+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.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/branches/hama-0.19/src/test/org/apache/hama/TestSparseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSparseVector.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSparseVector.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/TestSparseVector.java Thu Sep 10 05:32:52 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;
+
+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]));
+    }
+  }
+}

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/examples/TestFileMatrixBlockMult.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/examples/TestFileMatrixBlockMult.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/examples/TestFileMatrixBlockMult.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/examples/TestFileMatrixBlockMult.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,214 @@
+/**
+ * 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 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.conf.Configuration;
+import org.apache.hadoop.fs.LocalFileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.io.DoubleWritable;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.SequenceFile;
+import org.apache.hadoop.io.SequenceFile.CompressionType;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobClient;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+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.util.JobManager;
+import org.apache.hama.util.RandomVariable;
+
+public class TestFileMatrixBlockMult extends TestCase {
+  final static Log LOG = LogFactory.getLog(TestFileMatrixBlockMult.class
+      .getName());
+  private static HamaConfiguration conf;
+  private static Path[] path = new Path[2];
+  private static String collectionTable;
+
+  public static Test suite() {
+    TestSetup setup = new TestSetup(
+        new TestSuite(TestFileMatrixBlockMult.class)) {
+      protected void setUp() throws Exception {
+        HCluster hCluster = new HCluster();
+        hCluster.setUp();
+
+        conf = hCluster.getConf();
+        HBaseAdmin admin = new HBaseAdmin(conf);
+        collectionTable = "collect_" + RandomVariable.randMatrixPath();
+        HTableDescriptor desc = new HTableDescriptor(collectionTable);
+        desc.addFamily(new HColumnDescriptor(Bytes.toBytes(Constants.BLOCK)));
+        admin.createTable(desc);
+      }
+
+      protected void tearDown() {
+      }
+    };
+    return setup;
+  }
+
+  public void testCreateFiles() throws IOException {
+    Configuration conf = new Configuration();
+    LocalFileSystem fs = new LocalFileSystem();
+    fs.setConf(conf);
+    fs.getRawFileSystem().setConf(conf);
+
+    for (int i = 0; i < 2; i++) {
+      path[i] = new Path(System.getProperty("test.build.data", ".") + "/test"
+          + i + ".seq");
+      SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, path[i],
+          IntWritable.class, MapWritable.class, CompressionType.BLOCK);
+
+      MapWritable value = new MapWritable();
+      value.put(new IntWritable(0), new DoubleWritable(0.5));
+      value.put(new IntWritable(1), new DoubleWritable(0.1));
+      value.put(new IntWritable(2), new DoubleWritable(0.5));
+      value.put(new IntWritable(3), new DoubleWritable(0.1));
+
+      writer.append(new IntWritable(0), value);
+      writer.append(new IntWritable(1), value);
+      writer.append(new IntWritable(2), value);
+      writer.append(new IntWritable(3), value);
+
+      writer.close();
+    }
+
+    SequenceFile.Reader reader1 = new SequenceFile.Reader(fs, path[0], conf);
+    // read first value from reader1
+    IntWritable key = new IntWritable();
+    MapWritable val = new MapWritable();
+    reader1.next(key, val);
+
+    assertEquals(key.get(), 0);
+  }
+
+  public void testFileMatrixMult() throws IOException {
+    collectBlocksFromFile(path[0], true, collectionTable, conf);
+    collectBlocksFromFile(path[1], false, collectionTable, conf);
+
+    Matrix result = new DenseMatrix(conf, 4, 4);
+    JobConf jobConf = new JobConf(conf);
+    jobConf.setJobName("multiplication MR job : " + result.getPath());
+
+    BlockMultiplyMap.initJob(collectionTable, BlockMultiplyMap.class,
+        BlockID.class, BlockWritable.class, jobConf);
+    BlockMultiplyReduce.initJob(result.getPath(), BlockMultiplyReduce.class,
+        jobConf);
+
+    JobManager.execute(jobConf, result);
+
+    verifyMultResult(result);
+  }
+
+  private void verifyMultResult(Matrix result) throws IOException {
+    double[][] a = new double[][] { { 0.5, 0.1, 0.5, 0.1 },
+        { 0.5, 0.1, 0.5, 0.1 }, { 0.5, 0.1, 0.5, 0.1 }, { 0.5, 0.1, 0.5, 0.1 } };
+    double[][] b = new double[][] { { 0.5, 0.1, 0.5, 0.1 },
+        { 0.5, 0.1, 0.5, 0.1 }, { 0.5, 0.1, 0.5, 0.1 }, { 0.5, 0.1, 0.5, 0.1 } };
+    double[][] c = new double[4][4];
+
+    for (int i = 0; i < 4; i++) {
+      for (int j = 0; j < 4; j++) {
+        for (int k = 0; k < 4; k++) {
+          c[i][k] += a[i][j] * b[j][k];
+        }
+      }
+    }
+
+    for (int i = 0; i < result.getRows(); i++) {
+      for (int j = 0; j < result.getColumns(); j++) {
+        double gap = (c[i][j] - result.get(i, j));
+        assertTrue(gap < 0.000001 || gap < -0.000001);
+      }
+    }    
+  }
+
+  private static void collectBlocksFromFile(Path path, boolean b,
+      String collectionTable, HamaConfiguration conf) throws IOException {
+    JobConf jobConf = new JobConf(conf);
+    jobConf.setJobName("Blocking MR job" + path);
+
+    jobConf.setMapperClass(MyMapper.class);
+    jobConf.setInputFormat(SequenceFileInputFormat.class);
+    FileInputFormat.addInputPath(jobConf, path);
+
+    MyMapper.initJob(collectionTable, b, 2, 4, 4, jobConf);
+    JobClient.runJob(jobConf);
+  }
+
+  public static class MyMapper extends CollectBlocksMapReduceBase implements
+      CollectBlocksMap<IntWritable, MapWritable> {
+    private MapWritable value;
+
+    @Override
+    public void map(IntWritable key, MapWritable value,
+        OutputCollector<BlockID, MapWritable> output, Reporter reporter)
+        throws IOException {
+      int startColumn, endColumn, blkRow = key.get() / mBlockRowSize, i = 0;
+      this.value = value;
+      
+      do {
+        startColumn = i * mBlockColSize;
+        endColumn = startColumn + mBlockColSize - 1;
+        if (endColumn >= mColumns) // the last sub vector
+          endColumn = mColumns - 1;
+        output.collect(new BlockID(blkRow, i), subVector(key.get(), startColumn, endColumn));
+
+        i++;
+      } while (endColumn < (mColumns - 1));
+    }
+
+    private MapWritable subVector(int row, int i0, int i1) {
+      DenseVector res = new DenseVector();
+      res.setRow(row);
+      
+      for (int i = i0; i <= i1; i++) {
+        res.set(i, ((DoubleWritable) this.value.get(new IntWritable(i))).get());
+      }
+
+      return res.getEntries();
+    }
+  }
+}

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/graph/TestGraph.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/graph/TestGraph.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/graph/TestGraph.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/graph/TestGraph.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,144 @@
+/**
+ * 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.graph;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.hama.HCluster;
+import org.apache.hama.HamaConfiguration;
+import org.apache.log4j.Logger;
+
+public class TestGraph extends TestCase {
+  static final Logger LOG = Logger.getLogger(TestGraph.class);
+  private static HamaConfiguration conf;
+  private static Graph adj;
+  private static int[] result = new int[] { 4, 3, 2, 0, 1 };
+
+  public static Test suite() {
+    TestSetup setup = new TestSetup(new TestSuite(TestGraph.class)) {
+      protected void setUp() throws Exception {
+        HCluster hCluster = new HCluster();
+        hCluster.setUp();
+
+        conf = hCluster.getConf();
+        adj = new SparseGraph(conf);
+      }
+
+      protected void tearDown() {
+      }
+    };
+    return setup;
+  }
+
+  public void testAddEdge() throws IOException {
+    adj.addEdge(0, 1);
+    adj.addEdge(0, 2);
+    adj.addEdge(2, 3);
+    adj.addEdge(3, 4);
+    adj.addEdge(3, 6);
+    adj.addEdge(4, 6);
+
+    LocalBFSearcher bfs = new LocalBFSearcher(adj, 1);
+    int[] path = bfs.path(4);
+
+    for (int i = 0; i < path.length; i++) {
+      assertEquals(result[i], path[i]);
+    }
+  }
+
+  static class LocalBFSearcher {
+    private Map<Integer, Integer> visited = new HashMap<Integer, Integer>();
+
+    public LocalBFSearcher(Graph G, int s) throws IOException {
+      Queue q = new Queue();
+      q.enqueue(s);
+
+      while (!q.isEmpty()) {
+        int v = (Integer) q.dequeue();
+        int[] neighbors = G.neighborsOf(v);
+        for (int i = 0; i < neighbors.length; i++) {
+          int w = neighbors[i];
+          if (visited.get(w) == null) {
+            q.enqueue(w);
+            if (s != w)
+              visited.put(w, v);
+          }
+        }
+      }
+    }
+
+    public int pathLength(int v) {
+      int len = -1;
+      while (visited.get(v) != null) {
+        v = (Integer) visited.get(v);
+        len++;
+      }
+      return len;
+    }
+
+    public int[] path(int v) {
+      int N = pathLength(v);
+      int[] p = new int[N + 1];
+      for (int i = 0; i <= N; i++) {
+        p[i] = v;
+        v = (Integer) visited.get(v);
+      }
+      return p;
+    }
+  }
+
+  static class Queue {
+    private Node first;
+    private Node last;
+
+    public boolean isEmpty() {
+      return (first == null);
+    }
+
+    public void enqueue(Object anItem) {
+      Node x = new Node();
+      x.item = anItem;
+      x.next = null;
+      if (isEmpty())
+        first = x;
+      else
+        last.next = x;
+      last = x;
+    }
+
+    public Object dequeue() {
+      Object val = first.item;
+      first = first.next;
+      return val;
+    }
+  }
+
+  static class Node {
+    Object item;
+    Node next;
+  }
+}

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/io/TestBlockID.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/io/TestBlockID.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/io/TestBlockID.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/io/TestBlockID.java Thu Sep 10 05:32:52 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.io;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.io.DataInputBuffer;
+import org.apache.hadoop.io.DataOutputBuffer;
+
+public class TestBlockID extends TestCase {
+  final static Log LOG = LogFactory.getLog(TestBlockID.class.getName());
+
+  /**
+   * BlockID object compare
+   */
+  public void testCompare() {
+    BlockID a = new BlockID(1, 3);
+    BlockID b = new BlockID(1, 1);
+    assertEquals(a.compareTo(b), 1);
+
+    BlockID c = new BlockID(3, 1);
+    BlockID d = new BlockID(1, 1);
+    assertEquals(a.compareTo(c), -1);
+
+    assertEquals(b.compareTo(d), 0);
+  }
+  
+  /**
+   * BlockID object IO
+   * @throws IOException 
+   */
+  public void testIO() throws IOException {
+    DataOutputBuffer outBuf = new DataOutputBuffer();
+    DataInputBuffer inBuf = new DataInputBuffer();
+    
+    BlockID a = new BlockID(1, 3);
+    outBuf.reset();
+    a.write(outBuf);
+    
+    inBuf.reset(outBuf.getData(), outBuf.getLength());
+    BlockID b = new BlockID();
+    b.readFields(inBuf);
+    
+    assertEquals(0, a.compareTo(b));
+  }
+
+}

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/mapred/TestBlockMatrixMapReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/mapred/TestBlockMatrixMapReduce.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/mapred/TestBlockMatrixMapReduce.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/mapred/TestBlockMatrixMapReduce.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,60 @@
+/**
+ * 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.hama.DenseMatrix;
+import org.apache.hama.HCluster;
+import org.apache.log4j.Logger;
+
+public class TestBlockMatrixMapReduce extends HCluster {
+  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 = (DenseMatrix) m1.mult(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);
+      }
+    }
+  }
+}

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,58 @@
+/**
+ * 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.hama.DenseMatrix;
+import org.apache.hama.HCluster;
+import org.apache.hama.SparseMatrix;
+import org.apache.log4j.Logger;
+
+public class TestRandomMatrixMapReduce extends HCluster {
+  static final Logger LOG = Logger.getLogger(TestRandomMatrixMapReduce.class);
+  
+  public void testRandomMatrixMapReduce() throws IOException {
+    DenseMatrix rand = DenseMatrix.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) > -1);
+      }
+    }
+
+    rand.close();
+    
+    SparseMatrix rand2 = SparseMatrix.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();
+  }
+}

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/shell/parser/expression/TestHamaExpressionParser.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,182 @@
+package org.apache.hama.shell.parser.expression;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+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.shell.HamaShellEnv;
+import org.apache.hama.shell.execution.HamaExpression;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class TestHamaExpressionParser extends TestCase {
+
+  final static Log log = LogFactory.getLog(TestHamaExpressionParser.class);
+
+  // grammar test
+  static final String badExpr1 = "b + c;"; // we only accept evaluation
+  // expression
+  // or save expression
+  static final String badExpr2 = "a = b + c"; // missing ";"
+  static final String badExpr3 = "a = b + (c + d * b ;"; // missing ")"
+  // grammar test and aliases test
+  static final String Expr1 = "a = b;";
+  static final String Expr2 = "a = b + (c+d) * b;";
+  static final String Expr3 = "a = e;";
+  // execution test
+  // A + B : A.rows = B.rows ; A.columns = B.columns ;
+  // A * B : A.columns = B.rows ;
+  static final String Expr4 = "m1 = ( b + c ) * f ;"; // b.columns = c.columns =
+  // f.rows
+  static final String Expr4_Aliase = "m1";
+
+  static final String Expr5 = "m2 = ( b + c ) * g ;"; // c.columns != f.rows
+  static final String Expr5_Aliase = "m2";
+
+  static final String Expr6 = "m3 = b + f ; "; // b.columns != f.columns
+  static final String Expr6_Aliase = "m3";
+
+  private static int SIZE1 = 10;
+  private static int SIZE2 = 20;
+  private static Matrix B;
+  private static Matrix C;
+  private static Matrix D;
+
+  private static Matrix F;
+  private static Matrix G;
+
+  private static HamaConfiguration hConfig;
+  private static HamaShellEnv env;
+
+  public static Test suite() {
+    TestSetup setup = new TestSetup(new TestSuite(
+        TestHamaExpressionParser.class)) {
+      protected void setUp() throws Exception {
+        HCluster hCluster = new HCluster();
+        hCluster.setUp();
+
+        B = DenseMatrix.random(hCluster.getConf(), SIZE1, SIZE1);
+        C = DenseMatrix.random(hCluster.getConf(), SIZE1, SIZE1);
+        D = DenseMatrix.random(hCluster.getConf(), SIZE1, SIZE1);
+
+        F = DenseMatrix.random(hCluster.getConf(), SIZE1, SIZE2);
+        G = DenseMatrix.random(hCluster.getConf(), SIZE2, SIZE1);
+
+        hConfig = hCluster.getConf();
+        env = new HamaShellEnv();
+
+        setUpShellEnv();
+      }
+
+      protected void tearDown() {
+        log.info("tearDown()");
+        try {
+          close();
+        } catch (IOException e) {
+          e.printStackTrace();
+        }
+      }
+    };
+    return setup;
+  }
+
+  private static void close() throws IOException {
+    B.close();
+    C.close();
+    D.close();
+    F.close();
+    G.close();
+  }
+
+  private static void setUpShellEnv() {
+    env.setAliase("b", B);
+    env.setAliase("c", C);
+    env.setAliase("d", D);
+
+    env.setAliase("f", F);
+    env.setAliase("g", G);
+  }
+
+  private void testBadExpression(String expression) {
+    InputStream in = new ByteArrayInputStream(expression.getBytes());
+    HamaExpressionParser parser = new HamaExpressionParser(in, env, hConfig);
+    try {
+      HamaExpression hExpression = parser.Start();
+      assertNull(hExpression);
+    } catch (ParseException e) {
+    }
+  }
+
+  private void testNormalExpression(String expression) throws ParseException {
+    InputStream in = new ByteArrayInputStream(expression.getBytes());
+    HamaExpressionParser parser = new HamaExpressionParser(in, env, hConfig);
+    HamaExpression hExpression = parser.Start();
+    assertNotNull(hExpression);
+  }
+
+  /**
+   * Expression Grammar Parser Test
+   * 
+   * @throws ParseException
+   */
+  public void testGrammar() throws ParseException {
+    // badExpr1
+    testBadExpression(badExpr1);
+    // badExpr2
+    testBadExpression(badExpr2);
+    // badExpr3
+    testBadExpression(badExpr3);
+    // Expr1
+    testNormalExpression(Expr1);
+    // Expr2
+    testNormalExpression(Expr2);
+  }
+
+  /**
+   * Expression Aliases Test
+   * 
+   * we cannot use an aliases without initialization.
+   * 
+   * @throws ParseException
+   */
+  public void testAliases() throws ParseException {
+    // Expr3
+    testBadExpression(Expr3);
+    // Expr1
+    testNormalExpression(Expr1);
+  }
+
+  private void testExprExec(String expression, String matrixAliase,
+      boolean isNormalExpr) throws ParseException {
+    InputStream in = new ByteArrayInputStream(expression.getBytes());
+    HamaExpressionParser parser = new HamaExpressionParser(in, env, hConfig);
+    HamaExpression hExpression = parser.Start();
+    assertNotNull(hExpression);
+    hExpression.execute();
+    assertEquals(env.containAliase(matrixAliase), isNormalExpr);
+  }
+
+  /**
+   * Expression Execution Test
+   * 
+   * @throws ParseException
+   */
+  public void testExprExec() throws ParseException {
+    // Expr4
+    testExprExec(Expr4, Expr4_Aliase, true);
+    // Expr5
+    testExprExec(Expr5, Expr5_Aliase, false);
+    // Expr6
+    testExprExec(Expr6, Expr6_Aliase, false);
+  }
+
+}

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/util/TestNumeric.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/util/TestNumeric.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/util/TestNumeric.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/util/TestNumeric.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,54 @@
+/**
+ * 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.util;
+
+import junit.framework.TestCase;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hama.Constants;
+
+public class TestNumeric extends TestCase {
+  final static int TEST_INT = 3;
+  final static double TEST_DOUBLE = 0.4;
+
+  /**
+   * Integer conversion test
+   */
+  public void testInteger() {
+    assertEquals(BytesUtil.bytesToInt(BytesUtil.intToBytes(TEST_INT)), TEST_INT);
+  }
+
+  /**
+   * Double conversion test
+   */
+  public void testDouble() {
+    assertEquals(BytesUtil.bytesToDouble(BytesUtil.doubleToBytes(TEST_DOUBLE)),
+        TEST_DOUBLE);
+  }
+
+  /**
+   * Get the column index from hbase.
+   */
+  public void testGetColumnIndex() {
+    byte[] result = BytesUtil.getColumnIndex(3);
+    assertEquals(Bytes.toString(result), Constants.COLUMN
+        + BytesUtil.getColumnIndex(result));
+  }
+}

Added: incubator/hama/branches/hama-0.19/src/test/org/apache/hama/util/TestRandomVariable.java
URL: http://svn.apache.org/viewvc/incubator/hama/branches/hama-0.19/src/test/org/apache/hama/util/TestRandomVariable.java?rev=813233&view=auto
==============================================================================
--- incubator/hama/branches/hama-0.19/src/test/org/apache/hama/util/TestRandomVariable.java (added)
+++ incubator/hama/branches/hama-0.19/src/test/org/apache/hama/util/TestRandomVariable.java Thu Sep 10 05:32:52 2009
@@ -0,0 +1,74 @@
+/**
+ * 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.util;
+
+import org.apache.log4j.Logger;
+
+import junit.framework.TestCase;
+
+/**
+ * Random variable generation test
+ */
+public class TestRandomVariable extends TestCase {
+  static final Logger LOG = Logger.getLogger(TestRandomVariable.class);
+  final static int COUNT = 50;
+
+  /**
+   * Random object test
+   * 
+   * @throws Exception
+   */
+  public void testRand() throws Exception {
+    for (int i = 0; i < COUNT; i++) {
+      double result = RandomVariable.rand();
+      assertTrue(result >= 0.0d && result <= 1.0);
+    }
+  }
+
+  /**
+   * Random integer test
+   * 
+   * @throws Exception
+   */
+  public void testRandInt() throws Exception {
+    final int min = 122;
+    final int max = 561;
+
+    for (int i = 0; i < COUNT; i++) {
+      int result = RandomVariable.randInt(min, max);
+      assertTrue(result >= min && result <= max);
+    }
+  }
+
+  /**
+   * Uniform test
+   * 
+   * @throws Exception
+   */
+  public void testUniform() throws Exception {
+    final double min = 1.0d;
+    final double max = 3.0d;
+
+    for (int i = 0; i < COUNT; i++) {
+      double result = RandomVariable.uniform(min, max);
+      assertTrue(result >= min && result <= max);
+    }
+  }
+}



Mime
View raw message