From e152670ed293a032f68fb4c6c7f352d05f24e80b Mon Sep 17 00:00:00 2001 From: 298A-E9E3 <166412009+298A-E9E3@users.noreply.github.com> Date: Wed, 25 Mar 2026 10:37:40 -0400 Subject: [PATCH] Remove restrictions on operations; Numpy already has them built in This will allow for all operations to function on scalars and vectors --- src/vector_swizzling/__init__.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/vector_swizzling/__init__.py b/src/vector_swizzling/__init__.py index d8a454a..87f4730 100644 --- a/src/vector_swizzling/__init__.py +++ b/src/vector_swizzling/__init__.py @@ -1,6 +1,10 @@ import math import numpy as np from typing import Union +from typing import TypeAlias + +scalar: TypeAlias = Union[int, float] +vector: TypeAlias = Union[np.ndarray, tuple, list, "SVec"] class SVec: def __init__(self, *components): @@ -16,19 +20,19 @@ def __init__(self, *components): } flat_components = [] for item in components: - if isinstance(item, SVec): - flat_components.extend(item.components.tolist()) - elif isinstance(item, (list, tuple, np.ndarray)): + if isinstance(item, vector): flat_components.extend(item) - else: + elif isinstance(item, scalar): flat_components.append(item) + else: + flat_components.append(float(item)) self.components = np.array(flat_components, dtype=float) def __len__(self): return len(self.components) def __iter__(self): - return iter(self.components) + return iter(self.components.tolist()) def __getitem__(self, index): return self.components[index] @@ -79,22 +83,18 @@ def __setattr__(self, swizzle, other): self.components[self.lookup[component]] = other[i] def __add__(self, other): - if len(self) != len(other): - raise ValueError("Vectors must have the same size") - return self.__class__(*(self.components + other.components)) + return self.__class__(*(self.components + other)) def __sub__(self, other): - if len(self) != len(other): - raise ValueError("Vectors must have the same size") - return self.__class__(*(self.components - other.components)) + return self.__class__(*(self.components - other)) - def __mul__(self, scalar: Union[int, float]): + def __mul__(self, scalar): return self.__class__(*(self.components * scalar)) - def __truediv__(self, scalar: Union[int, float]): + def __truediv__(self, scalar): return self.__class__(*(self.components / scalar)) - def __floordiv__(self, scalar: Union[int, float]): + def __floordiv__(self, scalar): return self.__class__(*(self.components // scalar)) class SVec2(SVec):