简体   繁体   中英

Matrix Multiplication using different classes - Java

I have this assignment for my class where I have to create a Matrix Multiplication program. Here's the condition:

Implement two types of algorithms for multiplying two n × n matrices. Assume n is a power of 2:

  1. The straight-forward O(n^3) matrix multiplication algorithm.
  2. Strassen's matrix multiplication algorithm.

Evaluate your different algorithms, and write a short report. Create test matrices for different values of n (4, 10, 20,100). Generate matrices using random numbers. Compute the running time of your algorithms. Your report should include the running times and conclusions.

Here's my code so far:

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

    }
}

And here's my other class:

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

I know I have to pass an object/Matrix for the multiply method, but how would I do that? There are other concerns in my code, but I want to focus on passing objects right now.

Let's take a deep look to your code:

  1. Why do you have two double[][] inside the Matrix class? A Matrix is just one bidimensional array. You should delete the arrayB

     double[][] arrayA; 

     double[][] arrayB; 

  2. What's the point of the private constructor? For you, it is useless right now.

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

  1. In the public constructor, you are printing a Matrix, but you are not saving anywhere.

     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; 

     } 

Anyway, I think it would be much better to make 2 constructors

    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. Why your multiply method have 2 parameters? As it is inside the class Matrix (that have a double[][]variable). You only need a parameter (I think it is better to your example to have a Matrix parameter instead of a double[][]parameter and return also a Matrix).

  2. I don't like printing when you are creating or multiplying. It's much better to create a method to print the Matrix, and calling it when you want to print them.

So....the final code would be something like this:

public class MatrixMultiplication { public static void main(String[] args) { Random rand = new Random(); 公共类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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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