-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.c
executable file
·389 lines (307 loc) · 6.14 KB
/
utils.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "utils.h"
gsl_rng* RANDNUMGEN = NULL;
/*
* initialize the GSL random number generator
*
*/
void init_random_number_generator()
{
if (RANDNUMGEN != NULL) return;
RANDNUMGEN = gsl_rng_alloc(gsl_rng_taus);
long t1;
(void) time(&t1);
// !!! DEBUG
// t1 = 1147530551;
// t1 = 1201962438;
outlog("random seed = %ld\n", t1);
gsl_rng_set (RANDNUMGEN, t1);
}
/*
* check if a directory exists
*
*/
int directory_exist(const char *dname)
{
struct stat st;
int ret;
if (stat(dname,&st) != 0)
{
return 0;
}
ret = S_ISDIR(st.st_mode);
if(!ret)
{
errno = ENOTDIR;
}
return ret;
}
/*
* make directory
*
*/
void make_directory(char* name)
{
mkdir(name, S_IRUSR|S_IWUSR|S_IXUSR);
}
/*
* sample from unnnormalized log probabilities
*
*/
int sample_from_log(gsl_vector* log_prob)
{
double logsum = vget(log_prob, 0);
int i;
for (i = 1; i < log_prob->size; i++)
{
logsum = log_sum(logsum, vget(log_prob, i));
}
double x = runif();
double rolling_sum = exp(vget(log_prob, 0) - logsum);
int result = 0;
while (x >= rolling_sum)
{
result++;
rolling_sum += exp(vget(log_prob, result) - logsum);
}
return(result);
}
/*
* new integer vector
*
*/
int_vector* new_int_vector(int size)
{
int_vector* iv = malloc(sizeof(int_vector));
iv->size = size;
iv->val = malloc(sizeof(int) * iv->size);
int i;
for (i = 0; i < iv->size; i++)
ivset(iv, i, 0);
return(iv);
}
void delete_int_vector(int_vector* iv)
{
free(iv->val);
free(iv);
}
/*
* append value to an integer vector
*
*/
void ivappend(int_vector* iv, int val)
{
iv->size += 1;
iv->val = (int*) realloc(iv->val, sizeof(int) * (iv->size));
iv->val[iv->size - 1] = val;
}
/*
* read a vector from file
*
*/
void vct_fscanf(const char* filename, gsl_vector* v)
{
outlog("reading %ld vector from %s", v->size, filename);
FILE* fileptr;
fileptr = fopen(filename, "r");
gsl_vector_fscanf(fileptr, v);
fclose(fileptr);
}
/*
* copy an integer vector
*
*/
int_vector* iv_copy(int_vector* iv)
{
int_vector* ivc = malloc(sizeof(int_vector));
ivc->size = iv->size;
ivc->val = malloc(sizeof(int) * ivc->size);
int i;
for (i = 0; i < ivc->size; i++)
ivc->val[i] = iv->val[i];
return(ivc);
}
/*
* permute an integer vector
*
*/
void iv_permute(int_vector* iv)
{
gsl_ran_shuffle(RANDNUMGEN,
iv->val,
iv->size,
sizeof(int));
}
void iv_permute_from_perm(int_vector* iv, gsl_permutation* p)
{
assert(iv->size == p->size);
int_vector* ivc = iv_copy(iv);
int i;
for (i = 0; i < p->size; i++)
{
ivset(iv, i, ivget(ivc, p->data[i]));
}
delete_int_vector(ivc);
}
/*
* get a random permutation
*
*/
gsl_permutation* rpermutation(int size)
{
gsl_permutation* perm = gsl_permutation_calloc(size);
gsl_ran_shuffle(RANDNUMGEN,
perm->data, size, sizeof(size_t));
return(perm);
}
/*
* draw random numbers
*
*/
double runif()
{
return(gsl_rng_uniform(RANDNUMGEN));
}
double rgauss(double mean,
double stdev)
{
double v =
gsl_ran_gaussian_ratio_method(RANDNUMGEN, stdev) + mean;
return(v);
}
double rgamma(double shape,
double scale)
{
double v = gsl_ran_gamma(RANDNUMGEN, shape, scale);
return(v);
}
double log_dgamma(double x,
double shape,
double scale)
{
// f(x)= - a * log(s) + log_gamma(a) + (a-1) * log(x) - x/s
double v = - shape*log(scale)+lgam(shape)+(shape-1)*log(x)-x/scale;
return(v);
}
double rbeta(double a,
double b)
{
double v = gsl_ran_beta(RANDNUMGEN, a, b);
return(v);
}
int rbernoulli(double p)
{
int v = gsl_ran_bernoulli(RANDNUMGEN, p);
return(v);
}
/*
* sum a vector
*
*/
double sum(gsl_vector* v)
{
int i;
double sum = 0;
for (i = 0; i < v->size; i++)
sum += vget(v, i);
return(sum);
}
/*
* print a vector
*
*/
void print_vector(gsl_vector* v)
{
int i;
for (i = 0; i < v->size; i++)
{
printf(" [%d] %5.2e\n", i, vget(v, i));
}
}
/*
* read/write a vector and name to a file
*
*/
void write_vect(gsl_vector* vect, char* name, FILE* file)
{
int i;
fprintf(file, "%-10s", name);
for (i = 0; i < vect->size; i++)
{
fprintf(file, " %17.14e", vget(vect, i));
}
fprintf(file, "\n");
}
gsl_vector* read_vect(char* name, int size, FILE* file)
{
outlog("reading %d-vector %s", size, name);
gsl_vector* ret = gsl_vector_alloc(size);
int i;
float v;
fscanf(file, name);
for (i = 0; i < ret->size; i++)
{
fscanf(file, " %f", &v);
vset(ret, i, v);
}
fscanf(file, "\n");
print_vector(ret);
return(ret);
}
/*
* read a named integer from a file
*
*/
int read_int(char* name, FILE* file)
{
outlog("reading integer %s", name);
int ret;
fscanf(file, name);
fscanf(file, "%d\n", &ret);
outlog("read %d\n", ret);
return(ret);
}
void write_int(int x, char* name, FILE* file)
{
fprintf(file, name);
fprintf(file, " %d\n", x);
}
void write_double(double x, char* name, FILE* file)
{
fprintf(file, name);
fprintf(file, " %17.14e\n", x);
}
double read_double(char* name, FILE* file)
{
double ret;
fscanf(file, name);
fscanf(file, "%lf\n", &ret);
printf("read %f\n", ret);
return(ret);
}
FILE* LOG;
static time_t TVAL;
void outlog(char* fmt, ...)
{
if (LOG==NULL) { LOG=stdout; }
TVAL = time(NULL);
char* TIMESTR = ctime(&TVAL);
TIMESTR[24]=' ';
fprintf(LOG, "[ %20s] ", TIMESTR);
va_list args;
va_start(args, fmt);
vfprintf(LOG, fmt, args);
fprintf(LOG, "\n");
va_end(args);
fflush(LOG);
}
/*
* resize a gsl vector
*
*/
void resize(gsl_vector * vec, size_t newsize)
{
assert(vec->stride == 1 && vec->owner && vec->block->data == vec->data);
double * p = realloc(vec->block->data, newsize * sizeof(double));
vec->block->data = vec->data = p;
vec->size = newsize;
}