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 University Question Papers and Evaluation Scheme -Mathematics for Machine learning CST 284 KTU 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...

Vectors in Machine Learning

As data scientists we work with data in various formats such as text images and numerical values We often use vectors to represent data in a structured and efficient manner especially in machine learning applications In this blog post we will explore what vectors are in terms of machine learning their significance and how they are used What is a Vector? In mathematics, a vector is a mathematical object that has both magnitude and direction. In machine learning, a vector is a mathematical representation of a set of numerical values. Vectors are usually represented as arrays or lists of numbers, and each number in the list represents a specific feature or attribute of the data. For example, suppose we have a dataset of houses, and we want to predict their prices based on their features such as the number of bedrooms, the size of the house, and the location. We can represent each house as a vector, where each element of the vector represents a specific feature of the house, such as the nu...

2.14 Singular Value Decomposition

The Singular Value Decomposition ( SVD) of a matrix is a central matrix decomposition method in linear algebra.It can be applied to all matrices,not only to square matrices and it always exists.It has been referred to as the 'fundamental theorem of linear algebra'( strang 1993). SVD Theorem: Let $A^{m \times n}$ be a rectangular matrix of rank $r \in [0,min(m,n)]$. The SVD of A is a decomposition of the form. $A= U \Sigma V^T $ with an orthogonal matrix $U \in \mathbb{R}^{m \times m}$ with column vectors $u_i, i=1,\ldots,m$ and an orthogonal matrix $V \in \mathbb{R}^{n \times n}$ with column vectors $v_j, j=1,\ldots,n$.Moreover, $\Sigma$ is an $m \times n$ matrix with $\sum_{ii} = \sigma \ge 0$ and $\sigma_{ij}=0, i \ne j$. The diagonal entries $\Sigma_i=1,\ldots,r$ of $\sigma$ are called singular values . $u_i$ are called left singular vectors , and $v_j$ are called right singular vectors .By convention singular values are ordered ie; $\sigma_1 \ge \sigma_2 \ldots \sigma_r \...