-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterp_tab.c
More file actions
291 lines (224 loc) · 6.49 KB
/
interp_tab.c
File metadata and controls
291 lines (224 loc) · 6.49 KB
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
#include <glib.h>
#include <stdio.h>
#include <math.h>
#include "interp_tab.h"
/**
* Converts the provided interpolation table index into a double
*/
static double idx2dbl(int idx) {
int fr = (idx-1) % (int)INTERP_TAB_FR_MULT;
int x = (idx-1) / INTERP_TAB_FR_MULT;
if(idx < 0) {
g_error("idx2dbl: provided idx(%d) must be positive", idx);
}
if(idx == 0) {
return 0.0;
}
return (((INTERP_TAB_FR_OFFSET*fr)/INTERP_TAB_FR_MULT)+INTERP_TAB_FR_OFFSET) * pow(2.0, x+INTERP_TAB_MIN_EXP);
}
/**
* Converts the provided double to an interpolation table index.
*/
static inline int dbl2idx(double d) {
double fr;
int x,idx;
if(d < 0.0) {
g_error("dbl2idx: provided double (%g) must be positive", d);
}
if(d == 0.0) {
return 0.0;
}
fr = frexp(d, &x);
if(x < INTERP_TAB_MIN_EXP) {
return 0;
}
if(x > INTERP_TAB_MAX_EXP) {
g_warning("dbl2idx: value %g exceeds interpolation table maximum. Recombination rate values may be too high potentially because RECOMB_RATE_SCALE is set incorrectly.\n",d);
x = INTERP_TAB_MAX_EXP;
}
idx = (((fr-INTERP_TAB_FR_OFFSET)*INTERP_TAB_FR_OFFSET_INV) + (x-INTERP_TAB_MIN_EXP))*INTERP_TAB_FR_MULT + 1;
return idx;
}
/**
* Looks up entries in table that flank requested data point and
* performs interpolation to infer value at requested point.
* This is currently implemented as linear interpolation, but
* something more sophisticated could be introduced.
*
* This function is for interpolation of a function with 1 variable,
* for 2d interpolation use interp_tab_lookup().
*/
double interp_tab_lookup_1d(InterpTab *tab, double x) {
double x1, x2, y1, y2, y;
int x_idx1, x_idx2;
if(tab->z != NULL) {
g_error("interp_tab_lookup_1d: table initialized for 2d interpolation. "
"interp_tab_lookup function should be used instead.");
}
/* get indexes that correspond to table entries on either side
* of requested value
*/
x_idx2 = dbl2idx(x);
if(x_idx2 == 0) {
x_idx1 = 0;
x_idx2 = 1;
} else {
if(tab->x[x_idx2] >= x || x_idx2 == INTERP_TAB_N_ENTRIES-1) {
x_idx1 = x_idx2 - 1;
} else {
x_idx1 = x_idx2;
x_idx2 = x_idx1+1;
}
}
x1 = tab->x[x_idx1];
x2 = tab->x[x_idx2];
y1 = tab->y[x_idx1];
y2 = tab->y[x_idx2];
/* perform linear interpolation */
y = y1 + ((y2-y1)/(x2-x1)) * (x-x1);
/*
* fprintf(stderr, "y1=%g, y2=%g, x1=%g, x2=%g, x=%g, y=%g\n",
* y1,y2,x1,x2,x,y);
*/
return y;
}
/**
* Looks up nearest entries in table to the requested x and y values,
* and performs bilinear interpolation to estimate z value at
* requested point.
*/
inline double interp_tab_lookup(InterpTab *tab, double x, double y) {
double x1, x2, y1, y2, z;
int x_idx1, x_idx2, y_idx1, y_idx2;
if(tab->z == NULL) {
g_error("interp_tab_lookup: table initialized for 1d interpolation. "
"interp_tab_lookup_1d function should be used instead.");
}
/* get indexes that correspond to table entries on either side
* of requested value
*/
x_idx2 = dbl2idx(x);
if(x_idx2 == 0) {
x_idx1 = 0;
x_idx2 = 1;
} else {
if(tab->x[x_idx2] >= x || x_idx2 == INTERP_TAB_N_ENTRIES-1) {
x_idx1 = x_idx2 - 1;
} else {
x_idx1 = x_idx2;
x_idx2 = x_idx1+1;
}
}
y_idx2 = dbl2idx(y);
if(y_idx2 == 0) {
y_idx1 = 0;
y_idx2 = 1;
} else {
if(tab->y[y_idx2] >= y || y_idx2 == INTERP_TAB_N_ENTRIES-1) {
y_idx1 = y_idx2 - 1;
} else {
y_idx1 = y_idx2;
y_idx2 = y_idx1+1;
}
}
/* perform bilinear interpolation */
x1 = tab->x[x_idx1];
x2 = tab->x[x_idx2];
y1 = tab->x[y_idx1];
y2 = tab->x[y_idx2];
/* d_inv is 1/ ((x2-x1)*(y2-y1)); */
z = (tab->z[x_idx1][y_idx1] * (x2-x)*(y2-y) +
tab->z[x_idx2][y_idx1] * (x-x1)*(y2-y) +
tab->z[x_idx1][y_idx2] * (x2-x)*(y-y1) +
tab->z[x_idx2][y_idx2] * (x-x1)*(y-y1)) * tab->d_inv[x_idx1][y_idx1];
return z;
}
/**
* Creates an interpolation table for interpolation over 1 dimension
*/
InterpTab *interp_tab_create_1d(double (*f)(double, void *), void *param) {
InterpTab *tab;
int idx_x;
double x;
tab = g_new(InterpTab, 1);
tab->x = g_new(double, INTERP_TAB_N_ENTRIES);
tab->y = g_new(double, INTERP_TAB_N_ENTRIES);
tab->z = NULL;
tab->d_inv = NULL;
for(idx_x = 0; idx_x < INTERP_TAB_N_ENTRIES; idx_x++) {
x = idx2dbl(idx_x);
tab->x[idx_x] = x;
tab->y[idx_x] = f(x, param);
/*
* fprintf(stderr, "interp_tab_create_1d: x=%g, y=%g\n",
* tab->x[idx_x], tab->y[idx_x]);
*/
}
return tab;
}
/**
* Creates an interpolation table for the provided (2d) function
*/
InterpTab *interp_tab_create(double (*f)(double,double, void *), void *param) {
InterpTab *tab;
int idx_x, idx_y;
double x,y,z, x_diff, y_diff;
tab = g_new(InterpTab, 1);
tab->x = g_new(double, INTERP_TAB_N_ENTRIES);
tab->y = g_new(double, INTERP_TAB_N_ENTRIES);
tab->z = g_new(double *, INTERP_TAB_N_ENTRIES);
tab->d_inv = g_new(double *, INTERP_TAB_N_ENTRIES);
for(idx_x = 0; idx_x < INTERP_TAB_N_ENTRIES; idx_x++) {
x = idx2dbl(idx_x);
tab->x[idx_x] = x;
tab->z[idx_x] = g_new(double, INTERP_TAB_N_ENTRIES);
for(idx_y = 0; idx_y < INTERP_TAB_N_ENTRIES; idx_y++) {
y = idx2dbl(idx_y);
tab->y[idx_y] = y;
/* calculate function value at this point */
z = f(x,y, param);
tab->z[idx_x][idx_y] = z;
}
}
tab->d_inv = g_new(double *, INTERP_TAB_N_ENTRIES);
for(idx_x = 0; idx_x < INTERP_TAB_N_ENTRIES; idx_x++) {
tab->d_inv[idx_x] = g_new(double, INTERP_TAB_N_ENTRIES);
if(idx_x < INTERP_TAB_N_ENTRIES-1) {
x_diff = tab->x[idx_x+1] - tab->x[idx_x];
} else {
/* this becomes extrapolation past end of table */
x_diff = tab->x[idx_x] - tab->x[idx_x-1];
}
for(idx_y = 0; idx_y < INTERP_TAB_N_ENTRIES; idx_y++) {
if(idx_y < INTERP_TAB_N_ENTRIES-1) {
y_diff = tab->y[idx_y+1] - tab->y[idx_y];
} else {
/* extrapolation rather than interpolation */
y_diff = tab->y[idx_y] - tab->y[idx_y-1];
}
tab->d_inv[idx_x][idx_y] = 1.0 / (x_diff*y_diff);
}
}
return tab;
}
/**
* Frees the memory allocated for the provided interpolation table.
*/
void interp_tab_free(InterpTab *tab) {
int i;
g_free(tab->x);
g_free(tab->y);
if(tab->z) {
for(i = 0; i < INTERP_TAB_N_ENTRIES; i++) {
g_free(tab->z[i]);
}
g_free(tab->z);
}
if(tab->d_inv) {
for(i = 0; i < INTERP_TAB_N_ENTRIES; i++) {
g_free(tab->d_inv[i]);
}
g_free(tab->d_inv);
}
g_free(tab);
}