Understanding Eigenvalues and Eigenvectors for Python Programming
Eigenvalues and Eigenvectors
In linear algebra, eigenvalues and eigenvectors are a special kind of scalar and vector pair associated with a square matrix. Let's say you have a square matrix A
and an eigenvector v
. If you multiply the matrix by the eigenvector, the result is the eigenvector scaled by the eigenvalue:
A * v = λ * v
Here, λ (lambda) is the eigenvalue. The key point is that the eigenvector doesn't change direction when multiplied by the matrix, it only gets stretched or shrunk by the factor of the eigenvalue.
The numpy.linalg.eig
function in Python computes the eigenvalues and eigenvectors of a square matrix. However, the eigenvalues are not necessarily returned in any specific order. Here's how to sort them together:
Import NumPy:
import numpy as np
Create a sample matrix:
A = np.array([[1, 2], [2, 1]])
eigenvalues, eigenvectors = np.linalg.eig(A)
Sort together by eigenvalues:
- Use
np.abs(eigenvalues)
to get the absolute values of the eigenvalues, as the sort order might differ for complex numbers. - Use
np.argsort
to get the indices that would sort the array. - Since we want descending order (largest to smallest eigenvalues), reverse the order using
[::-1]
. - Apply the sorting indices to both
eigenvalues
andeigenvectors
to ensure they correspond to each other after sorting.
sort_index = np.argsort(np.abs(eigenvalues))[::-1] eigenvalues = eigenvalues[sort_index] eigenvectors = eigenvectors[:, sort_index]
- Use
Print sorted results:
print("Sorted Eigenvalues:", eigenvalues) print("Sorted Eigenvectors:\n", eigenvectors)
This will output the sorted eigenvalues and the corresponding eigenvectors in the same order.
Important Note:
If you're dealing with real symmetric or complex Hermitian matrices, you can use numpy.linalg.eigh
instead of numpy.linalg.eig
. This function is specifically designed for these types of matrices and returns the eigenvalues in ascending order by default.
import numpy as np
# Sample matrix
A = np.array([[1, 2], [2, 1]])
# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
# Sort eigenvalues (absolute values for complex cases) and reverse for descending order
sort_index = np.argsort(np.abs(eigenvalues))[::-1]
# Sort both eigenvalues and eigenvectors using the same indices
sorted_eigenvalues = eigenvalues[sort_index]
sorted_eigenvectors = eigenvectors[:, sort_index]
# Print results
print("Original Eigenvalues:", eigenvalues)
print("Original Eigenvectors:\n", eigenvectors)
print("\nSorted Eigenvalues:", sorted_eigenvalues)
print("Sorted Eigenvectors:\n", sorted_eigenvectors)
Explanation:
sort_index = np.argsort(np.abs(eigenvalues))[::-1]
:np.argsort( ... )
: This function returns the indices that would sort the array in ascending order.[::-1]
: This reverses the order to sort from largest to smallest (descending order).
sorted_eigenvalues = eigenvalues[sort_index]
: This line uses the sorting indices to reorder theeigenvalues
array.sorted_eigenvectors = eigenvectors[:, sort_index]
: This line uses slicing with[:, sort_index]
to reorder the columns of theeigenvectors
array based on the same sorting indices. This ensures the sorted eigenvectors correspond to the sorted eigenvalues.
Using sorted with a custom key function:
This method uses the built-in sorted
function with a custom key function to sort the eigenvalues and eigenvectors together.
import numpy as np
def sort_by_eigenvalue(eigenvalue_eigenvector):
# Extract the eigenvalue
eigenvalue, _ = eigenvalue_eigenvector
return abs(eigenvalue)
# ... (rest of your code to compute eigenvalues and eigenvectors)
# Sort together using a custom key function
sorted_data = sorted(zip(eigenvalues, eigenvectors), key=sort_by_eigenvalue, reverse=True)
# Unpack the sorted data
sorted_eigenvalues, sorted_eigenvectors = zip(*sorted_data)
# Print results
# ...
Custom Key Function:
def sort_by_eigenvalue(eigenvalue_eigenvector):
: This defines a function that takes a tuple containing an eigenvalue and its corresponding eigenvector.eigenvalue, _ = eigenvalue_eigenvector
: This extracts the eigenvalue from the tuple and discards the eigenvector (assigned to_
).return abs(eigenvalue)
: This returns the absolute value of the eigenvalue, which will be used for sorting.
Using SciPy scipy.linalg.eigsh (for specific matrix types):
from scipy import linalg # Import SciPy linalg submodule
# ... (create your real symmetric or complex Hermitian matrix A)
# Compute eigenvalues and eigenvectors with SciPy
eigenvalues = linalg.eigsh(A)
# Sort for descending order (optional)
if desired_order == 'descending':
eigenvalues = eigenvalues[::-1]
# Eigenvectors are already returned in corresponding order
# Print results
# ...
Remember that SciPy's eigsh
is only suitable for specific matrix types. If you're dealing with general square matrices, the methods using numpy.linalg.eig
and sorting would be the appropriate choice.
python sorting numpy