Neural Mastery
← Practice
Gram-Schmidt Orthonormalizationeasy

Gram-Schmidt Orthonormalization

easy Math for ML 15-20 min

NumPy allowed · plain Python earns 10 bonus points

Linear algebra step 1/4

A vector transforms

Matrix multiplication is the first computation behind every model.
Open lesson →
01vector
02matrix
03transform
04result
[2, −1] × [[1, 2], [−.5, 1]] = [0, −2]

Computed matrix × vector transformation

Construct an orthonormal basis using the classical Gram-Schmidt orthogonalization process.

Related Skills

Task

Implement gram_schmidt(vectors): sequentially project each vector onto the span of previously computed basis vectors, subtract projections to get orthogonal vector w, normalize w to unit length, and append to basis. Discard vectors whose remaining norm is < 1e-7. Round coordinates to 4 decimals.

Function Signature

gram_schmidt(vectors: list[list[float]]) -> list[list[float]]

Examples

Example 1: 2D Standard Basis
Input: vectors = [[1, 1], [0, 1]]
Output: [[0.7071, 0.7071], [-0.7071, 0.7071]]
Explanation: Orthogonalizing 2D plane vectors.
Example 2: Already Orthogonal
Input: vectors = [[2, 0], [0, 3]]
Output: [[1, 0], [0, 1]]
Explanation: Normalizes already orthogonal inputs.

Constraints

  • 1 <= len(vectors) <= 10.
  • Dimension 1 <= d <= 10.
Python3Saved ✓
def gram_schmidt(vectors: list[list[float]]) -> list[list[float]]:
"""Produce an orthonormal basis from linearly independent vectors using Gram-Schmidt.
Discard vectors with norm < 1e-7. Round coordinates to 4 decimals."""
# Your implementation here
pass

Case 1: 2D Standard Basis
Input: vectors = [[1, 1], [0, 1]]
Expected: [[0.7071, 0.7071], [-0.7071, 0.7071]]
Case 2: Already Orthogonal
Input: vectors = [[2, 0], [0, 3]]
Expected: [[1, 0], [0, 1]]