Eigenvalues and eigenvectors (eig) in SciPy - Time & Space Complexity
We want to understand how the time to find eigenvalues and eigenvectors changes as the matrix size grows.
How does the work increase when the matrix gets bigger?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.linalg import eig
n = 10 # Example size
A = np.random.rand(n, n) # Create a random n x n matrix
values, vectors = eig(A) # Compute eigenvalues and eigenvectors
This code creates a square matrix and computes its eigenvalues and eigenvectors using scipy.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Internal matrix factorizations and transformations to find eigenvalues and eigenvectors.
- How many times: These operations involve multiple passes over the n x n matrix, roughly proportional to n cubed steps.
As the matrix size n grows, the work needed grows much faster than n or n squared.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1,000 operations |
| 100 | About 1,000,000 operations |
| 1000 | About 1,000,000,000 operations |
Pattern observation: The operations grow roughly by the cube of n, so doubling n makes the work about eight times bigger.
Time Complexity: O(n^3)
This means the time to find eigenvalues and eigenvectors grows roughly with the cube of the matrix size.
[X] Wrong: "Finding eigenvalues is as fast as simple loops over the matrix, like O(n^2)."
[OK] Correct: The process involves complex matrix operations that require more work than just looking at each element once.
Knowing how eigenvalue computations scale helps you understand performance in data science tasks like PCA or stability analysis.
"What if the matrix is sparse instead of dense? How would the time complexity change?"
Practice
Solution
Step 1: Understand eigenvalues meaning
Eigenvalues measure the factor by which eigenvectors are stretched or shrunk when multiplied by the matrix.Step 2: Compare options with definition
How much eigenvectors stretch or shrink when the matrix is applied correctly describes this stretching/shrinking effect. Other options describe unrelated concepts.Final Answer:
How much eigenvectors stretch or shrink when the matrix is applied -> Option CQuick Check:
Eigenvalues = stretch/shrink factor [OK]
- Confusing eigenvalues with eigenvectors
- Thinking eigenvalues are directions
- Mixing eigenvalues with matrix sum
Solution
Step 1: Recall scipy function for eigenvalues
The function to compute eigenvalues and eigenvectors is named 'eig' and is in scipy.linalg.Step 2: Check import syntax
from scipy.linalg import eig correctly imports 'eig' from 'scipy.linalg'. Other options use incorrect module names or syntax.Final Answer:
from scipy.linalg import eig -> Option AQuick Check:
Correct import = from scipy.linalg import eig [OK]
- Using wrong module names like scipy.eigen
- Trying to import non-existent functions
- Incorrect import syntax
import numpy as np from scipy.linalg import eig A = np.array([[2, 0], [0, 3]]) values, vectors = eig(A) print(values)
Solution
Step 1: Understand matrix and eig function
Matrix A is diagonal with values 2 and 3 on the diagonal. Eigenvalues of a diagonal matrix are the diagonal elements.Step 2: Predict output of eig
scipy.linalg.eig returns eigenvalues as complex numbers, so output will be [2.+0.j 3.+0.j].Final Answer:
[2.+0.j 3.+0.j] -> Option AQuick Check:
Diagonal matrix eigenvalues = diagonal elements [OK]
- Expecting real numbers instead of complex
- Adding eigenvalues instead of listing
- Confusing eigenvalues with eigenvectors
import numpy as np from scipy.linalg import eig A = np.array([[1, 2, 3], [4, 5, 6]]) values, vectors = eig(A)
Solution
Step 1: Check matrix shape
Matrix A has shape (2, 3), which is not square (rows != columns).Step 2: Understand eig requirements
scipy.linalg.eig requires a square matrix to compute eigenvalues and eigenvectors.Final Answer:
Matrix A is not square -> Option BQuick Check:
eig needs square matrix [OK]
- Ignoring matrix shape requirement
- Assuming eig works on any matrix
- Blaming variable names or imports
B = [[4, 1], [2, 3]], which of the following is a correct eigenvalue computed using scipy.linalg.eig?Solution
Step 1: Calculate eigenvalues of B
Eigenvalues satisfy det(B - λI) = 0. For B=[[4,1],[2,3]], characteristic polynomial is (4-λ)(3-λ)-2=0 -> λ² -7λ +10=0.Step 2: Solve quadratic equation
Roots are λ = (7 ± sqrt(49 - 40))/2 = (7 ± 3)/2 -> λ=5 or λ=2.Step 3: Match options
5.0 + 0.j is 5.0 + 0.j, which matches one eigenvalue. Others do not match.Final Answer:
5.0 + 0.j -> Option DQuick Check:
Eigenvalues = 5 and 2 [OK]
- Miscomputing characteristic polynomial
- Ignoring complex number format
- Choosing wrong root from quadratic
Ready to prove it?
Pass the test and this pattern moves unknown
