Find inverse matrix using gauss jordan elimination python
To inverse square matrix of order n using Gauss Jordan Elimination, we first augment input matrix of size n x n by Identity Matrix of size n x n. After augmentation, row operation is carried out according to Gauss Jordan Elimination to transform
first n x n part of n x 2n augmented matrix to identity matrix. Output Enter order of matrix: 3 Enter Matrix Coefficients: a[0][0]=1 a[0][1]=1 a[0][2]=3 a[1][0]=1 a[1][1]=3 a[1][2]=-3 a[2][0]=-2 a[2][1]=-4 a[2][2]=-4 INVERSE MATRIX IS: 3.0 1.0 1.5 -1.25 -0.25 -0.75 -0.25 -0.25 -0.25 Recommended Readings
So I am trying to find inverse of a matrix (using Python lists) by Gauss-Jordan Elimination. But I am facing this peculiar problem. In the code below, I apply my code to the given matrix and it reduces to the identity matrix as intended.
The output is
But when I apply the same code, after adding lines of code for my identity matrix (which is part of the augmented matrix with the given matrix), It is not giving me the correct inverse when it should (as I am applying same operation on it as I'm applying on the given matrix).
The output is not the inverse matrix, but something else (though the last column has correct entries).
I tried using print statements while debugging, and I think the line where I divide the rows by the pivot, has some issue. It works fine in the original matrix but doesn't work with the identity matrix. Also, note that only the last column of the identity matrix gets converted to the correct entries of the inverse matrix. The correct inverse matrix for reference is
Any help would be much appreciated. Thanks in advance! How do you find the inverse of a matrix in python?Python provides a very easy method to calculate the inverse of a matrix. The function numpy. linalg. inv() which is available in the python NumPy module is used to compute the inverse of a matrix.
|