-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vector multiplications #1347
Vector multiplications #1347
Changes from 1 commit
c7f958c
e795b9f
640393e
443483d
bd3ece9
2cae390
70acd71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,11 +148,21 @@ def __add__(self, other): | |
def __sub__(self, other): | ||
return Vector(self.x - other[0], self.y - other[1], self.z - other[2]) | ||
|
||
def __mul__(self, n): | ||
return Vector(self.x * n, self.y * n, self.z * n) | ||
|
||
def __truediv__(self, n): | ||
return Vector(self.x / n, self.y / n, self.z / n) | ||
def __mul__(self, other): | ||
if isinstance(other, (int, float)): | ||
return Vector(self.x * other, self.y * other, self.z * other) | ||
elif isinstance(other, Vector): | ||
return Vector(self.x * other[0], self.y * other[1], self.z * other[2]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are very right.... another mistake of mine not careful about what copilot suggested! But this is not same with dot product tho, this returns element-wise multiplication, the result is still of a vector, not the sum of them |
||
else: | ||
Licini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError("Multiplication of Vector with unsupported type: {}".format(type(other))) | ||
|
||
def __truediv__(self, other): | ||
if isinstance(other, (int, float)): | ||
return Vector(self.x / other, self.y / other, self.z / other) | ||
elif isinstance(other, Vector): | ||
return Vector(self.x / other[0], self.y / other[1], self.z / other[2]) | ||
Licini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
Licini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError("Division of Vector with unsupported type: {}".format(type(other))) | ||
|
||
def __pow__(self, n): | ||
return Vector(self.x**n, self.y**n, self.z**n) | ||
|
@@ -190,6 +200,9 @@ def __ipow__(self, n): | |
self.z **= n | ||
return self | ||
|
||
def __rmul__(self, n): | ||
Licini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self.__mul__(n) | ||
|
||
# ========================================================================== | ||
# Properties | ||
# ========================================================================== | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we don't mind the performance penalty here i am okay with it...