Advertisement
Guest User

Untitled

a guest
May 5th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #include <algorithm>
  2. #include <ostream>
  3. #include "fraction.h"
  4. #include "Matrice.h"
  5.  
  6. Matrix::Matrix()
  7. : height(0),
  8. width(0),
  9. data(NULL)
  10. {}
  11. Matrix::Matrix(int aHeight, int aWidth)
  12. : height(aHeight),
  13. width(aWidth),
  14. data(NULL)
  15. {
  16. setSize(aHeight, aWidth);
  17. }
  18.  
  19. Matrix::Matrix(const Matrix& copy)
  20. :height(copy.height),
  21. width(copy.width),
  22. data(NULL)
  23. {
  24. setSize(copy.height, copy.width);
  25.  
  26. for (int r = 0; r < height; ++r)
  27. {
  28. data[r] = new Fraction[width];
  29. for (int c = 0; c < width; ++c)
  30. {
  31. data[r][c] = copy.data[r][c];
  32. }
  33. }
  34. }
  35.  
  36. Matrix::Matrix(Matrix&& other)
  37. : Matrix()
  38. {
  39. swap(*this, other);
  40. }
  41.  
  42. void Matrix::setSize(int aHeight, int aWidth)
  43. {
  44.  
  45. //Checks that we are not resizing to the same size
  46. //or haven't created date yet
  47. //and that new size is valid.
  48. if ((height != aHeight || width != aWidth) &&
  49. aHeight > 0 && aWidth > 0)
  50. {
  51. //Create new Data. We need this regardless if we're growing, shrinking or creating new data.
  52. Fraction** newData = new Fraction*[aHeight];
  53. for (int r = 0; r < aHeight; ++r)
  54. {
  55. newData[r] = new Fraction[aWidth];
  56. //Initialize explicitly to zero even if Fraction ctor alreader
  57. //does it. It show the intention of the programmer.
  58. for (int c = 0; c < width; ++c)
  59. {
  60. data[r][c] = 0;
  61. }
  62. }
  63. //Copy Data whatever amount of data we can keep.
  64. if (data != NULL)
  65. {
  66. int minHeight = std::min(height, aHeight);
  67. int minWidth = std::min(width, aWidth);
  68. for (int r = 0; r < minHeight; ++r)
  69. {
  70. for (int c = 0; c < minWidth; ++c)
  71. {
  72. newData[r][c] = data[r][c];
  73. }
  74. }
  75. for (int r = 0; r < height; ++r)
  76. {
  77. delete[] data[r];
  78. data[r] = 0;
  79. }
  80. delete[] data;
  81.  
  82. }
  83. //Assign new data
  84. data = newData;
  85. }
  86. height = aHeight;
  87. width = aWidth;
  88. }
  89.  
  90. void Matrix::setValue(int row, int col, const Fraction& value)
  91. {
  92. data[row][col] = value;
  93.  
  94. }
  95.  
  96. Fraction Matrix::getValue(int row, int col) const
  97. {
  98. return data[row][col];
  99. }
  100.  
  101. Matrix::~Matrix()
  102. {
  103. if (data != NULL)
  104. {
  105. for (int r = 0; r < height; ++r)
  106. {
  107. delete[] data[r];
  108. data[r] = 0;
  109. }
  110. delete[] data;
  111. data = 0;
  112. }
  113. }
  114.  
  115.  
  116. Matrix& Matrix::operator=(Matrix rhs)
  117. {
  118. swap(*this, rhs);
  119. return *this;
  120. }
  121.  
  122. Matrix& Matrix::operator+(const Matrix& rhs)const
  123. {
  124. Matrix result;
  125.  
  126. if (height == rhs.height && width == rhs.width)
  127. {
  128. result.setSize(height, width);
  129. for (int r = 0; r < rhs.height; ++r)
  130. {
  131. for (int c = 0; c < rhs.width; ++c)
  132. {
  133. result.data[r][c] = this->data[r][c] + rhs.data[r][c];
  134. }
  135. }
  136. }
  137. return result;
  138. }
  139.  
  140. Matrix& Matrix::operator-(const Matrix& rhs)const
  141. {
  142. Matrix result;
  143.  
  144. if (height == rhs.height && width == rhs.width)
  145. {
  146. for (int r = 0; r < rhs.height; ++r)
  147. {
  148. for (int c = 0; c < rhs.width; ++c)
  149. {
  150. result.data[r][c] = this->data[r][c] - rhs.data[r][c];
  151. }
  152. }
  153. }
  154. return result;
  155. }
  156.  
  157. Matrix& Matrix::operator*(const Matrix& rhs)const
  158. {
  159. Matrix result;
  160. if (this->width == rhs.height)
  161. {
  162. result.setSize(height, rhs.width);
  163. for (int r = 0; r < result.height; ++r)
  164. {
  165. for (int c = 0; c < result.width; ++c)
  166. {
  167. for (int i = 0; i < width; i++)
  168. {
  169. result.data[r][c] += this->data[r][i] * rhs.data[i][c];
  170. }
  171. }
  172. }
  173. }
  174. else
  175. {
  176. return result;
  177. }
  178. return result;
  179.  
  180. }
  181. void swap(Matrix& first, Matrix& second)
  182. {
  183. std::swap(first.height, second.height);
  184. std::swap(first.width, second.width);
  185. std::swap(first.data, second.data);
  186. }
  187. std::ostream& operator<<(std::ostream& out, const Matrix& rhs)
  188. {
  189. if (rhs.data != NULL)
  190. {
  191. for (int r = 0; r < rhs.height; ++r)
  192. {
  193. for (int c = 0; c < rhs.width; ++c)
  194. {
  195. out << rhs.data[r][c] << "\t";
  196. }
  197. out << "\n";
  198. }
  199. }
  200. else
  201. {
  202. out << "Matrix is empty \n";
  203. }
  204. return out;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement