How to make a vector class in python? Can anybody help me?

8/12/2019 2:24:44 AM

Jessi Betancur

4 Answers

New Answer


#Well first you need to declare the class self: class VectorExample: #Then you might want to declare a constructor and send some setup variables: def __init__(self, point1, point2): self.point1 = tuple(point1) self.point2 = tuple(point2) self.direction = tuple(p1 - p2 for p1, p2 in (self.point1, self.point2)) #Then you can define some magic methods, for example to make Vector1 * Vector2 to perform the dot operation: def __mul__(self, other): sum = 0 for i in range(len(self.point1)): sum += self.direction[i] * other.direction[i] return sum #Then you could make Vectors: v1 = VectorExample((0, 0), (3, 4)) v2 = VectorExample((1, 0), (5, 8)) #And perform dot operator: print(v1 * v2) #Output: 3


A vector, like a force with direction and magnitude? Or a vector like in HTML?


Thank you very much was a great help. Now I am trying to create the matrix class. You can help me?


Jessi Betancur I don't know anything about matrises, if you can somehow tell me what matrises are and how they can be used, then I might be able to help with the Matrix class. Atleast it sounds fair deal to me.