我对python3相当陌生。在秋天上课之前,我想在业余时间学习它。这个练习是我尝试过的最难的一次。我正在尝试获取代码并使用for循环而不是使用NUMPY以简单的形式重写它。最终的结果是编写以矩阵为参数的代码,并使用改良的schmidt算法计算并打印QR因式分解。任何帮助,将不胜感激。先感谢您。
#sample matrix
A = [[80,75,85],
[75,80,75],
[80,80,80]]
#function to find QR factorization of a square matrix
#parameter - matrix to be factorized
def findQRFactorization(A):
#check if A is a matrix
if any(isinstance(i,list) for i in A):
print("Is a matrix")
#number of rows and columns in A
rows = len(A)
cols = len(A[0])
print("rows:",rows," cols:",cols)
#check if A is square matrix
if rows != cols:
print("Not a square matrix. Aborting...")
else:
print("A square matrix. proceeding...")
#create an Identiry matrix of size rows x cols
I = [[0]*cols for i in range(rows)]
for i in range(rows):
I[i][i] = 1
#print(I)
#calculation of QR factorization based on householder reflection method
#QR factorization represents A as A = QR where Q is a orthogonal matrix
#while R is a upper triangular matrix.
#Initialize Q and R
Q = [[0.0] * cols for i in range(rows)]
R = A
#print("Q:",Q)
#print("R:",R)
#loop to perform QR factorization
for k in range(rows-1):
#calculate x, e
x = [row[k] for row in R[k:]]
e = [row[k] for row in I[k:]]
#calculate norm of x
norm_x = math.sqrt(sum([i**2 for i in x]))
#print("norm x:",norm_x)
#calculate alpha
sign = lambda x: (1, -1)[x < 0]
alpha = -sign(x[0])*norm_x
#print('alpha:',alpha)
#calculate minor matrix of u and v
u = list(map(lambda i,j: i + alpha * j, x, e))
norm_u = math.sqrt(sum([i**2 for i in u]))
v = list(map(lambda i: i/norm_u, u))
#calculate Q
Q_minor = [ [float(i==j) - 2.0 * v[i] * v[j] for i in range(rows-k)] for j in range(cols-k) ]
def Q_identity(Q_minor,i,j,k):
if i < k or j < k:
return float(i == j)
else:
return Q_minor[i-k][j-k]
Q_padded = [[Q_identity(Q_minor,i,j,k) for i in range(rows)] for j in range(cols)]
#update Q and R
def multiply(P,Q):
return [[sum(elemp * elemq for elemp, elemq in zip(rowp, colq)) for colq in zip(*Q)] for rowp in P]
if k == 0:
Q = Q_padded
R = multiply(Q_padded,A)
else:
Q = multiply(Q_padded,Q)
R = multiply(Q_padded,R)
#calculate transpose of Q
Q_dash = [[Q[i][j] for i in range(cols)] for j in range(rows)]
print("QR factorization of ",A)
print("Q: ",Q_dash)
print("R: ",R)
else:
print("Not a matrix. QR factorization not possible.")
#call function
findQRFactorization(A)