When you use SymPy's eigenvects() function on a matrix, it returns a list of tuples, where each tuple contains:
- The eigenvalue,
- The algebraic multiplicity of the eigenvalue,
- A list of eigenvectors associated with that eigenvalue.
To extract individual eigenvectors, you need to iterate through the result and access the eigenvectors in the third element of each tuple. Below is a Python script that demonstrates how to create a random 5x5 matrix, compute its eigenvectors, and extract the individual eigenvectors as SymPy Matrix objects.
Example Script
#!/usr/bin/env python3
import random
from sympy import Matrix, randMatrix
def random_matrix(rows: int,
cols: int,
min_val: int = 0,
max_val: int = 9) -> Matrix:
"""
Create a SymPy Matrix of size rows x cols with random integers in [min_val, max_val].
"""
data = [
[random.randint(min_val, max_val) for _ in range(cols)]
for _ in range(rows)
]
return Matrix(data)
if __name__ == "__main__":
# Create a random 5x5 matrix (must be square for eigenvects)
n = 5
M = random_matrix(n, n, -5, 5)
print("Matrix M:")
print(M)
# Compute eigenvalues and eigenvectors
eigenvects = M.eigenvects()
print("\nEigenvectors (raw output from eigenvects):")
print(eigenvects)
# Extract and print individual eigenvectors
print("\nIndividual eigenvectors:")
for i, (eigenval, multiplicity, evecs) in enumerate(eigenvects, 1):
print(f"\nEigenvalue {i}: {eigenval}")
print(f"Multiplicity: {multiplicity}")
for j, evec in enumerate(evecs, 1):
print(f" Eigenvector {j} (as column vector):")
print(evec)
# Example: Store eigenvectors in a list for further use
all_eigenvectors = []
for eigenval, multiplicity, evecs in eigenvects:
all_eigenvectors.extend(evecs) # Add each eigenvector to the list
print("\nAll eigenvectors stored in a list:")
for k, evec in enumerate(all_eigenvectors, 1):
print(f"Eigenvector {k}:")
print(evec)
Explanation of the Script
-
Matrix Creation:
- We create a random 5x5 matrix using the
random_matrix function. Note that eigenvects() requires a square matrix.
-
Calling eigenvects():
M.eigenvects() returns a list of tuples (eigenval, multiplicity, evecs), where:
eigenval is the eigenvalue (a SymPy expression or number),
multiplicity is the algebraic multiplicity of the eigenvalue,
evecs is a list of eigenvectors (each eigenvector is a SymPy Matrix object, typically a column vector).
-
Extracting Eigenvectors:
- We iterate over the list of tuples returned by
eigenvects().
- For each tuple, we access the list of eigenvectors (
evecs) and print or store each eigenvector individually.
- Each eigenvector is already a SymPy
Matrix object, so you can use it directly for further computations (e.g., matrix multiplication, normalization, etc.).
-
Storing Eigenvectors:
- If you want to collect all eigenvectors into a single list (e.g., for further processing), you can use a list comprehension or extend a list as shown in the script.
Example Output
Running the script might produce output like this (the exact numbers will vary due to randomness):
Matrix M:
Matrix([[3, -2, 0, 1, -4],
[1, 0, -3, 2, 1],
[-2, 1, 4, -1, 0],
[0, 2, -1, 3, -2],
[1, -1, 0, 2, -3]])
Eigenvectors (raw output from eigenvects):
[(1, 1, [Matrix([[1], [0], [1], [0], [1]])]), (2, 2, [Matrix([[0], [1], [0], [1], [0]]), Matrix([[1], [0], [0], [0], [1]])]), ...]
Individual eigenvectors:
Eigenvalue 1: 1
Multiplicity: 1
Eigenvector 1 (as column vector):
Matrix([[1], [0], [1], [0], [1]])
Eigenvalue 2: 2
Multiplicity: 2
Eigenvector 1 (as column vector):
Matrix([[0], [1], [0], [1], [0]])
Eigenvector 2 (as column vector):
Matrix([[1], [0], [0], [0], [1]])
...
All eigenvectors stored in a list:
Eigenvector 1:
Matrix([[1], [0], [1], [0], [1]])
Eigenvector 2:
Matrix([[0], [1], [0], [1], [0]])
Eigenvector 3:
Matrix([[1], [0], [0], [0], [1]])
...
Notes
Further Use
Once you have the eigenvectors as Matrix objects, you can:
- Use them in matrix operations (e.g.,
M * evec to verify they are eigenvectors),
- Normalize them,
- Use them to diagonalize the matrix (if possible),
- Or perform other linear algebra tasks.
Let me know if you need help with any specific follow-up tasks! 😊