2013-07-01 75 views
0
public Matrix<T, A> multiply(Matrix<T, A> right) throws MatrixException { 
    Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize); 



    if (rowSize != right.columnSize) 
     throw new MatrixException(
       "Cannot multiply matrices of different sizes"); 

    for (int i = 0; i < rowSize; i++) { 
     for (int j = 0; j < right.columnSize ; j++) 
      for(int k = 0; k < right.rowSize ; k++) 
      temp.matrix[i][j] = arithmetics.add(temp.matrix[i][j], 
     (arithmetics.multiply(matrix[i][k], right.matrix[k][j]))); 

    return temp; 

}} 

好了,所以我想mutiply 2點矩陣但矩陣mutiplication使用泛型

Matrix1 
0 0 1 
1 1 1 

Matrix5 
1 2 
2 4 
3 6 

我得到的

Matrix1 multiply Matrix5 
3 6 0 
6 12 0 

一個答案,但它應該是

3 6 
6 12 

因爲它矩陣複製

+0

它肯定看起來像你創建了錯誤的尺寸,結果矩陣。 –

回答

1

在你的第二個行:

Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize); 

它必須是:

Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, right.columnSize);