Gram-Schmidt Orthonormalization of Matrix Columns in NumPy

Quick Overview

Implement Gram-Schmidt in NumPy to turn the linearly independent columns of a matrix into an orthonormal basis for the same column space. It tests projections and normalization, vectorized column operations, and the numerical difference between classical and modified Gram-Schmidt.

Gram-Schmidt Orthonormalization of Matrix Columns in NumPy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `gram_schmidt(A)` for an `(m, n)` matrix with linearly independent columns. Return `Q` of shape `(m, n)` with orthonormal columns spanning the column space of `A` (equivalent to the `Q` of `np.linalg.qr(A, mode='reduced')`). Classical or modified Gram-Schmidt is allowed. The result must meet these requirements: - `Q.T @ Q` equals the `(n, n)` identity per `np.allclose`. - Each column of `A` is a linear combination of `Q`'s columns. - Use vectorized NumPy: no Python loops over individual entries. For example: ```python A = np.array([[1.0, 1.0], [1.0, 0.0], [0.0, 1.0]]) Q = gram_schmidt(A) np.allclose(Q.T @ Q, np.eye(2)) # True Q.shape # (3, 2) ``` ```hint Projections Look at what each new column shares with the columns already in `Q`, and what has to be removed before it can join them. ``` ```hint Which vector to project Decide whether each dot product uses the original column or the residual that already has the earlier components removed. The two agree in exact arithmetic and drift apart in floating point. ``` ### Constraints & Assumptions - `1 <= n <= m`. - The columns of `A` are linearly independent. - Column signs may differ from `np.linalg.qr`. - Return only `Q`; do not return `R`. ### What a Strong Answer Covers - The projection and normalization steps, and why they give `Q.T @ Q` close to the identity while keeping the span of `A`. - Column-level vectorized operations (dot products and matrix-vector products) with no loop over individual entries. - Which variant the code implements, classical or modified, and how the two differ in floating point. - What happens when a residual norm is zero or tiny, and why the checks use `np.allclose`. - Time and space complexity in terms of `m` and `n`. ### Follow-up Questions - Why is modified Gram-Schmidt more stable than the classical version? - How would you extend this to handle rank-deficient or nearly dependent columns? - How would you also build the upper-triangular `R` with `A = Q @ R`, and what sign do its diagonal entries have?

Overview: Implement Gram-Schmidt in NumPy to turn the linearly independent columns of a matrix into an orthonormal basis for the same column space. It tests projections and normalization, vectorized column operations, and the numerical difference between classical and modified Gram-Schmidt.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement gram_schmidt(A) for an (m, n) matrix with linearly independent columns. Return Q of shape (m, n) with orthonormal columns spanning the column space of A (equivalent to the Q of np.linalg.qr(A, mode='reduced')). Classical or modified Gram-Schmidt is allowed.

The result must meet these requirements:

  • Q.T @ Q equals the (n, n) identity per np.allclose .
  • Each column of A is a linear combination of Q 's columns.
  • Use vectorized NumPy: no Python loops over individual entries.

For example:

A = np.array([[1.0, 1.0],
              [1.0, 0.0],
              [0.0, 1.0]])
Q = gram_schmidt(A)
np.allclose(Q.T @ Q, np.eye(2))   # True
Q.shape                            # (3, 2)

Constraints & Assumptions

  • 1 <= n <= m .
  • The columns of A are linearly independent.
  • Column signs may differ from np.linalg.qr .
  • Return only Q ; do not return R .

What a Strong Answer Covers Guidance

  • The projection and normalization steps, and why they give Q.T @ Q close to the identity while keeping the span of A .
  • Column-level vectorized operations (dot products and matrix-vector products) with no loop over individual entries.
  • Which variant the code implements, classical or modified, and how the two differ in floating point.
  • What happens when a residual norm is zero or tiny, and why the checks use np.allclose .
  • Time and space complexity in terms of m and n .

Follow-up Questions Guidance

  • Why is modified Gram-Schmidt more stable than the classical version?
  • How would you extend this to handle rank-deficient or nearly dependent columns?
  • How would you also build the upper-triangular R with A = Q @ R , and what sign do its diagonal entries have?