Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #include "matrix.h"
  2.  
  3. RcMatrix::RcMatrix()
  4. {
  5. pArray = new Array(1,1);
  6. }
  7.  
  8. RcMatrix::RcMatrix(unsigned int column)
  9. {
  10. pArray = new Array(1,column);
  11. }
  12.  
  13. RcMatrix::RcMatrix(unsigned int line, unsigned int column)
  14. {
  15. pArray = new Array(line,column);
  16. }
  17.  
  18. RcMatrix::RcMatrix(unsigned int line, unsigned int column, double *tab)
  19. {
  20. pArray = new Array(line,column);
  21. write(tab);
  22. }
  23.  
  24. RcMatrix::~RcMatrix()
  25. {
  26. //delete pArray;
  27. }
  28.  
  29. RcMatrix::operator=(const RcMatrix& Right)
  30. {
  31.  
  32. if(pArray->n == 1) // Jezeli n==1 to wiemy, ze mozemy ja zwolnic i zaalokowac nowe dane
  33. {
  34. Right.pArray->n++; //Bedziemy wskazywac na Array po prawej stronie, wiec to tam zwiekszamy referencje
  35. pArray->array_free();
  36. pArray = Right.pArray;
  37. }
  38. else //Jezeli jest inaczej tablicy nie mozemy zwonlic, wiec tworzymy nowa tablice
  39. {
  40. this->detach();
  41. this->write(Right.pArray->m);
  42. }
  43. }
  44.  
  45. RcMatrix RcMatrix::operator+(const RcMatrix& Right) const
  46. {
  47. //check_range(*this,Right);
  48. RcMatrix result(pArray->line,pArray->column);
  49. for(int j = 0; j < pArray->line; j++)
  50. {
  51. for(int i = 0; i < pArray->column; i++)
  52. result.pArray->m[j][i] = pArray->m[j][i] + Right.pArray->m[j][i];
  53. }
  54. return result;
  55. }
  56.  
  57. RcMatrix RcMatrix::operator+=(const RcMatrix& Right)
  58. {
  59. if(pArray->n != 1)
  60. this->detach();
  61.  
  62. for(int j = 0; j < pArray->line; j++)
  63. {
  64. for(int i = 0; i < pArray->column; i++)
  65. pArray->m[j][i] += Right.pArray->m[j][i];
  66. }
  67. return *this;
  68. }
  69.  
  70. RcMatrix RcMatrix::operator-(const RcMatrix& Right) const
  71. {
  72. RcMatrix result(pArray->line,pArray->column);
  73. for(int j = 0; j < pArray->line; j++)
  74. {
  75. for(int i = 0; i < pArray->column; i++)
  76. result.pArray->m[j][i] = pArray->m[j][i] - Right.pArray->m[j][i];
  77. }
  78. return result;
  79. }
  80.  
  81. RcMatrix RcMatrix::operator-=(const RcMatrix& Right)
  82. {
  83. if(pArray->n != 1)
  84. this->detach();
  85.  
  86. for(int j = 0; j < pArray->line; j++)
  87. {
  88. for(int i = 0; i < pArray->column; i++)
  89. pArray->m[j][i] -= Right.pArray->m[j][i];
  90. }
  91. return *this;
  92. }
  93.  
  94. RcMatrix RcMatrix::operator-() const
  95. {
  96. RcMatrix result(pArray->line,pArray->column);
  97. for(int j = 0; j < pArray->line; j++)
  98. {
  99. for(int i = 0; i < pArray->column; i++)
  100. result.pArray->m[j][i] = - pArray->m[j][i];
  101. }
  102. return result;
  103. }
  104.  
  105.  
  106. RcMatrix RcMatrix::operator*(const RcMatrix& Right) const
  107. {
  108. RcMatrix result(pArray->line,pArray->column);
  109. for(int j = 0; j < pArray->line; j++)
  110. {
  111. for(int i = 0; i < pArray->column; i++)
  112. result.pArray->m[j][i] = pArray->m[j][i] * Right.pArray->m[j][i];
  113. }
  114. return result;
  115. }
  116.  
  117. RcMatrix RcMatrix::operator*=(const RcMatrix& Right)
  118. {
  119. if(pArray->n != 1)
  120. this->detach();
  121.  
  122. for(int j = 0; j < pArray->line; j++)
  123. {
  124. for(int i = 0; i < pArray->column; i++)
  125. pArray->m[j][i] *= Right.pArray->m[j][i];
  126. }
  127. return *this;
  128. }
  129.  
  130. bool RcMatrix::operator== (const RcMatrix & Right)const
  131. {
  132. for(int j = 0; j < pArray->line; j++)
  133. {
  134. for(int i = 0; i < pArray->column; i++)
  135. {
  136. if(pArray->m[j][i] != Right.pArray->m[j][i])
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. void RcMatrix::detach()
  143. {
  144. Array *result = new Array(pArray->line,pArray->column);
  145. pArray->n = 1;
  146. pArray = result;
  147. }
  148.  
  149. ostream & operator << (ostream & print, const RcMatrix & m)
  150. {
  151. for(int j = 0; j < m.pArray->line; j++)
  152. {
  153. for(int i = 0; i < m.pArray->column; i++)
  154. {
  155. print << m.pArray->m[j][i] << "\t";
  156. }
  157.  
  158. print << "\n";
  159. }
  160.  
  161. return print;
  162. }
  163.  
  164. double RcMatrix::operator()(unsigned int j, unsigned int i) const
  165. {
  166. return pArray->m[j][i];
  167. }
  168.  
  169. RcMatrix::Cref RcMatrix::operator()(unsigned int j, unsigned int i)
  170. {
  171. return Cref(*this,j,i);
  172. }
  173.  
  174. void RcMatrix::write(double **tab)
  175. {
  176. pArray->write(tab);
  177. }
  178.  
  179. void RcMatrix::write(double* tab)
  180. {
  181. for(int j = 0; j < pArray->line; j++)
  182. {
  183. for(int i = 0; i < pArray->column; i++)
  184. {
  185. pArray->m[j][i] = tab[j*pArray->column+i];
  186. }
  187. }
  188. }
  189.  
  190. inline double RcMatrix::read(unsigned int j, unsigned int i)
  191. {
  192. return pArray->m[j][i];
  193. }
  194.  
  195. unsigned int RcMatrix::getRef()
  196. {
  197. return pArray->n;
  198. };
  199.  
  200. void RcMatrix::check_range(const RcMatrix& a, const RcMatrix& b)
  201. {
  202. if( (a.pArray->line != b.pArray->line) || (a.pArray->column != b.pArray->column) )
  203. throw Range();
  204. }
  205.  
  206. RcMatrix::Array::Array(unsigned int line, unsigned int column):
  207. n(1), line(line), column(column)
  208. {
  209. m = new double *[line];
  210. for(int j = 0; j < line; j++)
  211. {
  212. m[j] = new double [column];
  213. for(int i = 0; i < column; i++)
  214. m[j][i] = 0;
  215. }
  216.  
  217. }
  218.  
  219. RcMatrix::Array::~Array()
  220. {
  221. for(int i = 0; i < line; i++)
  222. delete[] m[i];
  223. delete[] m;
  224. }
  225.  
  226.  
  227. void RcMatrix::Array::write(double **tab)
  228. {
  229. for(int j = 0; j < line; j++)
  230. for(int i = 0; i < column; i++)
  231. m[j][i] = tab[j][i];
  232. }
  233.  
  234. double RcMatrix::Array::read(unsigned int j, unsigned int i)
  235. {
  236. return m[j][i];
  237. }
  238.  
  239. void RcMatrix::Array::array_free()
  240. {
  241. for(int i = 0; i < line; i++)
  242. delete[] m[i];
  243. delete[] m;
  244. }
  245.  
  246. RcMatrix::Cref::operator double() const
  247. {
  248. return m.pArray->read(j,i);
  249. }
  250.  
  251. RcMatrix::Cref& RcMatrix::Cref::operator = (double value)
  252. {
  253. m.pArray->m[j][i] = value;
  254. return *this;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement