java - Divide and conquer matrix multiplication base case + how to split matrix into 4 quarters -
i've been trying code divide , conquer matrix multiplication algorithm
there's problem when try divide matrix 4 quarters gives me error arrayoutofindexbound
- i'm not sure if i'm right base case, can me out guys?
the problem @ double[][] a21
public static double[][] divideandconquer(double[][] , double[][] b, int dimension){ if (a.length == 1){ double[][] result = new double[1][1]; result[0][0]= a[0][0]*b[0][0]; return result; } else { int m = dimension/2; double[][] a11 = new double[m][m]; for(int = 0; < m ; i++){ (int j = 0 ; j< m ; j++) a11[i][j]= a[i][j]; } double[][] a21 = new double[m][m]; for(int = m; < dimension; i++){ (int j = 0 ; j< m ; j++) a21[i][j]= a[i][j]; } double[][] a12 = new double[m][m]; for(int = 0; < m ; i++){ (int j = m ; j< dimension ; j++) a12[i][j]= a[i][j]; } double[][] a22 = new double[m][m]; for(int = m; < dimension; i++){ (int j = m; j < dimension; j++) a21[i][j]= a[i][j]; } double[][] b11 = new double[m][m]; for(int = 0; < m ; i++){ (int j = 0 ; j< m ; j++) b11[i][j]= b[i][j]; } double[][] b12 = new double[m][m]; for(int = 0; < m ; i++){ (int j = m ; j< dimension ; j++) b12[i][j]= b[i][j]; } double[][] b21 = new double[m][m]; for(int = m; < dimension; i++){ (int j = 0 ; j< m ; j++) b21[i][j]= b[i][j]; } double[][] b22 = new double[m][m]; for(int = m; < dimension; i++){ (int j = m; j < dimension; j++) b21[i][j]= b[i][j]; } double[][] x1 = divideandconquer(a11,b11,m); double[][] x2 = divideandconquer(a12,b21,m); double[][] x3 = divideandconquer(a11,b12,m); double[][] x4 = divideandconquer(a12,b22,m); double[][] x5 = divideandconquer(a21,b11,m); double[][] x6 = divideandconquer(a22,b21,m); double[][] x7 = divideandconquer(a21,b12,m); double[][] x8 = divideandconquer(a22,b22,m); ..........................etc
as written, problem need subtract off array offset; e.g.,
a12[i][j]= a[i][j];
should be
a12[i][j-dimension]= a[i][j];
your bigger problem you're creating 4 new submatrices, going generate tons of garbage. once you've gotten working, think ways of doing "in-place" manipulating array indexes.
e.g., new api like
public static double[][] divideandconquer(double[][] , double[][] b, int aminindex, int amaxindex, int bminindex, bmaxindex){
and divide , conquer build subsets of min & max indices.
Comments
Post a Comment