Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.36 KB | None | 0 0
  1. def power_method(U_hat, U, tol=1e-4):
  2.     n, _ = U.shape
  3.     x = np.random.normal(size=(n,))
  4.     s = np.linalg.norm(x)
  5.     x = x / s
  6.     while True:
  7.         x = np.dot(U_hat, np.dot(U_hat.T, x)) - np.dot(U, np.dot(U.T, x))
  8.         s_new = np.linalg.norm(x)
  9.         if np.abs(s - s_new) < tol:
  10.             break
  11.         s = s_new
  12.         x = x / s
  13.     return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement