Advertisement
Infiniti_Inter

Full

Oct 16th, 2020 (edited)
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6.  
  7.  
  8.  
  9. #define forn(i, n) for(int i = 0; i < int(n); i++)
  10.  
  11. using namespace std;
  12. const int V = 10;//variant
  13. const int n = 4;
  14. const double k = 1e-2;
  15. using namespace std;
  16.  
  17.  
  18. void boost()
  19. {
  20. #ifdef _DEBUG
  21.     freopen("input.txt", "r", stdin);
  22.     freopen("output.txt", "w", stdout);
  23. #endif
  24. }
  25. vector< vector< double > > A(n);
  26. vector< double > b(n);
  27.  
  28. vector< vector< double > > B;
  29.  
  30. void norm(int k)
  31. {
  32.     double cur = A[k][k];
  33.     b[k] /= cur;
  34.     forn(j, n)
  35.     {
  36.         A[k][j] /= cur;
  37.     }
  38. }
  39. void print()
  40. {
  41.     cout << "\nA = \n";
  42.     forn(i, n)
  43.     {
  44.         forn(j, n)
  45.             cout << A[i][j] << " ";
  46.         cout << endl;
  47.     }
  48.     cout << "\nb = \n";
  49.     forn(i, n)
  50.         cout << b[i] << " ";
  51.     cout << "\n\n";
  52. }
  53.  
  54.  
  55. void toZero(int k)
  56. {
  57.  
  58.     for (int i = k + 1; i < n; ++i)
  59.     {
  60.         double cur = A[i][k] / A[k][k];
  61.         forn(j, n)
  62.         {
  63.             A[i][j] -= A[k][j] * cur;
  64.         }
  65.         b[i] -= b[k] * cur;
  66.     }
  67.  
  68. }
  69.  
  70.  
  71.  
  72. vector<double> backStep()
  73. {
  74.     vector<double> ans(n);
  75.     ans[n - 1] = b[n - 1];
  76.     for (int i = n - 2; i >= 0; --i)
  77.     {
  78.         double cur = b[i];
  79.         for (int j = n - 1; j > i; --j)
  80.         {
  81.             cur -= ans[j] * A[i][j];
  82.         }
  83.         ans[i] = cur;
  84.     }
  85.     return ans;
  86. }
  87.  
  88. vector<double> gauss()
  89. {
  90.  
  91.     forn(i, n - 1) {
  92.         norm(i);
  93.         toZero(i);
  94.         //print();
  95.     }
  96.     norm(n - 1);
  97.     return backStep();
  98.    
  99.    
  100. }
  101.  
  102. void init()
  103. {
  104.  
  105.     for (int i = 0; i < n; ++i)
  106.     {
  107.         A[i].resize(n);
  108.         A[i][i] = V + i * 2;
  109.     }
  110.     for (int i = 0; i < n; ++i)
  111.     {
  112.         for (int j = 0; j < n; ++j)
  113.         {
  114.             if (i == j)
  115.                 continue;
  116.             A[i][j] = (V + i * 2) * k;
  117.         }
  118.     }
  119.  
  120.     forn(i, n)
  121.     {
  122.         double sum = 0;
  123.         forn(j, n)
  124.         {
  125.             sum += (V + j * 2) * A[i][j];
  126.         }
  127.         b[i] = sum;
  128.     }
  129.  
  130.  
  131. }
  132.  
  133. double det()
  134. {
  135.     forn(i, n - 1)
  136.         toZero(i);
  137.     double res = 1;
  138.     forn(i, n)
  139.         res *= A[i][i];
  140.     return res;
  141. }
  142.  
  143. void printTransA()
  144. {
  145.     B = vector<vector<double> >(A);
  146.     forn(i, n)
  147.         forn(j, n)
  148.             B[i][j] = A[j][i];
  149.     cout << "\nA^T : \n";
  150.     forn(i, n) {
  151.         forn(j, n)
  152.             cout << B[i][j] << " ";
  153.         cout << endl;
  154.     }
  155. }
  156.  
  157. int main()
  158. {
  159.     boost();
  160.     init();
  161.     print();
  162.     vector<double> ans = gauss();
  163.     print();
  164.     cout << "\n ans =\n";
  165.     for (auto v : ans)
  166.         cout << v << " ";
  167.     cout << endl;
  168.     forn(i, 15)
  169.         cout << "=";
  170.     cout << endl;
  171.     init();
  172.     cout << "\nDet A = " << det() << endl;
  173.     print();
  174.     forn(i, 15)
  175.         cout << "=";
  176.     init();
  177.     printTransA();
  178.    
  179.  
  180. }
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement