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

Java programming, make the given code below parallel so that every matrix operat

ID: 3575385 • Letter: J

Question

Java programming, make the given code below parallel so that every matrix operation uses as many threads as there are elements in a matrix.

import java.util.*;

class Matrix
{
private int row, column;
  
private double[][] matrixElements;
  
//Constructor
public Matrix(int rows, int columns)
{
this.row = rows;
this.column = columns;
  
matrixElements = new double[row][column];
  
populatematrix(-100, 100);
}
  
//Constructor
public Matrix(double [][] matrixArray)
{
this.row = matrixArray.length;
this.column = (matrixArray[0]).length;

matrixElements = new double[row][column];
  
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
matrixElements[i][j] = matrixArray[i][j];
}
}
}

private void populatematrix(int min, int max)
{
Random randnum = new Random();
Random rand = new Random();

for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
matrixElements[i][j] = rand.nextInt((max - min) + 1) + min;
}
}
}
public Matrix add(Matrix otherMatrix)
{
double[][] resultMatrixArray = new double[this.row][this.column];
  
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
resultMatrixArray[i][j] = this.matrixElements[i][j] + otherMatrix.matrixElements[i][j];
}
}

return new Matrix(resultMatrixArray);
}

public Matrix subtract(Matrix otherMatrix)
{
double[][] resultMatrixArray = new double[row][column];

for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
resultMatrixArray[i][j] = this.matrixElements[i][j] - otherMatrix.matrixElements[i][j];
}
}

return new Matrix(resultMatrixArray);
}

public Matrix dotProduct (Matrix otherMatrix)
{
double[][] resultMatrixArray = new double[row][column];
  
double sum=0;

if ( this.column != otherMatrix.row )
System.out.println(" Matrices Multiplication is not possible... Invalid Dimensions... ");
else
{
for ( int c = 0 ; c < this.row ; c++ )
{
for ( int d = 0 ; d < otherMatrix.column ; d++ )
{   
for ( int k = 0 ; k < otherMatrix.row ; k++ )
{
sum = sum + ((this.matrixElements[c][k]) * (otherMatrix.matrixElements[k][d]));
}
resultMatrixArray[c][d] = sum;
sum = 0;
}
}
}
return new Matrix(resultMatrixArray);
}
  
public String getPrintableMatrix()
{
String result = "";

for (double[] row : matrixElements)
{
for (double j : row)
{
result += " " + j + " ";
}
  
result += " ";
}
  
return result;
}
}
  

Explanation / Answer

package snippet;


import java.util.*;
class Matrix implements Runnable
{
private int row, column;
  
private double[][] matrixElements;
  
//Constructor
public Matrix(int rows, int columns)
{
this.row = rows;
this.column = columns;
  
matrixElements = new double[row][column];
  
populatematrix(-100, 100);
}
  
//Constructor
public Matrix(double [][] matrixArray)
{
this.row = matrixArray.length;
this.column = (matrixArray[0]).length;
matrixElements = new double[row][column];
  
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
matrixElements[i][j] = matrixArray[i][j];
}
}
}
private void populatematrix(int min, int max)
{
Random randnum = new Random();
Random rand = new Random();
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
matrixElements[i][j] = rand.nextInt((max - min) + 1) + min;
}
}
}
public Matrix add(Matrix otherMatrix)
{
double[][] resultMatrixArray = new double[this.row][this.column];
  
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
resultMatrixArray[i][j] = this.matrixElements[i][j] + otherMatrix.matrixElements[i][j];
}
}
return new Matrix(resultMatrixArray);
}
public Matrix subtract(Matrix otherMatrix)
{
double[][] resultMatrixArray = new double[row][column];
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
resultMatrixArray[i][j] = this.matrixElements[i][j] - otherMatrix.matrixElements[i][j];
}
}
return new Matrix(resultMatrixArray);
}
public Matrix dotProduct (Matrix otherMatrix)
{
double[][] resultMatrixArray = new double[row][column];
  
double sum=0;
if ( this.column != otherMatrix.row )
System.out.println(" Matrices Multiplication is not possible... Invalid Dimensions... ");
else
{
for ( int c = 0 ; c < this.row ; c++ )
{
for ( int d = 0 ; d < otherMatrix.column ; d++ )
{   
for ( int k = 0 ; k < otherMatrix.row ; k++ )
{
sum = sum + ((this.matrixElements[c][k]) * (otherMatrix.matrixElements[k][d]));
}
resultMatrixArray[c][d] = sum;
sum = 0;
}
}
}
return new Matrix(resultMatrixArray);
}
  
public String getPrintableMatrix()
{
String result = "";
for (double[] row : matrixElements)
{
for (double j : row)
{
result += " " + j + " ";
}
  
result += " ";
}
  
return result;
}

public static void main(String[] args) {
       // TODO Auto-generated method stub
   Matrix myRunnable = new Matrix(3,3);
Thread t = new Thread(myRunnable);
t.setName("add");
t.start();

Thread tsub = new Thread(myRunnable);
tsub.setName("sub");
tsub.start();

Thread tdot = new Thread(myRunnable);
tdot.setName("dot");
tdot.start();

  
     
   }

@Override
public void run() {
   // TODO Auto-generated method stub
   if(Thread.currentThread().getName().equals("add"))
   {
       add(new Matrix(3, 3));
      
       System.out.println(getPrintableMatrix());
   }
  
   if(Thread.currentThread().getName().equals("sub"))
   {
       subtract(new Matrix(3, 3));
       System.out.println("Sub");
      
       System.out.println(getPrintableMatrix());
   }
  
   if(Thread.currentThread().getName().equals("dot"))
   {
       dotProduct(new Matrix(3, 3));
       System.out.println("Dot");
      
       System.out.println(getPrintableMatrix());
   }
  
}
}
  

=======================================================

Sub
75.0 -72.0 26.0
-65.0 -90.0 35.0
31.0 96.0 -97.0

75.0 -72.0 26.0
-65.0 -90.0 35.0
31.0 96.0 -97.0

Dot
75.0 -72.0 26.0
-65.0 -90.0 35.0
31.0 96.0 -97.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote