Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

strassens method error

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;
       }
     

      // add two arrays together
      public static int[][] addArrays(int n,int[][] a,int[][] b) {
             int[][] addA = new int[n][n];
            for (int i = 0; i < n; i++) {
                   for (int y = 0; y <n; y++) {
                              addA[i][y] = a[i][y]+b[i][y];      
                        }      
                  }            
      return addA;
      }
         
      //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);
               
                  
                nP1 = strassensMult(m,addArrays(m,ap00,ap11), addArrays(m,bp00,bp11));
                nP2 = strassensMult(m,addArrays(m,ap10,ap11), bp00);
                nP3 = strassensMult(m,ap00, subArrays(m,bp01,bp11));
                nP4 = strassensMult(m,ap11, subArrays(m,bp10,bp00));
                nP5 = strassensMult(m,addArrays(m,ap00,ap01), bp11);
                nP6 = strassensMult(m,subArrays(m,ap10,ap00), addArrays(m,bp00,bp01));       
                nP7 = strassensMult(m,subArrays(m,ap01,ap11), addArrays(m,bp10,bp11));
             
                C1= addArrays(m,subArrays(m,addArrays(m,nP1,nP4),nP5),nP7);
                C2= addArrays(m,nP3,nP5);
                C3= addArrays(m,nP2,nP4);
                C4= addArrays(m,subArrays(m,addArrays(m,nP1,nP3),nP2),nP6);
            
               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 {
                        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
                                
                        //read matrix from file
                        matrixSize= Integer.valueOf(in.readLine()).intValue();
                        a= new int[matrixSize][matrixSize];
                        for (int row=0;row<matrixSize;row++){
                           for (int col=0;col<matrixSize;col++){
                              a[row][col]=Integer.valueOf(in.readLine()).intValue();
                            }
                        }
                        
                        finMat=strassensMult(matrixSize,a,a);
                        printMatrix(finMat);
                  }catch (Exception ex){System.out.println("Error reading file");}
              }
      }

   }
0
MdFool
Asked:
MdFool
  • 6
  • 2
1 Solution
 
petmagdyCommented:
could u please post the contents of the file u r trying to read
0
 
petmagdyCommented:
also this:
>>catch (Exception ex){System.out.println("Error reading file");}
change it to this and post the results:

catch (Exception ex) {
                System.out.println("Error reading file");
                ex.printStackTrace();
            }
0
 
MdFoolAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
petmagdyCommented:
could u please also apply the my 2nd comment and post the result Error stack trace
0
 
petmagdyCommented:
ok I solve it for u, update ur try{...}catch(){...} block to the following:

                  try {
                BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\tempo\\data.txt")));
                matrixSize = 0;
                        //read matrix from file
                try
                        {
                      matrixSize = Integer.parseInt(in.readLine());
                        }
                catch(NumberFormatException ne)
                        {
                      
                        }
                        a = new int[matrixSize][matrixSize];
                        for (int row = 0; row < matrixSize; row++) {
                              for (int col = 0; col < matrixSize; col++) {
                            try
                                    {
                                          a[row][col] = Integer.parseInt(in.readLine());
                                    }
                            catch(NumberFormatException ne)
                                    {
                                  
                                    }
                                    
                              }
                        }

                        finMat = strassensMult(matrixSize, a, a);
                        printMatrix(finMat);
                  } catch (Exception ex) {
                        System.out.println("Error reading file");
                        ex.printStackTrace();
                  }
0
 
petmagdyCommented:
offcourse replac:

 >>              BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream "c:\\tempo\\data.txt")));

with:

                    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));

(ur version)

the reason for this if a line is empty in the line the process will exit with a NumberFormatException
 
0
 
MdFoolAuthor Commented:
i dont think i was having a number format exception though, it just wasnt calculating the correct matrix...
0
 
petmagdyCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now