I implemented the Householder transformation in Python, so that I can later use it in a QR decomposition. Unfortunately I haven't found a good concise source for reading up on the algorithm.
I am not really satisfied with my code - I mostly dislike its readability due to the number of various computations. I also find my usage of the identity matrices \$I\$ and \$J\$ inefficient. So I would be happy about any suggestions on improving this implementation.
def houseHolder2(m):
l = len(m)
for i in range(0, l - 1):
I = np.eye(l - i)
ai = m[:, i]
abs = np.linalg.norm(ai[i:l])
sign = np.sign(m[i, i])
vi = np.array([ai[i:l]]).T + sign * abs * np.array([I[:, 0]]).T
Qi = I - 2 * (vi @ vi.T) / (vi.T @ vi)
J = np.eye(l)
J[i:l, i:l] = Qi
m = J@m
return m
m
? From an efficiency view point, the real question is how many times are you repeating the loop, a few times, or hundreds. \$\endgroup\$I
withI[:,[0]]
; Or the whole line withvi=(ai[i:l] + sign * abs * I[:,0])[:,None]
\$\endgroup\$