53 return (ee.
val == 0.0 ? HUGE_VAL : fabs(ee.
err / ee.
val));
66 for (i = 0; i < h->
dim; ++i)
76 h.
data = (
double *) malloc(
sizeof(
double) * dim * 2);
77 for (i = 0; i < dim; ++i) {
78 h.
data[i] = center[i];
79 h.
data[i + dim] = width[i];
89 for (i = 0; i < dim; ++i) {
90 h.
data[i] = 0.5 * (xmin[i] + xmax[i]);
91 h.
data[i + dim] = 0.5 * (xmax[i] - xmin[i]);
128 R->
h.
data[d + dim] *= 0.5;
163 #if (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined (__x86_64__))
166 static unsigned ls0(
unsigned n)
170 __asm__(
"bsfl %1,%0":
"=r"(
count):
"rm"(
n));
174 static unsigned ls0(
unsigned n)
176 const unsigned bits[] = {
177 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
178 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
179 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
180 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
181 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
182 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
183 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
184 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7,
185 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
186 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
187 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
188 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
189 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
190 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
191 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
192 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 8,
196 while ((
n & 0xff) == 0xff) {
200 return bit + bits[
n & 0xff];
218 for (i = 0; i <
dim; ++i)
234 p[d] = (signs & mask) ? c[d] - r[d] : c[d] + r[d];
245 for (i = 0; i <
dim - 1; ++i) {
247 for (j = i + 1; j <
dim; ++j) {
267 double *sum0_,
const double *r1,
double *sum1_,
const double *r2,
double *sum2_)
270 unsigned i, dimDiffMax = 0;
271 double sum0, sum1 = 0, sum2 = 0;
273 double ratio = r1[0] / r2[0];
276 sum0 = f(
dim, p, fdata);
278 for (i = 0; i <
dim; i++) {
279 double f1a, f1b, f2a, f2b, diff;
282 sum1 += (f1a = f(
dim, p, fdata));
284 sum1 += (f1b = f(
dim, p, fdata));
286 sum2 += (f2a = f(
dim, p, fdata));
288 sum2 += (f2b = f(
dim, p, fdata));
291 diff = fabs(f1a + f1b - 2 * sum0 - ratio * (f2a + f2b - 2 * sum0));
293 if (diff > maxdiff) {
306 #define num0_0(dim) (1U)
307 #define numR0_0fs(dim) (2 * (dim))
308 #define numRR0_0fs(dim) (2 * (dim) * (dim-1))
309 #define numR_Rfs(dim) (1U << (dim))
325 double *widthLambda, *widthLambda2, *
p;
332 #define real(x) ((double)(x))
333 #define to_int(n) ((int)(n))
350 const double lambda2 = 0.3585685828003180919906451539079374954541;
351 const double lambda4 = 0.9486832980505137995996680633298155601160;
352 const double lambda5 = 0.6882472016116852977216287342936235251269;
353 const double weight2 = 980. / 6561.;
354 const double weight4 = 200. / 19683.;
355 const double weightE2 = 245. / 486.;
356 const double weightE4 = 25. / 729.;
359 unsigned i, dimDiffMax, dim = r_->
dim;
360 double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0, sum4, sum5, result, res5th;
361 const double *center = h->
data;
362 const double *width = h->
data + dim;
364 for (i = 0; i < dim; ++i)
367 for (i = 0; i < dim; ++i)
369 for (i = 0; i < dim; ++i)
381 for (i = 0; i < dim; ++i)
387 result = h->
vol * (r->
weight1 * sum1 + weight2 * sum2 + r->
weight3 * sum3 + weight4 * sum4 +
393 ee->
err = fabs(res5th - result);
402 if (dim < 2)
return 0;
415 r->
p = (
double *) malloc(
sizeof(
double) * dim * 3);
438 const unsigned n = 8;
439 const double xgk[8] = {
440 0.991455371120812639206854697526329,
441 0.949107912342758524526189684047851,
442 0.864864423359769072789712788640926,
443 0.741531185599394439863864773280788,
444 0.586087235467691130294144838258730,
445 0.405845151377397166906606412076961,
446 0.207784955007898467600689403773245,
447 0.000000000000000000000000000000000
451 static const double wg[4] = {
452 0.129484966168869693270611432679082,
453 0.279705391489276667901467771423780,
454 0.381830050505118944950369775488975,
455 0.417959183673469387755102040816327
457 static const double wgk[8] = {
458 0.022935322010529224963732008058970,
459 0.063092092629978553290700663189204,
460 0.104790010322250183839876322541518,
461 0.140653259715525918745189590510238,
462 0.169004726639267902826583426598550,
463 0.190350578064785409913256402421014,
464 0.204432940075298892414161999234649,
465 0.209482141084727828012999174891714
468 const double center = h->
data[0];
469 const double width = h->
data[1];
470 double fv1[7], fv2[7];
471 const double f_center = f(1, ¢er, fdata);
472 double result_gauss = f_center * wg[
n/2 - 1];
473 double result_kronrod = f_center * wgk[
n - 1];
474 double result_abs = fabs(result_kronrod);
475 double result_asc, mean, err;
480 for (j = 0; j < (
n - 1) / 2; ++j) {
481 unsigned int j2 = 2*j + 1;
482 double x, f1, f2, fsum, w = width * xgk[j2];
483 x = center - w; fv1[j2] = f1 = f(1, &x, fdata);
484 x = center + w; fv2[j2] = f2 = f(1, &x, fdata);
486 result_gauss += wg[j] * fsum;
487 result_kronrod += wgk[j2] * fsum;
488 result_abs += wgk[j2] * (fabs(f1) + fabs(f2));
491 for (j = 0; j <
n/2; ++j) {
492 unsigned int j2 = 2*j;
493 double x, f1, f2, w = width * xgk[j2];
494 x = center - w; fv1[j2] = f1 = f(1, &x, fdata);
495 x = center + w; fv2[j2] = f2 = f(1, &x, fdata);
496 result_kronrod += wgk[j2] * (f1 + f2);
497 result_abs += wgk[j2] * (fabs(f1) + fabs(f2));
501 ee->
val = result_kronrod * width;
504 mean = result_kronrod * 0.5;
505 result_asc = wgk[
n - 1] * fabs(f_center - mean);
506 for (j = 0; j <
n - 1; ++j)
507 result_asc += wgk[j] * (fabs(fv1[j]-mean) + fabs(fv2[j]-mean));
508 err = (result_kronrod - result_gauss) * width;
511 if (result_asc != 0 && err != 0) {
512 double scale = pow((200 * err / result_asc), 1.5);
514 err = result_asc * scale;
518 if (result_abs > DBL_MIN / (50 * DBL_EPSILON)) {
519 double min_err = 50 * DBL_EPSILON * result_abs;
531 if (dim != 1)
return 0;
546 #define KEY(hi) ((hi).ee.err)
595 int parent = (insert - 1) / 2;
601 h->
items[insert] = hi;
610 fprintf(stderr,
"attempted to pop an empty heap\n");
616 while ((child = i * 2 + 1) <
n) {
630 h->
items[i = largest] = swap;
643 maxEval,
double reqAbsError,
double reqRelError,
esterr *ee)
645 unsigned initialRegions;
648 unsigned initialPoints;
654 initialPoints = initialRegions * r->
num_points;
657 if (initialPoints > maxEval) {
659 initialPoints = initialRegions * r->
num_points;
661 maxEval -= initialPoints;
666 if (initialRegions == 0)
676 for (i = 0; i < maxIter; ++i) {
678 if (i >= minIter && (regions.
ee.
err <= reqAbsError
690 for (i = 0; i < regions.
n; ++i) {
702 unsigned dim,
const double *xmin,
const double *xmax,
703 unsigned maxEval,
double reqAbsError,
double reqRelError,
704 double *val,
double *estimated_error)
711 ee.
err = 1.0e30; ee.
val = 1.0e30;
714 ee.
val = f(0, xmin, fdata);
721 maxEval, reqAbsError, reqRelError,
724 *estimated_error = ee.
err;
static void destroy_region(region *R)
static void cut_region(region *R, region *R2)
static hypercube make_hypercube(unsigned dim, const double *center, const double *width)
static hypercube make_hypercube_range(unsigned dim, const double *xmin, const double *xmax)
static void destroy_rule(rule *r)
static heap heap_alloc(unsigned nalloc)
static double evalRR0_0fs(integrand f, void *fdata, unsigned dim, double *p, const double *c, const double *r)
static double relError(esterr ee)
static heap_item heap_pop(heap *h)
int adapt_integrate(integrand f, void *fdata, unsigned dim, const double *xmin, const double *xmax, unsigned maxEval, double reqAbsError, double reqRelError, double *val, double *estimated_error)
static void heap_push(heap *h, heap_item hi)
static unsigned evalR0_0fs4d(integrand f, void *fdata, unsigned dim, double *p, const double *c, double *sum0_, const double *r1, double *sum1_, const double *r2, double *sum2_)
static void heap_free(heap *h)
static double compute_vol(const hypercube *h)
static rule * make_rule75genzmalik(unsigned dim)
static int ruleadapt_integrate(rule *r, integrand f, void *fdata, const hypercube *h, unsigned maxEval, double reqAbsError, double reqRelError, esterr *ee)
static unsigned rule15gauss_evalError(rule *r, integrand f, void *fdata, const hypercube *h, esterr *ee)
static void destroy_hypercube(hypercube *h)
static unsigned rule75genzmalik_evalError(rule *r_, integrand f, void *fdata, const hypercube *h, esterr *ee)
static void destroy_rule75genzmalik(rule *r_)
static region eval_region(region R, integrand f, void *fdata, rule *r)
static unsigned ls0(unsigned n)
static region make_region(const hypercube *h)
static double evalR_Rfs(integrand f, void *fdata, unsigned dim, double *p, const double *c, const double *r)
static void heap_resize(heap *h, unsigned nalloc)
static rule * make_rule15gauss(unsigned dim)
double(* integrand)(unsigned ndim, const double *x, void *)
double sum(const array::Scalar &input)
Sums up all the values in an array::Scalar object. Ignores ghosts.
unsigned(* evalError)(struct rule_s *r, integrand f, void *fdata, const hypercube *h, esterr *ee)
void(* destroy)(struct rule_s *r)