Advertisement
dark-s0ul

lab3_masha

Jan 9th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <math.h>
  4. #include <stdio.h>
  5.  
  6. template <int N>
  7. struct vec {
  8.     double data[N];
  9.     double &operator[](int i) { return data[i]; }
  10.     const double &operator[](int i) const { return data[i]; }
  11. };
  12.  
  13. template <int N>
  14. vec<N> operator-(const vec<N> &lvalue, const vec<N> &rvalue) {
  15.     vec<N> result;
  16.     for (int i = 0; i < N; i++) result[i] = lvalue[i] - rvalue[i];
  17.     return result;
  18. }
  19.  
  20. template <int N>
  21. vec<N> operator+(const vec<N> &lvalue, const vec<N> &rvalue) {
  22.     vec<N> result;
  23.     for (int i = 0; i < N; i++) result[i] = lvalue[i] + rvalue[i];
  24.     return result;
  25. }
  26.  
  27. template <int N>
  28. vec<N> operator*(const vec<N> &lvalue, const double &rvalue) {
  29.     vec<N> result;
  30.     for (int i = 0; i < N; i++) result[i] = lvalue[i] * rvalue;
  31.     return result;
  32. }
  33.  
  34. template <int N>
  35. vec<N> operator/(const vec<N> &lvalue, const double &rvalue) {
  36.     return lvalue * (1.0 / rvalue);
  37. }
  38.  
  39. typedef vec<4> vec4;
  40. typedef vec<5> vec5;
  41. typedef vec<5> mat4x5[4];
  42.  
  43. void print(mat4x5 matrix) {
  44.     for (int i = 0; i < 4; i++) {
  45.         for (int j = 0; j <= 4; j++) {
  46.             printf("%-8g", matrix[i][j]);
  47.         }
  48.         printf("\n");
  49.     }
  50. }
  51.  
  52. void CompletePivoting(mat4x5 matrix) {
  53.     printf("Complete pivoting method\n");
  54.     print(matrix);
  55.  
  56.     mat4x5 temp;
  57.     for (int k = 0, p, q; k < 3; k++) {
  58.         double a = 0;
  59.         for (int i = 0; i < 4; i++) {
  60.             for (int j = 0; j < 4; j++) {
  61.                 double t = fabs(matrix[i][j]);
  62.                 if (t > a) {
  63.                     a = t;
  64.                     p = i;
  65.                     q = j;
  66.                 }
  67.             }
  68.         }
  69.  
  70.         temp[k] = matrix[p];
  71.         for (int i = 0; i < 4; i++) {
  72.             if (i == p) continue;
  73.  
  74.             matrix[i] = matrix[i] - matrix[p] * matrix[i][q] / a;
  75.         }
  76.  
  77.         matrix[p] = {0};
  78.  
  79.         for (int i = 0; i < 4; i++) {
  80.             matrix[i][q] = 0;
  81.         }
  82.  
  83.     }
  84.  
  85.     for (int i = 0; i < 4; i++) {
  86.         if (matrix[i][4] != 0) {
  87.             temp[3] = matrix[i];
  88.             break;
  89.         }
  90.     }
  91.  
  92.     vec4 x = {0};
  93.     for (int i = 3; i >= 0; i--) {
  94.         double R = temp[i][4];
  95.         for (int j = 0; j < 4; j++) {
  96.             if ((temp[i][j] != 0) && (x[j] != 0)) {
  97.                 R -= temp[i][j] * x[j];
  98.             }
  99.         }
  100.         for (int j = 0; j < 4; j++) {
  101.             if ((temp[i][j] != 0) && (x[j] == 0)) {
  102.                 x[j] = R / temp[i][j];
  103.             }
  104.         }
  105.     }
  106.  
  107.     for (int i = 0; i < 4; i++) {
  108.         printf("x%i = %f\n", i, x[i]);
  109.     }
  110. }
  111.  
  112. void SimpleIteration(mat4x5 matrix) {
  113.     printf("Simple iteration method\n");
  114.     print(matrix);
  115.  
  116.     for (int i = 0; i < 4; i++) {
  117.         matrix[i] = matrix[i] / matrix[i][i];
  118.     }
  119.  
  120.     double q = 0;
  121.     for (int i = 0; i < 4; i++) {
  122.         double s = 0;
  123.         for (int j = 0; j < 4; j++) {
  124.             if (i == j) continue;
  125.             s += fabs(matrix[i][j]);
  126.         }
  127.         if (s > q) q = s;
  128.     }
  129.  
  130.     vec4 x, x0 ;
  131.  
  132.     double error = 1e-8 * fabs(((1 - q) / q));
  133.     while(true) {
  134.         x0 = x;
  135.         for (int i = 0; i < 4; i++) {
  136.             x[i] = matrix[i][4];
  137.             for (int j = 0; j < 4; j++) {
  138.                 if (i == j) continue;
  139.                 x[i] -= matrix[i][j] * x0[j];
  140.             }
  141.         }
  142.  
  143.         double err = 0;
  144.         for (int i = 0; i < 4; i++) {
  145.             double t = fabs(x0[i] - x[i]);
  146.             if (t > err) err = t;
  147.         }
  148.  
  149.         if (err < error) break;
  150.     }
  151.  
  152.     for (int i = 0; i < 4; i++) {
  153.         printf("x%i = %f\n", i, x[i]);
  154.     }
  155. }
  156.  
  157. int main() {
  158.     mat4x5 matrix1 = {
  159.         13, 14, 17, 14, 146,
  160.         9, 26,  7,  9, 135,
  161.         8,  4, 25, 11, 119,
  162.         15, 11,  5, 16, 109
  163.     };
  164.  
  165.     mat4x5 matrix2 = {
  166.         11, -5,-10, 10,   1, // [0] + [3] - [2] - [1],
  167.          9, 26,  7,  9, 135, // [1],
  168.         13, 14, 17, 14, 146, // [0],
  169.         15, 11,  5, 16, 109, // [3],
  170.     };
  171.  
  172.     CompletePivoting(matrix1);
  173.     printf("\n");
  174.     SimpleIteration(matrix2);
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement