|
1 |
| -# Copyright (c) 2022, 2024, Oracle and/or its affiliates. |
| 1 | +# Copyright (c) 2022, 2025, Oracle and/or its affiliates. |
2 | 2 | # Licensed under the Universal Permissive License v 1.0 as shown at
|
3 | 3 | # https://oss.oracle.com/licenses/upl.
|
4 | 4 |
|
5 | 5 | from __future__ import annotations
|
6 | 6 |
|
7 | 7 | import base64
|
8 |
| -import math |
9 | 8 | from abc import ABC
|
10 | 9 | from collections import OrderedDict
|
11 | 10 | from typing import Any, Dict, List, Optional, TypeVar, Union, cast
|
12 | 11 |
|
13 | 12 | import jsonpickle
|
| 13 | +import numpy as np |
14 | 14 |
|
15 | 15 | from coherence.aggregator import EntryAggregator
|
16 | 16 | from coherence.extractor import ValueExtractor
|
@@ -342,19 +342,8 @@ class Vectors:
|
342 | 342 | EPSILON = 1e-30 # Python automatically handles float precision
|
343 | 343 |
|
344 | 344 | @staticmethod
|
345 |
| - def normalize(array: List[float]) -> List[float]: |
346 |
| - norm = 0.0 |
347 |
| - c_dim = len(array) |
348 |
| - |
349 |
| - # Calculate the norm (sum of squares) |
350 |
| - for v in array: |
351 |
| - norm += v * v |
352 |
| - |
353 |
| - # Compute the normalization factor (inverse of the square root of the sum of squares) |
354 |
| - norm = 1.0 / (math.sqrt(norm) + Vectors.EPSILON) |
355 |
| - |
356 |
| - # Apply the normalization factor to each element in the array |
357 |
| - for i in range(c_dim): |
358 |
| - array[i] = array[i] * norm |
359 |
| - |
360 |
| - return array |
| 345 | + def normalize(array: list[float]) -> list[float]: |
| 346 | + np_array = np.array(array, dtype=np.float64) |
| 347 | + norm = np.linalg.norm(np_array) + Vectors.EPSILON |
| 348 | + normalized_array = np_array / norm |
| 349 | + return normalized_array.tolist() |
0 commit comments