Untitled
unknown
python
2 years ago
1.0 kB
4
Indexable
Never
def Householder_step(A): #Dimensions of A m,n = np.shape(A) #Take out the first column in A. We want to mirror it to get ahat. a = A[:,0] #Calculate ahat=H*a ahat=array([norm(a)]+(m-1)*[0.]) #Calculate v v = a - ahat v=v/norm(v) #Calculate H for this step H = eye(m)-2*outer(v,v) return H def QRH(A): #Get dimensions of A m,n = np.shape(A) tempA = A #The Q matrix we will change after every step to the the final Q. Q = np.eye(m) for i in range(n): #Get the H matrix for every step Hi = Householder_step(tempA) #Get the matrix A which will be the input in the next step tempA = Hi@tempA # remove first row and column tempA = np.delete(tempA,0,0) tempA = np.delete(tempA,0,1) #Calculate the accumulated Q Q = Q@np.block([[np.eye(i), np.zeros((i,m-i))],[np.zeros((m-i,i)), Hi]]) #Calculate R R = Q.T@A return Q,R