Advertisement
Guest User

Untitled

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