Skip to content

Commit

Permalink
feat: Vector subtraction
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 13, 2024
1 parent 23caa76 commit 4a0a9d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions quantio/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def __add__(self, other: Vector[T] | np.ndarray) -> Vector[T]:
other_elements = other._elements if isinstance(other, Vector) else np.array(other)
return Vector[T](self._elements + other_elements)

def __sub__(self, other: Vector[T] | np.ndarray) -> Vector[T]:
"""Subtract another vector from this one."""
other_elements = other._elements if isinstance(other, Vector) else np.array(other)
return Vector[T](self._elements - other_elements)

def __eq__(self, other: object) -> bool:
"""Assess if this object is the same as another."""
if not isinstance(other, Vector):
Expand Down
18 changes: 17 additions & 1 deletion test/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from quantio import Vector, Length, CanNotAddTypesError
from quantio import Vector, Length, CanNotAddTypesError, CanNotSubtractTypesError


def test_init():
Expand Down Expand Up @@ -43,5 +43,21 @@ def test_addition__wrong_type():
vec1 + vec2


def test_subtract():
vec1: Vector[Length] = Vector([Length(meters=1), Length(meters=2)])
vec2: Vector[Length] = Vector([Length(meters=3), Length(meters=3)])

actual = vec2 - vec1
assert actual == Vector([Length(meters=2), Length(meters=1)])


def test_subtract__wrong_type():
vec1: Vector[Length] = Vector([Length(meters=1), Length(meters=2)])
vec2: Vector[float] = Vector([3, 4])

with pytest.raises(CanNotSubtractTypesError):
vec1 - vec2


if __name__ == "__main__":
pytest.main([__file__, "-v"])

0 comments on commit 4a0a9d7

Please sign in to comment.