Click4Ai

45.

Hard

Implement the Gram-Schmidt process to convert a set of linearly independent vectors into an orthogonal set.

Algorithm:

1. u1 = v1

2. u2 = v2 - proj_u1(v2)

3. u3 = v3 - proj_u1(v3) - proj_u2(v3)

4. Continue for all vectors...

Where proj_u(v) = (dot(v,u) / dot(u,u)) * u

Example:

Input: vectors = [[1, 1, 0], [1, 0, 1]]

Output: [[1, 1, 0], [0.5, -0.5, 1]]

**Explanation:** u1 = v1. u2 = v2 - proj_u1(v2). proj = (1/2)*[1,1,0] = [0.5,0.5,0]. u2 = [0.5,-0.5,1]

Constraints:

  • Vectors are linearly independent
  • Return list of orthogonal vectors (floats rounded to 4 decimal places)
  • Test Cases

    Test Case 1
    Input: [[1, 1, 0], [1, 0, 1]]
    Expected: [[1, 1, 0], [0.5, -0.5, 1.0]]
    Test Case 2
    Input: [[1, 0], [0, 1]]
    Expected: [[1, 0], [0, 1]]
    Test Case 3
    Input: [[1, 0], [1, 1]]
    Expected: [[1, 0], [0, 1]]
    + 2 hidden test cases