mercredi 26 octobre 2016

Vector's cross product and if statement in function

My program is working but not all of it's parts, I listed my problems in comment of program

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)

In the upper part I created logical algorithm that I think should work, but I can't put it to the program, for me it is not functional Please help, I tried everything

Aucun commentaire:

Enregistrer un commentaire