I was also tricked by my lecturer. I also thought that SVD was super simple and everything. But eigenvectors are not unique. Even when normalized, each column can be multiplied by a -1 to give the same answer.
The reason why your SVD does not work is that U and V are interlinked in that their column signs need to work together in some way. Doing separated diagonalizations ignores this inherent dependence and you are left with an incorrect answer most of the time.
See this Maths Exchange question and many others for similar explanations.
The following does what is is said in that Maths Exchange question.
import sympy A = sympy.Matrix([ [3, 2, 2], [2, 3, -2] ]) # A = U S V' if A.shape <= A.shape: A1 = A * A.T U, S = A1.diagonalize(normalize=True) V_T = S**-1 * U.T * A print(U * S * V_T) else: A2 = A.T * A V, S = A2.diagonalize(normalize=True) U = A * V * S**-1 print(U * S * V.T)
I have added two separate cases if A is “landscape” or “portrait” just because of the way the maths plays out in each one. Hopefully it is correct.
NB: I highly advise you to use some kind of numerical computing library instead of creating your own function using a symbolic maths library. If you are not using symbols, the numerical computing library is likely to be way faster.
CLICK HERE to find out more related problems solutions.