-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernelfunctions.h
62 lines (56 loc) · 1.56 KB
/
kernelfunctions.h
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
#ifndef KERNELFUNCTIONS_H
#define KERNELFUNCTIONS_H
struct Kernel
{
public:
static const int Linear = 0;
static const int Poly = 1;
static const int Gaussian = 2;
Eigen::MatrixXd linear(Eigen::MatrixXd &m1, Eigen::MatrixXd &m2)
{
return m1 * m2.transpose();
}
Eigen::MatrixXd poly(Eigen::MatrixXd &m1, Eigen::MatrixXd &m2)
{
return ((m1 * m2.transpose()).array() - 1).pow(poly_n);
}
Eigen::MatrixXd gaussian(Eigen::MatrixXd &m1, Eigen::MatrixXd &m2)
{
double gamma = 1. / 13;
Eigen::MatrixXd result(m1.rows(), m2.rows());
for (int i = 0; i < m1.rows(); ++i)
result.row(i) = ((m2.rowwise() - m1.row(i)).rowwise().lpNorm<1>().array().pow(2) * (-gamma)).exp();
return result;
}
void setKernel(int _kernel_type)
{
kernel_type = _kernel_type;
switch (kernel_type)
{
case 0:
kernel_function = &Kernel::linear;
break;
case 1:
kernel_function = &Kernel::poly;
break;
case 2:
kernel_function = &Kernel::gaussian;
break;
default:
kernel_function = &Kernel::linear;
break;
}
}
double K(double x1, double x2)
{
Eigen::MatrixXd X1(1, 1), X2(1, 1);
X1(0, 0) = x1;
X2(0, 0) = x2;
return (this->*kernel_function)(X1, X2)(0, 0);
}
protected:
int kernel_type;
int poly_n;
Eigen::MatrixXd (Kernel::*kernel_function) (Eigen::MatrixXd &m1, Eigen::MatrixXd &m2);
};
#endif