python中的QR分解基本Gram-Schmidt

时间:2019-04-12 09:27:02

标签: python

  

对任意mn实施基本的Gram-Schmidt分解   矩阵。确保对照提供的测试测试算法   案件。您可以假设m 10和n m(请参阅下一节   细节)。

我已经尝试了上面的方法,但是出了点问题,我需要对其进行另一种实现。

# This variable will keep track of the validity of our input.
inputStatus = True
vector = [1, 0, 1]
def twoNorm(vector):
    # This for loop will check each element of the vector to see if it's a number. 
   for i in range(len(vector)):  
        if ((type(vector[i]) != int) and (type(vector[i]) != float) and (type(vector[i]) != complex)):
            inputStatus = False
            print("Invalid Input")
            # If the input is valid the function continues to compute the 2-norm
            if inputStatus == True:
                result = 0
                for i in range(len(vector)):
                    result = result + (vector[i]**2)
                    result = result**(1/2)

                return result

print(twoNorm(vector))

def QR(matrix):
    if len(matrix[0]) != len(vector):
        print('Invalid')
    else:
        for i in vector:
            q[i] = []
            r[i][i] = twoNorm(vector)
            q[i].append(i * (1/(r[i][i])))
        return q[i]
matrix = [[1, 3], [4, 1], [2, 0]]
print(QR(matrix))

为什么会出现错误?

0 个答案:

没有答案