### Eclipse Workspace Patch 1.0 #P hama-trunk Index: src/java/org/apache/hama/util/Numeric.java =================================================================== --- src/java/org/apache/hama/util/Numeric.java (revision 0) +++ src/java/org/apache/hama/util/Numeric.java (revision 0) @@ -0,0 +1,90 @@ +/** + * 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.hadoop.hbase.util.Bytes; +import org.apache.hama.Constants; + +/** + * Provides a number format conversion + */ +public class Numeric { + /** + * Bytes to integer conversion + * + * @param bytes + * @return the converted value + */ + public static int bytesToInt(byte[] bytes) { + return Integer.parseInt(Bytes.toString(bytes)); + } + + /** + * Integer to bytes conversion + * + * @param integer + * @return the converted value + */ + public static byte[] intToBytes(int integer) { + return Bytes.toBytes(String.valueOf(integer)); + } + + /** + * Bytes to double conversion + * + * @param bytes + * @return the converted value + */ + public static double bytesToDouble(byte[] bytes) { + return Double.parseDouble(Bytes.toString(bytes)); + } + + /** + * Double to bytes conversion + * + * @param doubleValue + * @return the converted value + */ + public static byte[] doubleToBytes(Double doubleValue) { + return Bytes.toBytes(doubleValue.toString()); + } + + /** + * Gets the column index + * + * @param bytes + * @return the converted value + */ + public static int getColumnIndex(byte[] bytes) { + String cKey = new String(bytes); + return Integer.parseInt(cKey + .substring(cKey.indexOf(":") + 1, cKey.length())); + } + + /** + * Gets the column index + * + * @param integer + * @return the converted value + */ + public static byte[] getColumnIndex(int integer) { + return Bytes.toBytes(Constants.COLUMN + String.valueOf(integer)); + } +} Index: src/test/org/apache/hama/TestRandomVariable.java =================================================================== --- src/test/org/apache/hama/TestRandomVariable.java (revision 687699) +++ src/test/org/apache/hama/TestRandomVariable.java (working copy) @@ -1,71 +0,0 @@ -/** - * Copyright 2007 The Apache Software Foundation - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hama; - -import junit.framework.TestCase; - -/** - * Random variable generation test - */ -public class TestRandomVariable extends TestCase { - 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); - } - } -} Index: src/test/org/apache/hama/util/TestRandomVariable.java =================================================================== --- src/test/org/apache/hama/util/TestRandomVariable.java (revision 0) +++ src/test/org/apache/hama/util/TestRandomVariable.java (revision 0) @@ -0,0 +1,71 @@ +/** + * 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; + +/** + * Random variable generation test + */ +public class TestRandomVariable extends TestCase { + 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); + } + } +} Index: src/java/org/apache/hama/util/RandomVariable.java =================================================================== --- src/java/org/apache/hama/util/RandomVariable.java (revision 0) +++ src/java/org/apache/hama/util/RandomVariable.java (revision 0) @@ -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.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 name. + * + * @return random name + */ + public static String randMatrixName() { + String rName = Constants.RANDOM; + for (int i = 1; i <= 5; i++) { + char ch = (char) ((Math.random() * 26) + 97); + rName += ch; + } + + return rName; + } + + /** + * 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; + } +} Index: src/java/org/apache/hama/RandomVariable.java =================================================================== --- src/java/org/apache/hama/RandomVariable.java (revision 687699) +++ src/java/org/apache/hama/RandomVariable.java (working copy) @@ -1,224 +0,0 @@ -/** - * Copyright 2007 The Apache Software Foundation - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hama; - -/** - * 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. - */ - protected 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. - */ - protected 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 name. - * - * @return random name - */ - protected static String randMatrixName() { - String rName = Constants.RANDOM; - for (int i = 1; i <= 5; i++) { - char ch = (char) ((Math.random() * 26) + 97); - rName += ch; - } - - return rName; - } - - /** - * 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; - } -} Index: src/java/org/apache/hama/AbstractMatrix.java =================================================================== --- src/java/org/apache/hama/AbstractMatrix.java (revision 687699) +++ src/java/org/apache/hama/AbstractMatrix.java (working copy) @@ -30,13 +30,13 @@ import org.apache.hadoop.hbase.io.Cell; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.Text; +import org.apache.hama.util.Numeric; import org.apache.log4j.Logger; /** * Methods of the matrix classes */ -public abstract class AbstractMatrix extends AbstractBase implements - MatrixInterface { +public abstract class AbstractMatrix implements MatrixInterface { static final Logger LOG = Logger.getLogger(AbstractMatrix.class); /** Hama Configuration */ @@ -58,7 +58,7 @@ public void setConfiguration(HamaConfiguration conf) { config = (HamaConfiguration) conf; try { - admin = new HBaseAdmin(config); + admin = new HBaseAdmin(config); } catch (MasterNotRunningException e) { LOG.error(e, e); } @@ -86,7 +86,7 @@ try { c = table.get(row, column); if (c != null) { - result = bytesToDouble(c.getValue()); + result = Numeric.bytesToDouble(c.getValue()); } } catch (IOException e) { LOG.error(e, e); @@ -106,7 +106,7 @@ public Vector getRow(byte[] row) { try { - return new Vector(bytesToInt(row), table.getRow(row)); + return new Vector(Numeric.bytesToInt(row), table.getRow(row)); } catch (IOException e) { LOG.error(e, e); } @@ -139,7 +139,8 @@ /** {@inheritDoc} */ public void set(int i, int j, double value) { BatchUpdate b = new BatchUpdate(new Text(String.valueOf(i))); - b.put(new Text(Constants.COLUMN + String.valueOf(j)), doubleToBytes(value)); + b.put(new Text(Constants.COLUMN + String.valueOf(j)), Numeric + .doubleToBytes(value)); try { table.commit(b); } catch (IOException e) { Index: src/java/org/apache/hama/AbstractBase.java =================================================================== --- src/java/org/apache/hama/AbstractBase.java (revision 687699) +++ src/java/org/apache/hama/AbstractBase.java (working copy) @@ -1,90 +0,0 @@ -/** - * Copyright 2007 The Apache Software Foundation - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hama; - -import org.apache.hadoop.hbase.util.Bytes; - -/** - * Provides a number format conversion - */ -public abstract class AbstractBase { - - /** - * Bytes to integer conversion - * - * @param bytes - * @return the converted value - */ - public int bytesToInt(byte[] bytes) { - return Integer.parseInt(Bytes.toString(bytes)); - } - - /** - * Integer to bytes conversion - * - * @param integer - * @return the converted value - */ - public byte[] intToBytes(int integer) { - return Bytes.toBytes(String.valueOf(integer)); - } - - /** - * Bytes to double conversion - * - * @param bytes - * @return the converted value - */ - public double bytesToDouble(byte[] bytes) { - return Double.parseDouble(Bytes.toString(bytes)); - } - - /** - * Double to bytes conversion - * - * @param doubleValue - * @return the converted value - */ - public byte[] doubleToBytes(Double doubleValue) { - return Bytes.toBytes(doubleValue.toString()); - } - - /** - * Gets the column index - * - * @param bytes - * @return the converted value - */ - public int getColumnIndex(byte[] bytes) { - String cKey = new String(bytes); - return Integer.parseInt(cKey - .substring(cKey.indexOf(":") + 1, cKey.length())); - } - - /** - * Gets the column index - * - * @param integer - * @return the converted value - */ - public byte[] getColumnIndex(int integer) { - return Bytes.toBytes(Constants.COLUMN + String.valueOf(integer)); - } -} Index: src/java/org/apache/hama/Matrix.java =================================================================== --- src/java/org/apache/hama/Matrix.java (revision 687699) +++ src/java/org/apache/hama/Matrix.java (working copy) @@ -31,6 +31,7 @@ import org.apache.hama.algebra.AdditionReduce; import org.apache.hama.mapred.MatrixMap; import org.apache.hama.mapred.MatrixReduce; +import org.apache.hama.util.RandomVariable; /** * A library for mathematical operations on matrices of double. Index: src/java/org/apache/hama/util/package.html =================================================================== --- src/java/org/apache/hama/util/package.html (revision 0) +++ src/java/org/apache/hama/util/package.html (revision 0) @@ -0,0 +1,23 @@ + + + + + +Numerical and variable utilities + + Index: src/java/org/apache/hama/Vector.java =================================================================== --- src/java/org/apache/hama/Vector.java (revision 687699) +++ src/java/org/apache/hama/Vector.java (working copy) @@ -28,6 +28,7 @@ import org.apache.hadoop.hbase.io.RowResult; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hama.io.VectorWritable; +import org.apache.hama.util.Numeric; import org.apache.log4j.Logger; public class Vector extends VectorWritable implements VectorInterface { @@ -44,7 +45,7 @@ public Vector(int row, RowResult rowResult) { this.cells = new HbaseMapWritable(); - this.row = intToBytes(row); + this.row = Numeric.intToBytes(row); for (Map.Entry f : rowResult.entrySet()) { this.cells.put(f.getKey(), f.getValue()); } @@ -53,14 +54,14 @@ /** * Get the row for this Vector */ - public byte [] getRow() { + public byte[] getRow() { return row; } - + public HbaseMapWritable getCells() { return cells; } - + public void add(int index, double value) { // TODO Auto-generated method stub @@ -99,7 +100,7 @@ while (it.hasNext()) { byte[] key = it.next(); - double oValue = bytesToDouble(get(key).getValue()); + double oValue = Numeric.bytesToDouble(get(key).getValue()); double nValue = oValue * alpha; Cell cValue = new Cell(String.valueOf(nValue), System.currentTimeMillis()); cells.put(key, cValue); @@ -109,7 +110,8 @@ } public double get(int index) { - return bytesToDouble(cells.get(getColumnIndex(index)).getValue()); + return Numeric.bytesToDouble(cells.get(Numeric.getColumnIndex(index)) + .getValue()); } public double norm(Norm type) { @@ -125,7 +127,7 @@ public void set(int index, double value) { Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis()); - cells.put(getColumnIndex(index), cValue); + cells.put(Numeric.getColumnIndex(index), cValue); } public Vector set(Vector v) { @@ -139,7 +141,7 @@ Iterator it = keySet.iterator(); while (it.hasNext()) { - sum += bytesToDouble(get(it.next()).getValue()); + sum += Numeric.bytesToDouble(get(it.next()).getValue()); } return sum; @@ -152,7 +154,7 @@ Iterator it = keySet.iterator(); while (it.hasNext()) { - double value = bytesToDouble(get(it.next()).getValue()); + double value = Numeric.bytesToDouble(get(it.next()).getValue()); square_sum += value * value; } Index: src/java/org/apache/hama/io/VectorWritable.java =================================================================== --- src/java/org/apache/hama/io/VectorWritable.java (revision 687699) +++ src/java/org/apache/hama/io/VectorWritable.java (working copy) @@ -35,11 +35,10 @@ import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Writables; import org.apache.hadoop.io.Writable; -import org.apache.hama.AbstractBase; import org.apache.hama.Vector; +import org.apache.hama.util.Numeric; -public class VectorWritable extends AbstractBase implements Writable, - Map { +public class VectorWritable implements Writable, Map { public byte[] row; public HbaseMapWritable cells; @@ -128,7 +127,7 @@ * Get the double value without timestamp */ public double get(int key) { - return bytesToDouble(get(intToBytes(key)).getValue()); + return Numeric.bytesToDouble(get(Numeric.intToBytes(key)).getValue()); } public int size() { Index: src/java/org/apache/hama/mapred/MatrixInputFormatBase.java =================================================================== --- src/java/org/apache/hama/mapred/MatrixInputFormatBase.java (revision 687699) +++ src/java/org/apache/hama/mapred/MatrixInputFormatBase.java (working copy) @@ -22,14 +22,13 @@ import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapred.RecordReader; import org.apache.hadoop.mapred.Reporter; -import org.apache.hama.AbstractBase; import org.apache.hama.Vector; import org.apache.hama.io.VectorWritable; -public abstract class MatrixInputFormatBase extends AbstractBase -implements InputFormat { +public abstract class MatrixInputFormatBase implements + InputFormat { private final Log LOG = LogFactory.getLog(MatrixInputFormatBase.class); - private byte [][] inputColumns; + private byte[][] inputColumns; private HTable table; private TableRecordReader tableRecordReader; private RowFilterInterface rowFilter; @@ -37,37 +36,36 @@ /** * Iterate over an HBase table data, return (Text, VectorResult) pairs */ - protected class TableRecordReader - implements RecordReader { - private byte [] startRow; - private byte [] endRow; + protected class TableRecordReader implements + RecordReader { + private byte[] startRow; + private byte[] endRow; private RowFilterInterface trrRowFilter; private Scanner scanner; private HTable htable; - private byte [][] trrInputColumns; + private byte[][] trrInputColumns; /** * Build the scanner. Not done in constructor to allow for extension. - * + * * @throws IOException */ public void init() throws IOException { if ((endRow != null) && (endRow.length > 0)) { if (trrRowFilter != null) { - final Set rowFiltersSet = - new HashSet(); + final Set rowFiltersSet = new HashSet(); rowFiltersSet.add(new StopRowFilter(endRow)); rowFiltersSet.add(trrRowFilter); this.scanner = this.htable.getScanner(trrInputColumns, startRow, - new RowFilterSet(RowFilterSet.Operator.MUST_PASS_ALL, - rowFiltersSet)); + new RowFilterSet(RowFilterSet.Operator.MUST_PASS_ALL, + rowFiltersSet)); } else { - this.scanner = - this.htable.getScanner(trrInputColumns, startRow, endRow); + this.scanner = this.htable.getScanner(trrInputColumns, startRow, + endRow); } } else { - this.scanner = - this.htable.getScanner(trrInputColumns, startRow, trrRowFilter); + this.scanner = this.htable.getScanner(trrInputColumns, startRow, + trrRowFilter); } } @@ -81,22 +79,22 @@ /** * @param inputColumns the columns to be placed in {@link VectorWritable}. */ - public void setInputColumns(final byte [][] inputColumns) { + public void setInputColumns(final byte[][] inputColumns) { this.trrInputColumns = inputColumns; } /** * @param startRow the first row in the split */ - public void setStartRow(final byte [] startRow) { + public void setStartRow(final byte[] startRow) { this.startRow = startRow; } /** - * + * * @param endRow the last row in the split */ - public void setEndRow(final byte [] endRow) { + public void setEndRow(final byte[] endRow) { this.endRow = endRow; } @@ -114,7 +112,7 @@ /** * @return ImmutableBytesWritable - * + * * @see org.apache.hadoop.mapred.RecordReader#createKey() */ public ImmutableBytesWritable createKey() { @@ -123,7 +121,7 @@ /** * @return VectorResult - * + * * @see org.apache.hadoop.mapred.RecordReader#createValue() */ public Vector createValue() { @@ -146,15 +144,15 @@ /** * @param key HStoreKey as input key. * @param value MapWritable as input value - * - * Converts Scanner.next() to Text, VectorResult - * + * + * Converts Scanner.next() to Text, VectorResult + * * @return true if there was more data * @throws IOException */ @SuppressWarnings("unchecked") public boolean next(ImmutableBytesWritable key, Vector value) - throws IOException { + throws IOException { RowResult result = this.scanner.next(); boolean hasMore = result != null && result.size() > 0; if (hasMore) { @@ -166,17 +164,15 @@ } /** - * Builds a TableRecordReader. If no TableRecordReader was provided, uses - * the default. - * + * Builds a TableRecordReader. If no TableRecordReader was provided, uses the + * default. + * * @see org.apache.hadoop.mapred.InputFormat#getRecordReader(InputSplit, * JobConf, Reporter) */ - public RecordReader getRecordReader(InputSplit split, - @SuppressWarnings("unused") - JobConf job, @SuppressWarnings("unused") - Reporter reporter) - throws IOException { + public RecordReader getRecordReader( + InputSplit split, @SuppressWarnings("unused") JobConf job, + @SuppressWarnings("unused") Reporter reporter) throws IOException { TableSplit tSplit = (TableSplit) split; TableRecordReader trr = this.tableRecordReader; // if no table record reader was provided use default @@ -201,16 +197,17 @@ * multiple {@link HRegion}s and are grouped the most evenly possible. In the * case splits are uneven the bigger splits are placed first in the * {@link InputSplit} array. - * + * * @param job the map task {@link JobConf} * @param numSplits a hint to calculate the number of splits - * + * * @return the input splits - * - * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf, int) + * + * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf, + * int) */ public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException { - byte [][] startKeys = this.table.getStartKeys(); + byte[][] startKeys = this.table.getStartKeys(); if (startKeys == null || startKeys.length == 0) { throw new IOException("Expecting at least one region"); } @@ -243,13 +240,13 @@ /** * @param inputColumns to be passed in {@link VectorWritable} to the map task. */ - protected void setInputColums(byte [][] inputColumns) { + protected void setInputColums(byte[][] inputColumns) { this.inputColumns = inputColumns; } /** * Allows subclasses to set the {@link HTable}. - * + * * @param table to get the data from */ protected void setHTable(HTable table) { @@ -258,9 +255,9 @@ /** * Allows subclasses to set the {@link TableRecordReader}. - * - * @param tableRecordReader - * to provide other {@link TableRecordReader} implementations. + * + * @param tableRecordReader to provide other {@link TableRecordReader} + * implementations. */ protected void setTableRecordReader(TableRecordReader tableRecordReader) { this.tableRecordReader = tableRecordReader; @@ -268,10 +265,10 @@ /** * Allows subclasses to set the {@link RowFilterInterface} to be used. - * + * * @param rowFilter */ protected void setRowFilter(RowFilterInterface rowFilter) { this.rowFilter = rowFilter; } -} \ No newline at end of file +}