Skip to content

Commit

Permalink
feat: Vector division
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 13, 2024
1 parent 6366cea commit 9e497d1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
33 changes: 21 additions & 12 deletions quantio/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,31 @@ def __sub__(self, other: Vector[T] | np.ndarray) -> Vector[T]:

def __mul__(self, other: Vector | np.ndarray | float) -> np.ndarray:
"""Multipy this vector with either another vector or a scalar."""
if isinstance(other, (float, int)):
other_elements = np.array([other])
elif isinstance(other, _QuantityBase):
other_elements = np.array([other._base_value])
elif isinstance(other, Vector):
other_elements = other.to_numpy()
elif isinstance(other, np.ndarray):
other_elements = other
else:
raise TypeError

return self.to_numpy() * other_elements
return self.to_numpy() * _other_to_numpy(other)

def __truediv__(self, other: Vector | np.ndarray | float) -> np.ndarray:
"""Multipy this vector with either another vector or a scalar."""
return self.to_numpy() / _other_to_numpy(other)

def __eq__(self, other: object) -> bool:
"""Assess if this object is the same as another."""
if not isinstance(other, Vector):
return False

return np.all(other._elements == self._elements)


def _other_to_numpy(other: Vector | np.ndarray | float) -> np.ndarray:
if isinstance(other, (float, int)):
return np.array([other])

if isinstance(other, _QuantityBase):
return np.array([other._base_value])

if isinstance(other, Vector):
return other.to_numpy()

if isinstance(other, np.ndarray):
return other

raise TypeError
48 changes: 48 additions & 0 deletions test/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,53 @@ def test_multiply__wrong_dimension():
vec1 * vec2


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

actual = vec2 / vec1
assert np.all(actual == np.array([3, 2]))


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

actual = vec2 / vec1
assert np.all(actual == np.array([3, 2]))


def test_divide__with_array():
vec: Vector[Length] = Vector([Length(meters=3), Length(meters=4)])
array = np.array([1, 2])

actual = vec / array
assert np.all(actual == np.array([3, 2]))


def test_divide__with_scalar_float():
vec: Vector[Length] = Vector([Length(meters=2), Length(meters=4)])
scalar = 2

actual = vec / scalar
assert np.all(actual == np.array([1, 2]))


def test_divide__with_scalar_quantitiy():
vec: Vector[Length] = Vector([Length(meters=2), Length(meters=4)])
scalar = Length(meters=2)

actual = vec / scalar
assert np.all(actual == np.array([1, 2]))


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

with pytest.raises(ValueError):
vec1 / vec2


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

0 comments on commit 9e497d1

Please sign in to comment.