-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpcubature.c
424 lines (371 loc) · 13.2 KB
/
pcubature.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* Adaptive multidimensional integration of a vector of integrands.
*
* Copyright (c) 2005-2013 Steven G. Johnson
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/* p-adaptive cubature (adaptive by increasing the degree of the
cubature rule rather than subdividing the domain), using products
of Clenshaw-Curtis rules. This algorithm may be superior to
Genz-Malik for smooth integrands lacking strongly-localized
features, in moderate dimensions. */
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "cubature.h"
/* error return codes */
#define SUCCESS 0
#define FAILURE 1
/* pre-generated Clenshaw-Curtis rules and weights */
#include "clencurt.h"
/* no point in supporting very high dimensional integrals here */
#define MAXDIM (20U)
/***************************************************************************/
/* For adaptive cubature, thanks to the nesting of the C-C rules, we
can re-use the values from coarser grids for finer grids, and the
coarser grids are also used for error estimation.
A grid is determined by an m[dim] array, where m[i] denotes
2^(m[i]+1)+1 points in the i-th dimension.
*/
/* cache of the values for the m[dim] grid. If mi < dim, then we only
store the values corresponding to the difference between the m grid
and the grid with m[mi] -> m[mi]-1. (m[mi]-1 == -1 corresponds to
the trivial grid of one point in the center.) */
typedef struct cacheval_s {
unsigned m[MAXDIM];
unsigned mi;
double *val;
} cacheval;
/* array of ncache cachevals c[i] */
typedef struct valcache_s {
size_t ncache;
cacheval *c;
} valcache;
static void free_cachevals(valcache *v)
{
if (!v) return;
if (v->c) {
size_t i;
for (i = 0; i < v->ncache; ++i)
free(v->c[i].val);
free(v->c);
v->c = NULL;
}
v->ncache = 0;
}
/***************************************************************************/
/* recursive loop over all cubature points for the given (m,mi) cache entry:
add each point to the buffer buf, evaluating all at once whenever the
buffer is full or when we are done */
static int compute_cacheval(const unsigned *m, unsigned mi,
double *val, size_t *vali,
unsigned fdim, integrand_v f, void *fdata,
unsigned dim, unsigned id, double *p,
const double *xmin, const double *xmax,
double *buf, size_t nbuf, size_t *ibuf)
{
if (id == dim) { /* add point to buffer of points */
memcpy(buf + (*ibuf)++ * dim, p, sizeof(double) * dim);
if (*ibuf == nbuf) { /* flush buffer */
if (f(dim, nbuf, buf, fdata, fdim, val + *vali))
return FAILURE;
*vali += *ibuf * fdim;
*ibuf = 0;
}
}
else {
double c = (xmin[id] + xmax[id]) * 0.5;
double r = (xmax[id] - xmin[id]) * 0.5;
const double *x = clencurt_x
+ ((id == mi) ? (m[id] ? (1 << (m[id] - 1)) : 0) : 0);
unsigned i, nx = (id == mi ? (m[id] ? (1 << (m[id] - 1)) : 1)
: (1 << (m[id])));
if (id != mi) {
p[id] = c;
if (compute_cacheval(m, mi, val, vali, fdim, f, fdata,
dim, id + 1, p,
xmin, xmax, buf, nbuf, ibuf))
return FAILURE;
}
for (i = 0; i < nx; ++i) {
p[id] = c + r * x[i];
if (compute_cacheval(m, mi, val, vali, fdim, f, fdata,
dim, id + 1, p,
xmin, xmax, buf, nbuf, ibuf))
return FAILURE;
p[id] = c - r * x[i];
if (compute_cacheval(m, mi, val, vali, fdim, f, fdata,
dim, id + 1, p,
xmin, xmax, buf, nbuf, ibuf))
return FAILURE;
}
}
return SUCCESS;
}
static size_t num_cacheval(const unsigned *m, unsigned mi, unsigned dim)
{
unsigned i;
size_t nval = 1;
for (i = 0; i < dim; ++i) {
if (i == mi)
nval *= m[i] == 0 ? 2 : (1 << (m[i]));
else
nval *= (1 << (m[i] + 1)) + 1;
}
return nval;
}
static int add_cacheval(valcache *vc,
const unsigned *m, unsigned mi,
unsigned fdim, integrand_v f, void *fdata,
unsigned dim, const double *xmin, const double *xmax,
double *buf, size_t nbuf)
{
size_t ic = vc->ncache;
size_t nval, vali = 0, ibuf = 0;
double p[MAXDIM];
vc->c = (cacheval *) realloc(vc->c, sizeof(cacheval) * ++(vc->ncache));
if (!vc->c) return -1;
vc->c[ic].mi = mi;
memcpy(vc->c[ic].m, m, sizeof(unsigned) * dim);
nval = fdim * num_cacheval(m, mi, dim);
vc->c[ic].val = (double *) malloc(sizeof(double) * nval);
if (!vc->c[ic].val) return FAILURE;
if (compute_cacheval(m, mi, vc->c[ic].val, &vali,
fdim, f, fdata,
dim, 0, p, xmin, xmax,
buf, nbuf, &ibuf))
return FAILURE;
if (ibuf > 0) /* flush remaining buffer */
return f(dim, ibuf, buf, fdata, fdim, vc->c[ic].val + vali);
return SUCCESS;
}
/***************************************************************************/
/* recursive loop to evaluate the integral contribution from the cache
entry c, accumulating in val, for the given m[] except with m[md]
-> m[md] - 1 if md < dim, using the cached values (cm,cmi,cval). id is the
current loop dimension (from 0 to dim-1). */
static unsigned eval(const unsigned *cm, unsigned cmi, double *cval,
const unsigned *m, unsigned md,
unsigned fdim, unsigned dim, unsigned id,
double weight, double *val)
{
size_t voff = 0; /* amount caller should offset cval array afterwards */
if (id == dim) {
unsigned i;
for (i = 0; i < fdim; ++i) val[i] += cval[i] * weight;
voff = fdim;
}
else if (m[id] == 0 && id == md) /* using trivial rule for this dim */ {
voff = eval(cm, cmi, cval, m, md, fdim, dim, id+1, weight*2, val);
voff += fdim * (1 << cm[id]) * 2
* num_cacheval(cm + id+1, cmi - (id+1), dim - (id+1));
}
else {
unsigned i;
unsigned mid = m[id] - (id == md); /* order of C-C rule */
const double *w = clencurt_w + mid + (1 << mid) - 1
+ (id == cmi ? (cm[id] ? 1 + (1 << (cm[id]-1)) : 1) : 0);
unsigned cnx = (id == cmi ? (cm[id] ? (1 << (cm[id]-1)) : 1)
: (1 << (cm[id])));
unsigned nx = cm[id] <= mid ? cnx : (1 << mid);
if (id != cmi) {
voff = eval(cm, cmi, cval, m, md, fdim, dim, id + 1,
weight * w[0], val);
++w;
}
for (i = 0; i < nx; ++i) {
voff += eval(cm, cmi, cval + voff, m, md, fdim, dim, id + 1,
weight * w[i], val);
voff += eval(cm, cmi, cval + voff, m, md, fdim, dim, id + 1,
weight * w[i], val);
}
voff += (cnx - nx) * fdim * 2
* num_cacheval(cm + id+1, cmi - (id+1), dim - (id+1));
}
return voff;
}
/* loop over all cache entries that contribute to the integral,
(with m[md] decremented by 1) */
static void evals(valcache vc, const unsigned *m, unsigned md,
unsigned fdim, unsigned dim,
double V, double *val)
{
size_t i;
memset(val, 0, sizeof(double) * fdim);
for (i = 0; i < vc.ncache; ++i) {
if (vc.c[i].mi >= dim ||
vc.c[i].m[vc.c[i].mi] + (vc.c[i].mi == md) <= m[vc.c[i].mi])
eval(vc.c[i].m, vc.c[i].mi, vc.c[i].val,
m, md, fdim, dim, 0, V, val);
}
}
/* evaluate the integrals for the given m[] using the cached values in vc,
storing the integrals in val[], the error estimate in err[], and the
dimension to subdivide next (the largest error contribution) in *mi */
static void eval_integral(valcache vc, const unsigned *m,
unsigned fdim, unsigned dim, double V,
unsigned *mi, double *val, double *err, double *val1)
{
double maxerr = 0;
unsigned i, j;
evals(vc, m, dim, fdim, dim, V, val);
/* error estimates along each dimension by comparing val with
lower-order rule in that dimension; overall (conservative)
error estimate from maximum error of lower-order rules. */
memset(err, 0, sizeof(double) * fdim);
*mi = 0;
for (i = 0; i < dim; ++i) {
double emax = 0;
evals(vc, m, i, fdim, dim, V, val1);
for (j = 0; j < fdim; ++j) {
double e = fabs(val[j] - val1[j]);
if (e > emax) emax = e;
if (e > err[j]) err[j] = e;
}
if (emax > maxerr) {
maxerr = emax;
*mi = i;
}
}
/* printf("eval: %g +/- %g (dim %u)\n", val[0], err[0], *mi); */
}
/***************************************************************************/
static int converged(unsigned fdim, const double *vals, const double *errs,
double reqAbsError, double reqRelError, error_norm norm)
#define ERR(j) errs[j]
#define VAL(j) vals[j]
#include "converged.h"
/***************************************************************************/
/* Vectorized version with user-supplied buffer to store points and values.
The buffer *buf should be of length *nbuf * dim on entry (these parameters
are changed upon return to the final buffer and length that was used).
The buffer length will be kept <= max(max_nbuf, 1) * dim.
Also allows the caller to specify an array m[dim] of starting degrees
for the rule, which upon return will hold the final degrees. The
number of points in each dimension i is 2^(m[i]+1) + 1. */
int pcubature_v_buf(unsigned fdim, integrand_v f, void *fdata,
unsigned dim, const double *xmin, const double *xmax,
size_t maxEval,
double reqAbsError, double reqRelError,
error_norm norm,
unsigned *m,
double **buf, size_t *nbuf, size_t max_nbuf,
double *val, double *err)
{
int ret = FAILURE;
double V = 1;
size_t numEval = 0, new_nbuf;
unsigned i;
valcache vc = {0, NULL};
double *val1 = NULL;
if (fdim <= 1) norm = ERROR_INDIVIDUAL; /* norm is irrelevant */
if (norm < 0 || norm > ERROR_LINF) return FAILURE; /* invalid norm */
if (fdim == 0) return SUCCESS; /* nothing to do */
if (dim > MAXDIM) return FAILURE; /* unsupported */
if (dim == 0) { /* trivial case */
if (f(0, 1, xmin, fdata, fdim, val)) return FAILURE;
for (i = 0; i < fdim; ++i) err[i] = 0;
return SUCCESS;
}
for (i = 0; i < fdim; ++i) {
val[i] = 0;
err[i] = HUGE_VAL;
}
for (i = 0; i < dim; ++i)
V *= (xmax[i] - xmin[i]) * 0.5; /* scale factor for C-C volume */
new_nbuf = num_cacheval(m, dim, dim);
if (max_nbuf < 1) max_nbuf = 1;
if (new_nbuf > max_nbuf) new_nbuf = max_nbuf;
if (*nbuf < new_nbuf) {
free(*buf);
*buf = (double *) malloc(sizeof(double)
* (*nbuf = new_nbuf) * dim);
if (!*buf) goto done;
}
/* start by evaluating the m=0 cubature rule */
if (add_cacheval(&vc, m, dim, fdim, f, fdata, dim, xmin, xmax,
*buf, *nbuf) != SUCCESS)
goto done;
val1 = (double *) malloc(sizeof(double) * fdim);
while (1) {
unsigned mi;
eval_integral(vc, m, fdim, dim, V, &mi, val, err, val1);
if (converged(fdim, val, err, reqAbsError, reqRelError, norm)
|| (numEval > maxEval && maxEval)) {
ret = SUCCESS;
goto done;
}
m[mi] += 1;
if (m[mi] > clencurt_M) goto done; /* FAILURE */
new_nbuf = num_cacheval(m, mi, dim);
if (new_nbuf > *nbuf && *nbuf < max_nbuf) {
*nbuf = new_nbuf;
if (*nbuf > max_nbuf) *nbuf = max_nbuf;
free(*buf);
*buf = (double *) malloc(sizeof(double) * *nbuf * dim);
if (!*buf) goto done; /* FAILURE */
}
if (add_cacheval(&vc, m, mi, fdim, f, fdata,
dim, xmin, xmax, *buf, *nbuf) != SUCCESS)
goto done; /* FAILURE */
numEval += new_nbuf;
}
done:
free(val1);
free_cachevals(&vc);
return ret;
}
/***************************************************************************/
#define DEFAULT_MAX_NBUF (1U << 20)
int pcubature_v(unsigned fdim, integrand_v f, void *fdata,
unsigned dim, const double *xmin, const double *xmax,
size_t maxEval, double reqAbsError, double reqRelError,
error_norm norm,
double *val, double *err)
{
int ret;
size_t nbuf = 0;
unsigned m[MAXDIM];
double *buf = NULL;
memset(m, 0, sizeof(unsigned) * dim);
ret = pcubature_v_buf(fdim, f, fdata, dim, xmin, xmax,
maxEval, reqAbsError, reqRelError, norm,
m, &buf, &nbuf, DEFAULT_MAX_NBUF, val, err);
free(buf);
return ret;
}
#include "vwrapper.h"
int pcubature(unsigned fdim, integrand f, void *fdata,
unsigned dim, const double *xmin, const double *xmax,
size_t maxEval, double reqAbsError, double reqRelError,
error_norm norm,
double *val, double *err)
{
int ret;
size_t nbuf = 0;
unsigned m[MAXDIM];
double *buf = NULL;
fv_data d;
d.f = f; d.fdata = fdata;
memset(m, 0, sizeof(unsigned) * dim);
ret = pcubature_v_buf(
fdim, fv, &d, dim, xmin, xmax,
maxEval, reqAbsError, reqRelError, norm,
m, &buf, &nbuf, 16 /* max_nbuf > 0 to amortize function overhead */,
val, err);
free(buf);
return ret;
}