【问题标题】:Matrix Multiplication using different classes - Java使用不同类的矩阵乘法 - Java
【发布时间】:2015-12-31 07:42:59
【问题描述】:

我的班级有这个作业,我必须创建一个矩阵乘法程序。条件如下:

实现两个 n × n 矩阵相乘的两种算法。假设 n 是 2 的幂:

  1. 简单的 O(n^3) 矩阵乘法算法。
  2. Strassen 的矩阵乘法算法。

评估您的不同算法,并撰写一份简短报告。为不同的 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;
    }
}

我知道我必须为乘法方法传递一个对象/矩阵,但我该怎么做呢?我的代码中还有其他问题,但我现在想专注于传递对象。

【问题讨论】:

  • 你的矩阵类应该只包含一个数组,你的多个方法应该只有一个参数。

标签: java algorithm matrix data-structures multiplication


【解决方案1】:

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

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

    double[][] arrayA;
    

    double[][] arrayB;
    

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

    private Matrix(double[][] a, double[][] b)
    {
        arrayA = a;
        arrayB = b;
    }
    

  1. 在公共构造函数中,您正在打印一个矩阵,但您没有在任何地方保存。

    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个构造函数会好很多

    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 中(具有双 [] [] 变量)。您只需要一个参数(我认为您的示例最好使用 Matrix 参数而不是 double[][] 参数并返回一个 Matrix)。

  2. 我不喜欢在创建或乘法时打印。最好创建一个打印矩阵的方法,并在您想要打印它们时调用它。

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

主要 公共类矩阵乘法 { 公共静态无效主要(字符串 [] 参数) { 随机 rand = new Random(); int 行 = rand.nextInt(7) + 2; int 列 = 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();
        }
    }

矩阵类

    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();
            }
        }
    }

【讨论】:

  • 正确,我只是关注过往对象的疑惑
  • 我认为主要是正确的问题是在矩阵类的定义中。请参阅对问题的评论。
  • @popiandro - 感谢您的快速响应!我希望方法 multiply 接收 Matrix 类。但是在这种情况下,返回值不会是 double[][] 而不是 Matrix 吗?我之前尝试过该代码,但我总是遇到类型不兼容的问题。还是我必须更改乘法方法的整个代码才能返回适当的值?我什至应该在乘法方法中返回 c 吗?
  • @Kyshi 我已经编辑了答案,以更深入地解释我在您的代码中看到的错误。看看吧
  • @popiandro 我有 2 个 double[][] 的原因是因为我假设我必须从主类传递 2 个数组(a 和 b)。我完全忘记了我必须将值存储在某个地方,只是假设程序会自动保存它。无论如何,非常感谢您的所有帮助!它帮助我更好地理解了这些概念。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 2013-08-15
相关资源
最近更新 更多