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

Function normalize

vicinity/utils.py:9–42  ·  view source on GitHub ↗

Normalize a matrix of row vectors to unit length. Contains a shortcut if there are no zero vectors in the matrix. If there are zero vectors, we do some indexing tricks to avoid dividing by 0. :param vectors: The vectors to normalize. :param norms: Precomputed norms. If thi

(vectors: npt.NDArray, norms: npt.NDArray | None = None)

Source from the content-addressed store, hash-verified

7
8
9def normalize(vectors: npt.NDArray, norms: npt.NDArray | None = None) -> npt.NDArray:
10 """
11 Normalize a matrix of row vectors to unit length.
12
13 Contains a shortcut if there are no zero vectors in the matrix.
14 If there are zero vectors, we do some indexing tricks to avoid
15 dividing by 0.
16
17 :param vectors: The vectors to normalize.
18 :param norms: Precomputed norms. If this is None, the norms are computed.
19 :return: The input vectors, normalized to unit length.
20 """
21 if np.ndim(vectors) == 1:
22 norm_float = np.linalg.norm(vectors)
23 if np.isclose(norm_float, 0):
24 return np.zeros_like(vectors)
25 return vectors / norm_float
26
27 if norms is None:
28 norm: npt.NDArray = np.linalg.norm(vectors, axis=1)
29 else:
30 norm = norms
31
32 if np.any(np.isclose(norm, 0.0)):
33 vectors = np.copy(vectors)
34 nonzero = norm > 0.0
35 result = np.zeros_like(vectors)
36 masked_norm = norm[nonzero]
37 masked_vectors = vectors[nonzero]
38 result[nonzero] = masked_vectors / masked_norm[:, None]
39
40 return result
41 else:
42 return vectors / norm[:, None]
43
44
45def normalize_or_copy(vectors: npt.NDArray) -> npt.NDArray:

Callers 9

test_normalizeFunction · 0.90
_distMethod · 0.90
from_vectorsMethod · 0.90
queryMethod · 0.90
insertMethod · 0.90
thresholdMethod · 0.90
from_vectorsMethod · 0.90
queryMethod · 0.90
normalize_or_copyFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_normalizeFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…