Skip to content

Commit ca083c2

Browse files
committed
test: unit test vector_distance_f32 #47
1 parent ef244df commit ca083c2

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

tests/test_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
from objectbox.utils import *
4+
5+
6+
def test_vector_distance_f32():
7+
""" Tests distance values between two vectors. """
8+
9+
a = np.array([3.4, 2.9, -10, 1.0], dtype=np.float32)
10+
b = np.array([56., -1.2, 22, 2.0], dtype=np.float32)
11+
12+
a_norm = a / np.linalg.norm(a)
13+
b_norm = b / np.linalg.norm(b)
14+
15+
assert vector_distance_f32(VectorDistanceType.EUCLIDEAN, a, b, 4) == pytest.approx(np.dot(b - a, b - a))
16+
assert vector_distance_f32(VectorDistanceType.COSINE, a, b, 4) == pytest.approx(1.0469311)
17+
assert vector_distance_f32(VectorDistanceType.DOT_PRODUCT, a_norm, b_norm, 4) == pytest.approx(1.0469311)
18+
assert vector_distance_f32(VectorDistanceType.DOT_PRODUCT_NON_NORMALIZED, a, b, 4) == pytest.approx(1.519307)

0 commit comments

Comments
 (0)