Solved

# strassens method error

Posted on 2004-11-30
221 Views
hello, i need to finish this assignment but i dont understand whats wrong. it works for arrays of size 2x2 and 4x4 but not any larger and i cant figure it out. i just need some help in where to look for the problem

import java.io.*;

public class StrassensMatrixMult {

//get subArray from dimensions given
public static int[][] getSubArr(int[][]a ,int i0, int i1, int j0, int j1) {
int [][] subArray = new int[i1-i0+1][j1-j0+1];
for (int i = i0; i <= i1; i++) {
for (int j = j0; j <= j1; j++) {
subArray[i-i0][j-j0] = a[i][j];
}
}

return subArray;
}

public static int[][] addArrays(int n,int[][] a,int[][] b) {
for (int i = 0; i < n; i++) {
for (int y = 0; y <n; y++) {
}
}
}

//subtract two arrays
public static int[][] subArrays(int n,int[][] a,int[][] b) {
int[][] subA = new int[n][n];
for (int i = 0; i < 2; i++) {
for (int y = 0; y < 2; y++) {
subA[i][y] = a[i][y]-b[i][y];
}
}
return subA;
}

public static int[][] strassensMult(int n, int[][] a, int[][] b) {
int m;
int [][] C= new int[n][n];
int P1,P2,P3,P4,P5,P6,P7;
int [][] ap00,ap01,ap10,ap11,bp00,bp01,bp10,bp11;
int [][] nP1,nP2,nP3,nP4,nP5,nP6,nP7,C1,C2,C3,C4;

if (n==2)
{
P1=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
P2=(a[1][0]+a[1][1])*b[0][0];
P3=a[0][0]*(b[0][1]-b[1][1]);
P4=a[1][1]*(b[1][0]-b[0][0]);
P5=(a[0][0]+a[0][1])*b[1][1];
P6=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
P7=(a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

C[0][0]=P1+P4-P5+P7;
C[0][1]=P3+P5;
C[1][0]=P2+P4;
C[1][1]=P1+P3-P2+P6;

return (C);

}
else
{
m = n/2;

ap00=getSubArr(a,0,m-1,0,m-1);
ap01=getSubArr(a,0,m-1,m,n-1);
ap10=getSubArr(a,m,n-1,0,m-1);
ap11=getSubArr(a,m,n-1,m,n-1);

bp00=getSubArr(b,0,m-1,0,m-1);
bp01=getSubArr(b,0,m-1,m,n-1);
bp10=getSubArr(b,m,n-1,0,m-1);
bp11=getSubArr(b,m,n-1,m,n-1);

nP3 = strassensMult(m,ap00, subArrays(m,bp01,bp11));
nP4 = strassensMult(m,ap11, subArrays(m,bp10,bp00));

for (int i = 0; i < m; i++) {
for (int y = 0; y < m; y++) {

C[i][y]=C1[i][y];
C[i][y+m]=C2[i][y];
C[i+m][y]=C3[i][y];
C[i+m][y+m]=C4[i][y];

}

}
return C;
}

}

public static void printMatrix(int[][] c){

for (int i = 0; i < c.length; i++) {
for (int y = 0; y < c.length; y++) {
System.out.print(c[i][y]+" ");;
}
System.out.println("");
}

}

public static void main(String[] args) {
int[][]  a,finMat;

int matrixSize,hold;
String currLine;

if (args.length==0){System.out.println("Enter name of text file");}
else {
try {

a= new int[matrixSize][matrixSize];
for (int row=0;row<matrixSize;row++){
for (int col=0;col<matrixSize;col++){
}
}

finMat=strassensMult(matrixSize,a,a);
printMatrix(finMat);
}
}

}
0
Question by:MdFool

LVL 13

Expert Comment

could u please post the contents of the file u r trying to read
0

LVL 13

Expert Comment

also this:
change it to this and post the results:

catch (Exception ex) {
ex.printStackTrace();
}
0

Author Comment

8
5
7
7
2
9
1
6
8
9
9
4
2
6
2
6
7
7
2
5
5
3
2
1
0
8
8
6
8
4
4
5
2
5
9
8
8
7
1
8
6
9
0
1
0
1
2
0
7
3
0
4
8
0
5
6
0
5
6
0
7
1
7
7
7

(its the matrix in row major form with the first line showing the size of the matrix NxN)
0

LVL 13

Expert Comment

could u please also apply the my 2nd comment and post the result Error stack trace
0

LVL 13

Expert Comment

ok I solve it for u, update ur try{...}catch(){...} block to the following:

try {
matrixSize = 0;
try
{
}
catch(NumberFormatException ne)
{

}
a = new int[matrixSize][matrixSize];
for (int row = 0; row < matrixSize; row++) {
for (int col = 0; col < matrixSize; col++) {
try
{
}
catch(NumberFormatException ne)
{

}

}
}

finMat = strassensMult(matrixSize, a, a);
printMatrix(finMat);
} catch (Exception ex) {
ex.printStackTrace();
}
0

LVL 13

Expert Comment

offcourse replac:

with:

(ur version)

the reason for this if a line is empty in the line the process will exit with a NumberFormatException

0

Author Comment

i dont think i was having a number format exception though, it just wasnt calculating the correct matrix...
0

LVL 13

Accepted Solution

well, I tried it on my machine with the same file u have posted it throws number format exception because the last line was empty, when I catch the NumberFormatException the matrixes r printed, please give it a try
0

## Featured Post

### Suggested Solutions

copyEndy  challenge 15 38
triangle challenge 4 35
word0 challenge 3 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…