import numpy as np
import numpy.linalg as la
m = 4
n = m
A = np.random.randn(m, n)
print(A)
U, S, Vt = la.svd(A)
print(U)
print(U.shape)
print(Vt)
print(Vt.T.shape)
print(S)
print(S.shape)
Now compute the eigenvalues and eigenvectors of ATA as eigvals
and eigvecs
eigvals, eigvecs = la.eig(A.T.dot(A))
Eigenvalues are real and positive. Coincidence?
eigvals
eigvecs
are orthonormal! Check:
eigvecs.T @ eigvecs
Now piece together the SVD:
S2 = np.sqrt(eigvals)
V2 = eigvecs
U2 = A @ V2 @ la.inv(np.diag(S2))
m = 3
n = 5
A = np.random.randn(m, n)
print(A)
U, S, Vt = la.svd(A,full_matrices=True)
print(U)
print(U.shape)
print(Vt)
print(Vt.T.shape)
print(S)
print(S.shape)
eigvals, eigvecs = la.eig(A.T.dot(A))
print(eigvals)