linear_algebra.src.power_iteration

Functions

power_iteration(→ tuple[float, numpy.ndarray])

Power Iteration.

test_power_iteration(→ None)

Module Contents

linear_algebra.src.power_iteration.power_iteration(input_matrix: numpy.ndarray, vector: numpy.ndarray, error_tol: float = 1e-12, max_iterations: int = 100) tuple[float, numpy.ndarray]

Power Iteration. Find the largest eigenvalue and corresponding eigenvector of matrix input_matrix given a random vector in the same space. Will work so long as vector has component of largest eigenvector. input_matrix must be either real or Hermitian.

Input input_matrix: input matrix whose largest eigenvalue we will find. Numpy array. np.shape(input_matrix) == (N,N). vector: random initial vector in same space as matrix. Numpy array. np.shape(vector) == (N,) or (N,1)

Output largest_eigenvalue: largest eigenvalue of the matrix input_matrix. Float. Scalar. largest_eigenvector: eigenvector corresponding to largest_eigenvalue. Numpy array. np.shape(largest_eigenvector) == (N,) or (N,1).

>>> import numpy as np
>>> input_matrix = np.array([
... [41,  4, 20],
... [ 4, 26, 30],
... [20, 30, 50]
... ])
>>> vector = np.array([41,4,20])
>>> power_iteration(input_matrix,vector)
(79.66086378788381, array([0.44472726, 0.46209842, 0.76725662]))
linear_algebra.src.power_iteration.test_power_iteration() None
>>> test_power_iteration()  # self running tests