2013-04-02 2 views
0

저는 자바에서 행렬 연산에 Jama를 사용했지만 희소 행렬이 없기 때문에 Parallel Cold Library (PColt)을 사용하기 시작합니다. Colt의 다중 스레드 버전입니다. 나는 두 개의 사각형 행렬, AxB (행렬 - 행렬 곱셈, 요소 적 (또는 스칼라) 곱셈이 아님), 크기 (NxN)를 곱하려고 시도했다. PColt에서 행렬 - 행렬 곱셈에 대해 제공된 메소드를 찾을 수 없었다. t는 요소 단위의 곱셈을 원함), 다음과 같이 메소드를 코딩했습니다. 두 개의 행렬 N = 1000에 대해 곱하면 거의 5 분이 걸립니다. pcolt에서 두 개의 행렬을 곱하는 방법을 아는 분이라면 정말 감사 할 것입니다. 코드에 불필요하고 복잡도가 높은 버그가있는 경우 알려주십시오.자바 행렬 연산, 병렬 콜트 행렬 - 행렬 곱셈

/** 
* 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 Dokie, 나는 해결책을 가지고 다음과 같이 나의 방법이다. 실제로 위의 코드를 잊어 버리십시오. PColt는 행렬 행렬 곱셈을 제공하지만 메서드 이름이 혼란 스럽습니다.

어쨌든 두 행렬은 다음 방법을 사용하기 위해 muliply :

공개 DoubleMatrix2D zMult (DoubleMatrix2D B, C DoubleMatrix2D) 선형 대수 행렬 행렬 곱셈; C = A × B; (거짓 거짓 B, C, 1,0) A.zMult 상당

HERE

결과가 즉 C, LAST 파라미터에 저장되기 때문에, 파라미터 순서 조심 .... 그것은 정말 나에게 많은 시간을 낭비했다.

답변

2

매트릭스 승법은 DoubleAlgebra 클래스입니다.

DenseDoubleAlgebra algebra = new DenseDoubleAlgebra(); 
DoubleMatrix2D productMatrix = algebra.mult(aMatrix, anotherMatrix);