Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Identity and Inverse Matrices

Source
Source
Output
Populating the interactive namespace from numpy and matplotlib
Source

This chapter is light but contains some important definitions. The identity matrix or the inverse of a matrix are concepts that will be very useful in the next chapters. We will see at the end of this chapter that we can solve systems of linear equations by using the inverse matrix. So hang on!

Identity matrices

The identity matrix In\boldsymbol{I}_n is a special matrix of shape (n×nn \times n) that is filled with 0 except the diagonal that is filled with 1.

Example of an identity matrix
  • A 3 by 3 identity matrix

An identity matrix can be created with the Numpy function eye():

array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])

When ‘apply’ the identity matrix to a vector the result is this same vector:

Inx=x\boldsymbol{I}_n\boldsymbol{x} = \boldsymbol{x}

Example 1.

[100010001]×[x1x2x3]=[1×x1+0×x2+0×x30×x1+1×x2+0×x30×x1+0×x2+1×x3]=[x1x2x3]\begin{bmatrix} 1 & 0 & 0 \\\\ 0 & 1 & 0 \\\\ 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix} x_{1} \\\\ x_{2} \\\\ x_{3} \end{bmatrix}= \begin{bmatrix} 1 \times x_1 + 0 \times x_2 + 0\times x_3 \\\\ 0 \times x_1 + 1 \times x_2 + 0\times x_3 \\\\ 0 \times x_1 + 0 \times x_2 + 1\times x_3 \end{bmatrix}= \begin{bmatrix} x_{1} \\\\ x_{2} \\\\ x_{3} \end{bmatrix}
array([[2], [6], [3]])
array([[ 2.], [ 6.], [ 3.]])

Intuition

You can think of a matrix as a way to transform objects in a nn-dimensional space. It applies a linear transformation of the space. We can say that we apply a matrix to an element: this means that we do the dot product between this matrix and the element. We will see this notion thoroughly in the next chapters but the identity matrix is a good first example. It is a particular example because the space doesn’t change when we apply the identity matrix to it.

Note: The space doesn’t change when we apply the identity matrix to it. We saw that x\boldsymbol{x} was not altered after being multiplied by I\boldsymbol{I}.

Inverse Matrices

The matrix inverse of A\boldsymbol{A} is denoted A−1\boldsymbol{A}^{-1}. It is the matrix that results in the identity matrix when it is multiplied by A\boldsymbol{A}:

A−1A=In\boldsymbol{A}^{-1}\boldsymbol{A}=\boldsymbol{I}_n

This means that if we apply a linear transformation to the space with A\boldsymbol{A}, it is possible to go back with A−1\boldsymbol{A}^{-1}. It provides a way to cancel the transformation.

Example 2.

A=[30220−2011]\boldsymbol{A}=\begin{bmatrix} 3 & 0 & 2 \\\\ 2 & 0 & -2 \\\\ 0 & 1 & 1 \end{bmatrix}

For this example, we will use the Numpy function linalg.inv() to calculate the inverse of A\boldsymbol{A}. Let’s start by creating A\boldsymbol{A}:

array([[ 3, 0, 2], [ 2, 0, -2], [ 0, 1, 1]])

Now we calculate its inverse:

array([[ 0.2, 0.2, 0. ], [-0.2, 0.3, 1. ], [ 0.2, -0.3, -0. ]])

We can check that Ainv\boldsymbol{A_{inv}} is well the inverse of A\boldsymbol{A} with Python:

array([[ 1., 0., -0.], [ 0., 1., -0.], [ 0., 0., 1.]])

We will see that inverse of matrices can be very usefull, for instance to solve a set of linear equations. We must note however that non square matrices (matrices with more columns than rows or more rows than columns) don’t have inverse.

Sovling a system of linear equations

An introduction on system of linear equations can be found in previous section.

The inverse matrix can be used to solve the equation Ax=b\boldsymbol{Ax}=\boldsymbol{b} by adding it to each term:

