Infiniti_Inter

метод прогонки

Oct 22nd, 2020
726
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include<chrono>
  9. #include<random>
  10.  
  11.  
  12. #define forn(i, n) for(int i = 0; i < int(n); i++)
  13. using namespace std;
  14. const int V = 10;//variant
  15. const int n = 4;
  16. const double k = 1e-2;
  17. using namespace std;
  18.  
  19. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  20.  
  21. void boost()
  22. {
  23. #ifdef _DEBUG
  24.     freopen("input.txt", "r", stdin);
  25.     freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28. vector< vector< double > > A(n);
  29. vector<double> B(n);
  30.  
  31.  
  32. void init() {
  33.     forn(i, n)
  34.         A[i].resize(n);
  35.     forn(i, n)
  36.         for (int j = max(0, i - 1); j < min(i + 2, n); ++j)
  37.         {
  38.             A[i][j] = i * j + i + rnd()%11;
  39.         }
  40.     forn(i, n)
  41.         B[i] = rnd() % 23;
  42. }
  43. void print()
  44. {
  45.     forn(i, n) {
  46.         forn(j, n)
  47.             cout << A[i][j] << "\t";
  48.         cout << endl;
  49.     }
  50.     for (auto v : B)
  51.         cout << v << " ";
  52.     cout << endl;
  53. }
  54.  
  55.  
  56. vector<double> sweepMetod()
  57. {
  58.    
  59.     int N = n - 1;
  60.     double y, p[n], q[n], matRes[n];
  61.     y = A[0][0];
  62.     p[0] = -A[0][1] / y;
  63.     q[0] = B[0] / y;
  64.     for (int i = 1; i < N; ++i)
  65.     {
  66.         y = A[i][i] + A[i][i - 1] * p[i - 1];
  67.         p[i] = -A[i][i + 1] / y;
  68.         q[i] = (B[i] - A[i][i - 1] * q[i - 1]) / y;
  69.     }
  70.  
  71.     vector<double> res(n);
  72.     res[N] = (B[N] - A[N][N - 1] * q[N - 1])
  73.         / (A[N][N] + A[N][N - 1] * p[N - 1]);
  74.     for (int i = N - 1; i >= 0; i--)
  75.         res[i] = p[i] * res[i + 1] + q[i];
  76.     return res;
  77.  
  78. }
  79. void input()
  80. {
  81.     forn(i, n)
  82.         forn(j, n)
  83.         cin >> A[i][j];
  84.     forn(i, n)
  85.         cin >> B[i];
  86. }
  87.  
  88. int main()
  89. {
  90.     boost();
  91.     init();
  92.     //input();
  93.     print();
  94.     vector<double> ans = sweepMetod();
  95.     for (auto v : ans)
  96.         cout << v << " ";
  97. }
  98.  
  99.  
RAW Paste Data