Skip to content
DeepForgeby svx
All problems
HardLinear Algebrala-135

Householder Reflector Vector

Problem

Return the unit Householder vector v that reflects x onto the first coordinate axis.

With alpha = -sign(x[0]) * ||x||_2 (sign(0) treated as positive), set v = (x - alpha*e1) / ||x - alpha*e1||_2, so that H = I - 2*v*v^T maps x to alpha*e1. If x is the zero vector, return zeros.

Hint

Choose the sign of alpha to avoid cancellation with x[0]; the result has unit norm.

Test cases

case 1
in: [[3,4]]
expected: [0.8944271909999159,0.4472135954999579]
case 2
in: [[1,0]]
expected: [1,0]
case 3
in: [[-3,4]]
expected: [-0.8944271909999159,0.4472135954999579]
case 4
in: [[0,0]]
expected: [0,0]
Starter codePython
def householder_vector(x):
    # Your code here
    pass

4 test cases · run your code against them in the editor