简体   繁体   中英

Matrix Multiplication in Java Without Using Imports

I'm trying to make a method that reads input from 2 files, each containing a matrix. The objective is to check if they can be multiplied (if the length of the rows of one is the same as the length of the columns in the other) then create a product matrix of the two inputted matrices. Here is what I have so far. It doesn't work, and I wasn't expecting it to. I just want to get some help on the logic part of the method.

public class MatrixOps {

    public static double[][] multiply(double[][] matrix1, double[][] matrix2) {
        int matrix1Cols = matrix1.length;
        int matrix1Rows = matrix1[0].length;
        int matrix2Cols = matrix2.length;
        int matrix2Rows = matrix2[0].length;
        double[][] productMatrix = new double[0][0];

        if (matrix1Rows == matrix2Cols) {
            productMatrix = new double[matrix1Cols][matrix2Rows];
            for (int i = 0; i < matrix1Cols; i++) {
                for (int j = 0; j < matrix1Rows; j++) {
                    productMatrix[i][j] += matrix1[i][j] * matrix2[j][i];
                    for (int k = 0; k < matrix2Rows; k++) {
                        productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
                    }
                }
            }
        }

        if (matrix1Cols == matrix2Rows) {
            productMatrix = new double[matrix2Cols][matrix1Rows];
            for (int i = 0; i < matrix2Rows; i++) {
                for (int j = 0; j < matrix1Cols; j++) {
                    productMatrix[i][j] += matrix1[i][j] * matrix2[j][i];
                    for (int k = 0; k < matrix2Rows; k++) {
                        productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
                    }
                }
            }
        }
        return productMatrix;
    }

}

Any ideas on how to get it to work?

First of all, not necessary to do product matrix on both loops:

 for (int i = 0; i < matrix1Cols; i++) {
     for (int j = 0; j < matrix1Rows; j++) {
         productMatrix[i][j] += matrix1[i][j] * matrix2[j][i];
     for (int k = 0; k < matrix2Rows; k++) {
         productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
     }
 }

This is sufficient:

for (int i = 0; i < matrix1Cols; i++) {
    for (int j = 0; j < matrix1Rows; j++) {
        for (int k = 0; k < matrix2Rows; k++) {
            productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
       }
   }
}

Also if you are changing the order of the matrix multiplications, then it must be reflected on your operation (seems you are just doing copy paste)

so on the second part:

if (matrix1Cols == matrix2Rows) {

The matrix multiplication has to change from:

productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];

to:

productMatrix[i][j] += matrix2[i][j] * matrix1[j][k];

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