Is it legal to interchange rows during finding inverse matrix? I'm calculating an inverse matrix using Gauss-Jordan algorithm.
I'd like to use pivoting (with swapping rows, just like in standard Gauss algorithm) in order to avoid division by zero in some cases.
But I also noticed that interchanging rows affects inverse matrix and I don't fully understand how could I avoid division by zero on when running Gauss-Jordan algorithm backwards (when I transform reduced row echelon form of matrix A to identity matrix).
 A: I would suggest you watch the MIT OpenCourseware by Sir Gilbert Strang  or 3B1B series on Linear Algebra there you'll find how matrix product can be represented as the product of a linear combination of rows and columns
I'm sure you'll enjoy it!
Why is not illegal?
$$AB = C$$
Now, when you perform any row transformation on matrix $A$ then the same row transformation will reflect in matrix $C$
How?
$$C_{ij} = \sum_{k = 1}^{m}A_{ik}B_{kj}$$
Or should I write it as
$$\sum_{k = 1}^{m}A_{ik}B_{kj} = C_{ij}$$
The above formula tells that whatever changes you will make to matrix $A$ for row transformation the same matrix $C$ will reflect;
Similarly, for the column transformation on matrix $B$ will be the column transformation for matrix $C = AB$

*

*Fact: An illegal act on any step of mathematics would end you to an illegal result which in the above case it doesn't happen so must be legal.

A: The answer to the asked question is "Yes".
It is most definitely legal to swap rows when finding an inverse matrix.
Perhaps the above statement should be qualified with "by GaussJordan elimination" but I suspect not.
Row swapping is sometimes REQUIRED in the procedure for GaussJordan elimination as described by Gilbert Strang in his lectures and textbooks. My suspicion is that certain matrices REQUIRE row swapping in order to execute GaussJordan elimination no matter what procedure.
The Gilbert Strang references detail the procedure for finding the reduced row echelon format which naturally includes finding the inverse. Row swapping is a completely legal step in Gilbert Strang's references on GausJordan elimination.
Note that swapping rows most definitely changes the inverse of a matrix. Observe the following results in Matlab:
A = [
    1 2 3;
    4 5 6;
    7 8 10;
    ]
Ainv = inv(A)
B= [
    4 5 6;
    1 2 3;
    7 8 10;
    ]
Binv = inv(B)

But the fact that the invesrse of a matrix is not invariant to row swapping is not relevant to the answer of this question.
