1
1
#include < Rcpp.h>
2
2
3
- #include < cstdlib>
4
- #include < vector>
5
- #include < cmath>
6
- #include < algorithm>
7
- #include < stdexcept>
8
-
9
3
#include " ./sits_types.h"
10
4
11
5
using namespace Rcpp ;
12
6
13
7
/* *
14
- * Compute the p-norm distance between two 1D C++ vectors .
8
+ * Compute the p-norm between two time-series .
15
9
*
16
10
* @description
17
- * The p-norm, also known as the Minkowski norm, is a generalized norm
18
- * calculation that includes several types of distances based on the value of p.
11
+ * The `p-norm`, also known as the `Minkowski space`, is a generalized norm
12
+ * calculation that includes several types of distances based on the value
13
+ * of `p`.
19
14
*
20
- * Common values of p include:
15
+ * Common values of `p` include:
21
16
*
22
- * - p = 1 for the Manhattan (city block) distance;
23
- * - p = 2 for the Euclidean norm (distance).
17
+ * - ` p = 1` for the Manhattan (city block) distance;
18
+ * - ` p = 2` for the Euclidean norm (distance).
24
19
*
25
20
* More details about p-norms can be found on Wikipedia:
26
21
* https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm
27
22
*
28
- * @param a A 1D vector representing the first point in an m-dimensional space.
29
- * @param b A 1D vector representing the second point in an m-dimensional space.
30
- * @param p The value of the norm to use, determining the type of distance
31
- * calculated.
23
+ * @param a A `std::vector<double>` with time-series values.
24
+ * @param b A `std::vector<double>` with time-series values.
25
+ * @param p A `double` value of the norm to use, determining the type of
26
+ * distance calculated.
27
+ *
28
+ * @note
29
+ * Both vectors `a` and `b` must have the same length.
32
30
*
33
- * @note Both vectors 'a' and 'b' must have the same number of dimensions.
34
- * @note This function was adapted from the DTW implementation found at:
35
- * https://github.com/cjekel/DTW_cpp
31
+ * @note
32
+ * The implementation of this DTW distance calculation was adapted from the
33
+ * `DTW_cpp` single header library ( https://github.com/cjekel/DTW_cpp).
36
34
*
37
- * @return The p-norm distance between vectors 'a' and 'b' .
35
+ * @return The ` p-norm` value between vectors `a` and `b` .
38
36
*/
39
37
double p_norm (std::vector<double > a, std::vector<double > b, double p)
40
38
{
@@ -51,34 +49,25 @@ double p_norm(std::vector<double> a, std::vector<double> b, double p)
51
49
}
52
50
53
51
/* *
54
- * Compute the Dynamic Time Warping (DTW) distance between two 2D C++ vectors .
52
+ * Dynamic Time Warping (DTW) distance.
55
53
*
56
54
* @description
57
55
* This function calculates the Dynamic Time Warping (DTW) distance between
58
- * two sequences that can have a different number of data points but must
59
- * share the same number of dimensions. An exception is thrown if the dimensions
60
- * of the input vectors do not match.
56
+ * two time-series.
61
57
*
62
- * For more information on DTW, visit:
63
- * https://en.wikipedia.org/wiki/Dynamic_time_warping
58
+ * @param x A `std::vector<std::vector<double>>` with time-series values.
59
+ * @param y A `std::vector<std::vector<double>>` with time-series values.
64
60
*
65
- * @param a A 2D vector representing the first sequence
66
- * @param b A 2D vector representing the second sequence.
67
- * @param p The value of p-norm to use for distance calculation.
68
- *
69
- * @throws std::invalid_argument If the dimensions of 'a' and 'b' do not match.
61
+ * @reference
62
+ * Giorgino, T. (2009). Computing and Visualizing Dynamic Time Warping
63
+ * Alignments in R: The dtw Package. Journal of Statistical Software, 31(7),
64
+ * 1–24. https://doi.org/10.18637/jss.v031.i07
70
65
*
71
66
* @note
72
- * Both vectors 'a', and 'b' should be structured as follows:
73
- *
74
- * [number_of_data_points][number_of_dimensions]
75
- *
76
- * allowing the DTW distance computation to adapt to any p-norm value specified.
67
+ * The implementation of this DTW distance calculation was adapted from the
68
+ * `DTW_cpp` single header library (https://github.com/cjekel/DTW_cpp).
77
69
*
78
- * @note The implementation of this DTW distance calculation was adapted from:
79
- * https://github.com/cjekel/DTW_cpp
80
- *
81
- * @return The DTW distance between the two input sequences.
70
+ * @return DTW distance.
82
71
*/
83
72
double distance_dtw_op (std::vector<std::vector<double >> a,
84
73
std::vector<std::vector<double >> b,
@@ -87,15 +76,6 @@ double distance_dtw_op(std::vector<std::vector<double>> a,
87
76
int n = a.size ();
88
77
int o = b.size ();
89
78
90
- int a_m = a[0 ].size ();
91
- int b_m = b[0 ].size ();
92
-
93
- if (a_m != b_m)
94
- {
95
- throw std::invalid_argument (
96
- " a and b must have the same number of dimensions!"
97
- );
98
- }
99
79
std::vector<std::vector<double >> d (n, std::vector<double >(o, 0.0 ));
100
80
101
81
d[0 ][0 ] = p_norm (a[0 ], b[0 ], p);
@@ -121,22 +101,27 @@ double distance_dtw_op(std::vector<std::vector<double>> a,
121
101
}
122
102
123
103
/* *
124
- * Dynamic Time Warping (DTW) distance wrapper .
104
+ * Dynamic Time Warping (DTW) distance.
125
105
*
126
106
* @description
127
- * This function calculates prepare data from `Kohonen` package and calculate
128
- * the DTW distance between two array of points .
107
+ * This function calculates the Dynamic Time Warping (DTW) distance between
108
+ * two time-series .
129
109
*
130
- * @param a A 2D vector representing the first sequence .
131
- * @param b A 2D vector representing the second sequence .
132
- * @param np Number of points in vectors `a ` and `b `.
133
- * @param nNA Number of NA values in the vectors `a ` and `b `.
110
+ * @param x A `double *` Time-series data .
111
+ * @param y A `double *` Self-Organizing Maps (SOM) codebook .
112
+ * @param np `int` Number of points in arrays `p1 ` and `p2 `.
113
+ * @param nNA `int` Number of `NA` values in the arrays `p1 ` and `p2 `.
134
114
*
135
- * @note The function signature was created following the `Kohonen` R package
136
- * specifications for custom distance functions.
115
+ * @reference
116
+ * Giorgino, T. (2009). Computing and Visualizing Dynamic Time Warping
117
+ * Alignments in R: The dtw Package. Journal of Statistical Software, 31(7),
118
+ * 1–24. https://doi.org/10.18637/jss.v031.i07
137
119
*
120
+ * @note
121
+ * The implementation of this DTW distance calculation was adapted from the
122
+ * `DTW_cpp` single header library (https://github.com/cjekel/DTW_cpp).
138
123
*
139
- * @return The DTW distance between the two input sequences .
124
+ * @return DTW distance.
140
125
*/
141
126
double kohonen_dtw (double *p1, double *p2, int np, int nNA)
142
127
{
0 commit comments