Guest User

Untitled

a guest
Nov 14th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. h := 1 /* Initialization of the pivot row */
  2. k := 1 /* Initialization of the pivot column */
  3. while h ≤ m and k ≤ n
  4. /* Find the k-th pivot: */
  5. i_max := argmax (i = h ... m, abs(A[i, k]))
  6. if A[i_max, k] = 0
  7. /* No pivot in this column, pass to next column */
  8. k := k+1
  9. else
  10. swap rows(h, i_max)
  11. /* Do for all rows below pivot: */
  12. for i = h + 1 ... m:
  13. f := A[i, k] / A[h, k]
  14. /* Fill with zeros the lower part of pivot column: */
  15. A[i, k] := 0
  16. /* Do for all remaining elements in current row: */
  17. for j = k + 1 ... n:
  18. A[i, j] := A[i, j] - A[h, j] * f
  19. /* Increase pivot row and column */
  20. h := h+1
  21. k := k+1
  22.  
  23. #include <iostream>
  24. #include <vector>
  25. #include <algorithm>
  26. #include <cmath>
  27.  
  28. typedef std::vector<std::vector<int>> matrix;
  29. typedef long long ll;
  30.  
  31. void inverse_matrix(matrix &mat)
  32. {
  33. ll h = 1, k =1;
  34. auto m = mat.size(), n = mat[0].size();
  35.  
  36.  
  37. while (h <= m && k <= n)
  38. {
  39. ll i_max = 0;
  40. for (ll i = h; i <= m; ++i)
  41. {
  42. i_max = std::fmax(i, std::abs(mat[i][k]));
  43. }
  44.  
  45. if (mat[i_max][k] == 0)
  46. {
  47. ++k;
  48. }
  49.  
  50. auto temp = mat[h];
  51. mat[h] = mat[i_max];
  52. mat[i_max] = temp;
  53.  
  54. for (auto j = h + 1; j <= m; ++j)
  55. {
  56. auto f = mat[j][k] / mat[h][k];
  57. mat[j][k] = 0;
  58.  
  59. for (auto v = k + 1; v <= n; ++v)
  60. {
  61. mat[j][v] = mat[j][v] - mat[h][j] * f;
  62. }
  63. }
  64.  
  65. ++h;
  66. ++k;
  67. }
  68. }
  69.  
  70. int main() {
  71. matrix mat = {{2, 2}, {4, 5}};
  72. inverse_matrix(mat);
  73.  
  74. return 0;
  75. }
Add Comment
Please, Sign In to add comment