#12 · Linear Algebra · Hard
⊣ Solve on deep-ml.comPerform Singular Value Decomposition (SVD) on a 2x2 matrix, decomposing it into U Sigma V^T. Return the singular values and the matrices U and V.
import numpy as np
def svd_2x2(A: list[list[float]]) -> tuple:
A = np.array(A, dtype=float)
# Compute A^T * A and A * A^T
ATA = A.T @ A
AAT = A @ A.T
# Eigenvalues/vectors of A^T * A give V and sigma^2
eigvals_v, V = np.linalg.eigh(ATA)
# Sort by descending eigenvalue
idx = np.argsort(eigvals_v)[::-1]
eigvals_v = eigvals_v[idx]
V = V[:, idx]
# Singular values
sigma = np.sqrt(np.maximum(eigvals_v, 0))
# Compute U from A * V / sigma
U = np.zeros_like(A, dtype=float)
for i in range(len(sigma)):
if sigma[i] > 1e-10:
U[:, i] = (A @ V[:, i]) / sigma[i]
# Ensure proper orthogonal matrices (det = 1)
if np.linalg.det(U) < 0:
U[:, -1] *= -1
if np.linalg.det(V) < 0:
V[:, -1] *= -1
return U.tolist(), sigma.tolist(), V.T.tolist()