# Computing singular value decomposition

I am a bit confused with my calculation results of SVD for matrix A.

$$A= \left( \begin{array}{ccc} 4 & 2 & 1\\ 5 & 3 & -10\\ 9 & -12 & 11 \end{array} \right) \\\\ A = UDV^{T} \mbox{ for real matrix}$$

As I know that $U$ contains orthonormal eigenvectors of $AA^{T}$ and $V$ contains orthonormal eigenvectors of $A^{T}A$. Using python I have found eigenvalues and actual SVD:

import numpy as np

A = np.matrix([[4, 2, 1],
[5, 3, -10],
[9, -12, 11]])

# Calculating SVD via eigenvectors
aaeigval, aaeigvec = np.linalg.eig(A*A.T)
ateigval, ateigvec = np.linalg.eig(A.T*A)

MU = aaeigvec
MD = np.diag(np.sqrt(aaeigval))
MV = ateigvec.T

# Calculating SVD via embedded function
U, d, V = np.linalg.svd(A)
D = np.diag(d)

print("SVD MATRICES VIA NP.LINALG.SVD")
print(U)
print(D)
print(V)

print("\r\nSVD MATRICES VIA EIGENVECTORS")
print(MU)
print(MD)
print(MV)


The output is:

SVD MATRICES VIA NP.LINALG.SVD
[[-0.04227372 -0.27992453 -0.95909081]
[ 0.36803649 -0.89680917  0.24552484]
[-0.92884987 -0.34260117  0.1409339 ]]

[[ 19.67398841   0.           0.        ]
[  0.          10.02047314   0.        ]
[  0.           0.           3.67753968]]

[[-0.33996976  0.61836777 -0.70854912]
[-0.86694055  0.08591785  0.4909503 ]
[-0.36446486 -0.78117822 -0.50687863]]

SVD MATRICES VIA EIGENVECTORS
[[-0.95909081  0.27992453  0.04227372]
[ 0.24552484  0.89680917 -0.36803649]
[ 0.1409339   0.34260117  0.92884987]]

[[  3.67753968   0.           0.        ]
[  0.          10.02047314   0.        ]
[  0.           0.          19.67398841]]

[[-0.33996976  0.61836777 -0.70854912]
[ 0.86694055 -0.08591785 -0.4909503 ]
[ 0.36446486  0.78117822  0.50687863]]


As you can see some columns are flipped but that is not the problem. The problem is in different signs in columns. For instance column 1 of $U$ and column 3 of $MU$ (they have to be the same eigenvector) have different signs at their elements. Of course true decomposition is that of svd function. However I am still missing where I am wrong.

Hint: If $\mathbf{v}$ is a unit-norm eigen vector of a matrix $\mathbf{A}$ corresponding to the eigen value of $\lambda$ then $-\mathbf{v}$ is also a unit-norm eigenvector of $\mathbf{A}$. In particular, $$\mathbf{A}\mathbf{v}=\lambda \mathbf{v} \implies \mathbf{A}(-\mathbf{v})=\lambda(-\mathbf{v}).$$
• What happens if you instead of finding a solution of $x+1=0$ find a solution of $x^2-1=0$. Obviously, $x=\pm 1$ satisfy $x^2-1=0$. But, if you use only one solution $x=1$ instead of $x=-1$ then you might get an incorrect answer. Aug 14, 2017 at 17:11
• If you had computed $V^T$ as $D^{-1} U^T A$ then no matter what signs the eigen vectors had in $U$, $A=U D V^T$. Aug 14, 2017 at 17:17
• @LongSmith Just to clarify one point to you. When $A$ is fat/tall, the matrix $D$ is not square. In that case, $A U^T = D V^T$ or $A V = U D$ can be used to compute $V^T$ or $U$, respectively. Aug 15, 2017 at 16:59