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.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference: reconstructs the symmetric example
- •Example: reconstructs an upper-triangular matrix
- •Sample: reconstructs and matches singular values