Skip to main content

1.4 Gaussian Elimination to find the Inverse of a square matrix

A linear system is said to be square if the number of equations matches the number of unknowns. If the system $A x = b$ is square, then the coefficient matrix, $A$, is square. If $A$ has an inverse, then the solution to the system $A x = b$ can be found by multiplying both sides by $A ^{−1}$
$Ax=b$
$A^{-1}Ax=A^{-1}b$
$x=A^{-1}b$

If $A$ is an invertible $n$ by $n$ matrix, then the system $A x = b$ has a unique solution for every $n$‐vector $b$, and this solution equals $A ^{−1} b$.

Since the determination of $A^{ −1}$ typically requires more calculation than performing Gaussian elimination and back substitution, this is not necessarily an improved method of solving $A x = b$ (And, of course, if $A$ is not square, then it has no inverse, so this method is not even an option for non square systems.).However, if the coefficient matrix $A$ is square, and if $A ^{−1}$ is known or the solution of $A x = b$ is required for several different $b$'s, then this method is indeed useful, from both a theoretical and a practical point of view.

The elementary row operations that characterize Gauss‐Jordan elimination can be applied to compute the inverse of a square matrix.
If $E$ is the elementary matrix that results when a particular elementary row operation is performed on $I$(Identity Matrix), then the product $EA$ is equal to the matrix that would result if that same elementary row operation were applied to $A$. In other words, an elementary row operation on a matrix $A$ can be performed by multiplying $A$ on the left by the corresponding elementary matrix.

Thus the sequence of elementary matrix product will give the identity matrix $E_n...E_2E_1A=I$. where the elementary transformation matrix $E_n....E_2E_1$ will represent $A^{-1}$

We can solve the equation using an augmented notation
$[A|I_n] \ldots  \ldots  [I_n|A^{-1}]$
This means that , if we bring the augmented equation system into reduced row echelon form, we can read out the inverse on the right hand side of the equation system.Hence determining the inverse of a matrix is equivalent to solving system of linear equations.

Example:
Determine the inverse of  the matrix

The augmented matrix is








Since the transformation [ A | I] → [ I | A −1] reads


So the inverse of the matrix A is



Python Program
# Importing NumPy Library
import numpy as np
import sys

# Reading order of matrix
n = int(input('Enter order of matrix: '))

# Making numpy array of n x 2n size and initializing 
# to zero for storing augmented matrix
a = np.zeros((n,2*n))

# Reading matrix coefficients
print('Enter Matrix Coefficients:')
for i in range(n):
    for j in range(n):
        a[i][j] = float(input( 'a['+str(i)+']['+ str(j)+']='))

# Augmenting Identity Matrix of Order n
for i in range(n):        
    for j in range(n):
        if i == j:
            a[i][j+n] = 1

# Applying Guass Jordan Elimination
for i in range(n):
    if a[i][i] == 0.0:
        sys.exit('Divide by zero detected!')
        
    for j in range(n):
        if i != j:
            ratio = a[j][i]/a[i][i]

            for k in range(2*n):
                a[j][k] = a[j][k] - ratio * a[i][k]

# Row operation to make principal diagonal element to 1
for i in range(n):
    divisor = a[i][i]
    for j in range(2*n):
        a[i][j] = a[i][j]/divisor

# Displaying Inverse Matrix
print('\nINVERSE MATRIX IS:')
for i in range(n):
    for j in range(n, 2*n):
        print(a[i][j], end='\t')
    print()

output

Enter order of matrix: 4
Enter Matrix Coefficients:
a[0][0]=1
a[0][1]=0
a[0][2]=2
a[0][3]=0
a[1][0]=1
a[1][1]=1
a[1][2]=0
a[1][3]=0
a[2][0]=1
a[2][1]=2
a[2][2]=0
a[2][3]=1
a[3][0]=1
a[3][1]=1
a[3][2]=1
a[3][3]=1

INVERSE MATRIX IS:
-1.0	2.0	-2.0	2.0	
1.0	-1.0	2.0	-2.0	
1.0	-1.0	1.0	-1.0	
-1.0	0.0	-1.0	2.0	

sympy code
from sympy import Matrix 
M = Matrix(4, 4, [1, 0,0, 1, 0, 2, 1, 2, 2,1,0,1,2,0,1,4]) 
print(M.inv())

Determine the inverses of the following matrix if possible ( university question)

$\begin{bmatrix}
1 & 0 & 1 & 0\\
0 & 1 & 1 & 0\\
1 & 1& 0 & 1\\
1 & 1& 1 & 0
\end{bmatrix}$


Augmented matrix $AI$
$\begin{bmatrix}
1 & 0 & 1 & 0 &|& 1 &0 & 0 & 0\\
0 & 1 & 1 & 0&|& 0 & 1 & 0 & 0\\
1 & 1& 0 & 1&| & 0 & 0 & 1 & 0\\
1 & 1& 1 & 0 &|& 0 & 0 & 0 & 1
\end{bmatrix}$


