Skip to content
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

Merged
merged 7 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed use of `compas.geometry.allclose` to `compas.tolerance.TOL.is_allclose`.
* Changed use of `compas.geometry.close` to `compas.tolerance.TOL.is_close`.
* Changed imports of itertools to `compas.itertools` instead of `compas.utilities`.
* Updated `compas.geometry.vector` with `__rmul__` and allow element-wise multiplication and division with another vector.

### Removed

Expand Down
23 changes: 18 additions & 5 deletions src/compas/geometry/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Copy link
Member

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...

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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if other is Vector, why not other.x etc?
also, this is the same as return self.dot(other)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -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
# ==========================================================================
Expand Down
4 changes: 4 additions & 0 deletions tests/compas/geometry/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def test_vector_operators():
assert a * 2 == [a.x * 2, a.y * 2, a.z * 2]
assert a / 2 == [a.x / 2, a.y / 2, a.z / 2]
assert a**3 == [a.x**3, a.y**3, a.z**3]
assert 2 * a == [2 * a.x, 2 * a.y, 2 * a.z]
assert a * b == [a.x * b.x, a.y * b.y, a.z * b.z]
assert b * a == [a.x * b.x, a.y * b.y, a.z * b.z]
assert a / b == [a.x / b.x, a.y / b.y, a.z / b.z]


def test_vector_equality():
Expand Down
Loading