Skip to content

Commit 8d3e321

Browse files
committed
updated generic code in base_vector.py
1 parent a076b42 commit 8d3e321

1 file changed

Lines changed: 20 additions & 42 deletions

File tree

src/ncca/ngl/vector_base.py

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def __init__(self, *args: float, **kwargs: float) -> None:
4040
else:
4141
self._init_from_args(args)
4242

43-
def _init_from_kwargs(
44-
self, args: tuple[float, ...], kwargs: dict[str, float]
45-
) -> None:
43+
def _init_from_kwargs(self, args: tuple[float, ...], kwargs: dict[str, float]) -> None:
4644
"""Initialize vector from keyword arguments."""
4745
self._validate_component_count(len(args) + len(kwargs))
4846

@@ -68,13 +66,9 @@ def _init_from_args(self, args: tuple[float, ...]) -> None:
6866
def _validate_component_count(self, count: int) -> None:
6967
"""Validate the number of components doesn't exceed dimension."""
7068
if count > self.DIMENSION:
71-
raise ValueError(
72-
f"{self.__class__.__name__} requires at most {self.DIMENSION} components"
73-
)
69+
raise ValueError(f"{self.__class__.__name__} requires at most {self.DIMENSION} components")
7470

75-
def _set_positional_args(
76-
self, values: list[float], args: tuple[float, ...]
77-
) -> None:
71+
def _set_positional_args(self, values: list[float], args: tuple[float, ...]) -> None:
7872
"""Set positional argument values."""
7973
for i, arg in enumerate(args):
8074
values[i] = float(arg)
@@ -156,9 +150,7 @@ def __add__(self, rhs: Self) -> Self:
156150
VectorBase: A new vector that is the result of adding this vector and the rhs vector.
157151
"""
158152
if not isinstance(rhs, self.__class__):
159-
raise ValueError(
160-
f"Can only add {self.__class__.__name__} to {self.__class__.__name__}"
161-
)
153+
raise ValueError(f"Can only add {self.__class__.__name__} to {self.__class__.__name__}")
162154
result = self.__class__()
163155
result._data = self._data + rhs._data
164156
return result
@@ -174,9 +166,7 @@ def __iadd__(self, rhs: Self) -> Self:
174166
VectorBase: Returns this vector after adding the rhs vector.
175167
"""
176168
if not isinstance(rhs, self.__class__):
177-
raise ValueError(
178-
f"Can only add {self.__class__.__name__} to {self.__class__.__name__}"
179-
)
169+
raise ValueError(f"Can only add {self.__class__.__name__} to {self.__class__.__name__}")
180170
self._data += rhs._data
181171
return self
182172

@@ -191,9 +181,7 @@ def __sub__(self, rhs: Self) -> Self:
191181
VectorBase: A new vector that is the result of subtracting this vector and the rhs vector.
192182
"""
193183
if not isinstance(rhs, self.__class__):
194-
raise ValueError(
195-
f"Can only subtract {self.__class__.__name__} from {self.__class__.__name__}"
196-
)
184+
raise ValueError(f"Can only subtract {self.__class__.__name__} from {self.__class__.__name__}")
197185
result = self.__class__()
198186
result._data = self._data - rhs._data
199187
return result
@@ -209,9 +197,7 @@ def __isub__(self, rhs: Self) -> Self:
209197
VectorBase: Returns this vector after subtracting the rhs vector.
210198
"""
211199
if not isinstance(rhs, self.__class__):
212-
raise ValueError(
213-
f"Can only subtract {self.__class__.__name__} from {self.__class__.__name__}"
214-
)
200+
raise ValueError(f"Can only subtract {self.__class__.__name__} from {self.__class__.__name__}")
215201
self._data -= rhs._data
216202
return self
217203

@@ -274,11 +260,9 @@ def __mul__(self, rhs: Union[float, int]) -> Self:
274260
result._data = self._data * rhs
275261
return result
276262
else:
277-
raise ValueError(
278-
f"Can only do piecewise multiplication with a scalar, got {type(rhs)}"
279-
)
263+
raise ValueError(f"Can only do piecewise multiplication with a scalar, got {type(rhs)}")
280264

281-
def __rmul__(self, rhs: Union[float, int]) -> T:
265+
def __rmul__(self, rhs: Union[float, int]) -> Self:
282266
"""
283267
Piecewise scalar multiplication (right operand).
284268
@@ -290,7 +274,7 @@ def __rmul__(self, rhs: Union[float, int]) -> T:
290274
"""
291275
return self * rhs
292276

293-
def __truediv__(self, rhs: Union[float, int, T]) -> T:
277+
def __truediv__(self, rhs: Union[float, int, Self]) -> Self:
294278
"""
295279
Piecewise scalar or vector division.
296280
@@ -321,7 +305,7 @@ def __truediv__(self, rhs: Union[float, int, T]) -> T:
321305
f"Can only do piecewise division with a scalar or {self.__class__.__name__}, got {type(rhs)}"
322306
)
323307

324-
def dot(self, rhs: T) -> float:
308+
def dot(self, rhs: Self) -> float:
325309
"""
326310
Dot product of two vectors a.b.
327311
@@ -332,9 +316,7 @@ def dot(self, rhs: T) -> float:
332316
float: The dot product of the two vectors.
333317
"""
334318
if not isinstance(rhs, self.__class__):
335-
raise ValueError(
336-
f"Can only compute dot product with {self.__class__.__name__}"
337-
)
319+
raise ValueError(f"Can only compute dot product with {self.__class__.__name__}")
338320
return np.dot(self._data, rhs._data)
339321

340322
def length(self) -> float:
@@ -355,7 +337,7 @@ def length_squared(self) -> float:
355337
"""
356338
return np.dot(self._data, self._data)
357339

358-
def inner(self, rhs: T) -> float:
340+
def inner(self, rhs: Self) -> float:
359341
"""
360342
Inner product of two vectors a.b (alias for dot product).
361343
@@ -367,7 +349,7 @@ def inner(self, rhs: T) -> float:
367349
"""
368350
return self.dot(rhs)
369351

370-
def normalize(self) -> T:
352+
def normalize(self) -> Self:
371353
"""
372354
Normalize the vector to unit length.
373355
@@ -429,7 +411,7 @@ def to_tuple(self) -> tuple:
429411

430412
# Abstract methods that must be implemented by subclasses
431413
@abstractmethod
432-
def cross(self, rhs: T) -> Union[T, float]:
414+
def cross(self, rhs: Self) -> Union[Self, float]:
433415
"""
434416
Cross product of two vectors a x b.
435417
@@ -442,7 +424,7 @@ def cross(self, rhs: T) -> Union[T, float]:
442424
pass
443425

444426
@abstractmethod
445-
def reflect(self, n: T) -> T:
427+
def reflect(self, n: Self) -> Self:
446428
"""
447429
Reflect a vector about a normal.
448430
@@ -455,7 +437,7 @@ def reflect(self, n: T) -> T:
455437
pass
456438

457439
@abstractmethod
458-
def outer(self, rhs: T) -> Any:
440+
def outer(self, rhs: Self) -> Any:
459441
"""
460442
Outer product of two vectors a x b.
461443
@@ -468,7 +450,7 @@ def outer(self, rhs: T) -> Any:
468450
pass
469451

470452
@abstractmethod
471-
def __matmul__(self, rhs: Any) -> T:
453+
def __matmul__(self, rhs: Any) -> Self:
472454
"""
473455
Matrix multiplication Vector @ Matrix.
474456
@@ -513,9 +495,7 @@ def __getattr__(self, name: str):
513495
Raises:
514496
AttributeError: If the attribute does not exist.
515497
"""
516-
raise AttributeError(
517-
f"'{self.__class__.__name__}' object has no attribute '{name}'"
518-
)
498+
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
519499

520500
def __setattr__(self, name: str, value: Any) -> None:
521501
"""
@@ -535,9 +515,7 @@ def __setattr__(self, name: str, value: Any) -> None:
535515
elif name in self.COMPONENT_NAMES:
536516
super().__setattr__(name, value)
537517
else:
538-
raise AttributeError(
539-
f"'{self.__class__.__name__}' object has no attribute '{name}'"
540-
)
518+
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
541519

542520

543521
def _create_properties(cls: type) -> None:

0 commit comments

Comments
 (0)