# Square Matrix Multiply Recursive in Java using Divide and Conquer?

I have a school project to create 2 versions of a javacode that multiplies two square matrices. To make it easier, they only have to work for 2x2, 4x4, 8x8 etc. We have a pseudo code that looks like this (taken from another question in here from the same book, most likely):

We are gonna turn this into code (i only know Java), and we have to implement the partition part. We can choose if we want a normal array or a multidimensional array. The two versions of the code goes like this: One is gonna create sub matrices (arrays) in the partition, and the second is gonna use array indexes and pass them down.

What Im most confused about is the random use of array + array and int + int in the bottom. I get the idea of the code, but I have no idea how to implement this correctly.

Can anyone point me in the right direction??

## Answers

here is a Java implementation without coping the matrices. This works only for nxn matrices so that n= 2^x.

public static int[][] matrixMultiplicationFinal(int[][] A, int[][] B){ return matrixMultiplication( A, B, 0, 0, 0,0, A.length); } public static int[][] matrixMultiplication( int[][] A, int[][] B, int rowA, int colA, int rowB, int colB, int size){ int[][] C= new int[size][size]; if(size==1) C[0][0]= A[rowA][colA]*B[rowB][colB]; else{ int newSize= size/2; //C11 sumMatrix(C, matrixMultiplication(A, B, rowA, colA, rowB, colB, newSize), matrixMultiplication(A, B, rowA, colA+newSize, rowB+ newSize, colB, newSize), 0, 0); sumMatrix(C, matrixMultiplication(A, B, rowA, colA, rowB, colB + newSize, newSize), matrixMultiplication(A, B, rowA, colA+newSize, rowB+ newSize, colB+newSize, newSize), 0, newSize); sumMatrix(C, matrixMultiplication(A, B, rowA+ newSize, colA, rowB, colB, newSize), matrixMultiplication(A, B, rowA+ newSize, colA+newSize, rowB+ newSize, colB, newSize), newSize, 0); sumMatrix(C, matrixMultiplication(A, B, rowA+ newSize, colA, rowB, colB+newSize, newSize), matrixMultiplication(A, B, rowA+ newSize, colA+newSize, rowB+ newSize, colB+newSize, newSize), newSize, newSize); } return C; } private static void sumMatrix(int[][] C, int[][]A, int[][]B,int rowC, int colC){ int n=A.length; for(int i =0; i<n; i++){ for(int j=0; j<n; j++) C[i+rowC][j+colC]=A[i][j]+B[i][j]; } }

if(n==1) { c[0][0] = a[0][0] * b[0][0]; } else { do partition part; } return c;

The fork-join framework in Java 7 api is designed for doing these kind of problems very fast (by using all CPU-cores in your computer) by calling the multiply function recursively. Look at http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html.

You have to replace the split in the fork-join framework by matrix partition in your code and each time dividing into 4 sub-tasks (instead of 2 in the example given at the link above). Do not copy elements to create smaller matrices, it will slow down the program considerably (and require lot of memory!). Just change the start and end to define sub-matrix while passing to function. The threshold in this case is going to be 1 when you update C matrix by multiplying the scalars.

Tip: Test code with very small non-symmetrical matrices with size say 4x4 so that you can manually calculate and compare answers.