### Eclipse Workspace Patch 1.0 #P Apache_Hama Index: src/java/org/apache/hama/matrix/Matrix.java =================================================================== --- src/java/org/apache/hama/matrix/Matrix.java (revision 880726) +++ src/java/org/apache/hama/matrix/Matrix.java (working copy) @@ -196,6 +196,15 @@ public Matrix add(double alpha, Matrix B) throws IOException; /** + * Find x, such that Ax = b (x,b is vector, A is matrix) using Cramer's rule + * + * @param b + * @return x + * @throws IOException + */ + public Vector solution(Matrix b) throws IOException; + + /** * C = A*B * * @param B @@ -284,4 +293,15 @@ * @throws Exception */ public void close() throws IOException; -} + + /** + * Copy each values with values of matrix 'm' + * + * @param m + * @param row + * @param col + * @throws IOException + */ + public void copy(Matrix m, int row, int col) throws IOException; + +} \ No newline at end of file Index: src/examples/org/apache/hama/examples/SolutionFinding.java =================================================================== --- src/examples/org/apache/hama/examples/SolutionFinding.java (revision 0) +++ src/examples/org/apache/hama/examples/SolutionFinding.java (revision 0) @@ -0,0 +1,88 @@ +/** + * Copyright 2007 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hama.examples; + +import java.io.IOException; + +import org.apache.hama.HamaAdmin; +import org.apache.hama.HamaAdminImpl; +import org.apache.hama.matrix.Matrix; +import org.apache.hama.matrix.Vector; + + +public class SolutionFinding extends AbstractExample { + public static void main(String[] args) throws IOException { + if (args.length < 2) { + System.out + .println("solution [-m maps] [-r reduces] "); + System.exit(-1); + } else { + parseArgs(args); + } + + String matrixA = ARGS.get(0); + String matrixB = ARGS.get(1); + + HamaAdmin admin = new HamaAdminImpl(conf); + Matrix a = admin.getMatrix(matrixA); + Matrix b = admin.getMatrix(matrixB); + + int row = a.getRows(); + int col = a.getColumns(); + + if ( row !=col) { + System.out.println("A must be square matrix."); + System.exit(-1); + } + // Now only for dense matrix + if (!a.getType().equals(b.getType())) { + System.out.println(a.getType() + " != " + b.getType()); + System.exit(-1); + } + Vector x = a.solution(b); + if (x == null) { + System.out.println("It supports only dense matrix now."); + System.exit(-1); + } + + // For Test + printOut("A", col, row, a ); + printOut("b", 1, b.getColumns(), b ); + + for (int i = 0; i < row; i++) { + System.out.println("x" + (i+1) + "=" + x.get(i)); + } + + + } + + + public static void printOut (String displayName, int col, int row, Matrix mat) throws IOException + { + System.out.println("-----------"+displayName+"-----------"); + for (int i = 0; i < col; i++) { + for (int j = 0; j < row; j++) { + System.out.print( mat.get(i, j) + " "); + } + System.out.println(); + } + } +} + Index: src/examples/org/apache/hama/examples/ExampleDriver.java =================================================================== --- src/examples/org/apache/hama/examples/ExampleDriver.java (revision 880726) +++ src/examples/org/apache/hama/examples/ExampleDriver.java (working copy) @@ -29,6 +29,7 @@ pgd.addClass("rand", RandomMatrix.class, "Generate matrix with random elements."); pgd.addClass("add", MatrixAddition.class, "Mat-Mat addition."); pgd.addClass("mult", MatrixMultiplication.class, "Mat-Mat multiplication."); + pgd.addClass("solution", SolutionFinding.class, "Finding linear solution with cramer rule."); pgd.addClass("similarity", CosineSimilarityMatrix.class, "Cosine Similarity Matrix."); pgd.addClass("norm", MatrixNorm.class, "Maximum absolute row sum of matrix"); pgd.driver(args); Index: src/java/org/apache/hama/matrix/SparseMatrix.java =================================================================== --- src/java/org/apache/hama/matrix/SparseMatrix.java (revision 880726) +++ src/java/org/apache/hama/matrix/SparseMatrix.java (working copy) @@ -337,5 +337,23 @@ // TODO Auto-generated method stub return null; } + + /** + * Find x, such that Ax = b using Cramer's rule + * + * @param b + * @return x + * @throws IOException + */ + public Vector solution(Matrix b) throws IOException { + + + return null; + } + @Override + public void copy(Matrix m, int row, int col) throws IOException { + // TODO Auto-generated method stub + throw new IOException("Not supported"); + } } Index: src/java/org/apache/hama/matrix/DenseMatrix.java =================================================================== --- src/java/org/apache/hama/matrix/DenseMatrix.java (revision 880726) +++ src/java/org/apache/hama/matrix/DenseMatrix.java (working copy) @@ -1047,4 +1047,92 @@ } return success; } + + /** + * Find x, such that Ax = b (x,b is vector, A is matrix) using Cramer's rule + * + * @param b + * @return x + * @throws IOException + */ + public Vector solution(Matrix b) throws IOException { + + DenseVector dv = new DenseVector(); + + int numberOfsolutions = getRows(); + int numberOfcolumns = getColumns(); + + double detOfA = 0.0; + double detOfBj = 0.0; + // Calculate determinant of A (with Jacobi) + jacobiEigenValue(100); + detOfA = calculateDeterminant(numberOfsolutions); + + // Pre-processing!!! (In single machine) + // Generate temporary matrix to HBase for Bj (This is for parallel computing) + System.out.println("Pre-processing......"); + ArrayList B = new ArrayList(); + + for (int j = 0; j < numberOfsolutions; j++) { + DenseMatrix matrix = new DenseMatrix(config, numberOfsolutions, numberOfcolumns); + matrix.copy(this, numberOfsolutions, numberOfcolumns); + B.add(matrix); + } + + // + // Calculate determinant of Bj (Each cramer matrix) + // + System.out.println("Starting....dividing jobs"); + // TODO: make out-mappers for parallel. + for (int j = 0; j < numberOfsolutions; j++) { + + DenseMatrix Bj = B.get(j); + // Replace j-th column of Bj with j-th of A under cramer's rule + Bj.setColumn(j, b.getColumn(0)); + + Bj.jacobiEigenValue(100); + detOfBj =Bj.calculateDeterminant(numberOfsolutions); + + // Apply cramer's rule to find solution here + dv.set(j, detOfBj/detOfA); + } + + // TODO: delete temporary files in HBase + + return dv; + } + + /** + * Calculate determinant with Eigenvalues. + * This can be available after invoking 'jacobiEigenValue' method. + * Eigenvalues are laid down in diagonal terms of matrix. + * The multiple of each diagonal terms is so called determinant. + * + * @param size the number of rows + * @return determinant + * @throws IOException + */ + public double calculateDeterminant(int size) throws IOException { + Get get = null; + double ev = 0.d; + double ret = 1.d; + for (int i = 0; i < size; i++) { + get = new Get(BytesUtil.getRowIndex(i)); + ev = Bytes.toDouble(table.get(get).getValue( + Bytes.toBytes(Constants.EI), + Bytes.toBytes(Constants.EIVAL))); + ret *=ev; + } + return ret; + } + + public void copy(Matrix m, int row, int col) throws IOException { + + for (int i = 0; i