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