def find_eigenvector(A, tolerance=0.00001): guess = [1 for __ in A] while True: result = matrix_operate(A, guess) length = magnitude(result) next_guess = scalar_multiply(1 / length, result) if distance(guess, next_guess) < tolerance: return next_guess, length # eigenvector, eigenvalue guess = next_guess
def find_eigenvector(A, tolerance=0.00001): guess = [1 for __ in A] while True: result = matrix_operate(A, guess) length = magnitude(result) next_guess = scalar_multiply(1/length, result) if distance(guess, next_guess) < tolerance: return next_guess, length # eigenvector, eigenvalue guess = next_guess
def find_eigenvector(A, tolerance=0.00001): guess = [1 for __ in A] while True: # 计算结果向量 result = matrix_operate(A, guess) # 向量的模 length = magnitude(result) # 下一个向量,标量(1/length)和向量(result)的乘法, next_guess = scalar_multiply(1/length, result) # 两个向量的距离小于某个阙值则返回更新后的向量和向量的模 if distance(guess, next_guess) < tolerance: return next_guess, length # eigenvector, eigenvalue guess = next_guess
def direction(w): mag = magnitude(w) return [w_i / mag for w_i in w]
C = la.vector_sum([A, B]) print("A and B summary = ", C) C = la.scalar_multiply(10, A) print("10 * A = ", C) C = la.vector_mean([A, B]) print("A and B mean = ", C) C = la.dot(A, B) print("A dot B = ", C) C = la.sum_of_squares(A) print("A^2's summary = ", C) C = la.magnitude(A) print("A's magnitude = ", C) C = la.distance(A, B) print("A's distance = ", C) print() print("*** matrix ......") M = [[1, 2, 3], [5, 6, 7], [3, 6, 9]] print("M = ", M) shape = la.shape(M) print("M's shape = ", shape) row_1 = la.get_row(M, 1) print("M[1,:] = ", row_1)
def direction(w: Vector) -> Vector: """ Find direction vector""" mag = magnitude(w) return [w_i / mag for w_i in w]
def test_magnitude(self): self.assertEqual(5, magnitude([4, 3]))