Skip to content

Commit 20813f1

Browse files
authored
Add files via upload
1 parent 1a83be2 commit 20813f1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

include/GPR.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ void pdist(const std::vector<Vector2<T> > &X1, const std::vector<Vector2<T> > &X
3838
Dist(ri, ci) = (X1[ci] - X2[ri]).dot(X1[ci] - X2[ri]); // sqaured
3939
}
4040

41+
template <typename T = number_t>
42+
void self_pdist(const std::vector<Vector2<T> > &X1, MatrixX<T> &Dist){
43+
Dist.resize(X1.size(), X1.size());
44+
for(Eigen::Index ri = 0; ri < Dist.rows(); ++ri)
45+
for(Eigen::Index ci = ri + 1; ci < Dist.cols(); ++ci)
46+
{
47+
Dist(ri, ci) = (X1[ci] - X1[ri]).dot(X1[ci] - X1[ri]); // sqaured
48+
Dist(ci, ri) = Dist(ri, ci);
49+
}
50+
for(Eigen::Index ri = 0; ri < Dist.rows(); ++ri)
51+
Dist(ri, ri) = T(0.);
52+
53+
}
4154

4255
template <typename T = number_t>
4356
MatrixX<T> rbf_kernel_2d(const std::vector<Vector2<T> > &X1, const std::vector<Vector2<T> > &X2, const T sigma, const T l){
@@ -253,7 +266,7 @@ class GPR{
253266
{
254267
trX = train_x;
255268
trY = train_y;
256-
pdist(train_x, train_x, Dist);
269+
self_pdist(train_x, Dist);
257270
if(optimize)
258271
{
259272
LBFGSpp::LBFGSBParam<double> param;
@@ -342,7 +355,7 @@ class TGPR{
342355
T fit_predict(const std::vector<Vector2<T>> &train_x, const VectorX<T> &train_y, const Vector2<T> &test_x){
343356
int N = train_x.size();
344357
MatrixX<T> Dist;
345-
pdist<T>(train_x, train_x, Dist);
358+
self_pdist<T>(train_x, Dist);
346359
MatrixX<T> Kff;
347360
MatrixX<T> L;
348361
VectorX<T> alpha;

0 commit comments

Comments
 (0)