-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cpp
165 lines (141 loc) · 2.74 KB
/
util.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
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
/* C implementations of useful functions.
* Written by Tom Minka (unless otherwise noted).
*/
#define _USE_MATH_DEFINES 1
#include <stdlib.h>
#include <float.h>
#include "util.h"
#include <math.h>
#include <map>
#ifdef _MSC_VER
#define finite _finite
#define isnan _isnan
#endif
double gamln(double x)
{
#define M_lnSqrt2PI 0.91893853320467274178
static double gam_series[] = {
76.18009172947146,
-86.50532032941677,
24.01409824083091,
-1.231739572450155,
0.1208650973866179e-2,
-0.5395239384953e-5
};
int i;
double denom, x1, series;
if(x < 0) return NAN;
if(x == 0) return INFINITY;
if(!finite(x)) return x;
/* Lanczos method */
denom = x+1;
x1 = x + 5.5;
series = 1.000000000190015;
for(i = 0; i < 6; i++) {
series += gam_series[i] / denom;
denom += 1.0;
}
return( M_lnSqrt2PI + (x+0.5)*log(x1) - x1 + log(series/x) );
}
Vector gl_pc;
/* With 0.5 increments */
// x/2 is actual gamln parameter
void precomputegamLn(int size)
{
int i;
new (&gl_pc) Vector(size);
for (i=0;i<gl_pc.n;i++)
gl_pc[i] = gamln(i/2.0);
}
double gamlnd(int x,int d) // Actually works on x/2
{
double res = 0;
for (auto i = 0; i < d; i++)
{
res += gl_pc[x - i];
}
return (log(M_PI)*d*(d - 1) / 4) + res;
}
map<int, double> gl_memorized;
double getGamln(double x)
{
if (x - floor(x) == 0) { // x is integer
if (gl_memorized.find(x) == gl_memorized.end()) {
// Not in memory, calculate and memorize it.
gl_memorized[x] = gamln(x / 2.0);
}
return gl_memorized[x];
}
else { // x is float
return gamln(x / 2.0);
}
}
int sample(Vector& v)
{
double d = 0;
double r = urand();
int n = v.n;
int i;
for (i = 0; i < n;i++)
{
d += v[i];
if (d > r)
break;
}
return i;
}
int sampleFromLog(Vector & v)
{
int n = v.n;
double max = v.maximum();
v -= max;
v.transform(exp); // In-place use transform
v /= v.sum();
return sample(v);
}
// Ids indexed from 0
vector<int> trange(int max, int nparts, int id)
{
int chunksize = ceil( (1.0*max) / nparts);
int start = chunksize*id;
int end = start+chunksize;
end = (end > max) ? max : end;
return vector<int>({ start, end });
}
double harmean(double x,double y)
{
return 1.0/((1.0/x)+(1.0/y));
}
double sigmoid(double x)
{
return 1/(1+exp(-x));
}
bool checkVectors(Vector& v1, Vector& v2)
{
int iscopy = 1;
if (v1.data == v2.data)
printf("Points to same address\n");
else
{
if (v1.n == v2.n)
{
for (auto i = 0; i < v1.n; i++)
if (v1.data[i] != v2.data[i])
iscopy = 0;
}
if (iscopy)
printf("Copy vectors\n");
else
printf("Different vectors");
}
return iscopy;
}
bool fexists(const char *filename)
{
if (filename == NULL) return 0;
ifstream f(filename);
bool a = f.is_open();
if (a)
f.close();
return a;
}