Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <string>
- #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
- }
- vector< vector< double > > A(n);
- vector< double > b(n);
- vector< vector< double > > B;
- void norm(int k)
- {
- double cur = A[k][k];
- b[k] /= cur;
- forn(j, n)
- {
- A[k][j] /= 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";
- }
- void toZero(int k)
- {
- for (int i = k + 1; i < n; ++i)
- {
- double cur = A[i][k] / A[k][k];
- forn(j, n)
- {
- A[i][j] -= A[k][j] * cur;
- }
- b[i] -= b[k] * cur;
- }
- }
- vector<double> backStep()
- {
- 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;
- }
- vector<double> gauss()
- {
- forn(i, n - 1) {
- norm(i);
- toZero(i);
- //print();
- }
- norm(n - 1);
- return backStep();
- }
- 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;
- }
- }
- double det()
- {
- forn(i, n - 1)
- toZero(i);
- double res = 1;
- forn(i, n)
- res *= A[i][i];
- return res;
- }
- void printTransA()
- {
- B = vector<vector<double> >(A);
- forn(i, n)
- forn(j, n)
- B[i][j] = A[j][i];
- cout << "\nA^T : \n";
- forn(i, n) {
- forn(j, n)
- cout << B[i][j] << " ";
- cout << endl;
- }
- }
- int main()
- {
- boost();
- init();
- print();
- vector<double> ans = gauss();
- print();
- cout << "\n ans =\n";
- for (auto v : ans)
- cout << v << " ";
- cout << endl;
- forn(i, 15)
- cout << "=";
- cout << endl;
- init();
- cout << "\nDet A = " << det() << endl;
- print();
- forn(i, 15)
- cout << "=";
- init();
- printTransA();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement