Guest User

Untitled

a guest
Feb 21st, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stdexcept>
  5. #include <cstdlib>
  6.  
  7. class Matrix { //derived from example on page 450
  8. //wacky mix of C and C++ techniques
  9. //should really be using the Boost class instead of writing my own
  10. private:
  11. //choosing to use a two dimensional array because of the instructions
  12. //using a one dimensional array and managing the 'end' of the columns might be more efficient
  13. int ** m_; //pointer to array
  14. int rows_;
  15. int cols_;
  16.  
  17. int check_bounds(int r, int c) {
  18. if(r >= rows_ || r < 0 || c >= cols_ || c < 0)
  19. throw std::runtime_error("Out of bounds");
  20. }
  21.  
  22. public:
  23. Matrix(int r, int c): rows_(r), cols_(c) {
  24. m_ = new int*[r]; //first dimension
  25.  
  26. for(int i=0; i<r; i++)
  27. m_[i] = new int[c]; //second dimension
  28.  
  29. for(int i=0; i<r; i++)
  30. for(int j=0; j<c; j++)
  31. m_[i][j] = 0; //initialize to 0
  32. }
  33.  
  34. ~Matrix() {
  35. for(int i=0; i<rows_; i++)
  36. delete[] m_[i];
  37. delete[] m_;
  38. }
  39.  
  40. int rows() const { return rows_; }
  41. int cols() const { return cols_; }
  42.  
  43. //return a reference to the variable for editting
  44. //we choose to use the () operator instead of [] because () can take multiple arguments
  45. //using [] would only allow us to return a row, two [] could get a row and a column, but it's an awkward construct
  46. //in other words m[1][2] would first evaluate m[1] for a row, then [2] for that position in the resultant array
  47. //this is much cleaner
  48. int & operator() (int r, int c) { check_bounds(r,c); return m_[r][c]; }
  49. bool exists(int r, int c) {
  50. try { check_bounds(r,c); return true; }
  51. catch(...) { return false; }
  52. }
  53.  
  54. std::string to_str() const {
  55. std::stringstream ss;
  56. for(int i=0; i<rows_; i++) {
  57. for(int j=0; j<cols_; j++) {
  58. ss<<m_[i][j]<<" ";
  59. }
  60. if(i < rows_-1) ss << "\n"; // don't break after the last row
  61. }
  62. return ss.str();
  63. }
  64.  
  65. Matrix operator-(Matrix & m2) {
  66. Matrix m( std::max(this->rows(), m2.rows()), std::max(this->cols(), m2.cols()));
  67.  
  68. //terribly inefficient way to approach this, should probably be a functor
  69. //DRY violation for -
  70. for(int i=0; i < m.rows(); i++) {
  71. for(int j=0; j < m.cols(); j++) {
  72. if( (*this).exists(i,j) && m2.exists(i,j) )
  73. m(i,j) = (*this)(i,j) - m2(i,j);
  74. else if( (*this).exists(i,j) )
  75. m(i,j) = (*this)(i,j);
  76. else if( m2.exists(i,j) )
  77. m(i,j) = m2(i,j);
  78. else
  79. m(i,j) = 0;
  80. }
  81. }
  82. return m;
  83. }
  84.  
  85. Matrix operator+(Matrix & m2) {
  86. Matrix m( std::max(this->rows(), m2.rows()), std::max(this->cols(), m2.cols()));
  87.  
  88. //terribly inefficient way to approach
  89. //DRY violation for
  90. for(int i=0; i < m.rows(); i++) {
  91. for(int j=0; j < m.cols(); j++) {
  92. if( (*this).exists(i,j) && m2.exists(i,j) )
  93. m(i,j) = (*this)(i,j) + m2(i,j);
  94. else if( (*this).exists(i,j) )
  95. m(i,j) = (*this)(i,j);
  96. else if( m2.exists(i,j) )
  97. m(i,j) = m2(i,j);
  98. else
  99. m(i,j) = 0;
  100. }
  101. }
  102. return m;
  103. }
  104.  
  105. Matrix operator*(Matrix & m2) {
  106. if(this->cols() != m2.rows())
  107. throw std::runtime_error("Invalid matrix sizes, cols dont match rows");
  108.  
  109. Matrix m(this->rows(), m2.cols());
  110.  
  111. for(int i=0; i < this->rows(); i++)
  112. for(int j=0; j < m2.cols(); j++)
  113. for(int k=0; k < this->cols(); k++)
  114. m(i,j) += (*this)(i,k) * m2(k,j);
  115.  
  116. return m;
  117. }
  118.  
  119. Matrix operator/(Matrix & m2) {
  120. if(this->cols() != m2.rows())
  121. throw std::runtime_error("Invalid matrix sizes, cols dont match rows");
  122.  
  123. Matrix m(this->rows(), m2.cols());
  124.  
  125. for(int i=0; i < this->rows(); i++)
  126. for(int j=0; j < m2.cols(); j++)
  127. for(int k=0; k < this->cols(); k++)
  128. if((*this)(i,k) && m2(k,j))
  129. m(i,j) += (*this)(i,k) / m2(k,j);
  130.  
  131. return m;
  132. }
  133. };
  134.  
  135. int main(int argc, char** argv)
  136. {
  137. //data set 1
  138. Matrix m1(2,3);
  139. Matrix m2(3,3);
  140.  
  141. m1(0,0) = 1; m1(0,1) = 2; m1(0,2) = 3;
  142. m1(1,0) = 4; m1(1,1) = 5; m1(1,2) = 6;
  143.  
  144. m2(0,0) = 1; m2(0,1) = 0; m2(0,2) = 0;
  145. m2(1,0) = 0; m2(1,1) = 1; m2(1,2) = 0;
  146. m2(2,0) = 0; m2(2,1) = 0; m2(2,2) = 1;
  147.  
  148. std::cout << "m1: " << std::endl;
  149. std::cout << m1.to_str() << std::endl;
  150. std::cout << "--------" << std::endl;
  151.  
  152. std::cout << "m2: " << std::endl;
  153. std::cout << m2.to_str() << std::endl;
  154. std::cout << "--------" << std::endl;
  155.  
  156. std::cout << "m1 + m2: " << std::endl;
  157. std::cout << (m1 + m2).to_str() << std::endl;
  158. std::cout << "--------" << std::endl;
  159.  
  160. std::cout << "m1 - m2: " << std::endl;
  161. std::cout << (m1 - m2).to_str() << std::endl;
  162. std::cout << "--------" << std::endl;
  163.  
  164. std::cout << "m1 * m2: " << std::endl;
  165. std::cout << (m1 * m2).to_str() << std::endl;
  166. std::cout << "--------" << std::endl;
  167.  
  168. std::cout << "m1 / m2: " << std::endl;
  169. std::cout << (m1 / m2).to_str() << std::endl;
  170. std::cout << "--------" << std::endl;
  171.  
  172. //data set 2
  173. Matrix m3(2,3);
  174. Matrix m4(3,2);
  175.  
  176. m3(0,0) = 1; m3(0,1) = 0; m3(0,2) = -2;
  177. m3(1,0) = 0; m3(1,1) = 3; m3(1,2) = -1;
  178.  
  179. m4(0,0) = 0; m4(0,1) = 3;
  180. m4(1,0) = -2; m4(1,1) = -1;
  181. m4(2,0) = 0; m4(2,1) = 4;
  182.  
  183. std::cout << "m3: " << std::endl;
  184. std::cout << m3.to_str() << std::endl;
  185. std::cout << "--------" << std::endl;
  186.  
  187. std::cout << "m4: " << std::endl;
  188. std::cout << m4.to_str() << std::endl;
  189. std::cout << "--------" << std::endl;
  190.  
  191. std::cout << "m3 + m4: " << std::endl;
  192. std::cout << (m3 + m4).to_str() << std::endl;
  193. std::cout << "--------" << std::endl;
  194.  
  195. std::cout << "m3 - m4: " << std::endl;
  196. std::cout << (m3 - m4).to_str() << std::endl;
  197. std::cout << "--------" << std::endl;
  198.  
  199. std::cout << "m3 * m4: " << std::endl;
  200. std::cout << (m3 * m4).to_str() << std::endl;
  201. std::cout << "--------" << std::endl;
  202.  
  203. std::cout << "m3 / m4: " << std::endl;
  204. std::cout << (m3 / m4).to_str() << std::endl;
  205. std::cout << "--------" << std::endl;
  206.  
  207. }
Add Comment
Please, Sign In to add comment