π‘ Problem Formulation: When working with complex Hermitian or real symmetric matrices in Python, a common computation is to find their eigenvalues, which are essential for various applications in physics, engineering, and data science. An input might be a 2×2 matrix like [[2, 1], [1, 2]]
and the desired output would be the eigenvalues of this matrix, for example, [3, 1]
.
Method 1: Using NumPy’s numpy.linalg.eigh
NumPy’s numpy.linalg.eigh
function is specifically designed to compute the eigenvalues and eigenvectors of a Hermitian or symmetric real matrix. It returns two arrays: the eigenvalues and the normalized eigenvectors, and it guarantees to return the eigenvalues in ascending order.
Here’s an example:
import numpy as np # Define a symmetric matrix A = np.array([[2, 1], [1, 2]]) # Compute the eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eigh(A) print("Eigenvalues:", eigenvalues) print("Eigenvectors:\n", eigenvectors)
The output will be:
Eigenvalues: [1. 3.] Eigenvectors: [[-0.70710678 0.70710678] [ 0.70710678 0.70710678]]
This code snippet first imports NumPy, defines a simple 2×2 symmetric matrix A
, and then uses the np.linalg.eigh
function to calculate the eigenvalues and eigenvectors. The results are then printed to the console.
Method 2: Using NumPy’s numpy.linalg.eig
For General Matrices
While numpy.linalg.eig
is designed for general matrices, it can also be applied to Hermitian or symmetric matrices. It might not be as efficient as numpy.linalg.eigh
, but it’s still a reliable method to find eigenvalues.
Here’s an example:
import numpy as np # Define a symmetric matrix A = np.array([[2, 1], [1, 2]]) # Compute the eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eig(A) print("Eigenvalues:", eigenvalues) print("Eigenvectors:\n", eigenvectors)
The output will be:
Eigenvalues: [3. 1.] Eigenvectors: [[ 0.70710678 -0.70710678] [ 0.70710678 0.70710678]]
The np.linalg.eig
function is used here on the same symmetric matrix A
. It calculates both the eigenvalues and eigenvectors, which are printed to the console in the end.
Method 3: Using SciPy’s scipy.linalg.eigh
The SciPy library also has a function scipy.linalg.eigh
, similar to NumPy’s but often more efficient, especially for larger matrices. It is recommended when working with larger Hermitian or real symmetric matrices.
Here’s an example:
from scipy.linalg import eigh import numpy as np # Define a symmetric matrix A = np.array([[2, 1], [1, 2]]) # Compute the eigenvalues and eigenvectors eigenvalues, eigenvectors = eigh(A) print("Eigenvalues:", eigenvalues) print("Eigenvectors:\n", eigenvectors)
The output will be:
Eigenvalues: [1. 3.] Eigenvectors: [[-0.70710678 0.70710678] [ 0.70710678 0.70710678]]
After importing the necessary functions from SciPy and NumPy, the same 2×2 symmetric matrix is defined. The eigh
function from SciPy is then utilized to obtain the eigenvalues and eigenvectors. The results are identical to the NumPy approach but can be faster for larger matrices.
Method 4: Using SymPy for Symbolic Computation
SymPy can be used for symbolic mathematics in Python, including finding the eigenvalues symbolically. This method is useful for complex matrices where precision is crucial, or if an exact symbolic answer is required.
Here’s an example:
from sympy import Matrix # Define a symmetric matrix symbolically A = Matrix([[2, 1], [1, 2]]) # Compute the eigenvalues eigenvalues = A.eigenvals() print("Eigenvalues:", eigenvalues)
The output will be:
Eigenvalues: {3: 1, 1: 1}
Using SymPy’s Matrix
class, we define the same matrix symbolically. The eigenvals
method then provides a dictionary of eigenvalues and their multiplicities. SymPy offers exact arithmetic and manipulations, which can be critical for certain applications.
Bonus One-Liner Method 5: Lambda Function with NumPy
A quick one-liner in Python using NumPy involves defining a lambda function to encapsulate the process, making it easy to compute eigenvalues for any given matrix with minimal code.
Here’s an example:
eig = lambda A: np.linalg.eigh(A)[0] # Define a symmetric matrix A = np.array([[2, 1], [1, 2]]) # Use the lambda function to get the eigenvalues print("Eigenvalues:", eig(A))
The output will be:
Eigenvalues: [1. 3.]
The one-liner defines a lambda function eig
which takes a matrix A
, applies the NumPy function np.linalg.eigh
, and retrieves only the eigenvalues by indexing [0]
. This simplifies the process of getting eigenvalues for quick calculations.
Summary/Discussion
- Method 1: NumPy’s
numpy.linalg.eigh
. Optimal for Hermitian or symmetric matrices. Ensures ascending order of eigenvalues. Less suited for non-symmetric or non-Hermitian matrices. - Method 2: NumPy’s
numpy.linalg.eig
. General method for all kinds of matrices. Slightly less efficient for symmetric or Hermitian matrices compared tonumpy.linalg.eigh
. - Method 3: SciPy’s
scipy.linalg.eigh
. Similar to NumPy’s equivalent but often faster for larger matrices. Best suited for larger matrices due to its efficiency. - Method 4: SymPy. Provides exact symbolic results. Very useful for theoretical analysis. Not as fast as numerical methods for larger, complex matrices.
- Bonus Method 5: Lambda Function with NumPy. Convenient and quick for small tasks. Not as explicit as other methods, thus can be less readable.