Skip to content

Class swarmauri_core.pseudometrics.IPseudometric.IPseudometric

swarmauri_core.pseudometrics.IPseudometric.IPseudometric

Bases: ABC

Interface for a pseudometric distance function.

A pseudometric satisfies: 1. Non-negativity: d(x,y) ≥ 0 2. Symmetry: d(x,y) = d(y,x) 3. Triangle inequality: d(x,z) ≤ d(x,y) + d(y,z)

Unlike a metric, a pseudometric allows d(x,y) = 0 for x ≠ y, meaning it may not distinguish between distinct points.

distance abstractmethod

distance(x, y)

Calculate the pseudometric distance between two objects.

Parameters

x : Union[VectorType, MatrixType, Sequence[T], str, Callable] The first object y : Union[VectorType, MatrixType, Sequence[T], str, Callable] The second object

Returns

float The distance between x and y

Raises

TypeError If inputs are of incompatible types ValueError If inputs have incompatible dimensions

Source code in swarmauri_core/pseudometrics/IPseudometric.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@abstractmethod
def distance(
    self,
    x: Union[VectorType, MatrixType, Sequence[T], str, Callable],
    y: Union[VectorType, MatrixType, Sequence[T], str, Callable],
) -> float:
    """
    Calculate the pseudometric distance between two objects.

    Parameters
    ----------
    x : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The first object
    y : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The second object

    Returns
    -------
    float
        The distance between x and y

    Raises
    ------
    TypeError
        If inputs are of incompatible types
    ValueError
        If inputs have incompatible dimensions
    """
    pass

distances abstractmethod

distances(xs, ys)

Calculate the pairwise distances between two collections of objects.

Parameters

xs : Sequence[Union[VectorType, MatrixType, Sequence[T], str, Callable]] The first collection of objects ys : Sequence[Union[VectorType, MatrixType, Sequence[T], str, Callable]] The second collection of objects

Returns

List[List[float]] A matrix of distances where distances[i][j] is the distance between xs[i] and ys[j]

Raises

TypeError If inputs contain incompatible types ValueError If inputs have incompatible dimensions

Source code in swarmauri_core/pseudometrics/IPseudometric.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@abstractmethod
def distances(
    self,
    xs: Sequence[Union[VectorType, MatrixType, Sequence[T], str, Callable]],
    ys: Sequence[Union[VectorType, MatrixType, Sequence[T], str, Callable]],
) -> List[List[float]]:
    """
    Calculate the pairwise distances between two collections of objects.

    Parameters
    ----------
    xs : Sequence[Union[VectorType, MatrixType, Sequence[T], str, Callable]]
        The first collection of objects
    ys : Sequence[Union[VectorType, MatrixType, Sequence[T], str, Callable]]
        The second collection of objects

    Returns
    -------
    List[List[float]]
        A matrix of distances where distances[i][j] is the distance between xs[i] and ys[j]

    Raises
    ------
    TypeError
        If inputs contain incompatible types
    ValueError
        If inputs have incompatible dimensions
    """
    pass

check_non_negativity abstractmethod

check_non_negativity(x, y)

Check if the distance function satisfies the non-negativity property.

Parameters

x : Union[VectorType, MatrixType, Sequence[T], str, Callable] The first object y : Union[VectorType, MatrixType, Sequence[T], str, Callable] The second object

Returns

bool True if d(x,y) ≥ 0, False otherwise

Source code in swarmauri_core/pseudometrics/IPseudometric.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@abstractmethod
def check_non_negativity(
    self,
    x: Union[VectorType, MatrixType, Sequence[T], str, Callable],
    y: Union[VectorType, MatrixType, Sequence[T], str, Callable],
) -> bool:
    """
    Check if the distance function satisfies the non-negativity property.

    Parameters
    ----------
    x : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The first object
    y : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The second object

    Returns
    -------
    bool
        True if d(x,y) ≥ 0, False otherwise
    """
    pass

check_symmetry abstractmethod

check_symmetry(x, y, tolerance=1e-10)

Check if the distance function satisfies the symmetry property.

Parameters

x : Union[VectorType, MatrixType, Sequence[T], str, Callable] The first object y : Union[VectorType, MatrixType, Sequence[T], str, Callable] The second object tolerance : float, optional The tolerance for floating-point comparisons, by default 1e-10

Returns

bool True if d(x,y) = d(y,x) within tolerance, False otherwise

Source code in swarmauri_core/pseudometrics/IPseudometric.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
@abstractmethod
def check_symmetry(
    self,
    x: Union[VectorType, MatrixType, Sequence[T], str, Callable],
    y: Union[VectorType, MatrixType, Sequence[T], str, Callable],
    tolerance: float = 1e-10,
) -> bool:
    """
    Check if the distance function satisfies the symmetry property.

    Parameters
    ----------
    x : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The first object
    y : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The second object
    tolerance : float, optional
        The tolerance for floating-point comparisons, by default 1e-10

    Returns
    -------
    bool
        True if d(x,y) = d(y,x) within tolerance, False otherwise
    """
    pass

check_triangle_inequality abstractmethod

check_triangle_inequality(x, y, z, tolerance=1e-10)

Check if the distance function satisfies the triangle inequality.

Parameters

x : Union[VectorType, MatrixType, Sequence[T], str, Callable] The first object y : Union[VectorType, MatrixType, Sequence[T], str, Callable] The second object z : Union[VectorType, MatrixType, Sequence[T], str, Callable] The third object tolerance : float, optional The tolerance for floating-point comparisons, by default 1e-10

Returns

bool True if d(x,z) ≤ d(x,y) + d(y,z) within tolerance, False otherwise

Source code in swarmauri_core/pseudometrics/IPseudometric.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@abstractmethod
def check_triangle_inequality(
    self,
    x: Union[VectorType, MatrixType, Sequence[T], str, Callable],
    y: Union[VectorType, MatrixType, Sequence[T], str, Callable],
    z: Union[VectorType, MatrixType, Sequence[T], str, Callable],
    tolerance: float = 1e-10,
) -> bool:
    """
    Check if the distance function satisfies the triangle inequality.

    Parameters
    ----------
    x : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The first object
    y : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The second object
    z : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The third object
    tolerance : float, optional
        The tolerance for floating-point comparisons, by default 1e-10

    Returns
    -------
    bool
        True if d(x,z) ≤ d(x,y) + d(y,z) within tolerance, False otherwise
    """
    pass

check_weak_identity abstractmethod

check_weak_identity(x, y)

Check if the distance function satisfies the weak identity property.

In a pseudometric, d(x,y) = 0 is allowed even when x ≠ y. This method verifies that this property is properly handled.

Parameters

x : Union[VectorType, MatrixType, Sequence[T], str, Callable] The first object y : Union[VectorType, MatrixType, Sequence[T], str, Callable] The second object

Returns

bool True if the pseudometric properly handles the weak identity property

Source code in swarmauri_core/pseudometrics/IPseudometric.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
@abstractmethod
def check_weak_identity(
    self,
    x: Union[VectorType, MatrixType, Sequence[T], str, Callable],
    y: Union[VectorType, MatrixType, Sequence[T], str, Callable],
) -> bool:
    """
    Check if the distance function satisfies the weak identity property.

    In a pseudometric, d(x,y) = 0 is allowed even when x ≠ y.
    This method verifies that this property is properly handled.

    Parameters
    ----------
    x : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The first object
    y : Union[VectorType, MatrixType, Sequence[T], str, Callable]
        The second object

    Returns
    -------
    bool
        True if the pseudometric properly handles the weak identity property
    """
    pass