Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1.  
  2. inline matrix::matrix()
  3. {
  4. pArray= new Array(0,0);
  5.  
  6.  
  7. }
  8.  
  9. inline matrix::matrix( unsigned int k)
  10. {
  11. pArray= new Array(0,k);
  12.  
  13. }
  14. inline matrix::matrix(unsigned int w,unsigned int k)
  15. {
  16. pArray=new Array(w,k);
  17.  
  18. }
  19.  
  20. inline matrix::matrix(const matrix &pm)
  21. {
  22. pm.pArray->n++;
  23. for(int i=0;i<pArray->w;i++)
  24. {
  25. for(int j=0;j<pArray->k;j++)
  26. {
  27. pArray->m[i][j]=pm.pArray->m[i][j];
  28. }
  29. }
  30. }
  31.  
  32.  
  33. inline matrix::~matrix()
  34. {
  35. if(--pArray->n==0)
  36. delete pArray;
  37. }
  38.  
  39.  
  40. /*---------------------------------------------------------------------*/
  41. matrix matrix:: operator+(const matrix &pm) const
  42. {
  43.  
  44. if(pArray->k!=pm.pArray->k || pArray->w!=pm.pArray->w)
  45. {
  46. cout<<"Macierzy nie sa rowne!"<< endl;
  47. exit(1);
  48. }
  49. matrix plus(pArray->w,pArray->k);
  50. for( int i=0;i<pArray->w;i++)
  51. {
  52. for(int j=0;j<pArray->k;j++)
  53. {
  54. plus.pArray->m[i][j]=pArray->m[i][j]+pm.pArray->m[i][j];
  55. }
  56. }
  57. return plus;
  58. }
  59.  
  60.  
  61.  
  62. /*---------------------------------------------------------------------*/
  63.  
  64. matrix matrix::operator-(const matrix &pm) const
  65. {
  66.  
  67. if(pArray->k!=pm.pArray->k || pArray->w!=pm.pArray->w)
  68. {
  69. cout<<"Macierzy nie sa rowne!"<< endl;
  70. exit(1);
  71. }
  72. matrix minus (pArray->w,pArray->k);
  73. for( int i=0;i<pArray->w;i++)
  74. {
  75. for(int j=0;j<pArray->k;j++)
  76. {
  77. minus.pArray->m[i][j]=pArray->m[i][j]-pm.pArray->m[i][j];
  78. }
  79. }
  80. return minus;
  81. }
  82.  
  83. matrix & matrix::operator=(const matrix & pm)
  84. {
  85. if(--pArray->n == 1)//mozemy zwolnic i zaalokowac nowa pamiec
  86. {
  87. pm.pArray->n++;
  88. for(int i=0;i<pArray->w;i++)
  89. delete pArray->m[i];
  90. delete pArray->m;
  91. pArray=pm.pArray;
  92. }
  93. else // po prostu tworzymy nowa tablice
  94. {
  95. Array * t=new Array(pArray->w,pArray->k);
  96. pArray->n=1;
  97. }
  98. return *this;
  99.  
  100. }
  101.  
  102.  
  103.  
  104. /* matrix matrix::operator*(const matrix &pm) const
  105. {
  106.  
  107. if(pArray->k != pm.pArray->w )
  108. {
  109. cout << "Blad: Nie da sie pomnozyc tych macierzy" <<endl;
  110. return 0;
  111. }
  112.  
  113. matrix Multiplication(pm.pArray->w, pArray->k);
  114. double sum=0;
  115. for(int j = 0; j < pm.pArray->w; j++)
  116. {
  117. for(int i = 0; i < pArray->k; i++)
  118. {
  119. for(int a=0; ;a++)
  120. {
  121. if(a== pm.pArray->w || a == pArray->k )
  122. break;
  123.  
  124. sum+= pm.pArray->m[a][i] * pArray->m[j][a];
  125. Multiplication.pArray->m[j][i] = sum;
  126. }
  127. sum = 0;
  128. }
  129. }
  130.  
  131. return Multiplication;
  132.  
  133. }
  134. */
  135. matrix & matrix :: operator +=( const matrix &m)
  136. {
  137. if(pArray->k!=m.pArray->k || pArray->w!=m.pArray->w)
  138. {
  139. {
  140. cout<<"Macierzy nie sa rowne!"<< endl;
  141. exit(1);
  142. }
  143. if(pArray->n=0) // n=0 -> zwalniamy pamiec
  144. {
  145. for( int i=0;i<pArray->w;i++)
  146. delete [] pArray->m[i];
  147. delete [] pArray->m;
  148. }
  149. if else(pArray->n==1)
  150. {
  151. for( int i=0;i<pArray->w;i++)
  152. {
  153. for(int j=0;j<pArray->k;j++)
  154. {
  155. pArray->m[i][j]=pArray->m[i][j]+m.pArray->m[i][j];
  156. }
  157.  
  158. }
  159. }
  160. else
  161. {
  162. Array * t= new Array(pArray->w, pArray->k);
  163. for( int i=0;i<pArray->w;i++)
  164. {
  165. for(int j=0;j<pArray->k;j++)
  166. {
  167. t->m[i][j]=t->m[i][j]+m.pArray->m[i][j];
  168. pArray->n=1;
  169. pArray=t;
  170. }
  171. }
  172.  
  173. return *this;
  174. }
  175.  
  176.  
  177. matrix & matrix :: operator -=( const matrix *pm)
  178. {
  179. if(pArray->k!=m.pArray->k || pArray->w!=m.pArray->w)
  180. {
  181. {
  182. cout<<"Macierzy nie sa rowne!"<< endl;
  183. exit(1);
  184. }
  185. if(pArray->n=0) // n=0 -> zwalniamy pamiec
  186. {
  187. for( int i=0;i<pArray->w;i++)
  188. delete [] pArray->m[i];
  189. delete [] pArray->m;
  190. }
  191. if else(pArray->n==1)
  192. {
  193. for( int i=0;i<pArray->w;i++)
  194. {
  195. for(int j=0;j<pArray->k;j++)
  196. {
  197. pArray->m[i][j]=pArray->m[i][j]-m.pArray->m[i][j];
  198. }
  199.  
  200. }
  201. }
  202. else
  203. {
  204. Array * t= new Array(pArray->w, pArray->k);
  205. for( int i=0;i<pArray->w;i++)
  206. {
  207. for(int j=0;j<pArray->k;j++)
  208. {
  209. t->m[i][j]=t->m[i][j]-m.pArray->m[i][j];
  210. pArray->n=1;
  211. pArray=t;
  212. }
  213. }
  214. return *this;
  215. }
  216.  
  217.  
  218. /*---------------------------------------------------------------------*/
  219.  
  220.  
  221.  
  222.  
  223.  
  224. /*---------------------------------------------------------------------*/
  225.  
  226.  
  227. ostream& operator<<(ostream& Out, const matrix& pm)
  228. {
  229. for( int i=0;i<pm.pArray->w;i++)
  230. {
  231. for(int j=0;j<pm.pArray->k;j++)
  232. {
  233. Out<< pm.pArray->m[i][j]<<"\t";
  234. Out<<"\n";
  235. }
  236. }
  237. return Out;
  238. }
  239.  
  240. istream & operator >> (istream & in, const matrix & pm)
  241. {
  242. for( int i=0;i<pm.pArray->w;i++)
  243. {
  244. for(int j=0;j<pm.pArray->k;j++)
  245. {
  246. in>> pm.pArray->m[i][j];
  247. }
  248. }
  249. return in;
  250.  
  251. }
  252. bool matrix::operator ==(const matrix & pm) const
  253. {
  254. for(int j = 0; j < pm.pArray->k; j++)
  255. {
  256. for(int i = 0; i <pm.pArray-> w; i++)
  257. {
  258. if(pArray->m[j][i] != pm.pArray->m[j][i])
  259. return false;
  260. }
  261. }
  262.  
  263. return true;
  264. }
  265. /* inline void matrix :: read (unsigned int i, unsigned int j)
  266. {
  267.  
  268. }
  269.  
  270.  
  271. */
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280. /*matrix& matrix::operator=(const double *)*/
  281.  
  282. /*matrix::matrix(const double * )*/
  283.  
  284.  
  285. double matrix::operator()(unsigned int i,unsigned int j) const
  286. {
  287. return pArray->m[i][j];
  288. }
  289.  
  290. matrix::Dref matrix::operator()(unsigned int i, unsigned int j)
  291. {
  292. return Dref(*this,i,j);
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement