Guest User

Untitled

a guest
Dec 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "## The SVD decomposition of a symmetric matrix ##"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "Let S be a symmetric matrix:"
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 1,
  20. "metadata": {},
  21. "outputs": [],
  22. "source": [
  23. "import numpy as np"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 2,
  29. "metadata": {},
  30. "outputs": [],
  31. "source": [
  32. "S=np.array([[2, -1, 3], [-1, 2, -1], [3, -1, 2]])"
  33. ]
  34. },
  35. {
  36. "cell_type": "code",
  37. "execution_count": 3,
  38. "metadata": {},
  39. "outputs": [],
  40. "source": [
  41. "D, P=np.linalg.eigh(S)"
  42. ]
  43. },
  44. {
  45. "cell_type": "code",
  46. "execution_count": 4,
  47. "metadata": {},
  48. "outputs": [
  49. {
  50. "data": {
  51. "text/plain": [
  52. "array([-1. , 1.43844719, 5.56155281])"
  53. ]
  54. },
  55. "execution_count": 4,
  56. "metadata": {},
  57. "output_type": "execute_result"
  58. }
  59. ],
  60. "source": [
  61. "D"
  62. ]
  63. },
  64. {
  65. "cell_type": "code",
  66. "execution_count": 5,
  67. "metadata": {},
  68. "outputs": [
  69. {
  70. "data": {
  71. "text/plain": [
  72. "array([[ 7.07106781e-01, 2.60956474e-01, -6.57192300e-01],\n",
  73. " [ 2.08159786e-16, 9.29410263e-01, 3.69048184e-01],\n",
  74. " [-7.07106781e-01, 2.60956474e-01, -6.57192300e-01]])"
  75. ]
  76. },
  77. "execution_count": 5,
  78. "metadata": {},
  79. "output_type": "execute_result"
  80. }
  81. ],
  82. "source": [
  83. "P"
  84. ]
  85. },
  86. {
  87. "cell_type": "markdown",
  88. "metadata": {},
  89. "source": [
  90. "$PDP^T$:"
  91. ]
  92. },
  93. {
  94. "cell_type": "code",
  95. "execution_count": 6,
  96. "metadata": {},
  97. "outputs": [
  98. {
  99. "data": {
  100. "text/plain": [
  101. "array([[ 2., -1., 3.],\n",
  102. " [-1., 2., -1.],\n",
  103. " [ 3., -1., 2.]])"
  104. ]
  105. },
  106. "execution_count": 6,
  107. "metadata": {},
  108. "output_type": "execute_result"
  109. }
  110. ],
  111. "source": [
  112. "np.dot(P, np.dot(np.diag(D), P.T))"
  113. ]
  114. },
  115. {
  116. "cell_type": "markdown",
  117. "metadata": {},
  118. "source": [
  119. "Singular decomposition of the same matrix:"
  120. ]
  121. },
  122. {
  123. "cell_type": "code",
  124. "execution_count": 7,
  125. "metadata": {},
  126. "outputs": [],
  127. "source": [
  128. "U, Sigma, Vt =np.linalg.svd(S)"
  129. ]
  130. },
  131. {
  132. "cell_type": "code",
  133. "execution_count": 8,
  134. "metadata": {},
  135. "outputs": [
  136. {
  137. "data": {
  138. "text/plain": [
  139. "array([5.56155281, 1.43844719, 1. ])"
  140. ]
  141. },
  142. "execution_count": 8,
  143. "metadata": {},
  144. "output_type": "execute_result"
  145. }
  146. ],
  147. "source": [
  148. "Sigma"
  149. ]
  150. },
  151. {
  152. "cell_type": "markdown",
  153. "metadata": {},
  154. "source": [
  155. "Comparing $\\Sigma$ to D we notice that $\\Sigma=\\pi (|D|)$, where $\\pi$ is the permutation $\\pi(0)=2, \\pi(1)=1, \\pi(2)=0$,\n",
  156. "i.e. $\\pi(|\\lambda_0|)=\\sigma_2$, etc"
  157. ]
  158. },
  159. {
  160. "cell_type": "code",
  161. "execution_count": 9,
  162. "metadata": {},
  163. "outputs": [
  164. {
  165. "data": {
  166. "text/plain": [
  167. "array([[-6.57192300e-01, 2.60956474e-01, 7.07106781e-01],\n",
  168. " [ 3.69048184e-01, 9.29410263e-01, -1.59154867e-16],\n",
  169. " [-6.57192300e-01, 2.60956474e-01, -7.07106781e-01]])"
  170. ]
  171. },
  172. "execution_count": 9,
  173. "metadata": {},
  174. "output_type": "execute_result"
  175. }
  176. ],
  177. "source": [
  178. "U"
  179. ]
  180. },
  181. {
  182. "cell_type": "markdown",
  183. "metadata": {},
  184. "source": [
  185. "The columns in U are the column in P permuted by $\\pi$."
  186. ]
  187. },
  188. {
  189. "cell_type": "code",
  190. "execution_count": 10,
  191. "metadata": {},
  192. "outputs": [
  193. {
  194. "data": {
  195. "text/plain": [
  196. "array([[-6.57192300e-01, 3.69048184e-01, -6.57192300e-01],\n",
  197. " [ 2.60956474e-01, 9.29410263e-01, 2.60956474e-01],\n",
  198. " [-7.07106781e-01, -3.96348516e-16, 7.07106781e-01]])"
  199. ]
  200. },
  201. "execution_count": 10,
  202. "metadata": {},
  203. "output_type": "execute_result"
  204. }
  205. ],
  206. "source": [
  207. "Vt"
  208. ]
  209. },
  210. {
  211. "cell_type": "markdown",
  212. "metadata": {},
  213. "source": [
  214. "`Vt=U^T`"
  215. ]
  216. },
  217. {
  218. "cell_type": "code",
  219. "execution_count": 11,
  220. "metadata": {},
  221. "outputs": [
  222. {
  223. "data": {
  224. "text/plain": [
  225. "array([[-6.57192300e-01, 3.69048184e-01, -6.57192300e-01],\n",
  226. " [ 2.60956474e-01, 9.29410263e-01, 2.60956474e-01],\n",
  227. " [ 7.07106781e-01, -1.59154867e-16, -7.07106781e-01]])"
  228. ]
  229. },
  230. "execution_count": 11,
  231. "metadata": {},
  232. "output_type": "execute_result"
  233. }
  234. ],
  235. "source": [
  236. "U.T"
  237. ]
  238. },
  239. {
  240. "cell_type": "code",
  241. "execution_count": null,
  242. "metadata": {},
  243. "outputs": [],
  244. "source": []
  245. }
  246. ],
  247. "metadata": {
  248. "kernelspec": {
  249. "display_name": "Python 3",
  250. "language": "python",
  251. "name": "python3"
  252. },
  253. "language_info": {
  254. "codemirror_mode": {
  255. "name": "ipython",
  256. "version": 3
  257. },
  258. "file_extension": ".py",
  259. "mimetype": "text/x-python",
  260. "name": "python",
  261. "nbconvert_exporter": "python",
  262. "pygments_lexer": "ipython3",
  263. "version": "3.6.4"
  264. }
  265. },
  266. "nbformat": 4,
  267. "nbformat_minor": 2
  268. }
Add Comment
Please, Sign In to add comment