Skip to content

coordinate

Represents a coordinate in an N-dimensional space.

Attributes:

Name Type Description
coords tuple[int, ...]

The tuple representing the coordinate in N-dimensional space.

Methods:

Name Description
__add__

Adds another coordinate to this one.

__sub__

Subtracts another coordinate from this one.

__repr__

Returns a string representation of the coordinate.

__eq__

Checks equality of two coordinates.

__lt__

Compares if this coordinate is less than another.

__le__

Compares if this coordinate is less than or equal to another.

__gt__

Compares if this coordinate is greater than another.

__ge__

Compares if this coordinate is greater than or equal to another.

__hash__

Returns the hash of the coordinate.

dimension

Returns the dimension of the coordinate.

distance_to

Calculates the distance to another coordinate using a specified kernel.

Source code in tinybig/koala/geometry/coordinate.py
class coordinate:
    """
        Represents a coordinate in an N-dimensional space.

        Attributes
        ----------
        coords : tuple[int, ...]
            The tuple representing the coordinate in N-dimensional space.

        Methods
        -------
        __add__(other)
            Adds another coordinate to this one.
        __sub__(other)
            Subtracts another coordinate from this one.
        __repr__()
            Returns a string representation of the coordinate.
        __eq__(other)
            Checks equality of two coordinates.
        __lt__(other)
            Compares if this coordinate is less than another.
        __le__(other)
            Compares if this coordinate is less than or equal to another.
        __gt__(other)
            Compares if this coordinate is greater than another.
        __ge__(other)
            Compares if this coordinate is greater than or equal to another.
        __hash__()
            Returns the hash of the coordinate.
        dimension()
            Returns the dimension of the coordinate.
        distance_to(other, kernel_name='euclidean_distance')
            Calculates the distance to another coordinate using a specified kernel.
    """
    def __init__(self, coords: tuple[int, ...], *args, **kwargs):
        """
            Initializes a generic N-dimensional coordinate object.

            This class serves as a base for representing a point in an N-dimensional
            space, defined by a tuple of integer coordinates.

            Parameters
            ----------
            coords : tuple[int, ...]
                A tuple representing the N-dimensional coordinates of the point.
            *args, **kwargs
                Additional arguments for future extensibility.

            Attributes
            ----------
            coords : tuple[int, ...]
                Stores the coordinates of the point in N-dimensional space.

        """
        self.coords = coords

    def __add__(self, other):
        """
            Adds another coordinate to this one.

            Parameters
            ----------
            other : coordinate
                The coordinate to add.

            Returns
            -------
            coordinate
                The resulting coordinate after addition.

            Raises
            ------
            TypeError
                If the operands are not of the same type or dimensions.
        """
        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):
        """
            Subtracts another coordinate from this one.

            Parameters
            ----------
            other : coordinate
                The coordinate to subtract.

            Returns
            -------
            coordinate
                The resulting coordinate after subtraction.

            Raises
            ------
            TypeError
                If the operands are not of the same type or dimensions.
        """
        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):
        """
            Returns a string representation of the coordinate.

            Returns
            -------
            str
                The string representation of the coordinate.
        """
        return f"{self.__class__.__name__}{self.coords}"

    # Optionally, add equality checking
    def __eq__(self, other):
        """
            Checks equality of two coordinates.

            Parameters
            ----------
            other : coordinate
                The coordinate to compare.

            Returns
            -------
            bool
                True if coordinates are equal, False otherwise.
        """
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords == other.coords

    def __lt__(self, other):
        """
            Checks if this coordinate is less than another coordinate.

            Parameters
            ----------
            other : coordinate
                The coordinate to compare.

            Returns
            -------
            bool
                True if this coordinate is less than or equal to the other coordinate, False otherwise.

            Raises
            ------
            TypeError
                If the coordinates are not of the same type or dimension.
        """
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords < other.coords

    def __le__(self, other):
        """
            Checks if this coordinate is less than or equal to another coordinate.

            Parameters
            ----------
            other : coordinate
                The coordinate to compare.

            Returns
            -------
            bool
                True if this coordinate is less than or equal to the other coordinate, False otherwise.

            Raises
            ------
            TypeError
                If the coordinates are not of the same type or dimension.
        """
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords <= other.coords

    def __gt__(self, other):
        """
            Checks if this coordinate is greater than another coordinate.

            Parameters
            ----------
            other : coordinate
                The coordinate to compare.

            Returns
            -------
            bool
                True if this coordinate is greater than the other coordinate, False otherwise.

            Raises
            ------
            TypeError
                If the coordinates are not of the same type or dimension.
        """
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords > other.coords

    def __ge__(self, other):
        """
            Checks if this coordinate is greater than or equal to another coordinate.

            Parameters
            ----------
            other : coordinate
                The coordinate to compare.

            Returns
            -------
            bool
                True if this coordinate is greater than or equal to the other coordinate, False otherwise.

            Raises
            ------
            TypeError
                If the coordinates are not of the same type or dimension.
        """
        return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords >= other.coords

    def __hash__(self):
        """
            Returns the hash value of the coordinate.

            Returns
            -------
            int
                The hash value based on the coordinate's tuple representation.
        """
        return hash(self.coords)

    def dimension(self):
        """
            Returns the dimension of the coordinate.

            Returns
            -------
            int
                The dimension of the coordinate.
        """
        return len(self.coords)

    def distance_to(self, other, kernel_name: str = 'euclidean_distance'):
        """
            Calculates the distance to another coordinate using a specified kernel.

            Parameters
            ----------
            other : coordinate
                The coordinate to calculate the distance to.
            kernel_name : str, optional
                The name of the kernel to use for distance calculation (default is 'euclidean_distance').

            Returns
            -------
            float
                The distance to the other coordinate.

            Raises
            ------
            TypeError
                If the operands are not of the same type or dimensions.
        """
        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")

