Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1.  
  2. def rqi(A, x, k, verbose=False):
  3. partida = time()
  4. """
  5. Program 12.3 Rayleigh Quotient Iteration
  6. Input: matrix A, initial (nonzero) vector x, number of steps k
  7. Output: eigenvalue lam, eigenvector of inv(A-sI)
  8. """
  9. if verbose: print("Rayleigh Quotient Iteration\n%s"%('='*80))
  10. for j in range(k):
  11. u = x/np.linalg.norm(x)
  12. lam = np.dot(u.T, np.dot(A, u))
  13. try:
  14. x = solve(A -lam*np.eye(*A.shape), u)
  15. except np.linalg.LinAlgError:
  16. break
  17. if verbose: print("k=%d, lambda=%+.3f, u=%s"%(j,lam,str(u.T)))
  18. u = x/np.linalg.norm(x)
  19. lam = float(np.dot(u.T, np.dot(A, u)))
  20. if verbose: print("k=%d, lambda=%+.3f, u=%s\n"%(j+1,lam,str(u.T)))
  21. final = time()
  22. tiempo = final - partida
  23. iteracion = tiempo/k
  24. return (lam, u, iteracion)
  25.  
  26. # Probamos el algoritmo
  27. lam, v, tiempo = rqi(A, x, k=2)
  28. print("lambda = {0}".format(lam))
  29. print("v = {0}".format(v))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement