Skip to content

Commit

Permalink
perf: use numpy conversions for Vector.from_numpy()
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Dec 10, 2024
1 parent e3d28f4 commit 86053da
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion quantio/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ def from_numpy(
cls, array: np.ndarray, element_class: type[Quantity], unit: str
) -> Vector[Quantity]:
"""Construct a quantity vector from a numpy array."""
return Vector([element_class(**{unit: elem}) for elem in array])
vector: Vector[Quantity] = Vector([0])

if unit == element_class.BASE_UNIT:
vector._elements = array
else:
conversion_factor = getattr(element_class(1), unit)
vector._elements = array / conversion_factor

vector._quantitiy = element_class
return vector

def to_numpy(self, unit: str | None = None) -> np.ndarray[float]:
"""Convert this vector into a numpy array of floats."""
Expand Down

0 comments on commit 86053da

Please sign in to comment.