__add__(other)

Adds another coordinate to this one.

Parameters:

Name Type Description Default
other coordinate

The coordinate to add.

required

Returns:

Type Description
coordinate

The resulting coordinate after addition.

Raises:

Type Description
TypeError

If the operands are not of the same type or dimensions.

Source code in tinybig/koala/geometry/coordinate.py
def __add__(self, other):
    """
        Adds another coordinate to this one.

        Parameters
        ----------
        other : coordinate
            The coordinate to add.

        Returns
        -------
        coordinate
            The resulting coordinate after addition.

        Raises
        ------
        TypeError
            If the operands are not of the same type or dimensions.
    """
    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")

__eq__(other)

Checks equality of two coordinates.

Parameters:

Name Type Description Default
other coordinate

The coordinate to compare.

required

Returns:

Type Description
bool

True if coordinates are equal, False otherwise.

Source code in tinybig/koala/geometry/coordinate.py
def __eq__(self, other):
    """
        Checks equality of two coordinates.

        Parameters
        ----------
        other : coordinate
            The coordinate to compare.

        Returns
        -------
        bool
            True if coordinates are equal, False otherwise.
    """
    return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords == other.coords

__ge__(other)

Checks if this coordinate is greater than or equal to another coordinate.

Parameters:

Name Type Description Default
other coordinate

The coordinate to compare.

required

Returns:

Type Description
bool

True if this coordinate is greater than or equal to the other coordinate, False otherwise.

Raises:

Type Description
TypeError

If the coordinates are not of the same type or dimension.

Source code in tinybig/koala/geometry/coordinate.py
def __ge__(self, other):
    """
        Checks if this coordinate is greater than or equal to another coordinate.

        Parameters
        ----------
        other : coordinate
            The coordinate to compare.

        Returns
        -------
        bool
            True if this coordinate is greater than or equal to the other coordinate, False otherwise.

        Raises
        ------
        TypeError
            If the coordinates are not of the same type or dimension.
    """
    return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords >= other.coords

__gt__(other)

Checks if this coordinate is greater than another coordinate.

Parameters:

Name Type Description Default
other coordinate

The coordinate to compare.

required

Returns:

Type Description
bool

True if this coordinate is greater than the other coordinate, False otherwise.

Raises:

Type Description
TypeError

If the coordinates are not of the same type or dimension.

Source code in tinybig/koala/geometry/coordinate.py
def __gt__(self, other):
    """
        Checks if this coordinate is greater than another coordinate.

        Parameters
        ----------
        other : coordinate
            The coordinate to compare.

        Returns
        -------
        bool
            True if this coordinate is greater than the other coordinate, False otherwise.

        Raises
        ------
        TypeError
            If the coordinates are not of the same type or dimension.
    """
    return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords > other.coords

__hash__()

Returns the hash value of the coordinate.

Returns:

Type Description
int

The hash value based on the coordinate's tuple representation.

Source code in tinybig/koala/geometry/coordinate.py
def __hash__(self):
    """
        Returns the hash value of the coordinate.

        Returns
        -------
        int
            The hash value based on the coordinate's tuple representation.
    """
    return hash(self.coords)

__init__(coords, *args, **kwargs)

Initializes a generic N-dimensional coordinate object.

This class serves as a base for representing a point in an N-dimensional space, defined by a tuple of integer coordinates.

Parameters:

Name Type Description Default
coords tuple[int, ...]

A tuple representing the N-dimensional coordinates of the point.

required
*args

Additional arguments for future extensibility.

()
**kwargs

Additional arguments for future extensibility.

()

Attributes:

Name Type Description
coords tuple[int, ...]