A−1Ax=A−1b\boldsymbol{A}^{-1}\boldsymbol{Ax}=\boldsymbol{A}^{-1}\boldsymbol{b}

Since we know by definition that A−1A=I\boldsymbol{A}^{-1}\boldsymbol{A}=\boldsymbol{I}, we have:

Inx=A−1b\boldsymbol{I}_n\boldsymbol{x}=\boldsymbol{A}^{-1}\boldsymbol{b}

We saw that a vector is not changed when multiplied by the identity matrix. So we can write:

x=A−1b\boldsymbol{x}=\boldsymbol{A}^{-1}\boldsymbol{b}

This is great! We can solve a set of linear equation just by computing the inverse of A\boldsymbol{A} and apply this matrix to the vector of results b\boldsymbol{b}!

Let’s try that!

Example 3.

We will take a simple solvable example:

{y=2xy=−x+3\begin{cases} y = 2x \\\\ y = -x +3 \end{cases}

We will use the notation that we saw in previous section:

{A1,1x1+A1,2x2=b1A2,1x1+A2,2x2=b2\begin{cases} A_{1,1}x_1 + A_{1,2}x_2 = b_1 \\\\ A_{2,1}x_1 + A_{2,2}x_2= b_2 \end{cases}

Here, x1x_1 corresponds to xx and x2x_2 corresponds to yy. So we have:

{2x1−x2=0x1+x2=3\begin{cases} 2x_1 - x_2 = 0 \\\\ x_1 + x_2= 3 \end{cases}

Our matrix A\boldsymbol{A} of weights is:

A=[2−111]\boldsymbol{A}= \begin{bmatrix} 2 & -1 \\\\ 1 & 1 \end{bmatrix}

And the vector b\boldsymbol{b} containing the solutions of individual equations is:

b=[03]\boldsymbol{b}= \begin{bmatrix} 0 \\\\ 3 \end{bmatrix}

Under the matrix form, our systems becomes:

[2−111][x1x2]=[03]\begin{bmatrix} 2 & -1 \\\\ 1 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\\\ x_2 \end{bmatrix}= \begin{bmatrix} 0 \\\\ 3 \end{bmatrix}

Let’s find the inverse of A\boldsymbol{A}:

array([[ 2, -1], [ 1, 1]])
array([[ 0.33333333, 0.33333333], [-0.33333333, 0.66666667]])

We have also:

Since we saw that

x=A−1b\boldsymbol{x}=\boldsymbol{A}^{-1}\boldsymbol{b}

We have:

array([[ 1.], [ 2.]])

This is our solution!

x=[12]\boldsymbol{x}= \begin{bmatrix} 1 \\\\ 2 \end{bmatrix}

This means that the point of coordinates (1, 2) is the solution and is at the intersection of the lines representing the equations. Let’s plot them to check this solution:

<matplotlib.figure.Figure at 0x108c19d90>

We can see that the solution (corresponding to the line crossing) is when x=1x=1 and y=2y=2. It confirms what we found with the matrix inversion!

BONUS: Coding tip - Draw an equation

To draw the equation with Matplotlib, we first need to create a vector with all the xx values. Actually, since this is a line, only two points would have been sufficient. But with more complex functions, the length of the vector xx corresponds to the sampling rate. So here we used the Numpy function arrange() (see the doc) to create a vector from -10 to 10 (not included).

array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

The first argument is the starting point and the second the ending point. You can add a third argument to specify the step:

array([-10, -8, -6, -4, -2, 0, 2, 4, 6, 8])

Then we create a second vector yy that depends on the xx vector. Numpy will take each value of xx and apply the equation formula to it.

array([-19, -17, -15, -13, -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])

Finally, you just need to plot these vectors.

Singular matrices

Some matrices are not invertible. They are called singular.

Conclusion

This introduces different cases according to the linear system because A−1\boldsymbol{A}^{-1} exists only if the equation Ax=b\boldsymbol{Ax}=\boldsymbol{b} has one and only one solution. The next chapter is almost all about systems of linear equations and number of solutions.