forked from elbamos/largeVis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradients.cpp
99 lines (88 loc) · 3.66 KB
/
gradients.cpp
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
// [[Rcpp::plugins(openmp)]]
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(RcppProgress)]]
#include "largeVis.h"
#include "gradients.h"
/*
* Gradients
*/
Gradient::Gradient(const distancetype& g, const dimidxtype& d) :
gamma{g}, cap(5), D{d} {};
distancetype Gradient::distAndVector(const coordinatetype *x_i,
const coordinatetype *x_j,
coordinatetype *output) const {
double cnt = 0;
for (int d = 0; d < D; d++) {
const double t = x_i[d] - x_j[d];
output[d] = t;
cnt += t * t;
}
return cnt;
};
Gradient::~Gradient() {}
void Gradient::multModify(coordinatetype *col, const coordinatetype& adj) const {
for (dimidxtype i = 0; i != D; i++) col[i] = clamp(col[i] * adj);
};
void Gradient::multModifyPos(coordinatetype *col, const coordinatetype& adj) const {
for (dimidxtype i = 0; i != D; i++) col[i] *= adj;
};
coordinatetype Gradient::clamp(const coordinatetype& val) const {
return fmin(fmax(val, -cap), cap);
};
void Gradient::positiveGradient(const coordinatetype* i,
const coordinatetype* j,
coordinatetype* holder) const {
const double dist_squared = distAndVector(i, j, holder);
_positiveGradient(dist_squared, holder);
};
void Gradient::negativeGradient(const coordinatetype* i,
const coordinatetype* k,
coordinatetype* holder) const {
const double dist_squared = distAndVector(i, k, holder) ;
_negativeGradient(dist_squared, holder);
}
void AlphaGradient::_positiveGradient(const double& dist_squared,
coordinatetype* holder) const {
const distancetype grad = twoalpha / (1 + alpha * dist_squared);
multModifyPos(holder, grad);
};
void AlphaGradient::_negativeGradient(const double& dist_squared,
coordinatetype* holder) const {
const distancetype adk = alpha * dist_squared;
const distancetype grad = alphagamma / (dist_squared * (adk + 1));
multModify(holder, grad);
};
AlphaGradient::AlphaGradient(const distancetype& a,
const distancetype& g,
const dimidxtype& D) :
Gradient(g, D), alpha{a}, twoalpha(alpha * -2),
alphagamma(alpha * gamma * 2) { } ;
AlphaOneGradient::AlphaOneGradient(const distancetype& g,
const dimidxtype& d) :
AlphaGradient(1, g, d) { } ;
void AlphaOneGradient::_positiveGradient(const distancetype& dist_squared,
coordinatetype* holder) const {
const distancetype grad = - 2 / (1 + dist_squared);
multModifyPos(holder, grad);
};
void AlphaOneGradient::_negativeGradient(const distancetype& dist_squared,
coordinatetype* holder) const {
const distancetype grad = alphagamma / (1 + dist_squared) / (0.1 + dist_squared);
multModify(holder, grad);
};
ExpGradient::ExpGradient(const distancetype& g, const dimidxtype& d) :
Gradient(g, d), gammagamma(gamma * gamma) {
cap = gamma;
};
void ExpGradient::_positiveGradient(const distancetype& dist_squared,
coordinatetype* holder) const {
const distancetype expsq = exp(dist_squared);
const distancetype grad = (dist_squared > 4) ? -1 : -(expsq / (expsq + 1));
multModifyPos(holder, grad);
};
void ExpGradient::_negativeGradient(const distancetype& dist_squared,
coordinatetype* holder) const {
const distancetype grad = (dist_squared > gammagamma) ? 0 : gamma / (1 + exp(dist_squared));
multModify(holder, grad);
};