Skip to content

Commit

Permalink
Added Vector element type for data persistence util.
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC committed Mar 7, 2024
1 parent 4723ed6 commit 1441049
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion leads/data_persistence.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from copy import copy as _copy
from typing import TextIO as _TextIO, TypeVar as _TypeVar, Generic as _Generic, Sequence as _Sequence, \
override as _override
override as _override, Self as _Self, Any as _Any, Iterator as _Iterator

from numpy import mean as _mean

Expand Down Expand Up @@ -103,3 +103,34 @@ def append(self, element: T) -> None:
for e in self._compressor(self._chunk, self._chunk_scale):
self._push_to_data(e)
self._chunk.clear()


class Vector(object):
def __init__(self, *coordinates: _Any) -> None:
self._d: int = len(coordinates)
self._coordinates: tuple[_Any, ...] = coordinates

def __len__(self) -> int:
return self._d

def __iter__(self) -> _Iterator[_Any]:
return iter(self._coordinates)

def __getitem__(self, item: int | slice) -> _Self:
return Vector(*self._coordinates[item])

def _check_dimension(self, other: _Self) -> None:
if other._d != self._d:
raise ValueError("Cannot perform this operation on two vectors of different dimensions")

def __add__(self, other: _Self) -> _Self:
self._check_dimension(other)
return Vector(*(self._coordinates[i] + other._coordinates[i] for i in range(self._d)))

def __sub__(self, other: _Self) -> _Self:
self._check_dimension(other)
return Vector(*(self._coordinates[i] - other._coordinates[i] for i in range(self._d)))

def __mul__(self, other: _Self) -> _Self:
self._check_dimension(other)
return Vector(*(self._coordinates[i] * other._coordinates[i] for i in range(self._d)))

0 comments on commit 1441049

Please sign in to comment.