fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <vector>
  4. #include <iomanip> // std::setw
  5. using namespace std;
  6.  
  7. void input();
  8. void my_matrix();
  9. void show_matrix(vector<vector <double>>& matrix, int n, int m);
  10. double* LU(vector<vector <double>>& matrix, int n, int m);
  11.  
  12. int main() {
  13. int answ, n, m;
  14. cout << "1 - Input your matrix\n2 - God's matrix \n";
  15. cin >> answ;
  16. cout << endl;
  17. switch (answ) {
  18. case 1: input(); break;
  19. case 2: my_matrix(); return 0;
  20. }
  21. system("pause");
  22. return 0;
  23. }
  24.  
  25. void my_matrix() {
  26. vector <vector <double>> my_matrix = { { 2, 4, 4, -9, 21 },
  27. { 4, -9, -9, -8, -2 },
  28. { 4, -9, 5, 6, 40 },
  29. { -9, -8, 6, -3, 25 } };
  30. cout << endl << "God's matrix:" << endl;
  31. show_matrix(my_matrix, 4, 5);
  32. vector <vector <double>> LU_matrix = my_matrix;
  33. clock_t start = clock();
  34. double *solutions = LU(LU_matrix, 4, 5);
  35. for (int i = 0; i < 4; i++)
  36. cout << "X" << i + 1 << " = " << solutions[i] << endl;
  37. cout << "Duration " << ((double)(clock()-start) / CLOCKS_PER_SEC) << " sec" << endl;
  38. system("pause");
  39. }
  40.  
  41.  
  42. void input() {
  43. int n = 0;
  44. cout << "Input matrix size \n";
  45. cin >> n;
  46. cout << "Input matrix please \n";
  47. vector <vector <double>> matrix;
  48. matrix.assign(n, vector<double>(n+1));
  49. for (int i = 0; i < n; i++) {
  50. for (int k = 0; k < n+1; k++) {
  51. cin>> matrix[i][k];
  52. }
  53. }
  54. cout << "Your matrix:" << endl;
  55. show_matrix(matrix, n, n+1);
  56. clock_t start = clock();
  57. double* solutions = LU(matrix, n, n+1);
  58. for (int i = 0; i < n; i++)
  59. cout << "X" << i + 1 << " = " << solutions[i] << endl;
  60. cout << "Duration " << ((double)(clock() - start) / CLOCKS_PER_SEC) << " sec" << endl;
  61. system("pause");
  62. }
  63.  
  64. double* LU(vector<vector <double>>& matrix, int n, int m) {
  65. double ratio, * temp_line = new double[m];
  66. vector <vector <double>> L(n, vector<double>(n, 0)); //нижний треугольний вид
  67. for (int i = 0; i < n - 1; i++) {
  68. L[i][i] = 1; //в матрице L главная диагональ состоит из единиц
  69. if (matrix[i][i] == 0) { //если на диагонали 0, то меняем строку со следубщей местами
  70. for (int j = 0; j < m; j++)
  71. temp_line[j] = matrix[i][j]; //временная строка
  72. for (int j = 0; j < m; j++)
  73. matrix[i][j] = matrix[i + 1][j];
  74. for (int j = 0; j < m; j++)
  75. matrix[i + 1][j] = temp_line[j];
  76. }
  77. for (int j = i + 1; j < n; j++) {
  78. if (matrix[i][i] != 0) { //если в столбце не нули
  79. ratio = matrix[j][i] / matrix[i][i];
  80. L[j][i] = ratio; //L состоит из множетелей matrix, которые мы используем для перевода ее к треугольному виду
  81. for (int k = i; k < m - 1; k++) //m-1 т.к. столбец свободных членов не трогаем
  82. matrix[j][k] = matrix[j][k] - ratio * matrix[i][k]; //делаем нули в matrix, переводя ее к верхнему треугольному виду
  83. }
  84. }
  85. }
  86. L[n - 1][n - 1] = 1; //последний элемент главной диагонали = 0
  87.  
  88. double * temp_column = new double[n];
  89. for (int i = 0; i < n; i++)
  90. temp_column[i] = 0;
  91.  
  92. for (int i = 0; i < n; i++) {
  93. double sum = 0;
  94. for (int j = 0; j < i; j++) //проходим ниже главной диагонали L
  95. sum += temp_column[j] * L[i][j]; //находим сумму элементов строки L
  96. temp_column[i] = (matrix[i][m - 1] - sum) / L[i][i]; //находим значения Y, где L * Y = B, где B-столбец свободных членов
  97. }
  98.  
  99. if (matrix[n - 1][m - 3] != 0) { //проверяем на решаемость
  100. cout << "Infinitely many roots" << endl;
  101. system("pause");
  102. exit(0);
  103. }
  104. else if (matrix[n - 1][m - 2] == 0) { //проверяем на решаемость
  105. cout << "No roots" << endl;
  106. system("pause");
  107. exit(0);
  108. }
  109. double* roots = new double[n]; //массив корней
  110. for (int i = 0; i < n; i++)
  111. roots[i] = 0;
  112.  
  113. for (int i = m - 2; i >= 0; i--) { //находим значения X, где Y= U * X, где U=matrix, X-столбец(x1,x2...xn)
  114. double sum = 0;
  115. for (int j = m - 2; j > i; j--) //m-1 элемент = xn
  116. sum += matrix[i][j] * roots[j]; //сумма элементов строки
  117. roots[i] = (temp_column[i] - sum) / matrix[i][i]; //находим корни
  118. }
  119. return roots;
  120. }
  121.  
  122. void show_matrix(vector <vector <double>>& matrix, int n, int m) {
  123. for (int i = 0; i < n; i++) {
  124. for (int k = 0; k < m; k++)
  125. cout << setw(4) << matrix[i][k];
  126. cout << endl;
  127. }
  128. }
  129.  
  130.  
Success #stdin #stdout #stderr 0s 4332KB
stdin
2
stdout
1 - Input your matrix
2 - God's matrix 


God's matrix:
   2   4   4  -9  21
   4  -9  -9  -8  -2
   4  -9   5   6  40
  -9  -8   6  -3  25
X1 = 2
X2 = -2
X3 = 4
X4 = -1
Duration 1.4e-05 sec
stderr
sh: 1: pause: not found