### Eclipse Workspace Patch 1.0 #P Apache_Hama Index: src/java/org/apache/hama/matrix/SparseMatrix.java =================================================================== --- src/java/org/apache/hama/matrix/SparseMatrix.java (revision 891202) +++ src/java/org/apache/hama/matrix/SparseMatrix.java (working copy) @@ -47,6 +47,8 @@ import org.apache.hama.io.VectorUpdate; import org.apache.hama.mapreduce.RandomMatrixMapper; import org.apache.hama.mapreduce.RandomMatrixReducer; +import org.apache.hama.matrix.algebra.MatrixAdditionMap; +import org.apache.hama.matrix.algebra.MatrixAdditionReduce; import org.apache.hama.matrix.algebra.SparseMatrixVectorMultMap; import org.apache.hama.matrix.algebra.SparseMatrixVectorMultReduce; import org.apache.hama.util.BytesUtil; @@ -181,15 +183,53 @@ @Override public Matrix add(Matrix B) throws IOException { - // TODO Auto-generated method stub - return null; + + return add(1, B); } - @Override + /** + * C = alpha*B + A + * + * @param alpha + * @param B + * @return C + * @throws IOException + */ + @Override public Matrix add(double alpha, Matrix B) throws IOException { - // TODO Auto-generated method stub - return null; + ensureForAddition(B); + + SparseMatrix result = new SparseMatrix(config, this.getRows(), this + .getColumns()); + Job job = new Job(config, "addition MR job" + result.getPath()); + + Scan scan = new Scan(); + scan.addFamily(Constants.COLUMNFAMILY); + job.getConfiguration().set(MatrixAdditionMap.MATRIX_SUMMANDS, B.getPath()); + job.getConfiguration().set(MatrixAdditionMap.MATRIX_ALPHAS, + Double.toString(alpha)); + + TableMapReduceUtil.initTableMapperJob(this.getPath(), scan, + MatrixAdditionMap.class, IntWritable.class, MapWritable.class, job); + TableMapReduceUtil.initTableReducerJob(result.getPath(), + MatrixAdditionReduce.class, job); + try { + job.waitForCompletion(true); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + return result; } + + private void ensureForAddition(Matrix m) throws IOException { + if (getRows() != m.getRows() || getColumns() != m.getColumns()) { + throw new IOException( + "Matrices' rows and columns should be same while A+B."); + } + } @Override public double get(int i, int j) throws IOException { @@ -240,7 +280,28 @@ return this.getClass().getSimpleName(); } + /** + * C = B*constants with in-memory + * + * @param constants + * @param row + * @param col + * @return C + * @throws IOException + */ + public SparseMatrix mult( double constants) throws IOException { + int row = this.getRows(); + int col = this.getColumns(); + SparseMatrix result = new SparseMatrix(config, row, col); + for (int i = 0; i