Skip to main content

MATH 345: Linear Algebra and Optimization

Section B.10 NumPy Linear Algebra Commands

Use this section when code calls common NumPy linear-algebra commands: rank, determinant, solve, least squares, eigenvalues, QR, or SVD.
Basic computations. These commands return single quantities or solutions for common linear-algebra tasks.

Norm.

np.linalg.norm(x)
Read as. The norm \(\|x\|\text{.}\)
Shape/return. A scalar.
Used for. Lengths, distances, and residuals.

Numerical rank.

np.linalg.matrix_rank(A)
Read as. The numerical rank of \(A\text{.}\)
Shape/return. An integer.
Used for. Independent directions.

Determinant.

np.linalg.det(A)
Read as. The determinant of square \(A\text{.}\)
Shape/return. A scalar.
Used for. Invertibility checks.

Linear solve.

np.linalg.solve(A, b)
Read as. Solve \(Ax=b\) numerically.
Shape/return. A vector or matrix.
Used for. Square full-rank systems.

Least-squares coefficients.

np.linalg.lstsq(A, b, rcond=None)[0]
Read as. The least-squares coefficient vector.
Shape/return. A vector.
Used for. Regression, projection, and sampled polynomial approximation.
Eigenvalue commands. These commands read square or symmetric matrices through eigenvalues and eigenvectors.

General eigenvalues.

np.linalg.eig(H)
Read as. Eigenvalues and eigenvectors of a square matrix.
Shape/return. A pair: eigenvalue array and eigenvector matrix.
Used for. General eigenvalue computations.
Watch for. For symmetric real matrices, eigvalsh or eigh is usually cleaner.

Symmetric eigenvalues.

np.linalg.eigvalsh(H)
Read as. Eigenvalues of a symmetric matrix.
Shape/return. A one-dimensional array of real eigenvalues.
Used for. Hessian classification and symmetric quadratic forms.

Symmetric eigenvectors.

np.linalg.eigh(B)
Read as. Eigenvalues and orthonormal eigenvectors of a symmetric matrix.
Shape/return. A pair: eigenvalue array and eigenvector matrix.
Used for. Reading A.T @ A in singular-value computations.
Watch for. Eigenvalues are returned in increasing order.

Outer product.

np.outer(g, h)
Read as. The outer product \(\mathbf{g}\mathbf{h}^T\text{.}\)
Shape/return. Shape (m, d) when g has length m and h has length d.
Used for. Rank-one updates.
Watch for. This is different from g @ h, which is a dot product when both are one-dimensional arrays of the same length.
Factorizations. These commands split a matrix into pieces that can be reused for interpretation or reconstruction.

QR factorization.

np.linalg.qr(A)
Read as. QR factorization.
Shape/return. Q and R.
Used for. Least-squares checks.

Compact SVD.

np.linalg.svd(A, full_matrices=False)
Read as. Compact singular value decomposition.
Shape/return. U, s, and Vt.
Used for. Compression.
result = np.linalg.lstsq(A, b, rcond=None)
xhat = result[0]
The command np.linalg.lstsq returns several pieces of information. The coefficient vector is the first item, so the textbook often writes [0].
U, s, Vt = np.linalg.svd(A, full_matrices=False)
Ak = U[:, :k] @ np.diag(s[:k]) @ Vt[:k, :]

Checkpoint B.10.1. Check Yourself: Least Squares Output.

In the line that defines xhat using np.linalg.lstsq, what does xhat represent? Why is [0] used? What should be close to zero if the residual is orthogonal to the columns of A?

Checkpoint B.10.2. Check Yourself: SVD Slices.

In U[:, :k] @ np.diag(s[:k]) @ Vt[:k, :], which slice keeps the first \(k\) columns? Which slice keeps the first \(k\) rows?