forked from jMotif/jmotif-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.cpp
More file actions
68 lines (64 loc) · 1.91 KB
/
distance.cpp
File metadata and controls
68 lines (64 loc) · 1.91 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <RcppArmadillo.h>
using namespace Rcpp ;
//
#include <jmotif.h>
//
//' Finds the Euclidean distance between points.
//'
//' @param seq1 the array 1.
//' @param seq2 the array 2.
//' stops and the NAN is returned.
//' @useDynLib jmotif
//' @export
// [[Rcpp::export]]
double euclidean_dist(NumericVector seq1, NumericVector seq2) {
if(seq1.length() == seq2.length()){
double res = 0.0;
for(int i=0; i<seq1.length(); i++){
res = res + (seq1[i]-seq2[i])*(seq1[i]-seq2[i]);
}
return sqrt(res);
} else {
stop("arrays length are not equal");
return std::numeric_limits<double>::quiet_NaN();
}
}
// this a faster version for the internal use
//
double _euclidean_dist(std::vector<double>* seq1, std::vector<double>* seq2) {
double res = 0;
for(unsigned i=0; i<seq1->size(); i++){
res = res + (seq1->at(i) - seq2->at(i)) * (seq1->at(i) - seq2->at(i));
}
return sqrt(res);
}
//' Finds the Euclidean distance between points, if distance is above the threshold, abandons the computation
//' and returns NAN.
//'
//' @param seq1 the array 1.
//' @param seq2 the array 2.
//' @param upper_limit the max value after reaching which the distance computation
//' stops and the NAN is returned.
//' @useDynLib jmotif
//' @export
// [[Rcpp::export]]
double early_abandoned_dist(NumericVector seq1, NumericVector seq2, double upper_limit) {
if(seq1.length() == seq2.length()){
double limit = upper_limit;
if(limit != std::numeric_limits<double>::max()){
limit = upper_limit * upper_limit;
}
double res = 0.0;
for(int i=0; i<seq1.length(); i++){
res = res + (seq1[i]-seq2[i])*(seq1[i]-seq2[i]);
//Rcout << "res: " <<res <<" limit: " << limit <<"\n";
if(res > limit){
return std::numeric_limits<double>::quiet_NaN();
}
}
return sqrt(res);
} else {
stop("arrays length are not equal");
return std::numeric_limits<double>::quiet_NaN();
}
}