Stores the coordinates of the point in N-dimensional space.

Source code in tinybig/koala/geometry/coordinate.py
def __init__(self, coords: tuple[int, ...], *args, **kwargs):
    """
        Initializes a generic N-dimensional coordinate object.

        This class serves as a base for representing a point in an N-dimensional
        space, defined by a tuple of integer coordinates.

        Parameters
        ----------
        coords : tuple[int, ...]
            A tuple representing the N-dimensional coordinates of the point.
        *args, **kwargs
            Additional arguments for future extensibility.

        Attributes
        ----------
        coords : tuple[int, ...]
            Stores the coordinates of the point in N-dimensional space.

    """
    self.coords = coords

__le__(other)

Checks if this coordinate is less than or equal to another coordinate.

Parameters:

Name Type Description Default
other coordinate

The coordinate to compare.

required

Returns:

Type Description
bool

True if this coordinate is less than or equal to the other coordinate, False otherwise.

Raises:

Type Description
TypeError

If the coordinates are not of the same type or dimension.

Source code in tinybig/koala/geometry/coordinate.py
def __le__(self, other):
    """
        Checks if this coordinate is less than or equal to another coordinate.

        Parameters
        ----------
        other : coordinate
            The coordinate to compare.

        Returns
        -------
        bool
            True if this coordinate is less than or equal to the other coordinate, False otherwise.

        Raises
        ------
        TypeError
            If the coordinates are not of the same type or dimension.
    """
    return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords <= other.coords

__lt__(other)

Checks if this coordinate is less than another coordinate.

Parameters:

Name Type Description Default
other coordinate

The coordinate to compare.

required

Returns:

Type Description
bool

True if this coordinate is less than or equal to the other coordinate, False otherwise.

Raises:

Type Description
TypeError

If the coordinates are not of the same type or dimension.

Source code in tinybig/koala/geometry/coordinate.py
def __lt__(self, other):
    """
        Checks if this coordinate is less than another coordinate.

        Parameters
        ----------
        other : coordinate
            The coordinate to compare.

        Returns
        -------
        bool
            True if this coordinate is less than or equal to the other coordinate, False otherwise.

        Raises
        ------
        TypeError
            If the coordinates are not of the same type or dimension.
    """
    return isinstance(other, self.__class__) and self.dimension() == other.dimension() and self.coords < other.coords

__repr__()

Returns a string representation of the coordinate.

Returns:

Type Description
str

The string representation of the coordinate.

Source code in tinybig/koala/geometry/coordinate.py
def __repr__(self):
    """
        Returns a string representation of the coordinate.

        Returns
        -------
        str
            The string representation of the coordinate.
    """
    return f"{self.__class__.__name__}{self.coords}"

__sub__(other)

Subtracts another coordinate from this one.

Parameters:

Name Type Description Default
other coordinate

The coordinate to subtract.

required

Returns:

Type Description
coordinate

The resulting coordinate after subtraction.

Raises:

Type Description
TypeError

If the operands are not of the same type or dimensions.

Source code in tinybig/koala/geometry/coordinate.py
def __sub__(self, other):
    """
        Subtracts another coordinate from this one.

        Parameters
        ----------
        other : coordinate
            The coordinate to subtract.

        Returns
        -------
        coordinate
            The resulting coordinate after subtraction.

        Raises
        ------
        TypeError
            If the operands are not of the same type or dimensions.
    """
    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")

dimension()

Returns the dimension of the coordinate.

Returns:

Type Description
int

The dimension of the coordinate.

Source code in tinybig/koala/geometry/coordinate.py
def dimension(self):
    """
        Returns the dimension of the coordinate.

        Returns
        -------
        int
            The dimension of the coordinate.
    """
    return len(self.coords)

distance_to(other, kernel_name='euclidean_distance')

Calculates the distance to another coordinate using a specified kernel.

Parameters:

Name Type Description Default
other coordinate

The coordinate to calculate the distance to.

required
kernel_name str

The name of the kernel to use for distance calculation (default is 'euclidean_distance').

'euclidean_distance'

Returns:

Type Description
float

The distance to the other coordinate.

Raises:

Type Description
TypeError

If the operands are not of the same type or dimensions.

Source code in tinybig/koala/geometry/coordinate.py
def distance_to(self, other, kernel_name: str = 'euclidean_distance'):
    """
        Calculates the distance to another coordinate using a specified kernel.

        Parameters
        ----------
        other : coordinate
            The coordinate to calculate the distance to.
        kernel_name : str, optional
            The name of the kernel to use for distance calculation (default is 'euclidean_distance').

        Returns
        -------
        float
            The distance to the other coordinate.

        Raises
        ------
        TypeError
            If the operands are not of the same type or dimensions.
    """
    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")