Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 3. Write a method to multiply two matrices. The header of the method is

ID: 3889089 • Letter: E

Question

Exercise 3. Write a method to multiply two matrices. The header of the method is as follows public static double[ multiplyMatrix (double a, double b) Assume that the two input matrices are square (that is, they are n X n matrices) To multiply matrix a by matrix b, where c is the result matrix, use the formula a11 a12 a13 a21 a22 a23 a31 a32 a33 b11 b12 b13 b21 b22 b23 b31 b32 b33 c11 c12 c13 C21 c22 c23 c31 c32 c33 where cij = ailXb1j + ai2Xb21 + a13Xb31 Since we are only interested in the execution time, you may assume that all the elements of matrices a and b are identical. A sample screen dialog and output is given below Enter the size of each matrix: 100 Enter the matrix element (all elements of the matrices are assumed to be the same) 3. Execution time: 16 millisecs The template of the program with the main method is given below. It includes the code segment to get the execution time. Fill in the method to compute the product of the two matrices //Multiplication of two square matrices of size nX n each import java.util.Scanner; public class MatrixMult [ /*Main method/ public static void main (String[] args) Scanner keyboard- new Scanner (System.in); int n;

Explanation / Answer

import java.util.Scanner;
import java.io.*;

public class MatrixMult
{
public static void main(String args[])
{
  Scanner kb = new Scanner (System.in);
  int n;
  double num;
  System.out.print("Enter the size of each matrix : ");
  n=kb.nextInt();
  System.out.println("Enter the matrix elements ");
  System.out.print("All the elements of the matricies are assumed to be the same");
  num=kb.nextDouble();
  double[][] matrix1 = new double[n][n];
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)
    matrix1[i][j]=num;
  double[][] matrix2 = new double[n][n];
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)
    matrix2 [i][j]=num;
  long startTime,endTime,executionTime;
  startTime = System.currentTimeMillis();
  double [][] resultMatrix = new double [n][n];
  resultMatrix = multiplyMatrix(matrix1, matrix2);
  endTime = System.currentTimeMillis();
  executionTime=endTime - startTime;
  System.out.println("Execution Time :"+executionTime + " millisecs");
}
public static double [][] multiplyMatrix(double [][] matrix1, double [][] matrix2)
{
  int i,j,k;
  int n=matrix1[0].length;
  double result[][]=new double[n][n];
  for(i=0;i<n;i++)
   for(j=0;j<n;j++)
   {
    result[i][j]=0;
    for(k=0;k<n;k++)
     result[i][j]+=matrix1[i][k]*matrix2[k][j];
   }
  return result;
}
}

/*
I was executed on the environment Intel core i5 5th generation process
with 12 gb RAM.

Of course, with 4 GB RAM, we may get run time exception "java.lang.outofmemoryerror: java heap space"
Try to configure your JVM to use more memory as follows

export JVM_ARGS="-Xms750m -Xmx2048m -XX:MaxPermSize=1024m"
-Xmx<size>        ---Set maximum Java heap size
-Xms<size>        ---Set minimum Java heap size
-XX:MaxPermSize   --- Permanent generation of the heap space.

Permanent generation of the heap is used to store String pool and various Metadata required by JVM related to Class, method and other java primitives.

Since in most of JVM default size of Perm Space is around "64MB" you can easily run out of memory if you have too many classes or a huge number of Strings in your project.

An important point to remember is that it doesn't depend on –Xmx value so no matter how big your total heap size you can run OutOfMemory in perm space. The good thing is you can specify the size of permanent generation using JVM options "-XX: PermSize" and "-XX: MaxPermSize" based on your project need.

output:
Sizeof Matrix(n) | Execution time(millsecs)
---------------- | ------------------------
       100       |         19 millisecs
    200       |        205 millisecs
    300       |        543 millisecs
    400       |       1591 millisecs
    500       |       3101 millisecs
    600       |       6005 millisecs
    700       |      10199 millisecs
       800       |      15574 millisecs
       900       |      22948 millisecs
      1000       |      33557 millisecs
      1500       |     147006 millisecs
      2000       |     395524 millisecs
      3000       |    1094493 millisecs
---------------------------------------------
I am also attaching execution log


F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 100
Enter the matrix elements
All the elements of the matricies are assumed to be the same2
Execution Time :19 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 200
Enter the matrix elements
All the elements of the matricies are assumed to be the same2
Execution Time :205 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 300
Enter the matrix elements
All the elements of the matricies are assumed to be the same3
Execution Time :543 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 400
Enter the matrix elements
All the elements of the matricies are assumed to be the same4
Execution Time :1591 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 500
Enter the matrix elements
All the elements of the matricies are assumed to be the same3
Execution Time :3101 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 600
Enter the matrix elements
All the elements of the matricies are assumed to be the same2
Execution Time :6005 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 700
Enter the matrix elements
All the elements of the matricies are assumed to be the same2
Execution Time :10199 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 800
Enter the matrix elements
All the elements of the matricies are assumed to be the same3
Execution Time :15574 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 900
Enter the matrix elements
All the elements of the matricies are assumed to be the same3
Execution Time :22948 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 1000
Enter the matrix elements
All the elements of the matricies are assumed to be the same2
Execution Time :33557 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 1500
Enter the matrix elements
All the elements of the matricies are assumed to be the same2
Execution Time :147006 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 2000
Enter the matrix elements
All the elements of the matricies are assumed to be the same1
Execution Time :395524 millisecs

F:personalPersonalDataChegg-Sep-2017>java MatrixMult
Enter the size of each matrix : 3000
Enter the matrix elements
All the elements of the matricies are assumed to be the same1
Execution Time :1094493 millisecs
*/