Python3 QR因式分解Gram-Schmidt

时间:2018-07-01 05:39:42

标签: python-3.x

我正在尝试计算一个以矩阵为参数的代码,并使用改良的Gram-Schmidt算法计算并打印QR分解。我很想计算q [i]回来对我来说是无效的。非常感谢您的帮助。

def twoNorm(vector):
    '''
    twoNorm takes a vector as it's argument. It then computes the sum of  
    the squares of each element of the vector. It then returns the square 
    root of this sum.
    '''
    # This variable will keep track of the validity of our input.
    inputStatus = True  
    # 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
                # This for loop will compute the sum of the squares of the elements of the vector. 
                    for i in range(len(vector)):
                        result = result + (vector[i]**2)
                    result = result**(1/2)
                    return result
vector = [1, 0, 1]
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, 2], [0, 1], [1, 0]]

0 个答案:

没有答案