Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. #include "Matrix.hpp"
  2.  
  3. using namespace std;
  4.  
  5. struct Matrix::rcMatrix
  6. {
  7. int rows;
  8. int cols;
  9. double** matrix;
  10. int refCounter;
  11. rcMatrix(int r, int c, double** mat)
  12. {
  13. refCounter=1;
  14. rows=r;
  15. cols=c;
  16. matrix=mat;
  17. }
  18. ~rcMatrix()
  19. {
  20. for(int i=0;i<rows;i++)
  21. {
  22. delete [] matrix[i];
  23. }
  24. delete [] matrix;
  25. }
  26. void incRef()
  27. {
  28. refCounter++;
  29. }
  30. void decRef()
  31. {
  32. refCounter--;
  33. if(refCounter==0)delete this;
  34. }
  35. private:
  36. rcMatrix& operator = (const rcMatrix&);
  37. };
  38.  
  39. Matrix::Matrix(const char* nazwa)
  40. {
  41. int r,c; double** tmp;
  42. fstream plik;
  43. plik.open(nazwa,ios::in);
  44. if(plik.good()==true)
  45. {
  46. plik>>r;
  47. plik>>c;
  48. tmp=new double* [r];
  49. for(int i=0;i<r;i++)
  50. tmp[i]=new double [c];
  51. for(int i=0;i<r;i++)
  52. for(int j=0;j<c;j++) plik>>tmp[i][j];
  53. this->data=new rcMatrix(r,c,tmp);
  54. plik.close();
  55. }
  56. else
  57. {
  58. cout<<"Brak dostepu do pliku o nazwie '"<<nazwa<<"'!\n";
  59. throw wrong_file_name();
  60. }
  61. }
  62.  
  63. Matrix::Matrix(int a, int b)
  64. {
  65. double** tmp=new double* [a];
  66. for(int i=0;i<a;i++) tmp[i]=new double [b];
  67. for(int i=0;i<a;i++)
  68. {
  69. for(int j=0;j<b;j++)tmp[i][j]=0;
  70. }
  71. this->data = new rcMatrix(a,b,tmp);
  72. }
  73.  
  74. Matrix::Matrix(const Matrix& M)
  75. {
  76. int r=M.data->rows; int c=M.data->cols;
  77. double** tmp=new double* [r];
  78. for(int i=0;i<r;i++) tmp[i]=new double [c];
  79. for(int i=0;i<r;i++)
  80. {
  81. for(int j=0;j<c;j++) tmp[i][j]=M.data->matrix[i][j];
  82. }
  83. this->data=new rcMatrix(r,c,tmp);
  84. }
  85.  
  86. Matrix::~Matrix() { delete this->data; }
  87.  
  88. double Matrix::operator()(int x, int y) const
  89. {
  90. if(x>this->data->rows || y>this->data->cols) throw bad_index();
  91. return data->matrix[x-1][y-1];
  92. }
  93.  
  94. ostream& operator<<(ostream& out, const Matrix& M)
  95. {
  96. for(int i=0;i<M.data->rows;i++)
  97. {
  98. for(int j=0;j<M.data->cols;j++) out<<M.data->matrix[i][j]<<"\t";
  99. out<<endl;
  100. }
  101. return out;
  102. }
  103.  
  104. bool Matrix::operator==(const Matrix& M) const
  105. {
  106. if(this->data->rows!=M.data->rows) return false;
  107. if(this->data->cols!=M.data->cols) return false;
  108. for(int i=0;i<this->data->rows;i++)
  109. {
  110. for(int j=0;j<this->data->cols;j++) if(this->data->matrix[i][j]!=M.data->matrix[i][j]) return false;
  111. }
  112. return true;
  113. }
  114.  
  115. Matrix& Matrix::operator=(const Matrix& M)
  116. {
  117. int r=M.data->rows; int c=M.data->cols;
  118. int rc=M.data->refCounter;
  119. double** tmp=new double*[r];
  120. for(int i=0;i<r;i++) tmp[i]=new double[c];
  121. for(int i=0;i<r;i++)
  122. {
  123. for(int j=0;j<c;j++) tmp[i][j]=M.data->matrix[i][j];
  124. }
  125. this->data->rows=r;
  126. this->data->cols=c;
  127. this->data->refCounter=rc;
  128. for(int i=0;i<r;i++)
  129. {
  130. for(int j=0;j<c;j++) this->data->matrix[i][j]=tmp[i][j];
  131. }
  132. return *this;
  133. }
  134.  
  135. Matrix Matrix::operator+(const Matrix& M) const
  136. {
  137. Matrix tmp(*this);
  138. tmp+=M;
  139. return tmp;
  140. }
  141.  
  142. Matrix& Matrix::operator+=(const Matrix& M)
  143. {
  144. if(this->data->rows==M.data->rows && this->data->cols==M.data->cols)
  145. {
  146. for(int i=0;i<this->data->rows;i++)
  147. {
  148. for(int j=0;j<this->data->cols;j++) this->data->matrix[i][j]=this->data->matrix[i][j]+M.data->matrix[i][j];
  149. }
  150. }
  151. else throw different_sizes();
  152. return *this;
  153. }
  154.  
  155. Matrix Matrix::operator-(const Matrix& M) const
  156. {
  157. Matrix tmp(*this);
  158. tmp-=M;
  159. return tmp;
  160. }
  161.  
  162. Matrix& Matrix::operator-=(const Matrix& M)
  163. {
  164. if(this->data->rows==M.data->rows && this->data->cols==M.data->cols)
  165. {
  166. for(int i=0;i<this->data->rows;i++)
  167. {
  168. for(int j=0;j<this->data->cols;j++) this->data->matrix[i][j]=this->data->matrix[i][j]-M.data->matrix[i][j];
  169. }
  170. }
  171. else throw different_sizes();
  172. return *this;
  173. }
  174.  
  175. Matrix Matrix::operator*(const Matrix& M) const
  176. {
  177. Matrix tmp(*this);
  178. tmp*=M;
  179. return tmp;
  180. }
  181.  
  182. Matrix& Matrix::operator*=(const Matrix& M)
  183. {
  184. Matrix tmp(*this);
  185. int m, n, p;
  186. m=this->data->rows;
  187. n=this->data->cols;
  188. p=M.data->cols;
  189. for(int i=0;i<m;i++) delete [] this->data->matrix[i];
  190. delete [] this->data->matrix;
  191. this->data->matrix=new double* [m];
  192. for(int i=0;i<m;i++) this->data->matrix[i]=new double [p];
  193. if(this->data->cols==M.data->rows)
  194. {
  195. this->data->rows=m;
  196. this->data->cols=p;
  197. for(int i=0;i<m;i++)
  198. {
  199. for(int j=0;j<p;j++)
  200. {
  201. this->data->matrix[i][j]=0;
  202. for(int k=0;k<n;k++)
  203. {
  204. this->data->matrix[i][j]=this->data->matrix[i][j]+tmp.data->matrix[i][k]*M.data->matrix[k][j];
  205. }
  206. }
  207. }
  208. }
  209. else throw different_sizes();
  210. return *this;
  211. }
  212.  
  213. int Matrix::getRefCounter() const
  214. {
  215. return this->data->refCounter;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement