import numpy as np from scipy.linalg import svd A = np.array([[1, 2], [3, 4], [5, 6]]) U, S, VT = svd(A, full_matrices=False) print(U.shape, S.shape, VT.shape)
When full_matrices=False, U has shape (m, k), S has length k, and VT has shape (k, n), where k = min(m, n). For A with shape (3, 2), k=2, so U is (3, 2), S is (2,), and VT is (2, 2).
import numpy as np from scipy.linalg import svd A = np.array([[7, 8], [9, 10], [11, 12]]) U, S, VT = svd(A, full_matrices=False) # Which code reconstructs A correctly?
The original matrix A can be reconstructed by multiplying U, the diagonal matrix of singular values, and VT: A = U * diag(S) * VT.
Option C tries element-wise multiplication which is invalid for matrix reconstruction. Option C tries dot product with 1D array S which is invalid. Option C tries to diagonalize U which is incorrect.
import numpy as np from scipy.linalg import svd import matplotlib.pyplot as plt A = np.array([[3, 1, 1], [-1, 3, 1]]) U, S, VT = svd(A) # Which code plots singular values S as a bar chart?
Singular values are discrete and best shown with a bar chart. Option A uses plt.bar which is correct. Option A plots a line which is less clear. Option A uses scatter which is less common here. Option A uses histogram which is for distributions, not singular values.
Zero singular values mean the matrix does not have full rank. The rank equals the number of non-zero singular values. Here, rank is 2, so the matrix is rank deficient.
It is not full rank (B), symmetry (C) is unrelated to singular values, and invertibility (A) requires full rank.
import numpy as np from scipy.linalg import svd A = np.array([[1, 0], [0, 1]]) U, S, VT = svd(A) A_reconstructed = U @ S @ VT print(A_reconstructed)
S is a 1D array of singular values, not a 2D matrix. Multiplying U @ S causes a shape mismatch error because S is (2,) and U is (2,2). You must convert S to a diagonal matrix using np.diag(S) before multiplication.