# coding: utf-8 # # Computing the SVD # In[7]: import numpy as np import numpy.linalg as la # In[25]: np.random.seed(15) n = 5 A = np.random.randn(n, n) # Now compute the eigenvalues and eigenvectors of $A^TA$ as `eigvals` and `eigvecs` using `la.eig` or `la.eigh` (symmetric): # In[26]: eigvals, eigvecs = la.eigh(A.T.dot(A)) # In[27]: eigvals # Eigenvalues are real and positive. Coincidence? # In[28]: eigvecs.shape # Check that those are in fact eigenvectors and eigenvalues: # In[29]: B = A.T.dot(A) B - eigvecs.dot(np.diag(eigvals)).dot(la.inv(eigvecs)) # `eigvecs` are orthonormal! (Why?) # # Check: # In[30]: eigvecs.T.dot(eigvecs) - np.eye(n) # ------ # Now piece together the SVD: # In[19]: Sigma = np.diag(np.sqrt(eigvals)) # In[20]: V = eigvecs # In[38]: U = A.dot(V).dot(la.inv(Sigma)) # Check orthogonality of `U`: # In[39]: U.dot(U.T) - np.eye(n)