Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <utility>
  4. #include <vector>
  5.  
  6. std::vector<std::vector<std::vector<double>>> lu_composition(std::vector<std::vector<double>> matrix_a) {
  7.     std::vector<std::vector<std::vector<double>>> result;
  8.  
  9.     std::vector<std::vector<double>> matrix_u = std::move(matrix_a);
  10.     std::vector<std::vector<double>> matrix_l = {
  11.             {1, 0, 0, 0},
  12.             {0, 1, 0, 0},
  13.             {0, 0, 1, 0},
  14.             {0, 0, 0, 1}
  15.     };
  16.     for (int root_row = 0; root_row < matrix_u.size(); root_row++)
  17.     {
  18.         double multipler;
  19.  
  20.         for (int row = root_row + 1; row < matrix_u.size(); row++) {
  21.             for (int col = root_row; col < matrix_u.size(); ++col) {
  22.                 if (col == root_row) {
  23.                     multipler = -(matrix_u[row][col] / matrix_u[root_row][col]);
  24.                     matrix_l[row][col] = -multipler;
  25.                 }
  26.  
  27.                 matrix_u[row][col] = matrix_u[root_row][col] * multipler + matrix_u[row][col];
  28.             }
  29.  
  30. //            Displays matrix_u and matrix_l
  31. //
  32. //            for (auto a: matrix_u) {
  33. //                std::cout << "{";
  34. //                for (auto b: a) {
  35. //                    std::cout << b << " ";
  36. //                }
  37. //                std::cout << "}" << std::endl;
  38. //            }
  39. //
  40. //            for (auto a: matrix_l) {
  41. //                std::cout << "{";
  42. //                for (auto b: a) {
  43. //                    std::cout << b << " ";
  44. //                }
  45. //                std::cout << "}" << std::endl;
  46. //            }
  47. //
  48. //            std::cout << std::endl;
  49.         }
  50.     }
  51.  
  52.     result.push_back(matrix_u);
  53.     result.push_back(matrix_l);
  54.  
  55.     return result;
  56. };
  57.  
  58. // std::vector<double>
  59. void forward_subsitution(std::vector<std::vector<double>> matrix_l, std::vector<double> matrix_b) {
  60.     std::vector<double> matrix_y =
  61.             {
  62.                     0,
  63.                     0,
  64.                     0,
  65.                     0
  66.             };
  67.  
  68.     matrix_y[0] = matrix_b[0];
  69.  
  70.     for(int i = 1; i < matrix_b.size(); i++) {
  71.         double sum = 0;
  72.         for (int j = 0; j < i - 1; j++) {
  73.             sum =+ matrix_l[i][j] * matrix_y[j];
  74.         }
  75.         matrix_y[i] = (matrix_b[i] - sum) / matrix_l[i][i];
  76.     }
  77.  
  78.  
  79.     for(auto variable: matrix_y)
  80.         std::cout << variable << std::endl;
  81.     //return  matrix_y;
  82. }
  83.  
  84. int main() {
  85.     std::vector<std::vector<double>> matrix_u;
  86.     std::vector<std::vector<double>> matrix_l;
  87.     std::vector<double> matrix_y;
  88.     std::vector<std::vector<double>> matrix_a = {
  89.             {4, -2, 4, 2},
  90.             {3, 1, 4, 2},
  91.             {2, 4, 2, 1},
  92.             {2, -2, 4, 2}
  93.     };
  94.  
  95.     std::vector<double> matrix_b = {
  96.             8,
  97.             7,
  98.             10,
  99.             2
  100.     };
  101.  
  102.     std::vector<std::vector<std::vector<double>>> temp = lu_composition(matrix_a);
  103.  
  104.     matrix_u = std::move(temp[0]);
  105.     matrix_l = std::move(temp[1]);
  106.  
  107.     forward_subsitution(matrix_l, matrix_b);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement