I became confused about how singular value decomposition can be used to find generalized inverse of singular matrix.
Specifically, I am dealing with the matrix $G=\begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 0 & \sqrt{2} & \sqrt{2} & 0 \\ \sqrt{2} & 0 & 0 & \sqrt{2} \end{pmatrix}$. Calculating $A=G^{T}G$, $A=\begin{pmatrix} 3 & 0 & 1 & 2 \\ 0 & 3 & 2 & 1 \\ 1 & 2 & 3 & 0 \\ 2 & 1 & 0 & 3 \end{pmatrix}$ and its eigenvalues are $6,4,2,0$ so the matrix $A$ is singular. However, my textbook describes that it is possible to calculate the generalized inverse of $G$ using the decomposition called Lanczos decomposition, namely $G=U_{p}\Lambda_{p} V_p^{T}$. It is written that the generalized inverse is then $G^{-p}=V_{p} \Lambda_{p}^{-1} U_{p}^{T}$.
Now, matrices $U_{p}$, $\Lambda_{p}$, and $V_{p}^{T}$ are defined as follows: $\Lambda_{p}$ is the diagonal matrix where its diagonal components are $p$ nonzero eigenvalues of $G$ (in this case, $p=3$ because $rank(G)=3$). Also, it is written that $U_{p}$ and $V_{p}$ can be attained by calculating singular value decomposition of $GG^{T}$ and $G^{T}G$, respectively.
I understand $\Lambda_{p}$ part, but I want to know how to exactly calculate $U_{p}$ and $V_{p}$. I did an experiment using NumPy module but I think I am misunderstanding something because result is different from desired one.
I attach my Python code I used for computation.
import numpy as np
from numpy.linalg import svd
G=np.array([[1, 0, 1, 0],
[0, 1, 0, 1],
[0, np.sqrt(2), np.sqrt(2), 0],
[np.sqrt(2), 0, 0, np.sqrt(2)]])
V, S, Vh=svd(G.T@G)
Vp=V[:, :3] # Eigenvectors corresponding to 3 nonzero eigenvalues
U, S, Uh=svd(G@G.T)
Up=U[:, :3] # Eigenvectors corresponding to 3 nonzero eigenvalues
D=np.sqrt(np.diag(S[:3])) # square root of eigenvalues of G.T@G
print(Up@D@Vp.T)
print(G)
Two results must be equal according to the theory but it is not. Specifically, it seems that the first two rows of $U_{p}\Lambda_{p}V_{p}^{T}$ are swapped but I don't get why. I think this is because I incorrectly solved for $V_{p}$ and $U_{p}$. I want to know what am I misunderstanding.
