使用不同类的矩阵乘法 - Java

时间:2015-10-03 02:28:21

标签: java algorithm matrix data-structures multiplication

我的课程中有这个作业,我必须创建一个Matrix Multiplication程序。这是条件:

实现两种类型的算法,用于乘以两个n×n矩阵。假设n是2的幂:

  1. 直接O(n ^ 3)矩阵乘法算法。
  2. Strassen的矩阵乘法算法。
  3. 评估您的不同算法,并撰写简短报告。为不同的n(4,10,20,100)值创建测试矩阵。使用随机数生成矩阵。计算算法的运行时间。您的报告应包括运行时间和结论。

    到目前为止,这是我的代码:

    public class MatrixMultiplication 
    {
        public static void main(String[] args) 
        {
           Random rand = new Random();
           int rows = rand.nextInt(7) + 2;
           int columns = rand.nextInt(7) + 2;
    
           System.out.println("The matrix has " + rows + " randomized rows");
           System.out.println("The matrix has " + columns + " randomized column");
    
           System.out.println();
    
           double[][] a = new double[rows][columns];
           double[][] b = new double[columns][rows];
    
           System.out.println("The first matrix has the values: ");
           Matrix m1 = new Matrix(a);
    
           System.out.println("---------------------------------");
           System.out.println("The second matrix has the values: ");
           Matrix m2 = new Matrix(b);
    
           System.out.println();
    
           Matrix productRegular = m1.multiply(m2);
    
        }
    }
    

    这是我的另一堂课:

    import java.util.Random;
    
    class Matrix 
    { 
        double[][] arrayA;
        double[][] arrayB;
    
        private Matrix(double[][] a, double[][] b)
        {
            arrayA = a;
            arrayB = b;
        }
    
        public Matrix(double[][] array) //Create matrix values
        {
            Random rand = new Random();
    
            for(int i = 0; i < array.length; i++)
            {
                for(int j = 0; j < array[i].length; j++)
                {
                    array[i][j] = rand.nextInt(10);
                    System.out.print(array[i][j] + " | ");
                }
                System.out.println();
            }
        }
    
    
    
        public double multiply(double[][] a, double[][] b)
        {
            double[][] c = new double[a.length][b[0].length];
    
            System.out.println("Product of A and B is");
            for(int i = 0; i < a.length; i++)
            {
                for(int j = 0; j < b[0].length; j++)
                {
                    for(int k = 0; k < a[0].length; k++)
                    {
                        c[i][j] += a[i][k] * b[k][j];
                        System.out.println(c[i][j] + " | ");
                    }
                }
                System.out.println();
            }
    
            return c;
        }
    }
    

    我知道我必须为multiply方法传递一个object / Matrix,但是我该怎么办呢?我的代码还有其他问题,但我现在想专注于传递对象。

1 个答案:

答案 0 :(得分:2)

让我们深入了解您的代码:

  1. 为什么Matrix类中有两个double [] []?矩阵只是一个二维数组。你应该删除arrayB

    double[][] arrayA;
    

    double[][] arrayB;
    

  2. 私有构造函数有什么意义?对你来说,现在没用。

    private Matrix(double[][] a, double[][] b)
    {
        arrayA = a;
        arrayB = b;
    }
    
    1. 在公共构造函数中,您正在打印Matrix,但您没有在任何地方保存。

      public Matrix(double[][] array) //Create matrix values
      {
          Random rand = new Random();
      
          for(int i = 0; i < array.length; i++)
          {
              for(int j = 0; j < array[i].length; j++)
              {
                  array[i][j] = rand.nextInt(10);
                  System.out.print(array[i][j] + " | ");
              }
              System.out.println();
          }
      

          arrayA = array;
      

      }
      
    2. 无论如何,我认为制作2个构造函数会好得多。

          public Matrix(double[][] array) //you just pass an array created outside the class
          {
              arrayA = array;
          }
      
          public Matrix(int rows, int columns) //Create matrix values
          {
              double[][] array = new double [rows][columns];
              Random rand = new Random();
      
              for(int i = 0; i < array.length; i++)
              {
                  for(int j = 0; j < array[i].length; j++)
                  {
                      array[i][j] = rand.nextInt(10);
                      System.out.print(array[i][j] + " | ");
                  }
                  System.out.println();
              }
              arrayA = array;
          }
      
      1. 为什么你的乘法方法有2个参数?因为它在Matrix类中(具有double [] []变量)。你只需要一个参数(我认为你的例子最好有一个Matrix参数而不是double [] []参数,并返回一个矩阵)。

      2. 我不喜欢在创建或倍增时进行打印。最好创建一个打印Matrix的方法,并在打印时调用它。

      3. 所以....最终的代码将是这样的:

        主要的         公共类MatrixMultiplication         {             public static void main(String [] args)             {                随机rand = new Random();                int rows = rand.nextInt(7)+ 2;                int columns = rand.nextInt(7)+ 2;

                   System.out.println("The matrix has " + rows + " randomized rows");
                   System.out.println("The matrix has " + columns + " randomized column");
        
                   System.out.println();
        
                   System.out.println("The first matrix has the values: ");
                   Matrix m1 = new Matrix(rows,columns);
                   m1.print();
                   System.out.println("---------------------------------");
                   System.out.println("The second matrix has the values: ");
                   Matrix m2 = new Matrix(columns, rows);
        
                   m2.print();
                   System.out.println();
                   System.out.println("Product of A and B is");
                   Matrix productRegular = m1.multiply(m2);
                   productRegular.print();
                }
            }
        

        Matrix Class

            import java.util.Random;
        
            class Matrix 
            { 
                double[][] arrayA;
        
                public Matrix(double[][] array) //Create matrix values
                {
                    arrayA=array;
                }
        
                public Matrix(int rows, int columns) //Create matrix values
                {
                    double[][]array= new double[rows][columns];
                    Random rand = new Random();
        
                    for(int i = 0; i < array.length; i++)
                    {
                        for(int j = 0; j < array[i].length; j++)
                        {
                            array[i][j] = rand.nextInt(10);
                        }
                    }
                    arrayA=array;
                }
        
                public Matrix multiply(Matrix m)
                {
                    double[][]b=m.arrayA;
                    double[][] c = new double[arrayA.length][b[0].length];
        
                    for(int i = 0; i < arrayA.length; i++)
                    {
                        for(int j = 0; j < b[0].length; j++)
                        {
                            for(int k = 0; k < arrayA[0].length; k++)
                            {
                                c[i][j] += arrayA[i][k] * b[k][j];
                            }
                        }
                    }
        
                    return new Matrix(c);
                }
        
        
                public void print(){
                    for(int i=0;i<arrayA.length;i++){
                        for(int j=0;j<arrayA[0].length;j++){
                            System.out.print(arrayA[i][j] + " | ");
                        }
                        System.out.println();
                    }
                }
            }