algorithm - Matrix Multiplication using different classes - Java -


i have assignment class have create matrix multiplication program. here's condition:

implement 2 types of algorithms multiplying 2 n × n matrices. assume n power of 2:

  1. the straight-forward o(n^3) matrix multiplication algorithm.
  2. strassen’s matrix multiplication algorithm.

evaluate different algorithms, , write short report. create test matrices different values of n (4, 10, 20,100). generate matrices using random numbers. compute running time of algorithms. report should include running times , conclusions.

here's code 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[][] = new double[rows][columns];        double[][] b = new double[columns][rows];         system.out.println("the first matrix has values: ");        matrix m1 = new matrix(a);         system.out.println("---------------------------------");        system.out.println("the second matrix has values: ");        matrix m2 = new matrix(b);         system.out.println();         matrix productregular = m1.multiply(m2);      } } 

and here's 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 = 0; < 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 , b is");         for(int = 0; < 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 have pass object/matrix multiply method, how that? there other concerns in code, want focus on passing objects right now.

let's take deep code:

  1. why have 2 double[][] inside matrix class? matrix 1 bidimensional array. should delete arrayb

    double[][] arraya; 

    double[][] arrayb; 

  2. what's point of private constructor? you, useless right now.

    private matrix(double[][] a, double[][] b) {     arraya = a;     arrayb = b; } 

  1. in public constructor, printing matrix, not saving anywhere.

    public matrix(double[][] array) //create matrix values {     random rand = new random();      for(int = 0; < 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, think better make 2 constructors

    public matrix(double[][] array) //you pass array created outside class     {         arraya = array;     }      public matrix(int rows, int columns) //create matrix values     {         double[][] array = new double [rows][columns];         random rand = new random();          for(int = 0; < 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 multiply method have 2 parameters? inside class matrix (that have double[][]variable). need parameter (i think better example have matrix parameter instead of double[][]parameter , return matrix).

  2. i don't printing when creating or multiplying. it's better create method print matrix, , calling when want print them.

so....the final code this:

main 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();             system.out.println("the first matrix has values: ");            matrix m1 = new matrix(rows,columns);            m1.print();            system.out.println("---------------------------------");            system.out.println("the second matrix has values: ");            matrix m2 = new matrix(columns, rows);             m2.print();            system.out.println();            system.out.println("product of , 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 = 0; < 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 = 0; < 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();             }         }     } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -