Skip to content

coordinate

Source code in tinybig/koala/geometry/coordinate.py
class coordinate:
    def __init__(self, coords: tuple[int, ...], *args, **kwargs):
        self.coords = coords

    def __add__(self, other):
        if isinstance(other, self.__class__) and len(self.coords) == len(other.coords):
            return self.__class__(tuple(a + b for a, b in zip(self.coords, other.coords)))
        raise TypeError("Operands must be of type Coordinate and have the same dimensions")

    # Redefine the - operator
    def __sub__(self, other):
        if isinstance(other, self.__class__) and len(self.coords) == len(other.coords):
            return self.__class__(tuple(a - b for a, b in zip(self.coords, other.coords)))
        raise TypeError("Operands must be of type Coordinate and have the same dimensions")

    def __repr__(self):
        return f"{self.__class__.__name__}{self.coords}"

    # Optionally, add equality checking
    def __eq__(self, other):
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords == other.coords

    def __lt__(self, other):
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords < other.coords

    def __le__(self, other):
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords <= other.coords

    def __gt__(self, other):
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords > other.coords

    def __ge__(self, other):
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords >= other.coords

    def __hash__(self):
        return hash(self.coords)

    def dimension(self):
        return len(self.coords)

    def distance_to(self, other, kernel_name: str = 'euclidean_distance'):
        if isinstance(other, self.__class__) and len(self.coords) == len(other.coords):
            return kernel(kernel_name=kernel_name, x=torch.tensor(self.coords, dtype=torch.float), x2=torch.tensor(other.coords, dtype=torch.float)).item()
        raise TypeError("Operands must be of type Coordinate and have the same dimensions")