PIXELBANKv8.2.1
Menu

Gram-Schmidt Orthonormalization

MediumVectors

Implement the Gram-Schmidt process to orthonormalize a set of vectors using NumPy.

Given a list of linearly independent vectors, produce an orthonormal basis where:

  • Each output vector is unit length (norm = 1)
  • All output vectors are mutually orthogonal (dot product = 0)

The Gram-Schmidt process:

  1. Start with the first vector, normalize it: u1=v1v1\mathbf{u}_1 = \frac{\mathbf{v}_1}{\|\mathbf{v}_1\|}
  2. For each subsequent vector vk\mathbf{v}_k:
    • Subtract projections onto all previous orthonormal vectors
    • wk=vkj=1k1(vkuj)uj\mathbf{w}_k = \mathbf{v}_k - \sum_{j=1}^{k-1} (\mathbf{v}_k \cdot \mathbf{u}_j) \mathbf{u}_j
    • Normalize: uk=wkwk\mathbf{u}_k = \frac{\mathbf{w}_k}{\|\mathbf{w}_k\|}

This algorithm is fundamental in CV for creating orthonormal bases in feature spaces.

Example:

Input:
vectors = [[1, 1, 0], [1, 0, 1], [0, 1, 1]]
Output:
[[0.7071, 0.7071, 0.0], [0.4082, -0.4082, 0.8165], [-0.5774, 0.5774, 0.5774]]
Reasoning:

Step 1: Normalize first vector v1=[1,1,0]\mathbf{v}_1 = [1, 1, 0] v1=12+12+02=2\|\mathbf{v}_1\| = \sqrt{1^2 + 1^2 + 0^2} = \sqrt{2} u1=[12,12,0]=[0.7071,0.7071,0]\mathbf{u}_1 = [\frac{1}{\sqrt{2}}, \frac{1}{\sqrt{2}}, 0] = [0.7071, 0.7071, 0]

Step 2: Orthogonalize second vector v2=[1,0,1]\mathbf{v}_2 = [1, 0, 1] Projection: v2u1=1×0.7071+0×0.7071+1×0=0.7071\mathbf{v}_2 \cdot \mathbf{u}_1 = 1 \times 0.7071 + 0 \times 0.7071 + 1 \times 0 = 0.7071 w2=[1,0,1]0.7071×[0.7071,0.7071,0]=[0.5,0.5,1]\mathbf{w}_2 = [1, 0, 1] - 0.7071 \times [0.7071, 0.7071, 0] = [0.5, -0.5, 1] w2=0.25+0.25+1=1.51.2247\|\mathbf{w}_2\| = \sqrt{0.25 + 0.25 + 1} = \sqrt{1.5} \approx 1.2247 u2=[0.4082,0.4082,0.8165]\mathbf{u}_2 = [0.4082, -0.4082, 0.8165]

Step 3: Orthogonalize third vector similarly...

Constraints:

  • Input: List of n vectors, each of dimension d
  • Vectors are linearly independent
  • Return: List of n orthonormal vectors (as lists)
  • Round each component to 4 decimal places
Editor

Test Results

0/0
Run code to see test results.