两个向量python3的点积

两个向量python3的点积,python,python-3.x,Python,Python 3.x,我在计算QR(矩阵)部分代码中两个向量的点积时遇到问题。我试图用vector01对q[I]的结果进行点画,我不知道我的代码哪里不正确。任何帮助都很好,谢谢你。我的预期输出应该是sqrt(2) twoNorm()中的两个“if”结构不正确(可能只是一个您没有注意到的缩进问题?)。将下面的代码与您的代码进行比较。你会发现出了什么问题 def twoNorm(vector): ''' twoNorm takes a vector as it's argument. It then co

我在计算QR(矩阵)部分代码中两个向量的点积时遇到问题。我试图用vector01对q[I]的结果进行点画,我不知道我的代码哪里不正确。任何帮助都很好,谢谢你。我的预期输出应该是sqrt(2)


twoNorm()中的两个“if”结构不正确(可能只是一个您没有注意到的缩进问题?)。将下面的代码与您的代码进行比较。你会发现出了什么问题

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

我没有仔细检查QR(),但我确信这很麻烦,“还气”之后的任何东西都不会被执行。QR()是如何工作的?

您遇到了什么问题,异常,输出错误?你的预期输出是什么?是的,预期输出是sqrt(2),但我得到的答案无效QR函数中的
向量是什么:
如果len(矩阵)!=len(vector):
?vector在代码末尾定义为[1,0,1],我强烈建议使用现有的Python线性代数函数。Numpy的linalg.norm()函数可用于计算任意长度2(或长度n)向量集的2-norm(或n-norm)。Numpy的dot()函数可以等价地用于计算任意两个向量的点积。
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