This repository was archived by the owner on May 21, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgapkernel.cpp
More file actions
Latest commit
53 lines (48 loc) · 1.19 KB
/
Copy pathgapkernel.cpp
File metadata and controls
53 lines (48 loc) · 1.19 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
#include<Rcpp.h>
#include<math.h>// pow
usingnamespaceRcpp;
// [[Rcpp::export]]
longdoublecppKernel(std::string x, std::string y, double lambda, unsignedint p) {
int r = x.length();
int c = y.length();
unsignedint i = 0, j = 0;
longdouble **A = newlongdouble* [r];
longdouble **B = newlongdouble* [r+1];
B[0] = newlongdouble[c+1];
for(i = 0; i < r; i++) {
A[i] = newlongdouble[c];
B[i+1] = newlongdouble[c+1];
for(j = 0; j < c; j++) {
B[i][j] = 0;
B[i+1][j+1] = 0;
A[i][j] = (x[i] == y[j] ? pow(lambda, 2) : 0);
}
}
longdouble* k = newlongdouble[p];
k[0] = 0;
for(int l = 1; l < p; l++) {
k[l] = 0;
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
B[i+1][j+1] = A[i][j] + lambda * B[i][j+1] + lambda * B[i+1][j] - pow(lambda, 2) * B[i][j];
if(x[i] == y[j]) {
A[i][j] = pow(lambda, 2) * B[i][j];
k[l] = k[l] + A[i][j];
}
}
}
}
delete[] B[0];
for(i = 0; i < r; i++) {
delete[] A[i];
delete[] B[i+1];
}
delete[] A;
delete[] B;
longdouble result = k[p-1];
delete[] k;
return result;
}
// (test) code to run after compilation
/*** R
*/