Return the original vectors if they are already normalized. Otherwise, the vectors are normalized, and a new array is returned.
(vectors: npt.NDArray)
| 43 | |
| 44 | |
| 45 | def normalize_or_copy(vectors: npt.NDArray) -> npt.NDArray: |
| 46 | """ |
| 47 | Return the original vectors if they are already normalized. |
| 48 | |
| 49 | Otherwise, the vectors are normalized, and a new array is returned. |
| 50 | """ |
| 51 | norms = np.linalg.norm(vectors, axis=-1) |
| 52 | all_unit_length = np.allclose(norms[norms != 0], 1) |
| 53 | if all_unit_length: |
| 54 | return vectors |
| 55 | return normalize(vectors, norms) |
| 56 | |
| 57 | |
| 58 | class Metric(Enum): |
searching dependent graphs…