Question

In: Computer Science

write a C++ Program for matrix operations which include: 1. constructor in the form-- matrix::matrix(int numRows,...

write a C++ Program for matrix operations which include:

1. constructor in the form-- matrix::matrix(int numRows, int numColumns)

2. Implement a setter method of the form: -- void matrix::setElement(int row, int col)

3 Implement a getter method of the form: -- float matrix::getElement(int row, int col)

4. Make a threaded version of the matrix/matrix addition method.

5. Make a threaded version of the matrix/scalar multiplication method.

6. Matrix/matrix addition methods of the form: -- matrix matrix::matrixAdd(matrix inMatrix) // non-threaded version

matrix matrix::matrixAdd_threaded(matrix inMatrix) // threaded version

7. Matrix/scalar multiplication methods of the form: -- matrix matrix::scalarMult(float scalar) // non-threaded version

matrix matrix::scalarMult_threaded(float scalar) // threaded version

8. The vector reduce operation method should be of the form:

float matrix::reduceVec()

The reduce operation must use parallelization with the following algorithm. Use eight threads to compute this. Begin by splitting the vector (a 1xM matrix with one row and M columns) into 8 parts. Compute the sum of those parts and store them. Then, launch eight threads to sum the sums. Do this until you have a single number remaining.

Solutions

Expert Solution

Summary :

Implemented Threaded and Nonthreaded method functions for Addition , scalar multiplication .

For threaded version , it spawns thread = number of rows and waits for them to return , which in turn iterate over that row and calculate the values.

###################### Cpp ######################################

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
//#include <bits/stdc++.h>
//#include <boost/algorithm/string.hpp>
#include "matrices.h"
#include <thread>

using namespace std;

Matrix::Matrix(int iR, int iC) {
   this->nR = iR;
   nC = iC;
   values = new int* [this->nR];

   for (int i = 0; i < this->nR; i++) {
       values[i] = new int[nC];
   }

}

void Matrix::setElement(int row, int col, float iValue) {
   values[row][col] = iValue;
}


float Matrix::getElement(int row, int col) {
   return values[row][col];
}

Matrix* Matrix::matrixAdd(Matrix& matrx) {

   Matrix* m1 = new Matrix(this->nR, this->nC);

   for (int i = 0; i < this->nR; i++) {
       for (int j = 0; j < this->nC; j++) {
           m1->setElement(i, j, (this->getElement(i, j) + matrx.getElement(i, j)));
       }
   }
   return m1;

}

void Matrix::addValues(Matrix* rM, Matrix* aM, int rVal) {

   for (int k = 0; k < nC; k++) {
       rM->setElement(rVal, k, (values[rVal][k] + aM->getElement(rVal, k)));
   }
   return;
}

Matrix* Matrix::matrixAdd_threaded(Matrix* matrx) {
   Matrix* m1 = new Matrix(this->nR, this->nC);
   vector<std::thread> th;
  
   for (int cR = 0; cR < this->nR; cR++) {
       th.push_back(std::thread([=] { addValues(m1, matrx, cR); }));
   }
   for (auto it = th.begin(); it != th.end(); it++) {
       it->join();
   }
   return m1;
}

void Matrix::Scalar_multiply(Matrix* rM, float sVal, int cR) {
   for (int k = 0; k < this->nC; k++) {
       rM->setElement(cR, k, (sVal * (values[cR][k])));
   }
   return;
}


Matrix* Matrix::scalarMult_threaded(float sVal) {
   Matrix* m1 = new Matrix(this->nR, this->nC);
   vector<std::thread> th;
   for (int cR = 0; cR < this->nR; cR++) {
       th.push_back(std::thread([=] { Scalar_multiply(m1, sVal, cR); }));
   }
   for (auto it = th.begin(); it != th.end(); it++) {
       it->join();
   }
   return m1;
}


Matrix* Matrix::scalarMult(float x)
{
   Matrix* m1 = new Matrix(this->nR, this->nC);

   for (int i = 0; i < this->nR; i++) {
       for (int j = 0; j < this->nC; j++) {
           m1->setElement(i, j, (this->getElement(i, j) * x));
       }
   }
   return m1;

}

void Matrix::free_values() {
   for (int i = 0; i < this->nR; i++)
       delete[] values[i];

   delete[] values;

}


Matrix::~Matrix() {
   free_values();

}

std::ostream& operator<<(std::ostream& output, const Matrix& M) {
   output << M.nR << "X" << M.nC << "\n";
   for (int i = 0; i < M.nR; i++) {
       for (int j = 0; j < M.nC - 1; j++) {
           output << M.values[i][j] << "-";
       }
       output << M.values[i][M.nC - 1] << "\n";
   }
   return output;
}

std::istream& operator>> (std::istream& is, Matrix& M) {
   char c;
   is >> M.nR >> c >> M.nC;

   M.values = new int* [M.nR];

   for (int i = 0; i < M.nR; i++) {
       M.values[i] = new int[M.nC];
   }


   for (int i = 0; i < M.nR; i++) {
       for (int j = 0; j < M.nC - 1; j++) {
           is >> M.values[i][j] >> c;

       }
       is >> M.values[i][M.nC - 1];

   }
   return is;
}


int main() {
   std::istringstream iss1("2X3\n3-4-3\n5-6-3\n");
   std::istringstream iss2("2X3\n6-3-4\n5-5-6\n");

   std::istream is1(iss1.rdbuf());
   std::istream is2(iss2.rdbuf());

   Matrix c1(2, 3), c2(2, 3);
   is1 >> c1; //enter 4,2,4,2
   is2 >> c2;
   std::cout << " Matrix c1 : \n" << c1;
   std::cout << " Matrix c2 : \n" << c2;

   Matrix* cadd = c1.matrixAdd(c2);
   std::cout << " Result matrix of c1 + c2 : \n" << *cadd;
   Matrix* cs = c2.scalarMult(2.0);
   std::cout << " After c2.calarMult(2) \n" << *cs;

   Matrix* cadt = c1.matrixAdd_threaded(&c2);

   std::cout << " Result matrixT of c1 + c2 : \n" << *cadt;

   Matrix* sM = cadt->scalarMult_threaded(3.0);
   std::cout << " After Scalar multiplication with 3.0 \n" << *sM;


}

############################ End #################################

########################### Header File #########################

#include <iostream>
using namespace std;

class Matrix
{
//the private data members

        private :
                int **values ;
                int nR , nC ;
        public:

                Matrix(int, int);
               void setElement(int row, int col , float iValue);
               float getElement(int row , int col);
               Matrix* matrixAdd( Matrix &matrx ) ;
               Matrix* matrixAdd_threaded( Matrix *matrx ) ;
               void Scalar_multiply( Matrix *rM, float sVal, int cR);
               Matrix* scalarMult_threaded(float sVal);
               void addValues(Matrix *rM , Matrix *aM, int rN );
               Matrix* scalarMult(float x ) ;
               float reduceVec() ;
                friend std::ostream& operator<<( std::ostream &output, const Matrix &M ) ;
                friend std::istream& operator>> (std::istream& is, Matrix& M);
                void free_values() ;
               //void operator()();
                ~Matrix();

};

###############################################################

#########################Output #############################

##################################################################


Related Solutions

write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.        There should be two public methods within the class definition namely: void setMatrixValue(int i, int j); that should set m[i][j] with user defined values int getMatrixValue(int i, int j); that should return m[i][j] Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
C++ Program: Create a 3x3 matrix of int values. Implement five functions, each expecting the matrix...
C++ Program: Create a 3x3 matrix of int values. Implement five functions, each expecting the matrix as parameter input. The first function must use a nested loop to prompt a user to enter each value. Show the indices when prompting for input and populate the matrix with these values. The second function outputs the matrix and formats the output to align all columns and rows to accommodate values up to 1000. The third function finds and returns the minimum value...
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include the following attributes with appropriate types: i. Title ii. Director iii. Release Year iv. Rating (“G”, “PG”, “PG-13”, etc) - Write code that instantiates a movie object using value semantics as text: - Write code that instantiates a movie object using reference semantics: - Write the print_movie method code: - Write Constructor code: - Write Entire Movie class declaration
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT