-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernels.cpp
More file actions
194 lines (159 loc) · 4.48 KB
/
Copy pathkernels.cpp
File metadata and controls
194 lines (159 loc) · 4.48 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// [[Rcpp::depends("RcppArmadillo")]]
#include <RcppArmadillo.h>
#include <cmath>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace Rcpp;
// Euclidean norm
// [[Rcpp::export]]
double euclnormC(arma::colvec x){
double out = sqrt(sum(square(x)));
return(out);
}
// energy kernel
// [[Rcpp::export]]
double enerk(double x, double y){
double out = abs(x) + abs(y) - abs(x - y);
return(out);
}
// mv energy kernel
// [[Rcpp::export]]
double enerk_mv(arma::colvec x, arma::colvec y){
double out = euclnormC(x) + euclnormC(y) - euclnormC(x - y);
return(out);
}
// gaussian kernel
// [[Rcpp::export]]
double gaussk(double x, double y){
double out = exp(-pow(abs(x - y), 2));
return (out);
}
// mv gaussian kernel
// [[Rcpp::export]]
double gaussk_mv(arma::colvec x, arma::colvec y){
double out = exp(-pow(euclnormC(x - y), 2));
return (out);
}
// laplace kernel
// [[Rcpp::export]]
double laplk(double x, double y){
double out = exp(-abs(x - y));
return (out);
}
// mv laplace kernel
// [[Rcpp::export]]
double laplk_mv(arma::colvec x, arma::colvec y){
double out = exp(-euclnormC(x - y));
return (out);
}
// gaussian kernel matrix
// [[Rcpp::export]]
arma::mat gaussk_mat(const arma::mat& x, const arma::mat& y) {
int m = x.n_cols;
int n = x.n_rows;
arma::mat kmat(m, m, arma::fill::zeros);
// Precompute constants
double factor = 1.0 / n;
for (int i = 0; i < m; ++i) {
for (int j = i; j < m; ++j) {
// Compute the sum of Gaussian kernel values
double sum = arma::accu(exp(-arma::square(x.col(i) - y.col(j))));
// Assign to the upper triangle of kmat
kmat(i, j) = sum * factor;
// If i != j, assign the symmetric element
if (i != j)
kmat(j, i) = kmat(i, j);
}
}
return kmat;
}
// mv gaussian kernel matrix
// [[Rcpp::export]]
arma::mat gaussk_mv_mat(const arma::mat& x, const arma::mat& y) {
int m = x.n_cols;
arma::mat kmat(m, m, arma::fill::zeros);
for (int i = 0; i < m; ++i) {
for (int j = i; j < m; ++j) {
// Compute the sum of Gaussian kernel values
kmat(i, j) = gaussk_mv(x.col(i), y.col(j));
// If i != j, assign the symmetric element
if (i != j)
kmat(j, i) = kmat(i, j);
}
}
return kmat;
}
// laplace kernel matrix
// [[Rcpp::export]]
arma::mat laplk_mat(const arma::mat& x, const arma::mat& y, double sigma) {
int m = x.n_cols;
int n = x.n_rows;
arma::mat kmat(m, m, arma::fill::zeros);
// Precompute constants
double factor = 1.0 / n;
for (int i = 0; i < m; ++i) {
for (int j = i; j < m; ++j) {
// Compute the sum of Laplace kernel values
double sum = arma::accu(exp(abs(x.col(i) - y.col(j)) / sigma));
// Assign to the upper triangle of kmat
kmat(i, j) = sum * factor;
// If i != j, assign the symmetric element
if (i != j)
kmat(j, i) = kmat(i, j);
}
}
return kmat;
}
// mv laplace kernel matrix
// [[Rcpp::export]]
arma::mat laplk_mv_mat(const arma::mat& x, const arma::mat& y) {
int m = x.n_cols;
arma::mat kmat(m, m, arma::fill::zeros);
for (int i = 0; i < m; ++i) {
for (int j = i; j < m; ++j) {
// Compute the sum of Laplace kernel values
kmat(i, j) = laplk_mv(x.col(i), y.col(j));
// If i != j, assign the symmetric element
if (i != j)
kmat(j, i) = kmat(i, j);
}
}
return kmat;
}
// energy kernel matrix
// [[Rcpp::export]]
arma::mat enerk_mat(const arma::mat& x, const arma::mat& y) {
int m = x.n_cols;
int n = x.n_rows;
arma::mat kmat(m, m, arma::fill::zeros);
// Precompute constants
double factor = 1.0 / n;
for (int i = 0; i < m; ++i) {
for (int j = i; j < m; ++j) {
// Compute the sum of Energy kernel values
double sum = arma::accu(abs(x.col(i)) + abs(y.col(j)) - abs(x.col(i) - y.col(j)));
// Assign to the upper triangle of kmat
kmat(i, j) = sum * factor;
// If i != j, assign the symmetric element
if (i != j)
kmat(j, i) = kmat(i, j);
}
}
return kmat;
}
// mv energy kernel matrix
// [[Rcpp::export]]
arma::mat enerk_mv_mat(const arma::mat& x, const arma::mat& y) {
int m = x.n_cols;
arma::mat kmat(m, m, arma::fill::zeros);
for (int i = 0; i < m; ++i) {
for (int j = i; j < m; ++j) {
// Compute the sum of Energy kernel values
kmat(i, j) = enerk_mv(x.col(i), y.col(j));
// If i != j, assign the symmetric element
if (i != j)
kmat(j, i) = kmat(i, j);
}
}
return kmat;
}