Copyright (C) 2020 Andreas Kloeckner
import numpy as np
import numpy.linalg as la
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):
#clear
eigvals, eigvecs = la.eigh(A.T @ A)
eigvals
Eigenvalues are real and non-negative. Coincidence?
eigvecs.shape
Check that those are in fact eigenvectors and eigenvalues:
#clear
B = A.T @ A
B - eigvecs @ np.diag(eigvals) @ la.inv(eigvecs)
eigvecs
are orthonormal! (Why?)
Check:
#clear
la.norm(eigvecs.T @ eigvecs - np.eye(n))
Now piece together the SVD:
#clear
Sigma = np.diag(np.sqrt(eigvals))
#clear
V = eigvecs
#clear
U = A @ V @ la.inv(Sigma)
Check orthogonality of U
:
#clear
U @ U.T - np.eye(n)