Singular Value Decomposition (2x2)
Background
The Singular Value Decomposition factors any matrix as , where and are orthogonal and is diagonal with non-negative singular values. It is the most general matrix factorization — behind PCA, low-rank approximation, pseudo-inverses, and recommender systems. For a matrix it has a closed form via a single Jacobi rotation that diagonalizes .
Problem statement
Implement svd_2x2_singular_values(A) that returns the SVD of a matrix A. Diagonalize with one Jacobi rotation at angle (so ), take the singular values as the square roots of the resulting diagonal, then set :
(use when the diagonal entries of are equal).
Input
A—np.ndarrayof shape(2, 2)(assumed full rank).
Output
Returns a tuple (U, s, V_T): U is (2, 2) left singular vectors, s is (2,) singular values, and V_T is the (2, 2) transpose of the right singular vectors.
Examples
Example 1
Input: A = [[2, 1], [1, 2]]
Output: U ~= [[-0.7071, -0.7071], [-0.7071, 0.7071]], s = [3, 1],
V_T ~= [[-0.7071, -0.7071], [-0.7071, 0.7071]]
Explanation: has equal diagonal entries, so ; the singular values are and , and reconstructs .
Constraints
- One Jacobi rotation suffices because is a symmetric matrix.
- The decomposition has a sign ambiguity (columns of / may flip together); tests check the reconstruction rather than exact entries.
- Singular values are non-negative;
Ais assumed full rank (no zero singular value).
Notes
- Singular values are the square roots of the eigenvalues of , and the right singular vectors are that matrix's eigenvectors.
- This routine is the kernel of one-sided Jacobi SVD, which sweeps over all column pairs to decompose larger matrices.
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example: reconstructs [[2,1],[1,2]] with singular values {3, 1}
- •Reconstruction holds for a non-symmetric 2x2
- •Singular values match numpy's (up to ordering)
- •Right singular vectors are orthonormal