def find_eigenvector(m: Matrix, tolerance: float = 0.00001) -> Tuple[Vector, float]: guess = [random.random() for _ in m] while True: result = matrix_times_vector(m, guess) # transform guess norm = magnitude(result) # compute norm next_guess = [x / norm for x in result] # rescale if distance(guess, next_guess) < tolerance: # convergence so return (eigenvector, eigenvalue) return next_guess, norm guess = next_guess
def find_eigenvector(m: Matrix, tolerance: float = 0.00001) -> Tuple[Vector, float]: guess = [random.random() for _ in m] while True: result = matrix_times_vector(m, guess) # przekształcenie wartości losowej norm = magnitude(result) # wyliczenie współczynnika normalizującego next_guess = [x / norm for x in result] # przeskalowanie if distance(guess, next_guess) < tolerance: # punkt zbieżności, więc zwracamy (wektor własny, wartość własna) return next_guess, norm guess = next_guess
def direction(w: Vector) -> Vector: mag = magnitude(w) return [w_i / mag for w_i in w]