Guest User

Untitled

a guest
Jan 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. function lambdas=eigenValues(C,I)
  2. syms x;
  3. lambdas=sort(roots(double(fliplr(coeffs(det(C-I*x))))));
  4.  
  5. [V,D]=eig(C,I);
  6.  
  7. I =
  8.  
  9. 2 0 0
  10. 0 6 0
  11. 0 0 5
  12. C =
  13.  
  14. 4 7 0
  15. 7 8 -4
  16. 0 -4 1
  17.  
  18. [v,d]=eig(C,I)
  19.  
  20. v =
  21.  
  22. -0.3558 -0.3109 -0.5261
  23. 0.2778 0.1344 -0.2673
  24. 0.2383 -0.3737 0.0598
  25.  
  26.  
  27. d =
  28.  
  29. -0.7327 0 0
  30. 0 0.4876 0
  31. 0 0 3.7784
  32.  
  33. I=np.matrix([[2,0,0],
  34. [0,6,0],
  35. [0,0,5]])
  36.  
  37. C=np.matrix([[4,7,0],[7,8,-4],[0,-4,1]])
  38.  
  39.  
  40. np.linalg.eigh(C)
  41.  
  42. (array([-3., 1.91723747, 14.08276253]),
  43.  
  44.  
  45. matrix(
  46.  
  47. [[-0.57735027, 0.60061066, -0.55311256],
  48.  
  49. [ 0.57735027, -0.1787042 , -0.79670037],
  50.  
  51. [ 0.57735027, 0.77931486, 0.24358781]]))
  52.  
  53. # example problem
  54. >>> A = np.random.random((3, 3))
  55. >>> A = A.T @ A
  56. >>> I = np.identity(3) * np.random.random((3,))
  57.  
  58. # transform
  59. >>> J = np.sqrt(np.einsum('ii->i', I))
  60. >>> B = A / np.outer(J, J)
  61.  
  62. # solve
  63. >>> eval_, evec = np.linalg.eigh(B)
  64.  
  65. # back transform result
  66. >>> evec /= J[:, None]
  67.  
  68. # check
  69. >>> A @ evec
  70. array([[ -1.43653725e-02, 4.14643550e-01, -2.42340866e+00],
  71. [ -1.75615960e-03, -4.17347693e-01, -8.19546081e-01],
  72. [ 1.90178603e-02, 1.34837899e-01, -1.69999003e+00]])
  73. >>> eval_ * (I @ evec)
  74. array([[ -1.43653725e-02, 4.14643550e-01, -2.42340866e+00],
  75. [ -1.75615960e-03, -4.17347693e-01, -8.19546081e-01],
  76. [ 1.90178603e-02, 1.34837899e-01, -1.69999003e+00]])
  77.  
  78. >>> I=np.array([[2,0,0],[0,6,0],[0,0,5]])
  79. >>> C=np.array([[4,7,0],[7,8,-4],[0,-4,1]])
  80. >>>
  81. >>> J = np.sqrt(np.einsum('ii->i', I))
  82. >>> B = C / np.outer(J, J)
  83. >>> eval_, evec = np.linalg.eigh(B)
  84. >>> evec /= J[:, None]
  85. >>>
  86. >>> evec
  87. array([[-0.35578356, -0.31094779, -0.52605088],
  88. [ 0.27778714, 0.1343625 , -0.267297 ],
  89. [ 0.23826117, -0.37371199, 0.05975754]])
  90. >>> eval_
  91. array([-0.73271478, 0.48762792, 3.7784202 ])
  92.  
  93. eigvals, eigvecs = scipy.linalg.eigh(C, I)
  94.  
  95. >> from sympy import *
  96. >>> D = diag(2,6,5)
  97. >>> S = Matrix([[ 4, 7, 0],
  98. [ 7, 8,-4],
  99. [ 0,-4, 1]])
  100. >>> t = Symbol('t')
  101. >>> (t*D - S).det()
  102. 60*t**3 - 212*t**2 - 77*t + 81
  103.  
  104. roots = solve(60*t**3 - 212*t**2 - 77*t + 81,t)
  105. >>> roots
  106. [53/45 + (-1/2 - sqrt(3)*I/2)*(312469/182250 + sqrt(797521629)*I/16200)**(1/3) + 14701/(8100*(-1/2 - sqrt(3)*I/2)*(312469/182250 + sqrt(797521629)*I/16200)**(1/3)), 53/45 + 14701/(8100*(-1/2 + sqrt(3)*I/2)*(312469/182250 + sqrt(797521629)*I/16200)**(1/3)) + (-1/2 + sqrt(3)*I/2)*(312469/182250 + sqrt(797521629)*I/16200)**(1/3), 53/45 + 14701/(8100*(312469/182250 + sqrt(797521629)*I/16200)**(1/3)) + (312469/182250 + sqrt(797521629)*I/16200)**(1/3)]
  107.  
  108. >>> for r in roots:
  109. ... r.evalf()
  110. ...
  111. 0.487627918145732 + 0.e-22*I
  112. -0.73271478047926 - 0.e-22*I
  113. 3.77842019566686 - 0.e-21*I
Add Comment
Please, Sign In to add comment