Advertisement
dark-s0ul

lab3

Jan 18th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <cmath>
  6. #include <iomanip>
  7.  
  8. template <typename T, int N>
  9. struct vec {
  10.     T data[N];
  11.     T &operator[](int i) {
  12.         if (i >= N) throw "Out of range";
  13.         return data[i];
  14.     }
  15.     const T &operator[](int i) const {
  16.         if (i >= N) throw "Out of range";
  17.         return data[i];
  18.     }
  19.  
  20.     vec<T, N> operator*(const float &value) {
  21.         vec<T, N> result = { 0 };
  22.         for (int i = 0; i < N; i++) {
  23.             result[i] = data[i] * value;
  24.         }
  25.         return result;
  26.     }
  27.  
  28.     T dot(const vec<T, N> &value) {
  29.         T result = { 0 };
  30.         for (int i = 0; i < N; i++) {
  31.             result += data[i] * value[i];
  32.         }
  33.         return result;
  34.     }
  35.  
  36.     vec<T, N> &operator *=(const float &value) {
  37.         return *this = operator*(value);
  38.     }
  39.  
  40.     vec<T, N> &operator +=(const vec<T, N> &value) {
  41.         return *this = operator+(value);
  42.     }
  43.  
  44.     vec<T, N> operator+(const vec<T, N> &value) {
  45.         vec<T, N> result;
  46.         for (int i = 0; i < N; i++) {
  47.             result[i] = data[i] + value[i];
  48.         }
  49.         return result;
  50.     }
  51.  
  52.     vec<T, N> operator-(const vec<T, N> &value) {
  53.         vec<T, N> result;
  54.         for (int i = 0; i < N; i++) {
  55.             result[i] = data[i] - value[i];
  56.         }
  57.         return result;
  58.     }
  59. };
  60.  
  61. typedef vec<double, 4> vec4;
  62. typedef vec<vec4, 4> mat4x4;
  63.  
  64. typedef vec<double, 5> vec5;
  65. typedef vec<vec5, 4> mat4x5;
  66.  
  67. void print(const mat4x5 &matrix) {
  68.     for (int i = 0; i < 4; i++) {
  69.         for (int j = 0; j < 5; j++) {
  70.             printf("%-10g", matrix[i][j]);
  71.         }
  72.         printf("\n");
  73.     }
  74. }
  75.  
  76. void complete_pivoting(mat4x5 matrix) {
  77.     printf("complete pivoting\n");
  78.    
  79.     mat4x5 temp = { 0 };
  80.     for (int k = 0, p, q; k < 3; k++) {
  81.         double a = 0;
  82.  
  83.         for (int i = 0; i < 4; i++) {
  84.             for (int j = 0; j < 4; j++) {
  85.                 float m = fabs(matrix[i][j]);
  86.                 if (m > a) {
  87.                     a = m;
  88.                     p = i;
  89.                     q = j;
  90.                 }
  91.             }
  92.         }
  93.  
  94.         temp[k] = matrix[p];
  95.  
  96.         for (int i = 0; i < 4; i++) {
  97.             if (i == p) continue;
  98.  
  99.             matrix[i] += matrix[p] * (-matrix[i][q] / a);
  100.         }
  101.  
  102.         matrix[p] = { 0 };
  103.         for (int i = 0; i < 4; i++) {
  104.             matrix[i][q] = 0;
  105.         }
  106.     }
  107.  
  108.     for (int i = 0; i < 4; i++) {
  109.         if (matrix[i][4] != 0) {
  110.             temp[3] = matrix[i];
  111.             break;
  112.         }
  113.     }
  114.  
  115.     vec4 x = { 0 };
  116.     for (int i = 3; i >= 0; i--) {
  117.         double R = temp[i][4];
  118.         for (int j = 0; j < 4; j++) {
  119.             if ((temp[i][j] != 0) && (x[j] != 0)) {
  120.                 R -= temp[i][j] * x[j];
  121.             }
  122.         }
  123.         for (int j = 0; j < 4; j++) {
  124.             if ((temp[i][j] != 0) && (x[j] == 0)) {
  125.                 x[j] = R / temp[i][j];
  126.             }
  127.         }
  128.     }
  129.  
  130.     for (int i = 0; i < 4; i++) {
  131.         printf("x%i = %e\n", i, x[i]);
  132.     }
  133. }
  134.  
  135. void gauss_seidel(const mat4x5 &matrix) {
  136.     printf("gauss-seidel\n");
  137.     print(matrix);
  138.  
  139.     vec4 beta;
  140.     for (int i = 0; i < 4; i++) {
  141.         beta[i] = matrix[i][4] / matrix[i][i];
  142.     }
  143.  
  144.     mat4x4 alfa;
  145.     for (int i = 0; i < 4; i++) {
  146.         for (int j = 0; j < 4; j++) {
  147.             alfa[i][j] = (i != j) ? (-matrix[i][j] / matrix[i][i]) : 0;
  148.         }
  149.     }
  150.  
  151.     double q = 0;
  152.     for (int i = 0; i < 4; i++) {
  153.         double s = 0;
  154.         for (int j = 0; j < 4; j++) {
  155.             s += fabs(alfa[i][j]);
  156.         }
  157.         if (s > q) q = s;
  158.     }
  159.  
  160.     double t = fabs((1 - q) * 1e-8 / q);
  161.     printf("|      x1      |      x2      |      x3      |      x4      |\n");
  162.    
  163.     vec4 X = { 0 };
  164.     while (true) {
  165.         vec4 x0 = X;
  166.         for (int i = 0; i < 4; i++) {
  167.             X[i] = beta[i] + alfa[i].dot(X);
  168.         }
  169.  
  170.         printf("| %12.5e | %12.5e | %12.5e | %12.5e |\n", X[0], X[1], X[2], X[3]);
  171.        
  172.         double err = 0;
  173.         for (int i = 0; i < 4; i++) {
  174.             double delta = fabs(x0[i] - X[i]);
  175.             if (delta > err) {
  176.                 err = delta;
  177.             }
  178.         }
  179.         if (err <= t) break;
  180.     }
  181. }
  182.  
  183. int main() {
  184.     mat4x5 a = {
  185.         10,  5,  6, 16, 72,
  186.         19, 38, 18,  0, 76,
  187.         16, 19, 53, 17, 98,
  188.         10, 14, 14,  4, 48
  189.     };
  190.  
  191.     mat4x5 b = { a[3], a[1], a[2], a[0] };
  192.  
  193.     complete_pivoting(a);
  194.     printf("\n");
  195.     gauss_seidel(b);
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement