简体   繁体   中英

Java matrix operations, parallel colt matrix - matrix multiplication

I was using Jama for matrix operation in java but since they don't have sparse matrix then I start to use Parallel Cold Library (PColt) . It is a multithreaded version of Colt. I tried to multiply two square matrices,AxB (it is matrix-matrix multiplication, Not elementwise(or scalar) multiplication), size (NxN).I couldn't find a provided method for matrix-matrix multiplication in PColt (I don't want elementwise multiplication), so that I coded the method as following. When I multiply for N=1000 two matrices it tooks almost 5 minutes to complete. If anyone knows how to multiply two matrices in pcolt, it will be really grateful. Or if you see there is any bug in the code which is unnecessary and makes complexity high, please inform me. My method is as follows:

    /**
 * 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;
}

THE SOLUTION...

Okie Dokie, I got the solution. Actually forget the above code. PColt provides matrix-matrix multiplication, but the method name is confusing. In order to muliply two matrices use the following method:

public DoubleMatrix2D zMult(DoubleMatrix2D B,
                            DoubleMatrix2D C)

Linear algebraic matrix-matrix multiplication; C = A x B;

Here, be careful about the order of parameters, as the result is saved to the last parameter C . C can also be set to null, which causes a new matrix to be returned.

The matrix multiplication method is in the DoubleAlgebra class.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM