0

I'm trying to make submatrices to multiply two matrices into one. I've already been given the pseudocode, thus all attempts to find the proper way of implementing it in java, I'm still stuck on the basics. What would the overall structure of the java code for this pseoudocode look like ?

SQUARE-MATRIX-MULTITPLY-RECURSIVE(A,B)

n = A.rows
let C be a n x n matrix 
if n == 1  
   c11 = a11 * b11
else partition A, B and C
     C11 = SQURAE-MATRIX-MULTIPLY.RECURSIVE(A11,B11)
          + SQUARE-MARTIX-MULTIPLY-RECURSIVE(A12,B21)
     C12 = SQUARE-MATRIX-MULTIPLY-RECURSUÌVE(A11,B12)
          + SQUARE-MATRIX-MULTIPLY-RECURSIVE(A11,B12)
     C21 = SQUARE-MATRIX-MULTIPLY-RECURSUÌVE(A12,B22)
          + SQUARE-MATRIX-MULTIPLY-RECURSUÌVE(A22,B21)
     C22 = SQUARE-MATRIX-MULTIPLY-RECURSUÌVE()A21,B12)
          + SQUARE-MATRIX-MULTIPLY-RECURSUÌVE(A22,B22)
     return C;

Since the matrices A and B are n x n matrices, they can be represented by regular arrays of size n^2. The coefficient Cij in matrix (0-indexed) era accessed in array a with a[in + j].

My task is to implement two editions: one that copies the submatrices into new arrays, and one that use two index-variables to set what part of the matrix is being used

Paul Bastide
  • 1,505
  • 4
  • 17
  • 22
rismoHiof
  • 1
  • 6
  • Same question has been already asked in another thread. [http://stackoverflow.com/questions/21496538/square-matrix-multiply-recursive-in-java-using-divide-and-conquer](http://stackoverflow.com/questions/21496538/square-matrix-multiply-recursive-in-java-using-divide-and-conquer) – Sourabh Bhat Mar 11 '14 at 12:12

1 Answers1

0

Here is the implementation of SparceMatrix Multiplication

Vector class for sparse representation:

public class SparseVector {
    private HashST<Integer, Double> st;

    public SparseVector() {
        st = new HashST<Integer, Double>();
    }

    public int size() {
        return st.size();
    }

    public void put(int i, double x) {
        st.put(i, x);
    }

    public double get(int i) {
        if (!st.contains(i))
            return 0.0;
        else
            return st.get(i);
    }

    public double dot(double[] that) {
        double sum = 0.0;
        for (int i : st.keys())
            sum += that[i] * this.get(i);
        return sum;
    }
}

Main method/Calling code:

SparseVector[] a;
a = new SparseVector[N];
double[] x = new double[N];
double[] b = new double[N]; // Initialize a[] and x[]. ...
for (int i = 0; i < N; i++)
    b[i] = a[i].dot(x);
Prakash Bhagat
  • 1,406
  • 16
  • 30