Skip to content

Commit d09ef52

Browse files
Add distance tests
1 parent cbd6675 commit d09ef52

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

test/vdistance.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Random.seed!(0x3c021fc14523fc9a)
2+
@testset "1-dimensional distances" begin
3+
x, y = rand(1000), rand(1000)
4+
@test vmanhattan(x, y) mapreduce(abs -, +, x, y)
5+
@test vtmanhattan(x, y) mapreduce(abs -, +, x, y)
6+
@test veuclidean(x, y) mapreduce(abs2 -, +, x, y)
7+
@test vteuclidean(x, y) mapreduce(abs2 -, +, x, y)
8+
@test vchebyshev(x, y) mapreduce(abs -, max, x, y)
9+
@test vtchebyshev(x, y) mapreduce(abs -, max, x, y)
10+
for p (0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0)
11+
d = mapreduce((a, b) -> abs(a - b)^p, +, x, y)^(1/p)
12+
@test vminkowski(x, y, p) d
13+
@test vtminkowski(x, y, p) d
14+
end
15+
@test vminkowski(x, y, Inf) mapreduce(abs -, max, x, y)
16+
@test vminkowski(x, y, -Inf) mapreduce(abs -, min, x, y)
17+
@test vtminkowski(x, y, Inf) mapreduce(abs -, max, x, y)
18+
@test vtminkowski(x, y, -Inf) mapreduce(abs -, min, x, y)
19+
end
20+
21+
@testset "multi-dimensional distances" begin
22+
x, y = rand(10,10,10,10), rand(10,10,10,10)
23+
for dims (1, 2, 3, 4, (1,2), (1,3), (1,4), (2,3), (2,4), (3,4), (1,2,3), (1,2,4), (2,3,4))
24+
@test vmanhattan(x, y, dims=dims) mapreduce(abs -, +, x, y, dims=dims)
25+
@test vtmanhattan(x, y, dims=dims) mapreduce(abs -, +, x, y, dims=dims)
26+
@test veuclidean(x, y, dims=dims) .√mapreduce(abs2 -, +, x, y, dims=dims)
27+
@test vteuclidean(x, y, dims=dims) .√mapreduce(abs2 -, +, x, y, dims=dims)
28+
@test vchebyshev(x, y, dims=dims) mapreduce(abs -, max, x, y, dims=dims)
29+
@test vtchebyshev(x, y, dims=dims) mapreduce(abs -, max, x, y, dims=dims)
30+
for p (0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0)
31+
d = mapreduce((a, b) -> abs(a - b)^p, +, x, y, dims=dims).^(1/p)
32+
@test vminkowski(x, y, p, dims=dims) d
33+
@test vtminkowski(x, y, p, dims=dims) d
34+
end
35+
@test vminkowski(x, y, Inf, dims=dims) mapreduce(abs -, max, x, y, dims=dims)
36+
@test vminkowski(x, y, -Inf, dims=dims) mapreduce(abs -, min, x, y, dims=dims)
37+
@test vtminkowski(x, y, Inf, dims=dims) mapreduce(abs -, max, x, y, dims=dims)
38+
@test vtminkowski(x, y, -Inf, dims=dims) mapreduce(abs -, min, x, y, dims=dims)
39+
end
40+
41+
for p (0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0)
42+
d = mapreduce((a, b) -> abs(a - b)^p, +, x, y)^(1/p)
43+
for dims ((1,2,3,4), (1,2,3,4,5))
44+
@test first(vminkowski(x, y, p, dims=dims)) d
45+
@test first(vtminkowski(x, y, p, dims=dims)) d
46+
end
47+
end
48+
end
49+

0 commit comments

Comments
 (0)