Click4Ai

46.

Hard

Implement the power iteration method to find the dominant eigenvalue and its corresponding eigenvector of a matrix.

Algorithm:

1. Start with a random vector b

2. Repeat: b = A*b / ||A*b||

3. The dominant eigenvalue λ = (b^T * A * b) / (b^T * b)

Example:

Input: A = [[2, 1], [1, 2]], iterations = 100

Output: eigenvalue ≈ 3.0

**Explanation:** The dominant eigenvalue of [[2,1],[1,2]] is 3, with eigenvector [1/√2, 1/√2].

Constraints:

  • A is a square matrix with a unique dominant eigenvalue
  • Return (eigenvalue, eigenvector) tuple
  • Eigenvalue rounded to 4 decimal places
  • Test Cases

    Test Case 1
    Input: [[2, 1], [1, 2]], 100
    Expected: 3.0
    Test Case 2
    Input: [[4, 1], [2, 3]], 100
    Expected: 5.0
    Test Case 3
    Input: [[3, 0], [0, 1]], 50
    Expected: 3.0
    + 2 hidden test cases