📘
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:
- Start with the first vector, normalize it: u1=∥v1∥v1
- For each subsequent vector vk:
- Subtract projections onto all previous orthonormal vectors
- wk=vk−∑j=1k−1(vk⋅uj)uj
- Normalize: uk=∥wk∥wk
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] ∥v1∥=12+12+02=2 u1=[21,21,0]=[0.7071,0.7071,0]
Step 2: Orthogonalize second vector v2=[1,0,1] Projection: v2⋅u1=1×0.7071+0×0.7071+1×0=0.7071 w2=[1,0,1]−0.7071×[0.7071,0.7071,0]=[0.5,−0.5,1] ∥w2∥=0.25+0.25+1=1.5≈1.2247 u2=[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
Python 3.13.1
Test Results
0/0Run code to see test results.