-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
executable file
·116 lines (100 loc) · 2.74 KB
/
utils.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
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
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <memory.h>
#include <time.h>
#include <math.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_sf_psi.h>
#include <gsl/gsl_sf_gamma.h>
#include <vector>
using namespace std;
double log_sum(double log_a, double log_b);
double log_normalize(double * array, int nlen);
double log_normalize(vector<double> & vec, int nlen);
double log_subtract(double log_a, double log_b);
double log_factorial(int n, double a);
double similarity(const int* v1, const int* v2, int n);
bool file_exists(const char * filename);
bool dir_exists(const char * directory);
template <typename T> void free_vec_ptr(vector <T* > & v)
{
int size = v.size();
T* p = NULL;
for (int i = 0; i < size; i ++)
{
p = v[i];
delete [] p;
}
v.clear();
}
// find the max and argmax in an array
template <typename T> T max(const T * x, int n, int* argmax)
{
*argmax = 0;
T max_val = x[0];
for (int i = 1; i < n; i++)
if (x[i] > max_val)
{
max_val = x[i];
*argmax = i;
}
return max_val;
}
// find the max and argmax in an vector
template <typename T> T max_vec(vector<T> & v, int n, int* argmax)
{
*argmax = 0;
T max_val = v[0];
for (int i = 1; i < n; i++)
if (v[i] > max_val)
{
max_val = v[i];
*argmax = i;
}
return max_val;
}
// set a value to the entire array
template <typename T> void set_array(T * array, int size, T value)
{
for (int i = 0; i < size; i++) array[i] = value;
}
// swap two elements in an array
template < typename T > void swap_array_element(T * array, int i, int j)
{
if (i == j) return;
T a = array[i];
array[i] = array[j];
array[j] = a;
}
// set a value to a entrie vector
template <typename T> void set_vector(vector<T> & v, T value)
{
int size = v.size();
for (int i = 0; i < size; i++) v[i] = value;
}
// swap two elements in vector
template < typename T > void swap_vec_element(vector<T> & v, int i, int j)
{
if (i == j) return; // no need to swap
T a = v[i];
v[i] = v[j];
v[j] = a;
}
/// gsl_wrappers
//double lgamma(double x); // linux has this
unsigned int rmultinomial(const double* p, int n, double tot_p=-1);
double rgamma(double a, double b);
double rbeta(double a, double b);
unsigned int rbernoulli(double p);
double runiform();
void rshuffle (void* base, size_t n, size_t size);
unsigned long int runiform_int(unsigned long int n);
#endif