Verify Eigenvector
Implement a function to verify if a given vector is an eigenvector of a matrix with a specified eigenvalue. This concept is crucial in linear algebra as eigenvectors represent directions in which a matrix transformation scales a vector by a factor, known as the eigenvalue. The equation Av=λv represents this relationship, where A is the matrix, v is the eigenvector, and λ is the eigenvalue.
To verify if v is an eigenvector of A with eigenvalue λ, we need to check if the matrix A transforms v into a scaled version of itself. Here are the steps:
- Compute the product of A and v.
- Compute the product of λ and v.
- Compare the results to determine if they are equal.
This technique is widely used in computer vision for image compression and dimensionality reduction.
Example:
is_eigenvector([[2, 0], [0, 3]], [1, 0], 2)
True
A @ [1, 0] = [2, 0] = 2 × [1, 0]
Constraints:
- A is a square matrix (n×n) where 1 ≤ n ≤ 10
- v is a non-zero vector of length n
- λ is a scalar
- Use tolerance of 1e-6 for floating-point comparison
A vector v is an eigenvector of A with eigenvalue λ exactly when multiplying A by v is the same as multiplying v by the scalar λ. In code, you just need to check whether Av equals λv (up to any tolerance if using floats).
1. Background Knowledge
- An eigenvector of a matrix A is a nonzero vector v such that applying the linear transformation A to v only scales it, not change its direction:
Here, λ is the corresponding eigenvalue.
-
If you write A as an m×n matrix and v as an n×1 vector, then Av is just standard matrix–vector multiplication. The equation A\mathbf{v}=\lambdav is a vector equality: all components must match.
-
In this problem, you are not asked to find eigenvalues or eigenvectors; you are given A, v, and λ, and you only need to verify whether the eigenvector relationship holds.
2. Algorithm / Approach
Conceptual approach:
- Compute the matrix–vector product u=Av.
- Compute the scaled vector w=\lambdav.
- Compare u and w entry by entry:
- If they are equal (or within a small tolerance for floating-point), then v is an eigenvector with eigenvalue λ.
- Otherwise, it is not.
If the problem uses integers and exact equality, you can use a simple equality check. If it uses floats, use a small tolerance (e.g., check that the absolute difference of each component is less than some epsilon).
3. Step-by-Step Strategy
- Read inputs
- Matrix A of size n×n (or possibly m×n).
- Vector v of size n.
- Scalar λ.
- Compute Av For each row i of A:
Store results in array u.
- Compute λv For each index i:
Store results in array w.
- Compare vectors
- For each index i, check if u[i] equals w[i].
- If using floats, check abs(u[i] - w[i]) <= eps for some small eps, e.g. 1e-6.
- Return / print result
- If all components match, output something like "True" / 1 / "YES" depending on the platform.
- Otherwise, output "False" / 0 / "NO".
Example skeleton (Python-like pseudocode):
def is_eigenvector(A, v, lam, eps=1e-9):
n = len(v)
# compute A v
u = [0.0] * n
for i in range(n):
for j in range(n):
u[i] += A[i][j] * v[j]
# compute λ v
w = [lam * v[i] for i in range(n)]
# compare
for i in range(n):
if abs(u[i] - w[i]) > eps:
return False
return True
4. Common Pitfalls
-
Forgetting nonzero condition: By definition, an eigenvector must be nonzero. Some problems implicitly assume the given vector is nonzero; check the statement. If not, you may need to treat the zero vector as “not an eigenvector.”
-
Dimension mismatch: Ensure the length of v matches the number of columns of A. Otherwise, Av is not defined.
-
Floating-point precision:
-
Using exact equality (==) with floats can fail due to rounding.
-
Prefer comparing with a small tolerance eps.
-
Confusing direction vs. value: Av must equal λv component-wise. It is not enough that they just “look proportional” unless you explicitly compute λv and compare.
5. Time & Space Complexity
-
Time complexity
-
Computing Av takes O(n2) for an n×n matrix.
-
Computing λv takes O(n).
-
Comparing vectors takes O(n).
-
Overall: O(n2).
-
Space complexity
-
You need space for vectors u and w, each of size n.
-
Overall auxiliary space: O(n) (besides the input storage).