我在使用Jama進行java中的矩陣運算,但由於它們沒有稀疏矩陣,我開始使用Parallel Cold Library (PColt)。它是Colt的多線程版本。我嘗試乘以兩個矩形矩陣AxB(它是矩陣 - 矩陣乘法,非元素(或標量)乘法),大小(NxN)。我找不到在PColt中提供的矩陣 - 矩陣乘法的方法(I don' t想要元素乘法),所以我編碼的方法如下。當我乘以N = 1000兩個矩陣時,花了將近5分鐘才完成。 如果有人知道如何在pcolt中乘以兩個矩陣,它將會非常感激。 或者如果您發現代碼中存在任何不必要的錯誤並且使得複雜度較高,請通知我。我的方法如下:Java矩陣運算,並行柯爾特矩陣 - 矩陣乘法
/**
* Linear algebraic matrix-matrix multiplication; (new)A = A x B. A[i,j] =
* Sum(A[i,k] * B[k,j]), k=0..n-1. Matrix shapes: A(m x n), B(n x p), A(m x
* p).
*
* @param matrix1
* first matrix
* @param matrix2
* second matrix
* @return changes matrix1 with new values matrix-matrix multiplication
*/
public static FloatMatrix2D multiplyMatrix(FloatMatrix2D matrix1,
FloatMatrix2D matrix2) {
// create a matrix same size of input matrix
FloatMatrix2D resultMatrix = matrix1.like();
// matrix-matrix multiplication row of first matrix must be equal to
// column of second matrix
if (matrix1.rows() == matrix2.columns()) {
for (int i = 0; i < matrix1.rows(); i++) {
for (int j = 0; j < matrix2.columns(); j++) {
FloatMatrix1D rowVec = getRow(matrix1, i).copy();
FloatMatrix1D colVec = getCol(matrix2, j).copy();
// first multiplies each row with each column and then sum
// up the result and assign the result to value of matrix
float multOfVects = rowVec.assign(colVec,
FloatFunctions.mult).aggregate(FloatFunctions.plus,
FloatFunctions.identity);
// set sum of vectors to the new matrix
resultMatrix.setQuick(i, j, multOfVects);
}
System.out.println(" i th row "+ i);
}
} else {
System.err
.println("Row size of first matrix must be equal to Column size of second matrix: "
+ matrix1 + " != " + matrix2);
}
return resultMatrix;
}
////的解決方案...... Okie道基,我得到了解決。 其實忘了上面的代碼。 PColt提供矩陣矩陣乘法,但方法名稱很混亂。
反正爲了muliply兩個矩陣使用以下方法:
公共DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C) 線性代數矩陣的矩陣乘法; C = A×B;相當於A.zMult(B,C,1,0,假的,假的)
這裏必須注意參數的順序,因爲結果保存到最後一個參數,即Ç ....這真的花費我很多時間:|