def dominant_eigen_system(matrix, iterations): """ Calculates the dominant eigenvalue and a dominant unit eigenvector of a symmetric matrix using the power method. Parameters ---------- matrix : numpy.ndarray A symmetric matrix. iterations : int The number of iterations of the power method. Returns ------- tuple of (Int, numpy.ndarray) Dominant eigenvalue and a dominant unit eigenvector. The list is empty if the dominant eigenvalue is zero. """ row_count, col_count = matrix_size(matrix) # Create an initial unit vector unit_vector = np.zeros([col_count, 1]) unit_vector[0, 0] = 1 # Calculate dominant unit eigenvector for _ in range(iterations): product = matrix @ unit_vector vec_length = np.linalg.norm(product) # Reached zero eigenvalue if vec_length < ZERO_NUMBER: return (0, []) unit_vector = product / vec_length # Calculate dominant eigenvalue eigenvalue = dot_product(matrix @ unit_vector, unit_vector) return (eigenvalue, unit_vector)
def test_empty(self): matrix = [] rows, columns = matrix_size(matrix) assert rows == 0 assert columns == 0
def test_column_vector(self): matrix = [[1], [2], [3]] rows, columns = matrix_size(matrix) assert rows == 3 assert columns == 1
def test_row_vector2(self): matrix = [[1, 2, 3]] rows, columns = matrix_size(matrix) assert rows == 1 assert columns == 3
def test_matrix(self): matrix = [[1, 2], [4, 5], [7, 8]] rows, columns = matrix_size(matrix) assert rows == 3 assert columns == 2