Neural Mastery
← Practice
Power Iteration for Dominant Eigenpairmedium

Power Iteration for Dominant Eigenpair

medium Math for ML 15-20 min

NumPy allowed · plain Python earns 10 bonus points

Linear algebra step 1/4

A vector transforms

Matrix multiplication is the first computation behind every model.
Open lesson →
01vector
02matrix
03transform
04result
[2, −1] × [[1, 2], [−.5, 1]] = [0, −2]

Computed matrix × vector transformation

Estimate the dominant eigenvalue and eigenvector of a matrix via repeated matrix-vector multiplication.

Task

Implement power_iteration(A, num_iters=20): initialize vector b = [1.0]*n. At each iteration compute b_next = A * b and normalize b = b_next / ||b_next||. After num_iters, compute the Rayleigh quotient eigenvalue lambda = (b . A*b) / (b . b). Return {"eigenvalue": float, "eigenvector": list[float]} rounded to 4 decimals.

Function Signature

power_iteration(A: list[list[float]], num_iters: int = 20) -> dict[str, Any]

Examples

Example 1: Diagonal Matrix
Input: A = [[3, 0], [0, 1]] num_iters = 20
Output: {'eigenvalue': 3, 'eigenvector': [1, 0]}
Explanation: Diagonal matrix dominant eigenvalue is 3.0.
Example 2: Positive Symmetric
Input: A = [[2, 1], [1, 2]] num_iters = 25
Output: {'eigenvalue': 3, 'eigenvector': [0.7071, 0.7071]}
Explanation: Dominant eigenvalue 3.0 and eigenvector [0.7071, 0.7071].

Constraints

  • A is square n x n with 1 <= n <= 20.
  • num_iters >= 1.
Python3Saved ✓
def power_iteration(A: list[list[float]], num_iters: int = 20) -> dict[str, Any]:
"""Compute dominant eigenvalue and normalized eigenvector using power iteration.
Initialize with all-ones vector [1.0, ..., 1.0]. Return {'eigenvalue': float, 'eigenvector': list[float]}."""
# Your implementation here
pass

Case 1: Diagonal Matrix
Input: A = [[3, 0], [0, 1]] num_iters = 20
Expected: {'eigenvalue': 3, 'eigenvector': [1, 0]}
Case 2: Positive Symmetric
Input: A = [[2, 1], [1, 2]] num_iters = 25
Expected: {'eigenvalue': 3, 'eigenvector': [0.7071, 0.7071]}