def find_eigenvector(A, tolerance=0.00001): guess = [random.random() for _ in A] while True: result = lin_alg.matrix_operate(A, guess) length = lin_alg.magnitude(result) next_guess = lin_alg.scalar_multiply(1/length, result) if lin_alg.distance(guess, next_guess) < tolerance: return next_guess, length #eigenvector, eigenvalue guess = next_guess
def direction(w): mag = lin_alg.magnitude(w) return [w_i / mag for w_i in w]