From b6995ca0c3cb33975a4d7767bfc74e0013be0fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo?= <88985821+Joao-Paulo-Silva@users.noreply.github.com> Date: Mon, 16 Feb 2026 16:27:58 -0300 Subject: [PATCH 1/3] fix(distance): remove incorrect distance normalization --- aisp/utils/distance.py | 6 +++--- aisp/utils/tests/test_distance.py | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/aisp/utils/distance.py b/aisp/utils/distance.py index ec98b2b..5685b30 100644 --- a/aisp/utils/distance.py +++ b/aisp/utils/distance.py @@ -61,7 +61,7 @@ def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> float64 @njit() def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64: - r"""Calculate the normalized Manhattan distance between two points. + r"""Calculate the Manhattan distance between two points. .. math:: \frac{(|X_{1} - Y_{1}| + |X_{2} - Y_{2}| + \cdots + |X_{n} - Y_{n}|)}{n} @@ -82,7 +82,7 @@ def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64: if n == 0: return float64(-1.0) - return float64(np.sum(np.abs(u - v)) / n) + return float64(np.sum(np.abs(u - v))) @njit() @@ -120,7 +120,7 @@ def minkowski( if n == 0: return float64(-1.0) - return float64((np.sum(np.abs(u - v) ** p) ** (1 / p)) / n) + return float64(np.sum(np.abs(u - v) ** p) ** (1 / p)) @njit([(types.float64[:], types.float64[:], types.int32, types.float64)], cache=True) diff --git a/aisp/utils/tests/test_distance.py b/aisp/utils/tests/test_distance.py index cf8b878..db1e222 100644 --- a/aisp/utils/tests/test_distance.py +++ b/aisp/utils/tests/test_distance.py @@ -51,8 +51,8 @@ def test_hamming(u, v, expected_output): @pytest.mark.parametrize( "u, v, expected_output", [ - (np.array([1, 2, 3]), np.array([4, 5, 6]), 3), - (np.array([0, 0]), np.array([3, 4]), 3.5), + (np.array([1, 2, 3]), np.array([4, 5, 6]), 9), + (np.array([0, 0]), np.array([3, 4]), 7), ], ids=[ "Manhattan Distance - 3 dimensions", @@ -68,9 +68,9 @@ def test_cityblock(u, v, expected_output): @pytest.mark.parametrize( "u, v, p, expected_output", [ - (np.array([0, 0]), np.array([3, 4]), 1, 3.5), - (np.array([0, 0]), np.array([3, 4]), 2, 2.5), - (np.array([1, 2, 3]), np.array([4, 5, 6]), 3, 1.442), + (np.array([0, 0]), np.array([3, 4]), 1, 7), + (np.array([0.0, 0.0]), np.array([3.0, 4.0]), 2, 5), + (np.array([1, 2, 3]), np.array([4, 5, 6]), 3, 4.327), ], ids=[ "Minkowski p=1 - Manhattan", From 3cbfc26b3fdbcac519e1a037d81c75b0046d53d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo?= <88985821+Joao-Paulo-Silva@users.noreply.github.com> Date: Mon, 16 Feb 2026 16:32:30 -0300 Subject: [PATCH 2/3] docs(distance): update equation in docstring --- aisp/utils/distance.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/aisp/utils/distance.py b/aisp/utils/distance.py index 5685b30..50b487a 100644 --- a/aisp/utils/distance.py +++ b/aisp/utils/distance.py @@ -64,7 +64,7 @@ def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64: r"""Calculate the Manhattan distance between two points. .. math:: - \frac{(|X_{1} - Y_{1}| + |X_{2} - Y_{2}| + \cdots + |X_{n} - Y_{n}|)}{n} + |X_{1} - Y_{1}| + |X_{2} - Y_{2}| + \cdots + |X_{n} - Y_{n}| Parameters ---------- @@ -94,9 +94,7 @@ def minkowski( r"""Calculate the normalized Minkowski distance between two points. .. math:: - \frac{ - ((|X_{1} - Y_{1}|^p + |X_{2} - Y_{2}|^p + \cdots + |X_{n} - Y_{n}|^p)^\frac{1}{p}) - }{n} + (|X_{1} - Y_{1}|^p + |X_{2} - Y_{2}|^p + \cdots + |X_{n} - Y_{n}|^p)^\frac{1}{p} Parameters ---------- From 2e3dd8a18e1b9d2c514f33740e06395f71ca6db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo?= <88985821+Joao-Paulo-Silva@users.noreply.github.com> Date: Mon, 16 Feb 2026 16:52:40 -0300 Subject: [PATCH 3/3] docs(distance): update docstring --- aisp/utils/distance.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aisp/utils/distance.py b/aisp/utils/distance.py index 50b487a..078eae2 100644 --- a/aisp/utils/distance.py +++ b/aisp/utils/distance.py @@ -1,4 +1,4 @@ -"""Utility functions for normalized distance between arrays with numba decorators.""" +"""Utility functions for distance between arrays with numba decorators.""" import numpy as np import numpy.typing as npt @@ -76,7 +76,7 @@ def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64: Returns ------- float64 - Normalized Manhattan distance between two points. + Manhattan distance between two points. """ n = len(u) if n == 0: @@ -91,7 +91,7 @@ def minkowski( v: npt.NDArray[float64], p: float = 2.0 ) -> float64: - r"""Calculate the normalized Minkowski distance between two points. + r"""Calculate the Minkowski distance between two points. .. math:: (|X_{1} - Y_{1}|^p + |X_{2} - Y_{2}|^p + \cdots + |X_{n} - Y_{n}|^p)^\frac{1}{p} @@ -112,7 +112,7 @@ def minkowski( Returns ------- float64 - Normalized Minkowski distance between two points. + Minkowski distance between two points. """ n = len(u) if n == 0: