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

标签 java algorithm matrix data-structures multiplication

我的类(class)有这个作业,我必须在其中创建一个矩阵乘法程序。这是条件:

实现两种类型的算法以将两个 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;
    }
}

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

最佳答案

让我们深入研究一下您的代码:

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

    double[][] arrayA;
    

    double[][] arrayB;
    

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

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

  1. 在公共(public)构造函数中,您正在打印一个 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个constructor会好很多

    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. 为什么你的 multiply 方法有 2 个参数?因为它在 Matrix 类中(有一个 double[][] 变量)。您只需要一个参数(我认为您的示例最好使用 Matrix 参数而不是 double[][] 参数并返回一个 Matrix)。

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

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

主要 公共(public)类矩阵乘法 { public static void main(String[] args) { 随机数 = new Random(); int 行 = 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();
        }
    }

矩阵类

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

关于java - 使用不同类的矩阵乘法 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32918308/

相关文章:

java - Magnolia Blossom 模块找不到 .jsp 模板

java - 将 "duration" "Unit"转换为毫秒

algorithm - 如何在jsprit中添加时间限制?

Python-什么词可以删除最多的连续字母并且仍然是字典有效词?

c - 如何检查方阵主对角线的元素是否相同?

java - 调用 notifyDataSetChanged() 后丢失 EditText 值;

java - 如何在具有接口(interface)返回类型的方法中实现错误处理

algorithm - 找到由相邻网格点组成的多边形的所有内部网格点

r - 从 lmer 对象 (lme4, R) 中提取随机效应的原始模型矩阵

c - 矩阵生成器 - C 中的段错误