Advertisement
dark-s0ul

lab3_masha

Jan 9th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 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.     int p, q, ln = 0;
  57.  
  58.     mat4x5 temp;
  59.     for (int k = 0; k < 4 - 1; k++) {
  60.         double a = 0;
  61.         for (int i = 0; i < 4; i++) {
  62.             for (int j = 0; j < 4; j++) {
  63.                 double t = fabs(matrix[i][j]);
  64.                 if (t > a) {
  65.                     a = t;
  66.                     p = i;
  67.                     q = j;
  68.                 }
  69.             }
  70.         }
  71.  
  72.         temp[ln] = matrix[p];
  73.  
  74.         ln++;
  75.         for (int i = 0; i < 4; i++) {
  76.             if (i == p) continue;
  77.  
  78.             matrix[i] = matrix[i] - matrix[p] * matrix[i][q] / a;
  79.         }
  80.  
  81.         matrix[p] = {0};
  82.  
  83.         for (int i = 0; i < 4; i++) {
  84.             matrix[i][q] = 0;
  85.         }
  86.  
  87.     }
  88.  
  89.     for (int i = 0; i < 4; i++) {
  90.         if (matrix[i][4] != 0) {
  91.             p = i;
  92.         }
  93.     }
  94.  
  95.     temp[ln] = matrix[p];
  96.  
  97.     vec4 x = {0};
  98.     for (int i = 4 - 1; i >= 0; i--) {
  99.         double R = temp[i][4];
  100.         for (int j = 0; j < 4; j++) {
  101.             if ((temp[i][j] != 0) && (x[j] != 0)) {
  102.                 R -= temp[i][j] * x[j];
  103.             }
  104.         }
  105.         for (int j = 0; j < 4; j++) {
  106.             if ((temp[i][j] != 0) && (x[j] == 0)) {
  107.                 x[j] = R / temp[i][j];
  108.             }
  109.         }
  110.     }
  111.  
  112.     for (int i = 0; i < 4; i++) {
  113.         printf("x%i = %f\n", i, x[i]);
  114.     }
  115. }
  116.  
  117. void SimpleIteration(mat4x5 matrix) {
  118.     printf("Simple iteration method\n");
  119.     print(matrix);
  120.  
  121.     for (int i = 0; i < 4; i++) {
  122.         matrix[i] = matrix[i] / matrix[i][i];
  123.     }
  124.  
  125.     double q = 0;
  126.     for (int i = 0; i < 4; i++) {
  127.         double s = 0;
  128.         for (int j = 0; j < 4; j++) {
  129.             if (i == j) continue;
  130.             s += fabs(matrix[i][j]);
  131.         }
  132.         if (s > q) q = s;
  133.     }
  134.  
  135.     vec4 x, x0;
  136.     for (int i = 0; i < 4; i++) {
  137.         x0[i] = matrix[i][4];
  138.     }
  139.  
  140.     for (int i = 0; i < 4; i++) {
  141.         x[i] = matrix[i][4];
  142.         for (int j = 0; j < 4; j++) {
  143.             if (i == j) continue;
  144.             x[i] -= matrix[i][j] * x0[j];
  145.         }
  146.     }
  147.  
  148.     x0 = x;
  149.  
  150.     double err = 0;
  151.     double error = 1e-8 * fabs(((1 - q) / q));
  152.     do {
  153.         for (int i = 0; i < 4; i++) {
  154.             x[i] = matrix[i][4];
  155.             for (int j = 0; j < 4; j++) {
  156.                 if (i == j) continue;
  157.                 x[i] -= matrix[i][j] * x0[j];
  158.             }
  159.         }
  160.  
  161.         x0 = x - x0;
  162.  
  163.         err = fabs(x0[0]);
  164.         for (int i = 1; i < 4; i++) {
  165.             double t = fabs(x0[i]);
  166.             if (t > err) err = t;
  167.         }
  168.  
  169.         x0 = x;
  170.     } while (err > error);
  171.  
  172.     for (int i = 0; i < 4; i++) {
  173.         printf("x%i = %f\n", i, x[i]);
  174.     }
  175. }
  176.  
  177. int main() {
  178.     mat4x5 matrix1 = {
  179.         13, 14, 17, 14, 146,
  180.         9, 26,  7,  9, 135,
  181.         8,  4, 25, 11, 119,
  182.         15, 11,  5, 16, 109
  183.     };
  184.  
  185.     mat4x5 matrix2 = {
  186.         11, -5,-10, 10,   1, // [0] + [3] - [2] - [1],
  187.          9, 26,  7,  9, 135, // [1],
  188.         13, 14, 17, 14, 146, // [0],
  189.         15, 11,  5, 16, 109, // [3],
  190.     };
  191.  
  192.     CompletePivoting(matrix1);
  193.     printf("\n");
  194.     SimpleIteration(matrix2);
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement