Vector的交叉产品和if语句在功能上

时间:2016-10-26 22:25:30

标签: python if-statement vector

我的程序正在运行,但不是所有部分,我在程序评论中列出了我的问题

class MyVector:
    def __init__(self, vector):
        self.vector = vector

    def get_vector(self):
        return self.vector

    def __mul__(self, other):
        scalar_result = 0  # or 0.0
        for i in range(0, len(self.vector)):
            scalar_result += self.vector[i] * other.vector[i]
        return scalar_result

    def is_perpendicular_to(self, other):       #is there a way that I could make function which will print wheter they are perpendicular or not?
        if (scalar_result == 0):                #this isn't seem to work
            print("They are perpendicular")

    """
    Also I was hoping that I can get crossdot product of these two vectors, but I can not make it functional
    def crossProd(vector):
        dimension = len(a)
        c = []
        for i in range(dimension):
            c.append(0)
            for j in range(dimension):
                if j <> i:
                    for k in range(dimension):
                        if k <> i:
                            if k > j:
                                c[i] += a[j] * b[k]
                            elif k < j:
                                c[i] -= a[j] * b[k]
        return c
    """

if __name__ == "__main__":
     vec1 = MyVector([1, 0, 3, 0])
     vec2 = MyVector([0, 4, 0, 4])
     print(vec1.get_vector())
     print(vec2.get_vector())
     scalar_result = vec1 * vec2
     print(scalar_result)

在上半部分,我创建了我认为应该有效的逻辑算法,但是我不能把它放到程序中,对我来说它不起作用

0 个答案:

没有答案