Lesson 2
Diagonalization of Matrices with NumPy
Introduction to Diagonalization and NumPy

In this lesson, we will explore the diagonalization of matrices using NumPy. Diagonalization is a powerful technique that allows us to simplify complex matrix operations by converting a matrix into a diagonal form — but don't worry, we aren't covering the intricate mathematics behind it. Instead, we'll show you how to use NumPy to achieve this.

Quick Recap: Eigenvalues and Eigenvectors

As you may remember from the previous lesson, eigenvalues and eigenvectors play a crucial role in diagonalizing matrices. In simple terms, when you multiply a matrix by one of its eigenvectors, the result is just the eigenvector multiplied by a corresponding scalar value known as the eigenvalue. This property forms the backbone of the diagonalization process.

It's important to recall that the NumPy function np.linalg.eig() is used to compute eigenvalues and eigenvectors of a square matrix. We'll use this function extensively in the upcoming example, so having this context will be helpful.

Calculate Eigenvalues and Eigenvectors

Let's explore the steps to diagonalize a matrix using NumPy. First, let's calculate the eigenvalues and eigenvectors.

Python
1import numpy as np 2 3# Defining a square matrix 4matrix = np.array([[4, 1], [2, 3]]) 5 6# Eigenvalues and eigenvectors for diagonalization 7eigenvalues, eigenvectors = np.linalg.eig(matrix) 8 9# Print the results 10print("Eigenvalues:", eigenvalues) 11print("Eigenvectors:\n", eigenvectors) 12 13# Output: 14# Eigenvalues: [5. 2.] 15# Eigenvectors: 16# [[ 0.70710678 -0.4472136 ] 17# [ 0.70710678 0.89442719]]
  • Defining a Square Matrix:

    • A 2x2 matrix is defined using np.array. This matrix is used as the input for diagonalization: matrix = np.array([[4, 1], [2, 3]]).
  • Calculating Eigenvalues and Eigenvectors:

    • The NumPy function np.linalg.eig(matrix) calculates the eigenvalues and eigenvectors of the matrix: eigenvalues, eigenvectors = np.linalg.eig(matrix).
    • Eigenvalues will be used to form the diagonal matrix, and eigenvectors will assist in reconstructing the original matrix.
Form a Diagonal Matrix

Now, let's form the diagonal matrix with eigenvalues.

Python
1# Forming a diagonal matrix with eigenvalues 2diagonal_matrix = np.diag(eigenvalues) 3print("Diagonal Matrix:\n", diagonal_matrix) 4 5# Output: 6# Diagonal Matrix: 7# [[5. 0.] 8# [0. 2.]]
  • A diagonal matrix is created using the eigenvalues with np.diag(eigenvalues): diagonal_matrix = np.diag(eigenvalues).
  • This matrix contains the eigenvalues on its diagonal, with all other entries being zero.
Reconstruct the Original Matrix

Let's reconstract the original matrix and compare. The reconstructed matrix should be equal (or very close) to the original matrix.

Python
1# Reconstructing the original matrix 2reconstructed_matrix = eigenvectors @ diagonal_matrix @ np.linalg.inv(eigenvectors) 3 4# Display results 5print("Original Matrix:\n", matrix) 6print("Reconstructed Matrix:\n", reconstructed_matrix) 7 8# Output: 9# Original Matrix: 10# [[4 1] 11# [2 3]] 12# Reconstructed Matrix: 13# [[4. 1.] 14# [2. 3.]]
  • The original matrix is recovered using the formula A = PDP^(-1), where P is the matrix of eigenvectors: reconstructed_matrix = eigenvectors @ diagonal_matrix @ np.linalg.inv(eigenvectors).
  • The @ symbol is used for matrix multiplication.
Common Mistakes and Tips

When working with diagonalization, here are some common mistakes to watch out for:

  • Misidentifying Eigenvectors: Ensure that eigenvalues and their corresponding eigenvectors are correctly associated. They should be paired precisely as outputted by np.linalg.eig().
  • Matrix Multiplication Order: Be mindful of the order of multiplication when reconstructing the matrix using PDP^(-1), where P is the matrix of eigenvectors, D is the diagonal matrix of eigenvalues, and P^(-1) is the inverse of the matrix of eigenvectors.

Tips:

  • Always verify that the reconstructed matrix approximates the original matrix closely, considering floating-point arithmetic.
  • When troubleshooting, reassess your matrix definitions and transformations.
Summary and Preparation for Practice

In this lesson, you learned how to diagonalize a matrix using NumPy by calculating eigenvalues and eigenvectors and employing them to form a diagonal matrix and reconstruct the original matrix. These techniques simplify matrix operations and are crucial for more advanced linear algebra applications.

As you move on to the practice exercises that follow, apply these concepts to different matrices. Experiment with varying sizes and elements to solidify your understanding.

Congratulations on reaching the last lesson of this course! You've gained valuable skills in using NumPy for matrix operations and linear algebra tasks. With these tools, you are well-prepared to tackle more complex problems with confidence.

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