I have written an algorithm to compute the SVD of a 2x2 matrix. I was checking against a Mathematica query, and I noticed that the signs in the $U$ and $V$ matrices do not match those from my algorithm. Does this matter, or is it still a valid decomposition?
I plan to use the SVD to compute the polar decomposition as well. Do the SVD signs matter for use in the polar decomposition?
Here is an outline of the algorithm:
//Compute A^T*A
a = A(0,0)*A(0,0) + A(1,0)*A(1,0)
b = A(0,1)*A(0,1) + A(1,1)*A(1,1)
c = A(0,0)*A(0,1) + A(1,0)*A(1,1)
//Compute eigenvalues
root = sqrt((a-b)*(a-b) + 4*c*c)
eig1 = (a+b+root)/2
eig2 = (a+b-root)/2
//Compute singular values for "Sigma" matrix
s1 = sqrt(eig1)
s2 = sqrt(eig2)
//Compute eigenvectors for "V" matrix
eigvec1 = (c, eig1-a)
eigvec1.normalize()
eigvec2 = (-eigvec1(1), eigvec1(0))
//Compute columns of "U" matrix
col1 = (
(A(0,0)*eigvec1(0) + A(0,1)*eigvec1(1)) / s1,
(A(1,0)*eigvec1(0) + A(1,1)*eigvec1(1)) / s1,
)
col2 = (-col1(1), col1(0))
Edit:
I just found out that this algorithm won't work with the matrix: $\begin{bmatrix}1 & 1 \\ 1 & 0\end{bmatrix}$. The $U$ and $V$ matrices are correct, except the signs. How do I correct them to give proper signs?