python中QR分解的基本GramSchmidt

2024-10-02 20:35:03 发布

您现在位置:Python中文网/ 问答频道 /正文

Implement basic Gram-Schmidt factorisation for an arbitrary mn matrix. Make sure you test your algorithm against the provided test cases. You can assume that m 10 and n m (see next section for details).

我试过上面的方法,但是有点不对劲,我需要另一个实现。你知道吗

# 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))

为什么我会出错?你知道吗


Tags: andoftheintestforlenif