Sample Code

import numpy as np # 1. Define a sample 2D matrix (e.g., a 3x2 matrix) A = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) U, s, Vt = np.linalg.svd(A, full_matrices=False) print(f"U (Left Singular Vectors) shape: {U.shape}") print(U) print(f"\ns (Singular Values 1D array) shape: {s.shape}") print(s) print(f"\nVt (Right Singular Vectors, Transposed) shape: {Vt.shape}") print(Vt) print("-" * 40) # 3. Reconstruct the original matrix from the components # Note: 's' is returned as a 1D array, so we must convert it # to a diagonal matrix using np.diag() before multiplying. Sigma = np.diag(s) A_reconstructed = U @ Sigma @ Vt print("\nReconstructed Matrix A:") print(A_reconstructed) # Verify if reconstruction is identical to the original matrix is_close = np.allclose(A, A_reconstructed) print(f"\nDoes the reconstruction match the original? {is_close}")