简体   繁体   English

Java中用于矩阵乘法的数学运算符

[英]Math operators in Java for Matrix multiplication

Hi I am fairly new to Java coding so please excuse for any silly errors or questions. 嗨,我是Java编码的新手,所以请为任何愚蠢的错误或问题辩解。 I got this code from some internet source which multiplies two matrices and gives the resultant one in Java. 我是从某个互联网来源获得的代码,该代码将两个矩阵相乘,然后用Java给出结果。 I have edited it for my own use. 我已对其进行了编辑以供自己使用。 The code is as follow: 代码如下:

 for ( c = 0 ; c < 3 ; c++ )
         {
            for ( d = 0 ; d < 1 ; d++ )
            {   
               for ( int k = 0 ; k < 3 ; k++ )
               {
                  Math.sum = sum + transformation[c][k]*sub[k][d];
               }

               multiply[c][d] = sum;
               sum = 0;
            }
         }

         System.out.println("Product of entered matrices:-");

         for ( c = 0 ; c < 3 ; c++ )
         {
            for ( d = 0 ; d < 1 ; d++ )
               System.out.print(multiply[c][d]+"\t");

            System.out.print("\n");
         }

So now im getting red lines under sum and multiply saying 'sum cannot be resolved or is not a field' and 'multiply cannot be resolved to a variable'. 因此,现在我在总和下获得红线并乘以说“总和无法解析或不是字段”和“乘以无法解析为变量”。 Can anyone please explain the reason for the error and how it can be resolved. 谁能解释这个错误的原因以及如何解决。 Thanks 谢谢

public class Multiply { 公共课程乘以{

public static void main(String[] args) {
    int rows=3, columns=3;
    double multiply[][] = new double[rows][columns]; // product of transformation X sub
    double matA[][] = { { 2, 3, 6 }, { 1, 4, 6 }, { 4, 1, 3 } },
       matB[][] =  { { 2, 1, 0 }, { 3, 5, 1 }, { 3, 2, 1 } }, 
       sum;

    for (int k = 0; k < columns; k++) {
        for (int c = 0; c < rows; c++) {
            sum = 0;
            for (int d = 0; d < columns; d++) {
                sum = sum + matA[c][d] * matB[d][k];
            }
            multiply[c][k] = sum;
        }
    }
    System.out.println("Product of Matrix A & B matrices:-");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++)
            System.out.print(multiply[i][j] + "\t");
        System.out.print("\n");
    }
}

} }

You need this: 你需要这个:

    int multiply[][] = new int[somesize][somesize] ;
    for ( c = 0 ; c < 3 ; c++ )
    {
           for ( d = 0 ; d < 1 ; d++ )
           {   int sum = 0; // Creating local variable 'sum'
                  for ( int k = 0 ; k < 3 ; k++ )
                  {
                        sum = sum + transformation[c][k]*sub[k][d];
                  }

                  multiply[c][d] = sum;

            }


     }

I really is only that your taking the Math library sum method and setting it to a value; 我真的只是您采用数学库的sum方法并将其设置为一个值。 The method is supposed to return a value to you Sum = Math.sum(5,4) 该方法应该向您返回一个值Sum = Math.sum(5,4)

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

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