Welcome to the first lesson of our course on Linear Algebra with NumPy. In this lesson, we will explore the concepts of eigenvalues and eigenvectors. These concepts play a critical role in various fields such as engineering and data science. We'll focus on applying these concepts practically, without delving into the underlying mathematical theory.
By the end of this lesson, you'll be ready to use NumPy to compute eigenvalues and eigenvectors of your matrices. Let's start delving into the process, by breaking it down into clear, manageable steps.
In linear algebra, eigenvalues and eigenvectors are central to understanding how linear transformations affect vector spaces. Eigenvectors remain in their span (retain their direction) when a transformation is applied, merely being scaled by their corresponding eigenvalue—this scalar factor determines the degree of stretching or compression. This characteristic simplifies the analysis of transformations, allowing complex matrices to be broken down into simpler components. Such decomposition is vital for grasping the core structure of a transformation, helping us to interpret its fundamental behavior without unnecessary complexity.
First, we need to define a square matrix. A square matrix has the same number of rows and columns. Here's how you do it in NumPy:
Python1import numpy as np 2 3# Defining a square matrix 4matrix = np.array([[4, 1], [2, 3]])
This code creates a 2x2 matrix with predefined values.
Next, we use NumPy's np.linalg.eig()
function to calculate the eigenvalues and eigenvectors of our matrix:
Python1eigenvalues, eigenvectors = np.linalg.eig(matrix)
This function returns two outputs: eigenvalues
and eigenvectors
. The eigenvalues represent special scalars associated with the matrix, while the eigenvectors are the vectors associated with these scalars.
Finally, let's display the results to understand what we've computed:
Python1print("Matrix:\n", matrix) 2print("Eigenvalues:", eigenvalues) 3print("Eigenvectors:\n", eigenvectors) 4 5# Output: 6# Matrix: 7# [[4 1] 8# [2 3]] 9# Eigenvalues: [5. 2.] 10# Eigenvectors: 11# [[ 0.70710678 -0.4472136 ] 12# [ 0.70710678 0.89442719]]
The output shows the original matrix, the eigenvalues, and the eigenvectors. Note how each eigenvector corresponds to an eigenvalue.
One interesting property of eigenvalues is the scaling property. When a matrix is multiplied by a scalar, its eigenvalues are also multiplied by that scalar. This property is useful, for instance, when scaling spatial transformations or in stability analysis, as it directly affects the magnitude of the transformation without altering the directionality encoded by the eigenvectors.
Let's demonstrate this property with a code block:
Python1# Calculate eigenvalues of the original matrix 2original_eigenvalues, _ = np.linalg.eig(matrix) 3print("Original eigenvalues:", original_eigenvalues) 4 5# Scale the matrix by a scalar 6scalar = 3 7scaled_matrix = scalar * matrix 8 9# Calculate eigenvalues of the scaled matrix 10scaled_eigenvalues, _ = np.linalg.eig(scaled_matrix) 11print("Scaled eigenvalues:", scaled_eigenvalues) 12 13# Output: 14# Original eigenvalues: [5. 2.] 15# Scaled eigenvalues: [15. 6.]
As illustrated in the output, when we multiply the original matrix by a scalar (3 in this case), the eigenvalues are also multiplied by the same scalar.
Now that we've computed the eigenvalues and eigenvectors, let's discuss their applications. These computations are useful in tasks like system stability analysis, where engineers may determine how systems respond to different conditions by examining these values.
Please feel free to modify the matrix and observe how the eigenvalues and eigenvectors change. Experimenting with different matrices will bolster your understanding and comfort with these concepts.
To summarize, in this lesson, we explored the use of NumPy to efficiently compute eigenvalues and eigenvectors. You learned the simple steps to define a matrix and utilize NumPy's np.linalg.eig()
function to arrive at these computations.
As you proceed to the practice exercises on CodeSignal, you'll have a chance to reinforce what you've learned today. Expect opportunities to define your own matrices and compute their eigenvalues and eigenvectors. Should you wish to venture deeper, exploring mathematical theories behind these concepts can further enhance your comprehension.
Happy coding, and enjoy your exploration into the fascinating realm of linear algebra with NumPy!