From 86053da916979b02490ad0d6b031d3bf248b6183 Mon Sep 17 00:00:00 2001 From: unexcellent <> Date: Tue, 10 Dec 2024 11:25:26 +0100 Subject: [PATCH] perf: use numpy conversions for Vector.from_numpy() --- quantio/vector.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/quantio/vector.py b/quantio/vector.py index 31fde90..953dde1 100644 --- a/quantio/vector.py +++ b/quantio/vector.py @@ -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."""