This project will create a simple matrix calculator. The calculator will be able
ID: 3620720 • Letter: T
Question
This project will create a simple matrix calculator. The calculator will be able to:
?read in matrices (from a file)
?perform operations on those matrices (as specified in the file)
?display the results and write the results to a file
The operations we will be performing include:
?matrix addition
?matrix multiplication
?calculating the determinant of a matrix
?calculating the transpose of a matrix
Input File
A file will be organized as follows
#matrices specified in the file
first_matrix_#rows first_matrix_#columns
first_matrix
second_matrix_#rows second_matrix_#columns
second_matrix
.
.
.
operations_until_eof
so the following would be an example of the file
4
2 3
1 2 3
4 5 6
3 4
4 5 7 23
34 65 12 86
-1 38 -54 21
2 2
-1 5
-83 195
2 3
65 -4 123
54 12 -76
1+4
4*2
2T
3D
3I
Assumptions and Tips
?Let's say that matrices will be smaller than 25 rows and 25 columns and that you will have less than 25 of them in a single input file
?For the operations, there will be no spaces between matrices and operations
?For the operations, the numbering of matrices will start with 1
?Use functions as much as you can to
¦break down problems into smaller pieces
¦allow for easier debugging
Explanation / Answer
Dear, Here is the code Matrix.h class #include #include "./smcalc.h" using std::vector; using std::string; class Matrix { private: matrix_t matrix; string name; UI rows; UI cols; double det_formula_2() const; double det_formula_3() const; /* converts this->matrix from vec to vec */ dmatrix_t convert_to_double() const; double det_GEM() const; public: Matrix(const string &name, const matrix_t &matrix); string get_name() const; UI get_rows() const; UI get_cols() const; bool is_square() const; bool is_empty() const; matrix_t get_matrix() const; double get_determinant(); void set_name(const string name); void transpone(); Matrix add(const Matrix *secondmatrix, const string *resultname) const; Matrix multiply(const Matrix *secondmatrix, const string *resultname) const; void multiply_c(const int *scalar); vector operator[](const UI &rowID) const; }; Matrix::Matrix(const string & _name, const matrix_t & _matrix) :matrix(_matrix), name(_name) { this->rows = _matrix.size(); if (this->rows == 0) { this->cols = 0; } else { this->cols = _matrix[0].size(); } } matrix_t Matrix::get_matrix() const { return(this->matrix); } string Matrix::get_name() const { return(this->name); } UI Matrix::get_rows() const { return(this->rows); } UI Matrix::get_cols() const { return(this->cols); } void Matrix::set_name(const string _name) { this->name = _name; } bool Matrix::is_square() const { return(this->cols == this->rows); } bool Matrix::is_empty() const { return(((this->cols == 0) || (this->rows == 0))?true:false); } double Matrix::det_formula_2() const { double det = 0.0f; // Well, this is obvious // http://en.wikipedia.org/wiki/Determinant#2-by-2_matrices det += this->matrix[0][0] * this->matrix[1][1]; det -= this->matrix[0][1] * this->matrix[1][0]; return(det); } double Matrix::det_formula_3() const { double det = 0.0f; // RULE OF SARRUS // http://en.wikipedia.org/wiki/Rule_of_Sarrus det += this->matrix[0][0] * this->matrix[1][1] * this->matrix[2][2]; det += this->matrix[0][1] * this->matrix[1][2] * this->matrix[2][0]; det += this->matrix[0][2] * this->matrix[1][0] * this->matrix[2][1]; det -= this->matrix[0][2] * this->matrix[1][1] * this->matrix[2][0]; det -= this->matrix[0][1] * this->matrix[1][0] * this->matrix[2][2]; det -= this->matrix[0][0] * this->matrix[1][2] * this->matrix[2][1]; return(det); } dmatrix_t Matrix::convert_to_double() const { dmatrix_t newmatrix; for (UI i = 0; i cols; i++) { vector line; for (UI j = 0; j rows; j++) { line.push_back(this->matrix[i][j]); } newmatrix.push_back(line); } return(newmatrix); } double Matrix::det_GEM() const { double det = 1.0f; dmatrix_t matrix = convert_to_double(); bool change = false; int n = matrix.size(); // matrix is square, n = rows & n = cols for (int x = 1; x rows == 3) { det = det_formula_3(); } else { det = det_GEM(); // determinant by gauss elemination method } return(det); } void Matrix::transpone(void) { matrix_t transponed; if (this->rows == 0) return; // nothing to transpone for (unsigned int i = 0; i cols; i++) { vector line; for (unsigned int j = 0; j rows; j++) line.push_back(this->matrix[j][i]); transponed.push_back(line); } int tmp = this->rows; this->rows = this->cols; this->cols = tmp; this->matrix = transponed; } /* Sums this matrix with second matrix and return result matrix */ Matrix Matrix::add(const Matrix *secondmatrix, const string *resultname) const { assert(this->cols == secondmatrix->get_cols()); assert(this->rows == secondmatrix->get_rows()); matrix_t result; for (UI i = 0; i rows; i++) { vector line; for (UI j = 0; j cols; j++) line.push_back(this->matrix[i][j] + (*secondmatrix)[i][j]); result.push_back(line); } return(Matrix(*resultname, result)); } /* Multiply this matrix with given matrix and return result matrix */ Matrix Matrix::multiply(const Matrix *secondmatrix, const string *resultname) const { matrix_t result; assert(this->cols == secondmatrix->get_rows()); for (UI i = 0; i rows; i++) { vector line; for (UI j = 0; j get_cols(); j++) { int n = 0; for (UI k = 0; k cols; k++) { n += this->matrix[i][k] * (*secondmatrix)[k][j]; } line.push_back(n); } result.push_back(line); } return(Matrix(*resultname, result)); } /* Multiply this matrix with given constant */ void Matrix::multiply_c(const int *scalar) { for (UI i = 0; i rows; i++) for (UI j = 0; j cols; j++) this->matrix[i][j] *= *scalar; } vector Matrix::operator[](const UI & rowID) const { assert(rowID rows); return(this->matrix[rowID]); } int load_matrix_from_file(const string *file_name, matrix_t *matrix) { FILE *input; int rows = 0; int cols = 0; int x; input = fopen(file_name->c_str(), "r"); if (!input) return(1); fscanf(input, "%i", &rows); fscanf(input, "%i", &cols); if ((rows < 1) || (cols < 1)) return(1); matrix->clear(); for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.