简体   繁体   English

Java矩阵的加法和乘法

[英]Adding and multiplying Matrices Java

I am currently working on trying to add and multiply two matrices together and print the results. 我目前正在尝试将两个矩阵相加并相乘并打印结果。 I had done the multiplication right or at least I am certain however I cannot test the results as I do not know how to print it. 我已经完成了乘法运算,或者至少可以确定,但是由于我不知道如何打印,所以无法测试结果。 Everything seemed fine and I just needed to figure out how to print results but as soon as I placed in the addition of the matrices I ran into multiple issues. 一切似乎都很好,我只需要弄清楚如何打印结果,但是一旦添加矩阵,我就遇到了很多问题。 Here is the blank code I was given beforehand to finish in with what was necessary to add, multiply and print the matrices: 这是我事先获得的空白代码,以完成添加,乘法和打印矩阵所需的操作:

public class Matrix {

public static void matrix_add(double[][] a, double[][] b) {
// add matrices

}

public static void matrix_multi(double[][] a, double[][] b) {
// multiply matrices

}

public static void main(String[] args) {

 double ar1[][] =
        {{.7,.2,.1},
        {.3, .6, .1},
        {.5, .1, .4}};

 double ar2[][] =
        {{.2, .3, .5},
         {.1, .2, .1},
         {.1, .3, .4}};

matrix_add(ar1, ar2);
System.out.println();
matrix_multi(ar1, ar2);

 }

}

Here are my results after of which I am certain my calculations are correct for addition and multiplying the matrices: 这是我的结果,在此之后我可以确定我的计算对于矩阵的加法和乘法是正确的:

public class operationson2Darrays {

public static void matrix_add(double[][] ar1, double[][] ar2) {
    // add matrices
    double[][] ar4 = new  double[ar1.length][ar1[0].length];
    for(int i=0;i<ar1.length;i++){
        for(int j=0;j<ar1[0].length;j++){
            ar4[i][j] = ar1[i][j] + ar2[i][j];
        }
    }


public static void matrix_multi(double[][] ar1, double[][] ar2) {
    // multiply matrices
    int i, j, k;
    int row1 = ar1.length;
    int col1 = ar1[0].length;
    int row2 = ar2.length;
    int col2 = ar2[0].length;
    int[][] ar3 = new int[row1][col2];
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            for (k = 0; k < col1; k++) {
                ar3[i][j] += ar1[i][k] * ar2[k][j];
            }
        }
    }
}

public static void main(String[] args) {

    double ar1[][] =
            {{.7, .2, .1},
                    {.3, .6, .1},
                    {.5, .1, .4}};

    double ar2[][] =
            {{.2, .3, .5},
                    {.1, .2, .1},
                    {.1, .3, .4}};

    matrix_add(ar1, ar2);
    System.out.println();
    matrix_multi(ar1, ar2);


 }

}
}

I am currently running into lots of issues the first being that ar1 and ar2 are already being defined in scope. 我目前遇到很多问题,首先是ar1和ar2已经在作用域中定义了。 I understand what that means but I do not have the slightest clue how to fix it. 我理解这意味着什么,但我丝毫不知道如何解决。 It is also expecting tokens in this line : public static void main(String[] args) {... ?I am confused o what it is supposed to be excepting and lastly it is saying that this line is expecting a method call?: matrix_multi(ar1, ar2); 它在这行中也需要标记:public static void main(String [] args){...?我很困惑,应该排除什么,最后说这行在期待方法调用?: matrix_multi(ar1,ar2);

I am starting to get very confused and assuming my calculations are correct for each section if I remove the adding matrices all of a sudden all of the issues disappear. 我开始感到非常困惑,并假设如果我删除添加的矩阵,我的计算对于每个部分都是正确的,那么所有问题突然都消失了。 I would appreciate any help on these errors that I am receiving and how I can go about fixing this and also how I would be able to print the results of the matrices. 我将对收到的这些错误以及如何解决此问题以及如何打印矩阵结果提供任何帮助,我们将不胜感激。

You should have your methods return the new 2D array so you can do something with the array later, like print it for example. 您应该让您的方法返回新的2D数组,以便以后可以对该数组进行操作,例如打印它。 Also you want to have your multiply 2D array be a double[][] instead of an int[][] since you are multiplying two double[][]'s. 另外,由于要将两个double [] []相乘,因此您还希望将2D数组作为double [] []而不是int [] []。 Finally you can take the newly created arrays and pass them into a method that prints the contents of your arrays. 最后,您可以采用新创建的数组,并将它们传递到打印数组内容的方法中。

public static double[][] matrix_add(double[][] ar1, double[][] ar2) {
    // add matrices
    double[][] ar4 = new  double[ar1.length][ar1[0].length];
    for(int i=0;i<ar1.length;i++){
        for(int j=0;j<ar1[0].length;j++){
            ar4[i][j] = ar1[i][j] + ar2[i][j];
        }
    }

    return ar4;
}


public static double[][] matrix_multi(double[][] ar1, double[][] ar2) {
    // multiply matrices
    int i, j, k;
    int row1 = ar1.length;
    int col1 = ar1[0].length;
    int row2 = ar2.length;
    int col2 = ar2[0].length;
    double[][] ar3 = new double[row1][col2];
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            for (k = 0; k < col1; k++) {
                ar3[i][j] += ar1[i][k] * ar2[k][j];
            }
        }
    }

    return ar3;
}

public static void print2DArray(double[][] ar)
{
    for(int i=0;i<ar.length;i++){
        for(int j=0;j<ar[0].length;j++){
            System.out.print(ar[i][j] + " ");
        }
        System.out.println();
    }
}

public static void main(String[] args) {

    double ar1[][] =
            {{.7, .2, .1},
                    {.3, .6, .1},
                    {.5, .1, .4}};

    double ar2[][] =
            {{.2, .3, .5},
                    {.1, .2, .1},
                    {.1, .3, .4}};

    double[][] arAdd = matrix_add(ar1, ar2);
    print2DArray(arAdd);
    System.out.println();
    double[][] arMulti = matrix_multi(ar1, ar2);
    print2DArray(arMulti);

 }

Output 输出量

0.8999999999999999 0.5 0.6 
0.4 0.8 0.2 
0.6 0.4 0.8 

0.16999999999999998 0.28 0.41000000000000003 
0.13 0.24 0.25 
0.15000000000000002 0.29 0.42000000000000004 

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

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