0
votes

I'm multiplying Matrix1[10][2] * Matrix2[2][20] but every time my class tries to multiplies , always throws me the same exception as the mentioned title. But when i put in rows less than the number in columns on matrix 1 and columns less than number on rows on matrix two, it performs the matrix multiplication without problem this is my class for Multiplication

public class MatrixCompute extends RecursiveAction 
{ 

    private CreateMatrix a,b;
    private  CreateMatrix c;
    private int row; 


    MatrixCompute(CreateMatrix a , CreateMatrix b ,CreateMatrix c )
    {       
        this(a , b ,c,-1);
    }
    MatrixCompute(CreateMatrix a, CreateMatrix b, CreateMatrix c, int row)
    {
            if (a.getCols() != b.getRow()) 
            {
                throw new IllegalArgumentException("rows/columns mismatch");
            }
            this.a = a;
            this.b = b;
            this.c = c;
            this.row = row;
    }   

    @Override
    public void compute() 
    {
        if (row == -1)
        {
         List<MatrixCompute> tasks = new ArrayList<>();
         for (int row = 0; row < a.getRow(); row++)
         {
                    tasks.add(new MatrixCompute(a, b, c, row));
         }
         invokeAll(tasks);
       } 
       else
       {
          multiplyRowByColumn(a, b, c, row);
       }
    }

        void multiplyRowByColumn(CreateMatrix a, CreateMatrix b, CreateMatrix c, int row) {
             for (int j = 0; j < b.getCols(); j++) {
                    for (int k = 0; k < a.getCols(); k++) {
                        c.setValue(row, j, (int)(c.getValue(row, j) +  a.getValue(row, k)* b.getValue(k, j)));
                    }
                }
        }

}

and the class for wrapping a matrix:

public class CreateMatrix 
{
    private int[][] matrix; 


    public CreateMatrix (int row, int col )
    {
        matrix = new int[row][col]; 
    }

    public void fillMatrix()
    {
        for(int i = 0; i < matrix.length; i++)
        {
            for(int j = 0; j < matrix[i].length ;j++ )
            {
                 Random r = new Random();
                matrix[i][j] = r.nextInt() * 5;
            }   
        }

    }



    public int getCols()
    {
        return matrix[0].length;
    }
    public int getRow()
    {
       return matrix.length;
    }


     int getValue(int row, int col)
     {
            return matrix[row][col];
     }

    void setValue(int row, int col, int value)
    {
            matrix[row][col] = value;
     }
}

here is the statment where the operation is being executed :

 result = new CreateMatrix(row, col);
                     ForkJoinPool pool = new ForkJoinPool();
                     pool.invoke(new MatrixCompute(container[0], container[1], result));

and here where the matrix are being decalred:

CreateMatrix matrix1 =  new CreateMatrix(Integer.parseInt(txtfil.getText()), Integer.parseInt(txtcol.getText()));
                            container[0] = matrix1;
                            container[0].fillMatrix();

                            CreateMatrix matrix2 =  new CreateMatrix(Integer.parseInt(txrow.getText()), Integer.parseInt(txtcol2.getText()));
                            container[1] = matrix2;

an finally the size of matrix result is declared by txrow.getText() and txtcol.getText()

so since the only exception on multiplying matrix must be the columns on matrix one must be the same as row in Matrix2, so why is throwing me exception with greatest values in Matrix1's rows and Matrix2's colums

1
what are you actually doing with this in your MatrixCompute class's constructor MatrixCompute(CreateMatrix a , CreateMatrix b ,CreateMatrix c ) { this(a , b ,c,-1); }Ahad
it's just a flag for starting iterate over the rows of the matrix ,so when rows is equal to -1 starts iterating over the matrix to get the row's index of every matrixSalvador Romo
Post exception.talex
post exception ? what does that mean?Salvador Romo
When Java encounters an exception, it prints the exception to the terminal, along with the file and line number where it happened. This is more important in debugging exceptions than posting your entire source code.Ian Emnace

1 Answers

1
votes

First, a bit of Math. We know that An,m * Bm,p = Cn,p. Which means that for each cell in the row i column j in C, we have:

Cell Value

So to get C we have to:

int[][] matrixA = new int[n][m];
int[][] matrixB = new int[m][p];

//You could check if the matrixes above can multiply, by throwing an 
//exception if it does not.

int[][] matrixC = new int[n][p];

for ( int i = 0 ; i < n ; i++ ) {
    for ( int j = 0 ; j < p ; j++ ) {
        for ( int k = 0 ; k < m ; k++ ) {
            matrixC[i][j] += matrixA[i][k]*matrixB[k][j];
        }
    }
}

EDIT: The reason your code throws an ArrayIndexOutOfBoundsException exception, it's because you are trying iterate beside the bounds of an array, which means you are trying to access, per example, in a Matrix1[10][2] you are trying to read the value on Matrix1[11][3], which it does not exist.

You code is a bit confusing, so I just put a bit of math to help you understand a better, cleaner and simpler way.

I hope I have helped.

Have a nice day.