Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.68 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. bool CzySymetryczna(double ** &A, double * &b, unsigned matrix_size);
  13. void WyswietlMacierz(double ** A, double * b, unsigned matrix_size);
  14. void KopiowanieTablicy(double ** &A, double * &b, double ** &C, double * &d, unsigned matrix_size);
  15. void Choleski(double ** A, double * b, unsigned matrix_size);
  16. void MacierzLY(double ** L, double * b, unsigned matrix_size, vector<double> &y);
  17. void MacierzLTY(double ** LT, vector<double> &y, vector<double> &x, unsigned matrix_size);
  18. void TranspozycjaMacierzyLY(double ** L, double ** LT, unsigned matrix_size);
  19. void WyswietlMacierzBezWartosci(double ** A, unsigned matrix_size);
  20.  
  21. int main()
  22. {
  23.     double ** A;
  24.     double * b;
  25.     double ** C;
  26.     double * d;
  27.     int wybor;
  28.     unsigned matrix_size;
  29.     bool czyWczytany = false;
  30.     for(;;)
  31.     {
  32.         WyswietlMenu();
  33.         cout<<"Wybor => "; cin>>wybor;
  34.         system("clear");
  35.         switch(wybor)
  36.         {
  37.             case 1:
  38.                 if(WczytajPlik(C, d, &matrix_size)) {
  39.                     czyWczytany = true;
  40.                     cout<<"Plik zostal wczytany.\n";
  41.                     WyswietlMacierz(C, d, matrix_size);
  42.                     KopiowanieTablicy(A, b, C, d, matrix_size);
  43.                 }
  44.                 else
  45.                     cout<<"Nie udalo sie wczytac pliku\n";
  46.             break;
  47.             case 2:
  48.                 if(czyWczytany) {
  49.                     if(CzySymetryczna(A, b, matrix_size)) {
  50.                         cout<<"Macierz jest symetryczna. Przechodze do obliczen..."<<endl;
  51.                         Choleski(A, b, matrix_size);
  52.                     }
  53.                     else
  54.                         cout<<"Macierz nie jest symetryczna.\nNalezy wczytac inny uklad."<<endl;
  55.                     }
  56.                 else
  57.                     cout<<"Nalezy wczytac plik przed rozpoczeciem programu";
  58.             break;
  59.             case 3:
  60.                 return 0;
  61.             break;
  62.             default:
  63.                 cout<<"Nalezy wybrac opcje z zakresu [1-3]\n";
  64.             break;
  65.         }    
  66.     }
  67.  
  68.     return 0;
  69. }
  70.  
  71. void WyswietlMenu()
  72. {
  73.     cout<<"\n----MENU----\n";
  74.     cout<<"1. Wczytanie danych z pliku.\n";
  75.     cout<<"2. Metoda Choleskiego.\n";
  76.     cout<<"3. Wyjscie z programu.\n";
  77.     cout<<"\n";
  78. }
  79.  
  80. bool WczytajPlik(double ** &A, double * &b, unsigned *matrix_size)
  81. {
  82.     ifstream source_file("test.csv");
  83.     if (!source_file.is_open())
  84.     {
  85.         cout <<"The file has not been open!"<<endl;
  86.         return false;
  87.     }
  88.     source_file >> *matrix_size;
  89.     A = new double*[*matrix_size];
  90.     A[0] = new double[*matrix_size**matrix_size];
  91.     for(unsigned i = 1; i< *matrix_size; i++)
  92.         A[i] = A[i-1] + *matrix_size;
  93.     b = new double[*matrix_size];
  94.     char semicolumn;
  95.     for (unsigned i = 0; i < *matrix_size+1; i++)
  96.         source_file >> semicolumn;
  97.  
  98.     for (unsigned i = 0; i < *matrix_size; i++)
  99.     {
  100.         for (unsigned j = 0; j < *matrix_size; j++)
  101.         {
  102.             source_file >> A[i][j];
  103.             source_file >> semicolumn;
  104.         }
  105.         source_file >> semicolumn;
  106.         source_file >> b[i];
  107.     }
  108.     source_file.close();
  109.     return true;
  110. }
  111.  
  112. void WyswietlMacierz(double ** A, double * b, unsigned matrix_size)
  113. {
  114.     cout<<endl;
  115.     for(int i=0; i<matrix_size; i++) {
  116.         for(int j=0; j<matrix_size; j++) {
  117.             cout<<setw(10)<<A[i][j]<<" ";
  118.         }
  119.         cout<<"|"<<setw(10)<<b[i];
  120.         cout<<"\n";
  121.     }
  122.     cout<<endl;
  123.  
  124. }
  125. void WyswietlMacierzBezWartosci(double ** A, unsigned matrix_size)
  126. {
  127.     cout<<endl;
  128.     for(int i=0; i<matrix_size; i++) {
  129.         for(int j=0; j<matrix_size; j++) {
  130.             cout<<setw(10)<<A[i][j]<<" ";
  131.         }
  132.         cout<<"\n";
  133.     }
  134.     cout<<endl;
  135.  
  136. }
  137.  
  138. void KopiowanieTablicy(double ** &A, double * &b, double ** &C, double * &d, unsigned matrix_size)
  139. {
  140.     b = new double[matrix_size];
  141.     A = new double*[matrix_size];
  142.     for(int i=0; i<matrix_size; i++)
  143.         A[i] = new double[matrix_size];
  144.     for(int i=0; i<matrix_size; i++)
  145.         for(int j=0; j<matrix_size; j++)
  146.             A[i][j] = C[i][j];
  147.     for(int i=0; i<matrix_size; i++)
  148.         b[i] = d[i];
  149. }
  150.  
  151. bool CzySymetryczna(double ** &A, double * &b, unsigned matrix_size)
  152. {
  153.     for(int i=0; i<matrix_size; i++)
  154.         for(int j=0; j<matrix_size; j++)
  155.             if(A[i][j] != A[j][i])
  156.                 return false;
  157.     return true;
  158. }
  159.  
  160. void Choleski(double ** A, double * b, unsigned matrix_size)
  161. {
  162.     // s = kolumna
  163.     double suma=0;
  164.     vector<double> y; // Ly = b
  165.     vector<double> x; // LT * x = y
  166.     double **L = new double*[matrix_size]; //Macierz L
  167.     for(int i=0; i<matrix_size; i++)
  168.         L[i] = new double[matrix_size];
  169.     double **LT = new double*[matrix_size]; //Macierz Transponowana L
  170.     for(int i=0; i<matrix_size; i++)
  171.         LT[i] = new double[matrix_size];
  172.  
  173.     cout<<"\n\n------------Macierz A-----------\n";
  174.     WyswietlMacierz(A, b, matrix_size);
  175.  
  176.     cout<<"Macierz L  przed rozkladem.\n";
  177.     WyswietlMacierz(L, b, matrix_size);
  178.     for(int s=0; s<matrix_size; s++)
  179.     {
  180.         cout<<"----Kolumna nr"<<s+1<<"----"<<endl;
  181.         for(int i=s; i<matrix_size; i++)
  182.         {
  183.             suma=0;
  184.             cout<<"Wiersz = "<<i+1<<endl;
  185.             if(s == i)
  186.             {
  187.                 //cout<<"s == i iteracja: "<<i<<endl;
  188.                 for(int j=0; j<s; j++)
  189.                 {
  190.                     suma+= pow(L[s][j], 2);
  191.                     //cout<<"L["<<s<<"]["<<s<<"] = "<<L[s][j]<<endl;
  192.                     //cout<<"Suma = "<<suma<<endl;
  193.                 }
  194.                    
  195.                 L[s][s] = sqrt(A[s][s] - suma);
  196.                 cout<<"Wstawiam "<<L[s][s]<<" w miejsce o wspolrzednych ("<<s+1<<","<<s+1<<")"<<endl;
  197.                 WyswietlMacierz(L, b, matrix_size);
  198.  
  199.             }
  200.             else
  201.             {
  202.                 //cout<<"else s==1 iteracja: "<<i<<endl;
  203.                 for(int j=0; j<s; j++) {
  204.                     suma+= L[i][j]*L[s][j];
  205.                     //cout<<"Suma = "<<suma<<endl;
  206.                 }
  207.                 L[i][s] = (A[i][s] - suma)/L[s][s] ;
  208.                 cout<<"Wstawiam "<<L[i][s]<<" w miejsce o wspolrzednych ("<<i+1<<","<<s+1<<")"<<endl;
  209.                 WyswietlMacierz(L, b, matrix_size);
  210.             }
  211.         }
  212.     }
  213.     cout<<"Macierz L po rozkladzie.\n";
  214.     WyswietlMacierz(L, b, matrix_size);
  215.     cout<<"\n--- Ly = b ---\n\n";
  216.     MacierzLY(L, b, matrix_size, y);
  217.     cout<<"\n--- LT * x = y ---\n";    
  218.     cout<<"\n1)Transponowanie macierzy L\n";    
  219.     TranspozycjaMacierzyLY(L, LT, matrix_size);
  220.     cout<<"\n2)Wyliczam wartosci x\n";  
  221.     MacierzLTY(LT, y, x, matrix_size);
  222. }
  223.  
  224. void MacierzLY(double ** L, double * b, unsigned matrix_size, vector<double> &y)
  225. {
  226.     double suma = 0;
  227.     int pomocnyIterator = matrix_size-2;
  228.     y.push_back(b[0] / L[0][0]);
  229.    
  230.    
  231.     for(int i=1; i<matrix_size; i++)
  232.     {
  233.         suma = 0;
  234.         for(int j=0; j<pomocnyIterator; j++)
  235.         {
  236.             suma += L[i][j]*y[j];
  237.             //cout<<"Suma = "<<suma<<endl;
  238.         }
  239.         b[i] = b[i] - suma;
  240.         y.push_back(b[i] / L[i][i]);
  241.         pomocnyIterator++;
  242.     }
  243.  
  244.     for(int i=0; i<matrix_size; i++)
  245.         cout<<"y"<<i+1<<" = "<<y[i]<<endl;
  246. }
  247.  
  248. void MacierzLTY(double ** LT, vector<double> &y, vector<double> &x, unsigned matrix_size)
  249. {
  250.     double suma = 0;
  251.     int pomocnyIterator = matrix_size-2;
  252.     int iteratorOdcietych = 0;
  253.     int pom = matrix_size -1;
  254.     WyswietlMacierzBezWartosci(LT, matrix_size);
  255.  
  256.     x.push_back( y[pom] / LT[pom][pom] );
  257.     for(int i=matrix_size-2; i>=0; i--)
  258.     {
  259.         //cout<<"\n\nWiersz nr"<<i+1<<"\n\n";
  260.         suma = 0;
  261.         iteratorOdcietych = 0;
  262.         for(int j=matrix_size-1; j>pomocnyIterator; j--)
  263.         {
  264.             //cout<<"Wchodze do petli for z iterarotem J\n";
  265.             suma += LT[i][j]*x[iteratorOdcietych];
  266.             iteratorOdcietych++;
  267.             //cout<<"Suma = "<<suma<<endl;
  268.         }
  269.         y[i] = y[i] - suma;
  270.         pomocnyIterator--;
  271.         x.push_back( y[i] / LT[i][i] );
  272.     }
  273.     cout<<"\nRozwiazanie ukladu:\n\n";
  274.     for(int i=0; i<matrix_size; i++)
  275.         cout<<"x"<<i+1<<" = "<<x[i]<<endl;
  276.  
  277. }
  278.  
  279. void TranspozycjaMacierzyLY(double ** L, double ** LT, unsigned matrix_size)
  280. {
  281.     for(int i=0; i<matrix_size; i++)
  282.         for(int j=0; j<matrix_size; j++)
  283.             LT[i][j] = L[j][i];
  284.  
  285.     WyswietlMacierzBezWartosci(LT, matrix_size);
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement