Advertisement
Infiniti_Inter

Gauss

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