In: Computer Science
I need implement the operation bellow. Please add comment on some lines to compare with my code. The program will implement the following operations on the Matrix class: 1. Matrix addition 2. Matrix subtraction 3. Matrix multiplication 4. Matrix scalar multiplication 5. Matrix transpose Vector class will implement the following operations: 1. Vector length 2. Vector inner (dot product) 3. Vector angle Vector 3D Class The class Vector3D is derived from the Vector class, and it implements a vector in a three dimensional space. In this case, any instance of the Vector3D will have three dimensions, and hence is a 3 x 1 matrix. In the Vector3D class, you will implement the cross product operator, and you will have two versions of it. The first version will be a member function that computes the cross product between two vectors v1 and v2, and assigns the result to v1. The second version will be a nonmember function that computes the cross product between v1 and v2, and then assigns the result to a new vector. Distribution Files Your implementation will consist of adding C++ code to implement three modules: Matrix.cpp, Vector.cpp, and Vector3D.cpp. You will receive all the .h files with declaration of the Matrix, Vector and Vector3D classes. In addition, you will be provided with a main program that uses the Matrix, Vector, and Vector3D classes, and interacts with the user to ask his/her input on the operations to be performed. Finally, you will be given a Makefile with all the commands needed to compile and submit your project. 1. Object.h – declaration of the Object type that will be stored in the matrix or vector. 2. Matrix.h – declaration of the Matrix class. 3. Matrix.cpp – implementation of the Matrix class. YOU MUST IMPLEMENT THE METHODS TO APPEAR IN THIS FILE. 4. Vector.h – declaration of the Vector class. 5. Vector.cpp – implementation of the Vector class. YOU MUST IMPLEMENT THE METHODS TO APPEAR IN THIS FILE. 6. Vector3D.h – declaration of the Vector3D class. 7. Vector3D.cpp – implementation of the Vector3D class. YOU MUST IMPLEMENT THE METHODS TO APPEAR IN THIS FILE. 8. matrix_main.cpp – main program to the Matrix, Vector and Vector3D classes. DO NOT MODIFY THIS FILE!!!! 9. Makefile – file with the commands to compile and submit you project. 10. test1.in – test input file 1. NOTE: YOU PROGRAM MUST PASS THIS FILE WITHOUT ERRORS IN ORDER TO BE CONSIDERED A RUNNING PROGRAM. 11. test1.out – expected output from test input file 1. 12. test2.in – test input file 2. 13. test2.out – expected output from test input file 2. 14. test3.in – test input file 3. 15. test3.out – expected output file from test input file 3. 16. prof_matrix_main – professor’s version of the matrix_main program. NOTE: Known to be working correctly. Matrix.cpp / #include "Matrix.h" namespace Mat_Vectors { /////////////////// DO NOT MODIFY THESE TWO FUNCTIONS///////////// // Output operator (Friend function) ostream& operator<<(ostream &out, const Matrix& M) { int i = 0, j = 0, len1 = 0, len2 = 0; len1 = M.get_rows(); len2 = M.get_cols(); for (i = 0; i < len1; ++i){ for (j = 0; j < len2; ++j){ out << M.get_value(i,j); if (j != len2 - 1){ out << " "; } } out << "\n"; } return out; } // Input operator (Friend function) istream& operator>>(istream &in, Matrix& M){ int i = 0, j = 0, rows = 0, cols = 0; int new_val; in >> rows; in >> cols; M = Matrix(rows, cols); for (i = 0; i < rows; ++i){ for (j = 0; j < cols; ++j){ in >> new_val; M.set_value(i,j,new_val); } } return in; } /////////////////////////////////////////////////////////////// // ADD YOUR CODE HERE } -------------------------------------------------------------------------------------------------------------- Matrix.h #ifndef _MATRIX_H #define _MATRIX_H #include <iostream> #include <cassert> #include <cstdio> #include "Object.h" using namespace std; using namespace Mat_Vectors; namespace Mat_Vectors{ class Matrix { public: // Various constants related to matrix size static const int MAX_ROWS = 100; static const int MAX_COLUMNS = 100; // Define size type for matrix typedef std::size_t size_type; // Define value type for matrix typedef Object value_type; // Public methods for the matrix class // Constructor Matrix (size_type rows = 1, size_type cols = 2); // Accessor methods // Returns the element [i,j] value_type get_value(size_type i, size_type j) const; // Set the element [i,j] void set_value(size_type i, size_type j, value_type val); // Return current number of rows in the matrix size_type get_rows() const; // Set current number of rows void set_rows(size_type new_r); // Return the current number of columns size_type get_cols() const; // Set current number of columns void set_cols(size_type new_c); // Matrix operations // Matrix equality bool operator==(const Matrix& M) const; // Matrix self-addition const Matrix& operator+=(const Matrix& M); // Matrix self-substraction const Matrix& operator-=(const Matrix& M); // Matrix self-multiplication const Matrix& operator*=(const Matrix& M); // Scalar self-multiplication const Matrix& operator*=(int r); // Matrix transpose Matrix transpose() const; // Input operator (Friend function) friend istream& operator>>(istream &in, Matrix& M); // Output operator (Friend function) friend ostream& operator<<(ostream &out, const Matrix& M); private: // Elements in the matrix value_type elements[MAX_ROWS][MAX_COLUMNS]; // Current number of rows in the matrix size_type curr_rows; // Current number of columns in the matrix size_type curr_cols; }; // Non-member matrix algebra operations // Matrix addition Matrix operator+(const Matrix& M1, const Matrix& M2); // Matrix substraction Matrix operator-(const Matrix& M1, const Matrix& M2); // Matrix multiplication Matrix operator*(const Matrix& M1, const Matrix& M2); } #endif ---------------------------------------------------------------------------------------------------------------- MatrixMain.cpp / #include <iostream> #include <cstdlib> #include "Matrix.h" #include "Vector.h" #include "Vector3D.h" using namespace std; using namespace Mat_Vectors; void print_menu(); void matrix_addition(); void matrix_substraction(); void matrix_multiplication(); void matrix_scalar_mult(); void matrix_transpose(); void vector_length(); void vector_inner_prod(); void vector_cross_prod(); void vector_angle(); int main(){ int opt = 0; cout << "Welcome to the Matrix Test Program" << endl; do{ print_menu(); cout << "Enter option: "; cin >> opt; cout << "Option entered: " << opt << endl; switch(opt){ case 1: matrix_addition(); break; case 2: matrix_substraction(); break; case 3: matrix_multiplication(); break; case 4: matrix_scalar_mult(); break; case 5: matrix_transpose(); break; case 6: vector_length(); break; case 7: vector_inner_prod(); break; case 8: vector_cross_prod(); break; case 9: vector_angle(); break; case 10: opt = -1; break; default: cout << "Illegal option: " << opt << endl; } } while(opt != -1); cout << "Thanks for using the Matrix Test Program." << endl; exit(0); } void print_menu(){ cout << "***************************" << endl; cout << "Available Operations:"<< endl; cout << "\t 1. Matrix Addition" << endl; cout << "\t 2. Matrix Substraction" << endl; cout << "\t 3. Matrix Multiplication" << endl; cout << "\t 4. Matrix Scalar Multiplication" << endl; cout << "\t 5. Matrix Transpose" << endl; cout << "\t 6. Vector lenght" << endl; cout << "\t 7. Vector Inner (dot) Product" << endl; cout << "\t 8. Vector Cross Product" << endl; cout << "\t 9. Vector angle " << endl; cout << "\t 10. Exit" << endl; } void matrix_addition(){ Matrix M1, M2, M3; cout << "Enter the first matrix: " << endl; cin >> M1; cout << "Enter the second matrix: " << endl; cin >> M2; cout << "Result: " << endl; cout << M1; cout << "\t + " << endl; cout << M2; cout << "\t = " << endl; M3 = M1 + M2; cout << M3; } void matrix_substraction(){ Matrix M1, M2, M3; cout << "Enter the first matrix: " << endl; cin >> M1; cout << "Enter the second matrix: " << endl; cin >> M2; cout << "Result: " << endl; cout << M1; cout << "\t - " << endl; cout << M2; cout << "\t = " << endl; M3 = M1 - M2; cout << M3; } void matrix_multiplication(){ Matrix M1, M2, M3; cout << "Enter the first matrix: " << endl; cin >> M1; cout << "Enter the second matrix: " << endl; cin >> M2; cout << "Result: " << endl; cout << M1; cout << "\t * " << endl; cout << M2; cout << "\t = " << endl; M3 = M1 * M2; cout << M3; } void matrix_scalar_mult(){ Matrix M1; int scalar = 0; cout << "Enter the matrix: " << endl; cin >> M1; cout << "Enter the scalar value: " << endl; cin >> scalar; cout << "Result: " << endl; cout << M1; cout << "\t * " << endl; cout << scalar << endl; cout << "\t = " << endl; M1*=scalar; cout << M1; } void matrix_transpose(){ Matrix M1, M2; cout << "Enter the matrix: " << endl; cin >> M1; cout << "The tranpose of matrix: " << endl; cout << M1; cout << "is the matrix: " << endl; M2 = M1.transpose(); cout << M2; } void vector_length(){ Vector V1; cout << "Enter the vector: " << endl; cin >> V1; cout << "The length of the vector: " << endl; cout << V1; cout << "is: " << V1.length() << endl; } void vector_inner_prod(){ Vector V1, V2; double result = 0.0; cout << "Enter the first vector: " << endl; cin >> V1; cout << "Enter the second vector: " << endl; cin >> V2; cout << "Result: " << endl; cout << V1; cout << " | " << endl; cout << V2; cout << " = "; result = V1 | V2; cout << result << endl; } void vector_cross_prod(){ Vector3D V1, V2, V3; cout << "Enter the first vector: " << endl; cin >> V1; cout << "Enter the second vector: " << endl; cin >> V2; cout << "Result: " << endl; cout << V1; cout << " & " << endl; cout << V2; cout << " = " << endl; V3 = V1 & V2; cout << V3; } void vector_angle(){ Vector V1, V2; double result = 0.0; cout << "Enter the first vector: " << endl; cin >> V1; cout << "Enter the second vector: " << endl; cin >> V2; cout << "The angle between: " << endl; cout << V1; cout << " and " << endl; cout << V2; cout << " is " << endl; result = V1.angle(V2); cout << result << endl; } ------------------------------------------------------------------------------------------------------------------------ Object.h #ifndef _OBJECT_H #define _OBJECT_H namespace Mat_Vectors{ // Int elements will be stored in the matrix typedef int Object; } #endif --------------------------------------------------------------------------------------------------------- Vector.cpp #include "Vector.h" namespace Mat_Vectors{ // ADD YOUR CODE HERE } ------------------------------------------------------------------------------------------------------ Vector.h #ifndef _VECTOR_ND_H #define _VECTOR_ND_H #include <cmath> #include "Matrix.h" namespace Mat_Vectors{ class Vector : public Matrix { public: // Constructor Vector(int dimensions = 3); // Return number of dimensions size_type get_dimensions() const {return this->get_rows();} // Returns a given coordinate value_type get_coord(int coord) const {return this->get_value(coord,0);} // Sets a given coordinate void set_coord(int coord, value_type new_val) {this->set_value(coord, 0, new_val);} // Vector Magnitude double length() const; // Vector Inner Product double operator|(const Vector& V) const; // Angle between vectors double angle(const Vector& V) const; }; } #endif ---------------------------------------------------------------------------------------------------------- Vector3D.cpp #include "Vector3D.h" namespace Mat_Vectors { // ADD YOUR CODE HERE } ------------------------------------------------------------------------------------------------------- Vector3D.h #ifndef _VECTOR_3D_H #define _VECTOR_3D_H #include "Vector.h" namespace Mat_Vectors{ class Vector3D : public Vector { public: // Constructor Vector3D(); // Vector Self-cross product const Vector3D& operator&=(const Vector3D& V); }; // Non-member function Vector3D operator&(const Vector3D& V1, const Vector3D& V2);
} #endif
#include <stdio.h>
#define N 4
// This function stores transpose of A[][] in B[][]
void transpose(int A[][N], int B[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
B[i][j] = A[j][i];
}
int main()
{
int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
int B[N][N], i, j;
transpose(A, B);
printf("Result matrix is \n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
printf("%d ", B[i][j]);
printf("\n");
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
// maximum size of matrix
#define MAX 4
// maximum number of threads
#define MAX_THREAD 4
int matA[MAX][MAX];
int matB[MAX][MAX];
int matC[MAX][MAX];
int step_i = 0;
void* multi(void* arg)
{
int core = step_i++;
// Each thread computes 1/4th of matrix multiplication
for (int i = core * MAX / 4; i < (core + 1) * MAX / 4; i++)
for (int j = 0; j < MAX; j++)
for (int k = 0; k < MAX; k++)
matC[i][j] += matA[i][k] * matB[k][j];
}
// Driver Code
int main()
{
// Generating random values in matA and matB
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
matA[i][j] = rand() % 10;
matB[i][j] = rand() % 10;
}
}
// Displaying matA
cout << endl
<< "Matrix A" << endl;
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++)
cout << matA[i][j] << " ";
cout << endl;
}
// Displaying matB
cout << endl
<< "Matrix B" << endl;
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++)
cout << matB[i][j] << " ";
cout << endl;
}
// declaring four threads
pthread_t threads[MAX_THREAD];
// Creating four threads, each evaluating its own part
for (int i = 0; i < MAX_THREAD; i++) {
int* p;
pthread_create(&threads[i], NULL, multi, (void*)(p));
}
// joining and waiting for all threads to complete
for (int i = 0; i < MAX_THREAD; i++)
pthread_join(threads[i], NULL);
// Displaying the result matrix
cout << endl
<< "Multiplication of A and B" << endl;
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++)
cout << matC[i][j] << " ";
cout << endl;
}
return 0;
}