Singular Value Decomposition (2x2)Hard

Singular Value Decomposition (2x2)

Background

The Singular Value Decomposition factors any matrix as A=UΣVA = U\Sigma V^\top, where UU and VV are orthogonal and Σ\Sigma 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 2×22\times 2 matrix it has a closed form via a single Jacobi rotation that diagonalizes AAA^\top A.

Problem statement

Implement svd_2x2_singular_values(A) that returns the SVD (U,s,V)(U, s, V^\top) of a 2×22\times 2 matrix A. Diagonalize AAA^\top A with one Jacobi rotation RR at angle θ\theta (so V=RV = R), take the singular values as the square roots of the resulting diagonal, then set U=AVΣ1U = A V \Sigma^{-1}:

θ=12arctan ⁣2(AA)01(AA)00(AA)11,A=UΣV\theta = \tfrac{1}{2}\arctan\!\frac{2\,(A^\top A)_{01}}{(A^\top A)_{00} - (A^\top A)_{11}}, \qquad A = U\,\Sigma\,V^\top

(use θ=π/4\theta = \pi/4 when the diagonal entries of AAA^\top A are equal).

Input

  • Anp.ndarray of 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: AA=[5445]A^\top A = \begin{bmatrix}5 & 4\\ 4 & 5\end{bmatrix} has equal diagonal entries, so θ=π/4\theta = \pi/4; the singular values are 9=3\sqrt{9} = 3 and 1=1\sqrt{1} = 1, and UΣVU\Sigma V^\top reconstructs AA.

Constraints

  • One Jacobi rotation suffices because AAA^\top A is a symmetric 2×22\times 2 matrix.
  • The decomposition has a sign ambiguity (columns of UU/VV may flip together); tests check the reconstruction UΣV=AU\Sigma V^\top = A rather than exact entries.
  • Singular values are non-negative; A is assumed full rank (no zero singular value).

Notes

  • Singular values are the square roots of the eigenvalues of AAA^\top A, and the right singular vectors are that matrix's eigenvectors.
  • This 2×22\times 2 routine is the kernel of one-sided Jacobi SVD, which sweeps over all column pairs to decompose larger matrices.
Python
Loading...

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