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.
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.
-
First, we define the matrix using
np.array
:Python1import numpy as np 2 3# Defining an invertible matrix 4matrix = np.array([[4, 7], [2, 6]])
-
We now use
NumPy
'snp.linalg.inv()
function to calculate the inverse:Python1# Matrix inversion 2inverse = np.linalg.inv(matrix)
-
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:
Python1# Verifying the inversion (A * A_inv should yield the identity matrix) 2identity_check = np.matmul(matrix, inverse)
-
Finally, we can display the results:
Python1# 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.
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 text1⎡ a b ⎤ ⎡ x ⎤ ⎡ e ⎤ 2⎢ ⎥ * ⎢ ⎥ = ⎢ ⎥ 3⎣ c d ⎦ ⎣ y ⎦ ⎣ f ⎦
The solution [x, y]
can be found using the inverse of the coefficient matrix:
Python1# 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.
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.
Python1# 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.
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!