Lesson 5
Matrix Inversion and Applications with NumPy
Introduction to Matrix Inversion

Welcome back! As we continue our journey through linear algebra with NumPy, we come to an essential concept: matrix inversion. Matrix inversion plays a crucial role in solving systems of linear equations and in various applications across science and engineering.

Matrix Inversion with NumPy: Step-by-Step Example

Before diving into code, it is important to remember what an invertible matrix is. An invertible matrix, also known as a non-singular matrix, is a square matrix that has an inverse. Only square matrices (matrices with the same number of rows and columns) that have non-zero determinants are invertible. This property is crucial for applications such as solving systems of linear equations, which we will explore soon.

Let's walk through the process of inverting a matrix using NumPy. We'll demonstrate this with a simple 2x2 matrix.

  1. First, we define the matrix using np.array:

    Python
    1import numpy as np 2 3# Defining an invertible matrix 4matrix = np.array([[4, 7], [2, 6]])
  2. We now use NumPy's np.linalg.inv() function to calculate the inverse:

    Python
    1# Matrix inversion 2inverse = np.linalg.inv(matrix)
  3. To verify the inversion, we multiply the original matrix by its inverse. The result should be the identity matrix, showing that the inversion was successful:

    Python
    1# Verifying the inversion (A * A_inv should yield the identity matrix) 2identity_check = np.matmul(matrix, inverse)
  4. Finally, we can display the results:

    Python
    1# Display results 2print("Matrix:\n", matrix) 3print("Inverse:\n", inverse) 4print("Identity Check (A * A_inv):\n", identity_check) 5 6# Output: 7# Matrix: 8# [[4 7] 9# [2 6]] 10# Inverse: 11# [[ 0.6 -0.7] 12# [-0.2 0.4]] 13# Identity Check (A * A_inv): 14# [[1.00000000e+00 -1.11022302e-16] 15# [1.11022302e-16 1.00000000e+00]]

Each step above confirms the validity of the inverse. Note that the identity matrix, while expected to be exact, may show very small numerical errors due to floating-point precision, as shown above.

Application Example: Solving a System of Linear Equations

Matrix inversion has significant practical applications. One of the classic uses is solving a system of linear equations. In this example, a, b, c, d, e, and f are known values, while x and y are the unknowns that we want to solve for. Consider the following system of equations:

Plain text
1⎡ a b ⎤ ⎡ x ⎤ ⎡ e ⎤ 2⎢ ⎥ * ⎢ ⎥ = ⎢ ⎥ 3⎣ c d ⎦ ⎣ y ⎦ ⎣ f ⎦

The solution [x, y] can be found using the inverse of the coefficient matrix:

Python
1# Example system of equations 2coeff_matrix = np.array([[4, 7], [2, 6]]) # [a, b], [c, d] = [4, 7], [2, 6] 3constants = np.array([1, 1]) # [e, f] = [1, 1] 4 5# Solving for [x, y] 6solution = np.linalg.inv(coeff_matrix).dot(constants) 7print("Solution [x, y]:", solution) 8 9# Output: 10# Solution [x, y]: [-3. 2.]

Here, the matrix inversion helps us find the values of (x) and (y) that satisfy both equations.

Common Errors and Troubleshooting

When working with matrix inversion, one common error is trying to invert a non-invertible matrix. This typically raises a LinAlgError in NumPy. To avoid this, you can check the determinant of the matrix before attempting inversion. If the determinant is zero, the matrix is not invertible.

Python
1# Checking if a matrix is invertible 2if np.linalg.det(matrix) == 0: 3 print("Matrix is not invertible.") 4else: 5 print("Matrix is invertible.")

Always ensure compatibility with the dimensions and properties of matrices to prevent errors.

Review, Practice Preparation, and Course Completion

In this lesson, you learned how to perform matrix inversion using NumPy, applied it to solve real-world problems, and understood the importance of matrix properties. As you transition to the practice exercises, remember to apply these principles and concepts to reinforce your knowledge.

Congratulations on reaching the end of this course! Mastering these skills with NumPy enhances your ability to tackle various linear algebra problems efficiently. You now have a strong foundation in vector and matrix operations, ready to tackle complex challenges in data science and engineering. Well done!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.