I am trying to implement a function for Singular Value Decomposition (SVD) in Python by myself, using the eigenvalue decomposition of A^T A and AA^T, but the reconstructed matrix (B) does not always match the original matrix (A). Here is my code:
import numpy as np# Generate a random matrix Arow, col = 3, 3A = np.random.normal(size=row * col).reshape(row, col)# Eigen decomposition of A^T*A and A*A^TATA = A.T @ AAAT = A @ A.Teigenvalues_ATA, eigenvectors_ATA = np.linalg.eig(ATA)eigenvalues_AAT, eigenvectors_AAT = np.linalg.eig(AAT)# Sort eigenvalues and eigenvectorsidx_ATA = eigenvalues_ATA.argsort()[::-1]idx_AAT = eigenvalues_AAT.argsort()[::-1]sorted_eigenvectors_ATA = eigenvectors_ATA[:, idx_ATA]sorted_eigenvectors_AAT = eigenvectors_AAT[:, idx_AAT]# Calculate singular valuessorted_singularvalues_ATA = np.sqrt(np.abs(eigenvalues_ATA[idx_ATA]))sorted_singularvalues_AAT = np.sqrt(np.abs(eigenvalues_AAT[idx_AAT]))# Construct diagonal matrix SS = np.zeros_like(A)np.fill_diagonal(S, sorted_singularvalues_ATA)# Reconstruct matrix BB = sorted_eigenvectors_AAT @ S @ sorted_eigenvectors_ATA.Tprint(np.allclose(A, B))Could anyone know why this reconstruction only occasionally match the original matrix A?
# Example it worksA_equal = [-1.59038869, -0.28431377, 0.36309318, 0.07133563, -0.20420962, 1.82207923, 0.84681193, 0.31419994, -0.93808105]# Example it failsA_not_equal = [ 1.61171729, 0.6436384, 0.47359656, -1.04121454, 0.17558459, 0.36595138, 0.40957221, 0.20499528, 0.18525562]A = np.array(A_not_equal).reshape(3,3)# Expected output[[ 1.61171729 0.6436384 0.47359656] [-1.04121454 0.17558459 0.36595138] [ 0.40957221 0.20499528 0.18525562]]# Actual output[[-1.61240387 -0.63872607 -0.47789066] [ 1.04102391 -0.17422069 -0.36714363] [-0.40734839 -0.22090623 -0.17134711]]