matrix - Strassen Multiplication Algorithm StackOverFlow Error -
i working on implementing straussen's multiplication. below method multiplying them in divide , conquer approach.
public static double[][] multiply(double[][] a, double[][] b) { int n = a.length; double[][] r = new double[n][n]; /** base case **/ //if (n == 1){ // r[0][0] = a[0][0] * b[0][0]; // } //else{ double[][] a11 = new double [n/2][n/2]; double[][] a12 = new double [n/2][n/2]; double[][] a21 = new double [n/2][n/2]; double[][] a22 = new double [n/2][n/2]; double[][] b11 = new double [n/2][n/2]; double[][] b12 = new double [n/2][n/2]; double[][] b21 = new double [n/2][n/2]; double[][] b22 = new double [n/2][n/2]; /** dividing matrix 4 halves **/ split(a, a11, 0 , 0); split(a, a12, 0 , n/2); split(a, a21, n/2, 0); split(a, a22, n/2, n/2); /** dividing matrix b 4 halves **/ split(b, b11, 0 , 0); split(b, b12, 0 , n/2); split(b, b21, n/2, 0); split(b, b22, n/2, n/2); /** m1 = (a11 + a22)(b11 + b22) m2 = (a21 + a22) b11 m3 = a11 (b12 - b22) m4 = a22 (b21 - b11) m5 = (a11 + a12) b22 m6 = (a21 - a11) (b11 + b12) m7 = (a12 - a22) (b21 + b22) **/ double [][] m1 = multiply(add(a11, a22), add(b11, b22)); double [][] m2 = multiply(add(a21, a22), b11); double [][] m3 = multiply(a11, sub(b12, b22)); double [][] m4 = multiply(a22, sub(b21, b11)); double [][] m5 = multiply(add(a11, a12), b22); double [][] m6 = multiply(sub(a21, a11), add(b11, b12)); double [][] m7 = multiply(sub(a12, a22), add(b21, b22)); /** c11 = m1 + m4 - m5 + m7 c12 = m3 + m5 c21 = m2 + m4 c22 = m1 - m2 + m3 + m6 **/ double [][] c11 = add(sub(add(m1, m4), m5), m7); double [][] c12 = add(m3, m5); double [][] c21 = add(m2, m4); double [][] c22 = add(sub(add(m1, m3), m2), m6); /** join 4 halves 1 result matrix **/ join(c11, r, 0 , 0); join(c12, r, 0 , n/2); join(c21, r, n/2, 0); join(c22, r, n/2, n/2); /** return result **/ return r; }
in order implement code, reading in 2 txt files, 1 matrix , 1 matrix b. terms of testing have 2 matrices being exact same:
5
3.250 6.130 3.180 7.680 9.060
5.450 1.660 6.790 6.650 4.250
4.460 8.260 7.870 7.880 1.890
1.460 8.510 8.510 3.510 1.440
1.590 7.160 4.400 3.310 1.970
where first line n, , following lines matrix.
my problem getting stack overflow error on line
int n = a.length;
which can't seem figure out why or look. question is, problem lie in algorithm? or problem in main method?
Comments
Post a Comment