Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include<chrono>
- #include<random>
- #define forn(i, n) for(int i = 0; i < int(n); i++)
- using namespace std;
- const int V = 10;//variant
- const int n = 4;
- const double k = 1e-2;
- using namespace std;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- void boost()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- vector< vector< double > > A(n);
- vector<double> B(n);
- void init() {
- forn(i, n)
- A[i].resize(n);
- forn(i, n)
- for (int j = max(0, i - 1); j < min(i + 2, n); ++j)
- {
- A[i][j] = i * j + i + rnd()%11;
- }
- forn(i, n)
- B[i] = rnd() % 23;
- }
- void print()
- {
- forn(i, n) {
- forn(j, n)
- cout << A[i][j] << "\t";
- cout << endl;
- }
- for (auto v : B)
- cout << v << " ";
- cout << endl;
- }
- vector<double> sweepMetod()
- {
- int N = n - 1;
- double y, p[n], q[n], matRes[n];
- y = A[0][0];
- p[0] = -A[0][1] / y;
- q[0] = B[0] / y;
- for (int i = 1; i < N; ++i)
- {
- y = A[i][i] + A[i][i - 1] * p[i - 1];
- p[i] = -A[i][i + 1] / y;
- q[i] = (B[i] - A[i][i - 1] * q[i - 1]) / y;
- }
- vector<double> res(n);
- res[N] = (B[N] - A[N][N - 1] * q[N - 1])
- / (A[N][N] + A[N][N - 1] * p[N - 1]);
- for (int i = N - 1; i >= 0; i--)
- res[i] = p[i] * res[i + 1] + q[i];
- return res;
- }
- void input()
- {
- forn(i, n)
- forn(j, n)
- cin >> A[i][j];
- forn(i, n)
- cin >> B[i];
- }
- int main()
- {
- boost();
- init();
- //input();
- print();
- vector<double> ans = sweepMetod();
- for (auto v : ans)
- cout << v << " ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement