-
Notifications
You must be signed in to change notification settings - Fork 21
/
utils.cpp
161 lines (138 loc) · 2.94 KB
/
utils.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
#include "utils.h"
/*
* given log(a) and log(b), return log(a + b)
*
*/
double log_sum(double log_a, double log_b)
{
double v;
if (log_a < log_b)
v = log_b+log(1 + exp(log_a-log_b));
else
v = log_a+log(1 + exp(log_b-log_a));
return v;
}
/**
* Proc to calculate the value of the trigamma, the second
* derivative of the loggamma function. Accepts positive matrices.
* From Abromowitz and Stegun. Uses formulas 6.4.11 and 6.4.12 with
* recurrence formula 6.4.6. Each requires workspace at least 5
* times the size of X.
*
**/
double trigamma(double x)
{
double p;
int i;
x = x+6;
p = 1/(x*x);
p = (((((0.075757575757576*p-0.033333333333333)*p+0.0238095238095238)*p-0.033333333333333)*p+0.166666666666667)*p+1)/x+0.5*p;
for (i=0; i<6 ;i++)
{
x = x-1;
p = 1/(x*x)+p;
}
return p;
}
/*
* taylor approximation of first derivative of the log gamma function
*
*/
double digamma(double x)
{
double p;
x = x+6;
p = 1/(x*x);
p = (((0.004166666666667*p-0.003968253986254)*p+0.008333333333333)*p-0.083333333333333)*p;
p = p+log(x)-0.5/x-1/(x-1)-1/(x-2)-1/(x-3)-1/(x-4)-1/(x-5)-1/(x-6);
return p;
}
/*
* this log gamma function has the implementation of this function
*
*/
/* double lgamma(double x)
* {
* double x0,x2,xp,gl,gl0;
* int n,k;
* static double a[] = {
* 8.333333333333333e-02,
* -2.777777777777778e-03,
* 7.936507936507937e-04,
* -5.952380952380952e-04,
* 8.417508417508418e-04,
* -1.917526917526918e-03,
* 6.410256410256410e-03,
* -2.955065359477124e-02,
* 1.796443723688307e-01,
* -1.39243221690590
* };
*
* x0 = x;
* if (x <= 0.0) return 1e308;
* else if ((x == 1.0) || (x == 2.0)) return 0.0;
* else if (x <= 7.0) {
* n = (int)(7-x);
* x0 = x+n;
* }
* x2 = 1.0/(x0*x0);
* xp = 2.0*M_PI;
* gl0 = a[9];
* for (k=8;k>=0;k--) {
* gl0 = gl0*x2 + a[k];
* }
* gl = gl0/x0+0.5*log(xp)+(x0-0.5)*log(x0)-x0;
* if (x <= 7.0) {
* for (k=1;k<=n;k++) {
* gl -= log(x0-1.0);
* x0 -= 1.0;
* }
* }
* return gl;
* }
*/
/*
* make directory
*
*/
void make_directory(char* name)
{
mkdir(name, S_IRUSR|S_IWUSR|S_IXUSR);
}
/*
* argmax
*
*/
int argmax(double* x, int n)
{
int i, argmax = 0;
double max = x[0];
for (i = 1; i < n; i++)
{
if (x[i] > max)
{
max = x[i];
argmax = i;
}
}
return argmax;
}
/*
* return the correponding index in the n(n+1)/2 given row and col
* this is a upper triangle matrix, we can do this since this is
* a symmetric matrix
*
*/
int map_idx(int row, int col, int dim)
{
int swap, idx;
if (row > col)
{
swap = row;
row = col;
col = swap;
}
//now row <= col
idx = (2*dim - row + 1)*row/2 + col - row;
return idx;
}