MCPcopy Create free account
hub / github.com/MinishLab/vicinity / EuclideanBasicBackend

Class EuclideanBasicBackend

vicinity/backends/basic.py:231–247  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

229
230
231class EuclideanBasicBackend(BasicBackend):
232 def __init__(self, vectors: npt.NDArray, arguments: BasicArgs) -> None:
233 """Initialize the Euclidean basic backend."""
234 super().__init__(vectors=vectors, arguments=arguments)
235 self.squared_norm_vectors = (self._vectors**2).sum(1)
236
237 def _update_precomputed_data(self) -> None:
238 """Update precomputed data for Euclidean distance."""
239 self.squared_norm_vectors = (self._vectors**2).sum(1)
240
241 def _dist(self, x: npt.NDArray) -> npt.NDArray:
242 """Compute Euclidean distance."""
243 x_norm = (x**2).sum(1)
244 dists_squared = (x_norm[:, None] + self.squared_norm_vectors[None, :]) - 2 * (x @ self._vectors.T)
245 # Ensure non-negative distances
246 dists_squared = np.clip(dists_squared, 0, None)
247 return np.sqrt(dists_squared)

Callers 2

from_vectorsMethod · 0.85
loadMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…