-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_SKWD.R
39 lines (33 loc) · 1.11 KB
/
test_SKWD.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# @fileoverview Copyright (c) 2019-2020, Stefano Gualandi,
# via Ferrata, 1, I-27100, Pavia, Italy
#
# @author stefano.gualandi@gmail.com (Stefano Gualandi)
library(SpatialKWD)
# Define first histogram
a <- new(SpatialKWD::Histogram2D)
# Add at position (0,0) a unit of mass
a$add(0, 0, 1)
# Normalize the histogram
a$normalize()
# Define second histogram
b <- new(SpatialKWD::Histogram2D)
# Add at position (1,1) two units of mass
b$add(1, 1, 2)
# Normalize the histogram
b$normalize()
# Define third histogram
c <- new(SpatialKWD::Histogram2D)
# Add at position (1,0) and (0,1) an half unit of mass
c$add(1, 0, 0.5)
c$add(0, 1, 0.5)
# Add at position (5,5) a unit of massvi
c$add(5, 5, 1)
# Normalize the histogram
c$normalize()
# Define a solver and compute the distance
# Kantorovich-Wasserstein distance of order 1
# with L_2 as ground distance
s <- new(SpatialKWD::Solver)
print(sprintf("d(a,b) = %f, runtime: %.3f", s$distance(a, b, 3), s$runtime()))
print(sprintf("d(a,c) = %f, runtime: %.3f", s$distance(a, c, 3), s$runtime()))
print(sprintf("d(b,c) = %f, runtime: %.3f", s$distance(b, c, 3), s$runtime()))