Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- h := 1 /* Initialization of the pivot row */
- k := 1 /* Initialization of the pivot column */
- while h ≤ m and k ≤ n
- /* Find the k-th pivot: */
- i_max := argmax (i = h ... m, abs(A[i, k]))
- if A[i_max, k] = 0
- /* No pivot in this column, pass to next column */
- k := k+1
- else
- swap rows(h, i_max)
- /* Do for all rows below pivot: */
- for i = h + 1 ... m:
- f := A[i, k] / A[h, k]
- /* Fill with zeros the lower part of pivot column: */
- A[i, k] := 0
- /* Do for all remaining elements in current row: */
- for j = k + 1 ... n:
- A[i, j] := A[i, j] - A[h, j] * f
- /* Increase pivot row and column */
- h := h+1
- k := k+1
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- typedef std::vector<std::vector<int>> matrix;
- typedef long long ll;
- void inverse_matrix(matrix &mat)
- {
- ll h = 1, k =1;
- auto m = mat.size(), n = mat[0].size();
- while (h <= m && k <= n)
- {
- ll i_max = 0;
- for (ll i = h; i <= m; ++i)
- {
- i_max = std::fmax(i, std::abs(mat[i][k]));
- }
- if (mat[i_max][k] == 0)
- {
- ++k;
- }
- auto temp = mat[h];
- mat[h] = mat[i_max];
- mat[i_max] = temp;
- for (auto j = h + 1; j <= m; ++j)
- {
- auto f = mat[j][k] / mat[h][k];
- mat[j][k] = 0;
- for (auto v = k + 1; v <= n; ++v)
- {
- mat[j][v] = mat[j][v] - mat[h][j] * f;
- }
- }
- ++h;
- ++k;
- }
- }
- int main() {
- matrix mat = {{2, 2}, {4, 5}};
- inverse_matrix(mat);
- return 0;
- }
Add Comment
Please, Sign In to add comment