$R_3=R_3-R_1, R_4=R_4-R_1$
$\begin{bmatrix}
1 & 0 & 1 & 0 &|& 1 &0 & 0 & 0\\
0 & 1 & 1 & 0&|& 0 & 1 & 0 & 0\\
0 & 1& -1 & 1&| & -1 & 0 & 1 & 0\\
0 & 1& 0 & 0 &|& -1 & 0 & 0 & 1
\end{bmatrix}$


$R_2=R_2-R_4, R_3=R_3-R_4$
$\begin{bmatrix}
1 & 0 & 1 & 0 &|& 1 &0 & 0 & 0\\
0 & 0 & 1 & 0&|& 1 & 1 & 0 & -1\\
0 & 0& -1 & 1&| & 0 & 0 & 1 & -1\\
0 & 1& 0 & 0 &|& -1 & 0 & 0 & 1
\end{bmatrix}$


Swap $R_2$ and $R_4$$\begin{bmatrix}
1 & 0 & 1 & 0 &|& 1 &0 & 0 & 0\\
0 & 1& 0 & 0 &|& -1 & 0 & 0 & 1\\
0 & 0& -1 & 1&| & 0 & 0 & 1 & -1\\
0 & 0 & 1 & 0&|& 1 & 1 & 0 & -1\\
\end{bmatrix}$


$R_1=R_1-R_4$, $R_3=R_3+R_4$$\begin{bmatrix}
1 & 0 & 0 & 0 &|& 0 &-1 & 0 & 1\\
0 & 1& 0 & 0 &|& -1 & 0 & 0 & 1\\
0 & 0& 0 & 1&| & 1 & 1 & 1 & -2\\
0 & 0 & 1 & 0&|& 1 & 1 & 0 & -1\\
\end{bmatrix}$


swap $R_3$ and $R_4$$\begin{bmatrix}
1 & 0 & 0 & 0 &|& 0 &-1 & 0 & 1\\
0 & 1& 0 & 0 &|& -1 & 0 & 0 & 1\\
0 & 0 & 1 & 0&|& 1 & 1 & 0 & -1\\
0 & 0& 0 & 1&| & 1 & 1 & 1 & -2\\
\end{bmatrix}$


So the inverse is$\begin{bmatrix}
0 &-1 & 0 & 1\\
-1 & 0 & 0 & 1\\
1 & 1 & 0 & -1\\
1 & 1 & 1 & -2\\
\end{bmatrix}$

Determine the inverses of the following matrix using Gaussian elimination method.Show all steps( university question)

$\begin{bmatrix}
1 & 0 & 0 & 1\\
0 & 2 & 1 & 2\\
2 & 1& 0 & 1\\
2 & 0& 1 & 4
\end{bmatrix}$

Augmented matrix $AI$
$\begin{bmatrix}
1 & 0 & 0 & 1 &|& 1 &0 & 0 & 0\\
0 & 2 & 1 & 2&|& 0 & 1 & 0 & 0\\
2 & 1& 0 & 1&| & 0 & 0 & 1 & 0\\
2& 0& 1 & 4 &|& 0 & 0 & 0 & 1
\end{bmatrix}$

$R2=R2/2$
$R3=R1 \times -2 + R3$
$R4=R1 \times -2 + R4$
$\begin{bmatrix}
1 & 0 & 0 & 1 &|& 1 &0 & 0 & 0\\
0 &1 & 1/2 & 1&|& 0 & 1/2 & 0 & 0\\
0 & 1& 0 & -1&| & -2 & 0 & 1 & 0\\
0& 0& 1 & 2 &|& -2 & 0 & 0 & 1
\end{bmatrix}$


$R3=R3-R2$
$\begin{bmatrix}
1 & 0 & 0 & 1 &|& 1 &0 & 0 & 0\\
0 &1 & 1/2 & 1&|& 0 & 1/2 & 0 & 0\\
0 & 0& -1/2 & -2&| & -2 &-1/2 & 1 & 0\\
0& 0& 1 & 2 &|& -2 & 0 & 0 & 1
\end{bmatrix}$


$R3=R3 \times -2$
$\begin{bmatrix}
1 & 0 & 0 & 1 &|& 1 &0 & 0 & 0\\
0 &1 & 1/2 & 1&|& 0 & 1/2 & 0 & 0\\
0 & 0&1 & 4&| & 4 &1 & -2 & 0\\
0& 0& 1 & 2 &|& -2 & 0 & 0 & 1
\end{bmatrix}$


$R4=R4-R3$
$\begin{bmatrix}
1 & 0 & 0 & 1 &|& 1 &0 & 0 & 0\\
0 &1 & 1/2 & 1&|& 0 & 1/2 & 0 & 0\\
0 & 0&1 & 4&| & 4 &1 & -2 & 0\\
0& 0& 0 & -2 &|& -6 & -1 & 2& 1
\end{bmatrix}$


$R4=R4/-2$
$\begin{bmatrix}
1 & 0 & 0 & 1 &|& 1 &0 & 0 & 0\\
0 &1 & 1/2 & 1&|& 0 & 1/2 & 0 & 0\\
0 & 0&1 & 4&| & 4 &1 & -2 & 0\\
0& 0& 0 & 1 &|& 3 & 1/2 & -1& -1/2
\end{bmatrix}$


