Skip to content

Commit 891b752

Browse files
committed
Added Minkowski distance
1 parent 2bca7e5 commit 891b752

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Minkowski Distance distance implementation
2+
-- See: http://en.wikipedia.org/wiki/Minkowski_distance
3+
4+
-- Returns the Minkowski distance between two vectors
5+
-- p : a first vector
6+
-- q : a second vector
7+
-- n : the distance order
8+
-- returns : the distance between p and q
9+
local function minkowski_distance(p, q, n)
10+
assert(#p == #q, 'vectors must have the same length')
11+
local s = 0
12+
for i in ipairs(p) do
13+
s = s + math.abs(p[i] - q[i]) ^ n
14+
end
15+
return s ^ (1 / n)
16+
end
17+
18+
return minkowski_distance

0 commit comments

Comments
 (0)