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

Add constant multiplication on trajectories #33

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 17 additions & 2 deletions tests/test_trajectory/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_iteration(points, angles, traj):
assert ang == approx(tp.ang, APPROX_REL_TOLERANCE) # Angle


def test_contant_addition(points, traj):
def test_constant_addition(points, traj):
new_traj = traj + 10
new_points = points + 10

Expand Down Expand Up @@ -77,7 +77,7 @@ def test_wrong_addition(traj):
traj += 'wrong'


def test_contant_substraction(points, traj):
def test_constant_substraction(points, traj):
new_traj = traj - 10
new_points = points - 10

Expand Down Expand Up @@ -105,3 +105,18 @@ def test_traj_substraction(points, traj):
def test_wrong_substraction(traj):
with pytest.raises(TypeError):
traj -= 'wrong'


def test_constant_multiplication(points, traj):
new_traj = traj * 3
new_points = points * 3

for true_point, point in zip(new_points, new_traj.r):
assert true_point == approx(point, APPROX_REL_TOLERANCE)


def test_wrong_multiplication(traj):
with pytest.raises(TypeError):
traj *= 'wrong'
with pytest.raises(TypeError):
traj *= [1, 2]
16 changes: 16 additions & 0 deletions yupi/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ def __radd__(self, other):
def __rsub__(self, other):
return self - other

def __imul__(self, other):
if isinstance(other, (int, float)):
self.r *= other
return self

raise TypeError("unsoported operation (*) between 'Trajectory' and "
f"'{type(other).__name__}'")

def __mul__(self, other):
traj = self.copy()
traj *= other
return traj

def __rmul__(self, other):
return self * other

def _save_json(self, path: str):
def convert_to_list(vec: Vector):
if vec is None:
Expand Down