Use Singular Value Decomposition (SVD) to approximate a matrix using only the top k singular values. This is the foundation for dimensionality reduction and data compression.
**Formula:** A_approx = U[:, :k] * diag(S[:k]) * Vt[:k, :]
Example:
Input: A = [[1, 2], [3, 4]], k = 1
Output: Rank-1 approximation of A (best low-rank approximation)
**Explanation:** SVD decomposes A = U * diag(S) * Vt. Using only the top k singular values gives the best rank-k approximation.
Constraints:
Test Cases
Test Case 1
Input:
[[1, 2], [3, 4]], 1Expected:
rank-1 approximationTest Case 2
Input:
[[1, 0], [0, 1]], 2Expected:
[[1.0, 0.0], [0.0, 1.0]]Test Case 3
Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], 2Expected:
near-original matrix+ 2 hidden test cases