简体   繁体   English

不使用导入的Java中的矩阵乘法

[英]Matrix Multiplication in Java Without Using Imports

I'm trying to make a method that reads input from 2 files, each containing a matrix. 我试图做一个方法,可以从2个文件中读取输入,每个文件都包含一个矩阵。 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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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