Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions aisp/utils/distance.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -61,10 +61,10 @@ 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}
|X_{1} - Y_{1}| + |X_{2} - Y_{2}| + \cdots + |X_{n} - Y_{n}|

Parameters
----------
Expand All @@ -76,13 +76,13 @@ 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:
return float64(-1.0)

return float64(np.sum(np.abs(u - v)) / n)
return float64(np.sum(np.abs(u - v)))


@njit()
Expand All @@ -91,12 +91,10 @@ 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::
\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
----------
Expand All @@ -114,13 +112,13 @@ def minkowski(
Returns
-------
float64
Normalized Minkowski distance between two points.
Minkowski distance between two points.
"""
n = len(u)
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)
Expand Down
10 changes: 5 additions & 5 deletions aisp/utils/tests/test_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down