Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. #include "Matrix.h"
  2.  
  3. void CMatrix::Add(const CMatrix & m) throw(invalid_argument)
  4. {
  5. uint32_t rows2, columns2;
  6. this->GetSize(rows2, columns2);
  7. m.GetSize(rows, columns);
  8.  
  9. if (rows != rows2 || columns != columns2) throw invalid_argument("invalid argument");
  10. else {
  11. CMatrix mat = CMatrix(m);
  12. for (unsigned int i = 0; i < rows; i++)
  13. {
  14. for (unsigned int j = 0; j < columns; j++)
  15. {
  16.  
  17. this->matrix[i][j] = matrix[i][j] + mat.GetElement(i, j);
  18. }
  19.  
  20. }
  21. }
  22. }
  23.  
  24. void CMatrix::Sub(const CMatrix & m) throw(invalid_argument)
  25. {
  26. uint32_t rows2, columns2;
  27. this->GetSize(rows2, columns2);
  28. m.GetSize(rows, columns);
  29. CMatrix mat = m;
  30. if (rows != rows2 || columns != columns2) throw invalid_argument("invalid argument");
  31. for (unsigned int i = 0; i < rows; i++)
  32. {
  33. for (unsigned int j = 0; j < columns; j++)
  34. {
  35.  
  36. this->matrix[i][j] = matrix[i][j] - mat.GetElement(i, j);
  37. }
  38.  
  39. }
  40. }
  41.  
  42. CMatrix CMatrix::Mul(const CMatrix & m) const throw(invalid_argument)
  43. {
  44.  
  45. uint32_t rows, columns, rows2, columns2;
  46. m.GetSize(rows, columns);
  47. this->GetSize(rows2, columns2);
  48. if (columns2 != rows) throw invalid_argument("incalid argument");
  49. for (unsigned int i = 0; i < rows2; i++)
  50. {
  51. for (unsigned int j = 0; j < columns; j++)
  52. {
  53. double s = 0;
  54.  
  55. for (unsigned int k = 0; k < columns2; k++)
  56. {
  57. s = s + this->matrix[i][k] * m.matrix[k][j];
  58. }
  59.  
  60. }
  61. }
  62. }
  63. void CMatrix::Mul(const double & scalar) throw(invalid_argument)
  64. {
  65.  
  66. for (unsigned int i = 0; i < rows; i++)
  67. {
  68. for (unsigned int j = 0; j < columns; j++)
  69. {
  70. matrix[i][j] = matrix[i][j] * scalar;
  71.  
  72. }
  73.  
  74. }
  75. }
  76.  
  77. void CMatrix::Div(const double & scalar) throw(invalid_argument)
  78. {
  79. for (unsigned int i = 0; i < rows; i++)
  80. {
  81. for (unsigned int j = 0; j < columns; j++)
  82. {
  83. matrix[i][j] = matrix[i][j] / scalar;
  84.  
  85. }
  86.  
  87. }
  88. }
  89.  
  90. CMatrix::CMatrix(uint32_t rows, uint32_t columns)
  91. {
  92. this->rows = rows;
  93. this->columns = columns;
  94. matrix = new double*[rows];
  95. for (unsigned int i = 0; i < rows; i++) {
  96. matrix[i] = new double[columns];
  97. }
  98.  
  99. for (unsigned int n = 0; n < rows; n++)
  100. {
  101. for (unsigned int m = 0; m < columns; m++)
  102. {
  103. matrix[n][m] = 0;
  104. }
  105. }
  106. }
  107.  
  108. CMatrix::CMatrix(uint32_t rows, uint32_t columns, double ** mat)
  109. {
  110.  
  111.  
  112. matrix = new double*[rows];
  113. for (unsigned int i = 0; i < rows; i++) {
  114. matrix[i] = new double[columns];
  115. }
  116.  
  117. for (uint32_t a = 0; a < rows; a++) {
  118. for (uint32_t b = 0; b < columns; b++) {
  119. matrix[a][b] = mat[a][b];
  120. }
  121.  
  122. }
  123.  
  124. this->rows = rows;
  125. this->columns = columns;
  126. }
  127.  
  128. CMatrix::CMatrix(uint32_t rows, uint32_t columns, double * mat)
  129. {
  130.  
  131.  
  132. matrix = new double*[rows];
  133. for (unsigned int i = 0; i < rows; i++) {
  134. matrix[i] = new double[columns];
  135. }
  136. for (unsigned int i = 0; i < rows; i++)
  137. {
  138. for (uint32_t a = 0; a < rows; a++) {
  139. for (uint32_t b = 0; b < columns; b++) {
  140. matrix[a][b] = *(mat + a*columns) + b;
  141. }
  142.  
  143. }
  144. this->rows = rows;
  145. this->columns = columns;
  146. }
  147. }
  148.  
  149. CMatrix::CMatrix(const CMatrix & M)
  150. {
  151.  
  152. uint32_t rows2, columns2;
  153. this->GetSize(rows2, columns2);
  154. M.GetSize(rows, columns);
  155. CMatrix new_macierz(rows, columns);
  156.  
  157. for (uint32_t a = 0; a < rows; a++) {
  158. for (uint32_t b = 0; b < columns; b++) {
  159. new_macierz[a][b]=M.matrix[a][b];
  160. }
  161. }
  162.  
  163.  
  164. }
  165.  
  166. CMatrix & CMatrix::operator=(const CMatrix & m)
  167. {
  168.  
  169. double ** n_mat = new double*[m.rows];
  170. for (unsigned int i = 0; i < m.rows; i++)
  171. {
  172. n_mat[i] = new double[m.columns];
  173. }
  174.  
  175.  
  176.  
  177. for (unsigned int i = 0; i < m.rows; i++)
  178. {
  179. memcpy(n_mat[i], m.matrix[i], sizeof(double)*m.columns);
  180. }
  181.  
  182.  
  183.  
  184. for (unsigned int i = 0; i < rows; i++)
  185. {
  186. delete[] matrix[i];
  187. }
  188. delete[] matrix;
  189.  
  190.  
  191.  
  192.  
  193. matrix = n_mat;
  194.  
  195. rows = m.rows;
  196. columns = m.columns;
  197.  
  198.  
  199. return *this;
  200.  
  201. }
  202.  
  203. CMatrix::~CMatrix()
  204. {
  205. for (unsigned int i = 0; i < rows; i++)
  206. {
  207. delete[] matrix[i];
  208. }
  209. delete[] matrix;
  210. }
  211.  
  212. void CMatrix::GetSize(uint32_t & rows, uint32_t & columns) const
  213. {
  214. columns = this->columns;
  215. rows = this->rows;
  216.  
  217.  
  218. }
  219.  
  220. double & CMatrix::GetElement(uint32_t row, uint32_t column) throw(invalid_argument)
  221. {
  222. if (rows < 0 || columns < 0) throw invalid_argument("invalid argument");
  223. return matrix[row][column];
  224. // TODOreturn;: tu wstawić instrukcję return
  225. }
  226.  
  227. double * CMatrix::operator[](uint32_t row) throw(invalid_argument)
  228. {
  229. return this->matrix[row];
  230. }
  231.  
  232. CMatrix CMatrix::operator+(const CMatrix & m) const
  233. {
  234. CMatrix matrix = CMatrix(this->rows, this->columns, this->matrix);
  235. matrix.Add(m);
  236. return matrix;
  237. }
  238.  
  239. CMatrix & CMatrix::operator+=(const CMatrix & m)
  240. {
  241. CMatrix matrix = *this;
  242. matrix.Add(m);
  243. return matrix;
  244. }
  245.  
  246. CMatrix CMatrix::operator-(const CMatrix & m) const
  247. {
  248. CMatrix matrix = CMatrix(this->rows, this->columns, this->matrix);
  249. matrix.Sub(m);
  250. return matrix;
  251.  
  252. }
  253.  
  254. CMatrix & CMatrix::operator-=(const CMatrix & m)
  255. {
  256. CMatrix matrix = *this;
  257. matrix.Sub(m);
  258. return matrix;
  259.  
  260. }
  261.  
  262. CMatrix CMatrix::operator*(const CMatrix & m) const
  263. {
  264. CMatrix matrix = CMatrix(this->rows, this->columns, this->matrix);
  265. matrix.Mul(m);
  266. return matrix;
  267. }
  268.  
  269. CMatrix & CMatrix::operator*=(const CMatrix & m)
  270. {
  271. CMatrix matrix = *this;
  272. matrix.Mul(m);
  273. return matrix;
  274. }
  275.  
  276. CMatrix CMatrix::operator*(const double & scalar) const
  277. {
  278. CMatrix matrix = CMatrix(this->rows, this->columns, this->matrix);
  279. matrix.Mul(scalar);
  280. return matrix;
  281.  
  282.  
  283.  
  284. }
  285.  
  286. CMatrix & CMatrix::operator*=(const double & scalar)
  287. {
  288. CMatrix *mac = this;
  289. mac->Mul(scalar);
  290. return *mac;
  291. }
  292.  
  293. CMatrix CMatrix::operator/(const double & scalar) const
  294. {
  295. CMatrix mac = CMatrix(this->rows, this->columns, this->matrix);
  296. mac.Div(scalar);
  297. return mac;
  298. }
  299.  
  300. CMatrix & CMatrix::operator/=(const double & scalar)
  301. {
  302. CMatrix *mac = this;
  303. mac->Div(scalar);
  304. return *mac;
  305. }
  306.  
  307. CMatrix CMatrix::operator-() const
  308. {
  309. CMatrix mac = CMatrix(this->rows, this->columns, this->matrix);
  310. mac.Mul(-1);
  311. return mac;
  312. }
  313.  
  314. CMatrix CMatrix::T() const
  315. {
  316. CMatrix mac = CMatrix(this->rows, this->columns, this->matrix);
  317. for (unsigned int i = 0; i < rows; i++) {
  318. for (unsigned int j = 0; j < rows; j++)
  319. mac[j][i] = matrix[i][j];
  320. return mac;
  321. }
  322. }
  323.  
  324. ostream & operator<<(ostream & stream, const CMatrix & m)
  325. {
  326. return stream;
  327. }
  328.  
  329. CVector::CVector(uint32_t columns): CMatrix(columns,1)
  330. {
  331.  
  332. }
  333.  
  334. CVector::CVector(uint32_t columns, double * mat):CMatrix(columns,1,matrix)
  335. {
  336.  
  337. }
  338.  
  339. CVector & CVector::operator=(const CMatrix & m)
  340. {
  341. return *this;
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement