Skip to content

Commit

Permalink
feat: Vector.to_numpy()
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 13, 2024
1 parent 26a89aa commit 6366cea
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions quantio/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ class Vector(Generic[T]):
def __init__(self, elements: list | tuple | np.ndarray) -> None:
self._elements = np.array(elements)

def to_numpy(self) -> np.ndarray[float]:
"""Convert this vector into a numpy array of floats."""
if len(self._elements) == 0:
return np.array([])

if isinstance(self._elements[0], _QuantityBase):
return np.array([element._base_value for element in self._elements])

return np.array([float(element) for element in self._elements])

@classmethod
def __class_getitem__(cls, *_: object) -> type:
"""Return this class for type hinting."""
Expand All @@ -42,30 +52,18 @@ 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(self._elements[0], _QuantityBase):
self_elements = np.array([element._base_value for element in self._elements])
else:
self_elements = self._elements

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):
if isinstance(self._elements[0], _QuantityBase):
other_elements = np.array([element._base_value for element in other._elements])
else:
other_elements = other._elements

other_elements = other.to_numpy()
elif isinstance(other, np.ndarray):
other_elements = other

else:
raise TypeError

return self_elements * other_elements
return self.to_numpy() * other_elements

def __eq__(self, other: object) -> bool:
"""Assess if this object is the same as another."""
Expand Down

0 comments on commit 6366cea

Please sign in to comment.