$R1=R1-R4$
$\begin{bmatrix}
1 & 0 & 0 & 0 &|& -2 &-1/2 & 1 & 1/2\\
0 &1 & 1/2 & 1&|& 0 & 1/2 & 0 & 0\\
0 & 0&1 & 4&| & 4 &1 & -2 & 0\\
0& 0& 0 & 1 &|& 3 & 1/2 & -1& -1/2
\end{bmatrix}$


$R2=R3 \times -1/2 +R2$
$\begin{bmatrix}
1 & 0 & 0 & 0 &|& -2 &-1/2 & 1 & 1/2\\
0 &1 & 0 & -1&|& -2& 0 & 1 & 0\\
0 & 0&1 & 4&| & 4 &1 & -2 & 0\\
0& 0& 0 & 1 &|& 3 & 1/2 & -1& -1/2
\end{bmatrix}$


$R2=R2 +R4$
$\begin{bmatrix}
1 & 0 & 0 & 0 &|& -2 &-1/2 & 1 & 1/2\\
0 &1 & 0 & 0&|& 1& 1/2 &0 & -1/2\\
0 & 0&1 & 4&| & 4 &1 & -2 & 0\\
0& 0& 0 & 1 &|& 3 & 1/2 & -1& -1/2
\end{bmatrix}$


$R3=R4 \times -4 +R3$
$\begin{bmatrix}
1 & 0 & 0 & 0 &|& -2 &-1/2 & 1 & 1/2\\
0 &1 & 0 & 0&|& 1& 1/2 &0 & -1/2\\
0 & 0&1 & 0&| & -8 &-1 & 2 & 2\\
0& 0& 0 & 1 &|& 3 & 1/2 & -1& -1/2
\end{bmatrix}$


The inverse is
$\begin{bmatrix}
-2 &-1/2 & 1 & 1/2\\
1& 1/2 &0 & -1/2\\
-8 &-1 & 2 & 2\\
3 & 1/2 & -1& -1/2
\end{bmatrix}$

Comments

Popular posts from this blog

Mathematics for Machine Learning- CST 284 - KTU Minor Notes - Dr Binu V P

  Introduction About Me Syllabus Course Outcomes and Model Question Paper Question Paper July 2021 and evaluation scheme Question Paper June 2022 and evaluation scheme Overview of Machine Learning What is Machine Learning (video) Learn the Seven Steps in Machine Learning (video) Linear Algebra in Machine Learning Module I- Linear Algebra 1.Geometry of Linear Equations (video-Gilbert Strang) 2.Elimination with Matrices (video-Gilbert Strang) 3.Solving System of equations using Gauss Elimination Method 4.Row Echelon form and Reduced Row Echelon Form -Python Code 5.Solving system of equations Python code 6. Practice problems Gauss Elimination ( contact) 7.Finding Inverse using Gauss Jordan Elimination  (video) 8.Finding Inverse using Gauss Jordan Elimination-Python code Vectors in Machine Learning- Basics 9.Vector spaces and sub spaces 10.Linear Independence 11.Linear Independence, Basis and Dimension (video) 12.Generating set basis and span 13.Rank of a Matrix 14.Linear Mapping and Matri

1.1 Solving system of equations using Gauss Elimination Method

Elementary Transformations Key to solving a system of linear equations are elementary transformations that keep the solution set the same, but that transform the equation system into a simpler form: Exchange of two equations (rows in the matrix representing the system of equations) Multiplication of an equation (row) with a constant  Addition of two equations (rows) Add a scalar multiple of one row to the other. Row Echelon Form A matrix is in row-echelon form if All rows that contain only zeros are at the bottom of the matrix; correspondingly,all rows that contain at least one nonzero element are on top of rows that contain only zeros. Looking at nonzero rows only, the first nonzero number from the left pivot (also called the pivot or the leading coefficient) is always strictly to the right of the  pivot of the row above it. The row-echelon form is where the leading (first non-zero) entry of each row has only zeroes below it. These leading entries are called pivots Example: $\begin

4.3 Sum Rule, Product Rule, and Bayes’ Theorem

 We think of probability theory as an extension to logical reasoning Probabilistic modeling  provides a principled foundation for designing machine learning methods. Once we have defined probability distributions corresponding to the uncertainties of the data and our problem, it turns out that there are only two fundamental rules, the sum rule and the product rule. Let $p(x,y)$ is the joint distribution of the two random variables $x, y$. The distributions $p(x)$ and $p(y)$ are the corresponding marginal distributions, and $p(y |x)$ is the conditional distribution of $y$ given $x$. Sum Rule The addition rule states the probability of two events is the sum of the probability that either will happen minus the probability that both will happen. The addition rule is: $P(A∪B)=P(A)+P(B)−P(A∩B)$ Suppose $A$ and $B$ are disjoint, their intersection is empty. Then the probability of their intersection is zero. In symbols:  $P(A∩B)=0$  The addition law then simplifies to: $P(A∪B)=P(A)+P(B)$  wh