Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #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;
- void boost()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- }
- vector< vector< double > > A(n);
- vector< double > b(n);
- void norm(int k)
- {
- double cur = A[k][k];
- b[k] /= cur;
- forn(j, n)
- {
- A[k][j] /= cur;
- }
- }
- void toZero(int k)
- {
- for (int i = k + 1; i < n; ++i)
- {
- double cur = A[i][k];
- forn(j, n)
- {
- A[i][j] -= A[k][j] * cur;
- }
- b[i] -= b[k] * cur;
- }
- }
- void print()
- {
- cout << "\nA = \n";
- forn(i, n)
- {
- forn(j, n)
- cout << A[i][j] << " ";
- cout << endl;
- }
- cout << "\nb = \n";
- forn(i, n)
- cout << b[i] << " ";
- cout << "\n\n";
- }
- vector<double> gauss()
- {
- forn(i, n - 1) {
- norm(i);
- toZero(i);
- print();
- }
- norm(n - 1);
- vector<double> ans(n);
- ans[n - 1] = b[n - 1];
- for (int i = n - 2; i >= 0; --i)
- {
- double cur = b[i];
- for (int j = n - 1; j > i; --j)
- {
- cur -= ans[j] * A[i][j];
- }
- ans[i] = cur;
- }
- return ans;
- }
- void init()
- {
- for (int i = 0; i < n; ++i)
- {
- A[i].resize(n);
- A[i][i] = V + i * 2;
- }
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- if (i == j)
- continue;
- A[i][j] = (V + i * 2) * k;
- }
- }
- forn(i, n)
- {
- double sum = 0;
- forn(j, n)
- {
- sum += (V + j * 2) * A[i][j];
- }
- b[i] = sum;
- }
- print();
- }
- int main()
- {
- boost();
- init();
- vector<double> ans = gauss();
- print();
- cout << "\n ans =\n";
- for (auto v : ans)
- cout << v << " ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement