I had a linear algebra assignment on showing the relation between the left singular vectors of mxn matrix A
and that of mxm [email protected]
. By plugging in A = U S VT
we can prove that [email protected] = U S**2 U.T
, meaning that the left singular vectors of [email protected]
are the same as those of A
. However I can not verify it in numpy.
Here is the code I had
m, n = 100, 85
for _ in range(10):
A = np.random.randint(0, 100, (m, n))
AAT = A @ A.T
u, *_ = np.linalg.svd(A)
u2, *_ = np.linalg.svd(AAT)
assert np.allclose(np.abs(u), np.abs(u2))
Yet I can not pass the assert test. I am aware that SVD is not unique but I wonder what is the best way to verify the result.
CodePudding user response:
The last n - m columns of U form a basis of the nullspace of A^T. A^T has the same nullspace as AA^T, so the last n - m columns of U_2 form a basis of the same space. Maybe there's a better way to show this, but here's one:
>>> np.linalg.matrix_rank(np.hstack((u[:, n:], u2[:, n:])), 1e-10)
15 # m - n
Those last columns don't need to be any particular basis vectors as long as they span the correct space, so one shouldn't expect them to be equal.