Advertisement
Georgiy031

Untitled

Nov 5th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. #define all(x) x.begin(), x.end()
  7. #define rall(x) x.rbegin(), x.rend()
  8. //#define endl '\n'
  9. #define boostIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  10. ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
  11. typedef long double ld;
  12. typedef vector<vector<ld>> matrix;
  13.  
  14. matrix mul(const matrix& a, const matrix& b) {
  15.     matrix ans(a.size(), vector<ld>(b[0].size()));
  16.     for (int i = 0; i < a.size(); ++i)
  17.         for (int j = 0; j < b[0].size(); ++j)
  18.             for (int k = 0; k < b.size(); ++k)
  19.                 ans[i][j] += a[i][k] * b[k][j];
  20.  
  21.     return ans;
  22. }
  23. void print(string name,const matrix& a) {
  24.     cout << name << " = \n";
  25.     for (auto row : a) {
  26.         for (auto x : row) {
  27.             cout << fixed << setw(15) << x;
  28.         }
  29.         cout << endl;
  30.     }
  31. }
  32.  
  33. int main() {
  34.  
  35.     matrix A = {
  36.         {352809.0 / 50000,-233253.0 / 100000.0,-662893.0 / 100000.0,406369.0 / 50000.0,-518723.0 / 100000.0},
  37.         {65007.0 / 10000.0,-281899.0 / 100000.0,-451277.0 / 100000.0,-27897.0 / 10000.0,906247.0 / 100000.0},
  38.         {-787103.0 / 100000,17213.0 / 2000,75103.0 / 10000,855647.0 / 100000.0,-614673.0 / 100000.0},
  39.         {15981.0 / 5000.0,-210577.0 / 10000000.0,26733.0 / 4000.0,-493469.0 / 50000.0,31539.0 / 6250.0},
  40.         {177343.0 / 100000.0,-453597.0 / 100000,-187841.0 / 100000,93833.0 / 12500.0,-417783.0 / 50000.0}
  41.     };
  42.     matrix b = { {2.23609}, {-8.3874}, {-2.47353}, {7.43828}, {-0.746177} };
  43.     print("A", A);
  44.     matrix L = {
  45.         {1,0,0,0,0},
  46.         {36115.0 / 39201,1,0,0,0},
  47.         {-787103.0 / 705618,-15692444283.0 / 1751193736,1,0,0},
  48.         {53270.0 / 117603,-135308249341.0 / 87559686800,1436157077576729763.0 / 1702456511488374550,1,0},
  49.         {177343.0 / 705618,10322237821.0 / 1751193736,-22718846198335381.0 / 34049130229767491.0,1572772953187969223765.0 / 3211649824104307778373.0,1}
  50.     };
  51.     print("L", L);
  52.     matrix U = {
  53.         {352809.0 / 50000,-233253.0 / 100000,-662893.0 / 100000,406369.0 / 50000,-518723.0 / 100000},
  54.         {0,-218899217.0 / 326675000,3124935509.0 / 1960050000,-251799599.0 / 24500625,1695608431.0 / 122503125},
  55.         {0,0,34049130229767491.0 / 2364111543600000,-176060634498793.0 / 2364111543600,26501529123999967.0 / 236411154360000},
  56.         {0,0,0,9634949472312923335119.0 / 288551951099724500000.0,-379615477841909321289209.0 / 5771039021994490000000.0},
  57.         {0,0,0,0,118003540165276470656138483873.0 / 6423299648208615556746000000.0}
  58.     };
  59.     print("U", U);
  60.     auto A2 = mul(L, U);
  61.     print("A2", A2);
  62.     matrix y(5, vector<ld>(1));
  63.     for (int i = 0; i < 5; ++i) {
  64.         ld sum = 0;
  65.         for (int j = 0; j < i; ++j)
  66.             sum += L[i][j] * y[j][0];
  67.         y[i][0] = (b[i][0] - sum) / L[i][i];
  68.     }
  69.     print("y", y);
  70.     print("L * y", mul(L, y));
  71.  
  72.     matrix x(5, vector<ld>(1));
  73.     for (int i = 4; i >= 0; --i) {
  74.         ld sum = 0;
  75.         for (int j = i + 1; j < 5; ++j) {
  76.             sum += U[i][j] * x[j][0];
  77.         }
  78.         x[i][0] = (y[i][0] - sum) / U[i][i];
  79.     }
  80.     print("x", x);
  81.     print("A * x", mul(A, x));
  82. }
  83.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement