dark-s0ul

lab3

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