Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <stdlib.h>
  8.  
  9. using namespace std;
  10. bool WczytajPlik(double ** &A, double * &b, unsigned *matrix_size);
  11. void WyswietlMenu();
  12. void WyswietlMacierz(double ** A, double * b, unsigned matrix_size);
  13. void KopiowanieTablicy(double ** &A, double * &b, double ** &C, double * &d, unsigned matrix_size);
  14. void TranspozycjaMacierzy(double ** w, double ** wT, unsigned matrix_size);
  15. void WyswietlMacierzBezWartosci(double ** A, unsigned matrix_size);
  16. double CoefficientB(double ** &A, vector<double> &w, unsigned matrix_size, int step);
  17. void WektorU(double ** &A, double * &b, unsigned matrix_size);
  18. double Sign(double value);
  19. vector<double> Aprim(double ** &A, vector<double> &w, unsigned matrix_size,int col, int step);
  20. vector<double> Bprim(double * &b, vector<double> &w, unsigned matrix_size, int step);
  21. double Alfa(double ** &A, vector<double> &w, unsigned matrix_size, int step, int col);
  22. double CoefficientB(double ** &A, vector<double> &w, unsigned matrix_size, int step);
  23.  
  24. void NewB(double * &b, vector<double> &newB, unsigned matrix_size, int step);
  25. void NewMatrix(double ** &A, vector<vector<double> > &MatrixPrim, unsigned matrix_size, int step);
  26.  
  27. int main()
  28. {
  29. double ** A;
  30. double * b;
  31. double ** C;
  32. double * d;
  33. int wybor;
  34. unsigned matrix_size;
  35. bool czyWczytany = false;
  36. for(;;)
  37. {
  38. WyswietlMenu();
  39. cout<<"Wybor => "; cin>>wybor;
  40. system("clear");
  41. switch(wybor)
  42. {
  43. case 1:
  44. if(WczytajPlik(C, d, &matrix_size)) {
  45. czyWczytany = true;
  46. cout<<"Plik zostal wczytany.\n";
  47. WyswietlMacierz(C, d, matrix_size);
  48. KopiowanieTablicy(A, b, C, d, matrix_size);
  49. }
  50. else
  51. cout<<"Nie udalo sie wczytac pliku\n";
  52. break;
  53. case 2:
  54. if(czyWczytany) {
  55. WektorU(A, b, matrix_size);
  56. }
  57. else
  58. cout<<"Nalezy wczytac plik przed rozpoczeciem programu";
  59. break;
  60. case 3:
  61. return 0;
  62. break;
  63. default:
  64. cout<<"Nalezy wybrac opcje z zakresu [1-3]\n";
  65. break;
  66. }
  67. }
  68.  
  69. return 0;
  70. }
  71.  
  72. void WektorU(double ** &A, double * &b, unsigned matrix_size)
  73. {
  74. vector<double> w;
  75. vector<double> newB;
  76. vector<vector<double> > MatrixPrim;
  77. vector<double> aprim;
  78. double sum = 0;
  79. int iteratorW = 0;
  80. cout<<"\n\n\n----POCZATEK PROGRAMU----\n\n\n";
  81. for(int step=0; step<matrix_size; step++)
  82. {
  83. iteratorW = 0;
  84. cout<<"\nKrok nr"<<step+1<<endl;
  85. for(int row=step; row<matrix_size; row++)
  86. {
  87. if(step == row) {
  88. for(int el=step; el<matrix_size; el++)
  89. {
  90. sum+= pow(A[el][step], 2);
  91. // cout<<"A[el][col]="<<A[el][col]<<endl;
  92. // cout<<"Suma = "<<sum<<endl;
  93. }
  94. // cout<<"A[col][col] + Sign(A[col][col])*sqrt(sum))"<<A[step][step]<<"+"<<Sign(A[step][step])<<"*"<<sqrt(sum)<<endl;
  95. w.push_back(A[step][step] + Sign(A[step][step])*sqrt(sum));
  96. cout<<"\nW["<<iteratorW<<"] = "<<w[iteratorW]<<endl;
  97. iteratorW++;
  98. }
  99. else
  100. {
  101. w.push_back(A[row][step]);
  102. cout<<"W["<<iteratorW<<"] = "<<w[iteratorW]<<endl;
  103. iteratorW++;
  104. }
  105. }
  106. for(int col=step; col<matrix_size; col++)
  107. {
  108. cout<<"\n\n!-!-!------------------!-!-!\n";
  109. sum = 0;
  110.  
  111.  
  112. cout<<endl;
  113. aprim = Aprim(A, w, matrix_size, col, step);
  114. MatrixPrim.push_back(aprim);
  115. aprim.clear();
  116. }
  117. cout<<endl;
  118. cout<<endl;
  119.  
  120. newB = Bprim(b, w, matrix_size, step);
  121. NewMatrix(A, MatrixPrim, matrix_size, step);
  122. MatrixPrim.clear();
  123. NewB(b, newB, matrix_size, step);
  124. newB.clear();
  125. WyswietlMacierz(A, b, matrix_size);
  126. w.clear();
  127.  
  128. }
  129. }
  130.  
  131. void NewMatrix(double ** &A, vector<vector<double> > &MatrixPrim, unsigned matrix_size, int step)
  132. {
  133. const int newStep = step;
  134. for(int i=newStep, iV=0; i<matrix_size && iV<matrix_size; i++, iV++)
  135. for(int j=newStep, jV=0; j<matrix_size && jV<matrix_size; j++, jV++)
  136. A[i][j] = MatrixPrim[jV][iV];
  137. }
  138.  
  139. void NewB(double * &b, vector<double> &newB, unsigned matrix_size, int step)
  140. {
  141. const int newStep = step;
  142. for(int i=newStep, iV=0; i<matrix_size && iV<matrix_size; i++, iV++)
  143. b[i] = newB[iV];
  144. }
  145.  
  146. vector<double> Aprim(double ** &A, vector<double> &w, unsigned matrix_size, int col, int step)
  147. {
  148. double B = CoefficientB(A, w, matrix_size, step);
  149. int iterationW = 0;
  150. vector<double> newA;
  151. double alfa = Alfa(A, w, matrix_size, step, col);
  152. cout<<"alfa"<<col+1<<" = "<<alfa<<endl;
  153. cout<<"B = "<<B<<endl;
  154. for(int i=step; i<matrix_size; i++)
  155. {
  156. cout<<endl<<"------------------"<<endl;
  157. cout<<"A["<<i<<"]["<<col<<"] = "<<A[i][col]<<setw(10)<<" "<<"(2*alfa/ B)*w[iterationW]) = "<<(2*alfa/ B)*w[iterationW]<<endl;
  158. cout<<"------------------"<<endl;
  159. newA.push_back(A[i][col] - (2*alfa/B)*w[iterationW]);
  160. cout<<"A"<<i+1<<"' = "<<newA[i]<<setw(5)<<" ";
  161. iterationW++;
  162. }
  163. return newA;
  164. }
  165.  
  166. vector<double> Bprim(double * &b, vector<double> &w, unsigned matrix_size, int step)
  167. {
  168. vector<double> newB;
  169. double B = 24;
  170. double alfa = 0;
  171. int iterationW = 0;
  172. for(int i=step; i<matrix_size; i++) {
  173. alfa += b[i] * w[iterationW];
  174. iterationW++;
  175.  
  176. }
  177. cout<<"alfa b = "<< alfa<<endl;
  178. iterationW = 0;
  179. for(int i=step; i<matrix_size; i++) {
  180. newB.push_back(b[i] - (2*alfa/B)*w[iterationW]);
  181. cout<<"\nb[i] - (2*alfa/B)*w[iterationW] == "<<b[i]<<" - "<<(2*alfa/B)<<" * "<<w[iterationW]<<endl;
  182. cout<<"b"<<i+1<<"' = "<<newB[i]<<setw(5)<<" ";
  183. iterationW++;
  184. }
  185. return newB;
  186. }
  187.  
  188. double CoefficientB(double ** &A, vector<double> &w, unsigned matrix_size, int step)
  189. {
  190. vector<double> alfa;
  191. vector<double> Aprim;
  192. double B=0;
  193. for(int i=0; i<matrix_size-step; i++)
  194. B += pow(w[i], 2);
  195.  
  196. return B;
  197. }
  198.  
  199. double Alfa(double ** &A, vector<double> &w, unsigned matrix_size, int step, int col)
  200. {
  201. double sum = 0;
  202. int iteratorW = 0;
  203. for(int i=step; i<matrix_size; i++) {
  204. sum += A[i][col] * w[iteratorW];
  205. iteratorW++;
  206. }
  207.  
  208. return sum;
  209. }
  210.  
  211. double MultiplyVectors(vector<double> &w1, vector<double> &w2, unsigned matrix_size)
  212. {
  213. double sum = 0;
  214. for(int i=0; i<matrix_size; i++)
  215. sum += w1[i] * w2[i];
  216. return sum;
  217. }
  218.  
  219. double Sign(double value) {
  220. if(value < 0)
  221. return -1;
  222. else if(value > 0)
  223. return 1;
  224. else
  225. return 0;
  226. }
  227.  
  228. void WyswietlMenu()
  229. {
  230. cout<<"\n----MENU----\n";
  231. cout<<"1. Wczytanie danych z pliku.\n";
  232. cout<<"2. Householder.\n";
  233. cout<<"3. Wyjscie z programu.\n";
  234. cout<<"\n";
  235. }
  236.  
  237. bool WczytajPlik(double ** &A, double * &b, unsigned *matrix_size)
  238. {
  239. ifstream source_file("test.csv");
  240. if (!source_file.is_open())
  241. {
  242. cout <<"The file has not been open!"<<endl;
  243. return false;
  244. }
  245. source_file >> *matrix_size;
  246. A = new double*[*matrix_size];
  247. A[0] = new double[*matrix_size**matrix_size];
  248. for(unsigned i = 1; i< *matrix_size; i++)
  249. A[i] = A[i-1] + *matrix_size;
  250. b = new double[*matrix_size];
  251. char semicolumn;
  252. for (unsigned i = 0; i < *matrix_size+1; i++)
  253. source_file >> semicolumn;
  254.  
  255. for (unsigned i = 0; i < *matrix_size; i++)
  256. {
  257. for (unsigned j = 0; j < *matrix_size; j++)
  258. {
  259. source_file >> A[i][j];
  260. source_file >> semicolumn;
  261. }
  262. source_file >> semicolumn;
  263. source_file >> b[i];
  264. }
  265. source_file.close();
  266. return true;
  267. }
  268.  
  269. void WyswietlMacierz(double ** A, double * b, unsigned matrix_size)
  270. {
  271. cout<<endl;
  272. for(int i=0; i<matrix_size; i++) {
  273. for(int j=0; j<matrix_size; j++) {
  274. cout<<setw(10)<<A[i][j]<<" ";
  275. }
  276. cout<<"|"<<setw(10)<<b[i];
  277. cout<<"\n";
  278. }
  279. cout<<endl;
  280.  
  281. }
  282. void WyswietlMacierzBezWartosci(double ** A, unsigned matrix_size)
  283. {
  284. cout<<endl;
  285. for(int i=0; i<matrix_size; i++) {
  286. for(int j=0; j<matrix_size; j++) {
  287. cout<<setw(10)<<A[i][j]<<" ";
  288. }
  289. cout<<"\n";
  290. }
  291. cout<<endl;
  292.  
  293. }
  294.  
  295. void KopiowanieTablicy(double ** &A, double * &b, double ** &C, double * &d, unsigned matrix_size)
  296. {
  297. b = new double[matrix_size];
  298. A = new double*[matrix_size];
  299. for(int i=0; i<matrix_size; i++)
  300. A[i] = new double[matrix_size];
  301. for(int i=0; i<matrix_size; i++)
  302. for(int j=0; j<matrix_size; j++)
  303. A[i][j] = C[i][j];
  304. for(int i=0; i<matrix_size; i++)
  305. b[i] = d[i];
  306. }
  307.  
  308. void TranspozycjaMacierzy(double ** w, double ** wT, unsigned matrix_size)
  309. {
  310. for(int i=0; i<matrix_size; i++)
  311. for(int j=0; j<matrix_size; j++)
  312. wT[i][j] = w[j][i];
  313.  
  314. WyswietlMacierzBezWartosci(wT, matrix_size);
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement