gsl_monte__vegas.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /* monte/vegas.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Michael Booth
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /* Author: MJB */
  20. /* Modified by: Brian Gough, 12/2000 */
  21. /* This is an implementation of the adaptive Monte-Carlo algorithm
  22. of G. P. Lepage, originally described in J. Comp. Phys. 27, 192(1978).
  23. The current version of the algorithm was described in the Cornell
  24. preprint CLNS-80/447 of March, 1980.
  25. This code follows most closely the c version by D.R.Yennie, coded
  26. in 1984.
  27. The input coordinates are x[j], with upper and lower limits xu[j]
  28. and xl[j]. The integration length in the j-th direction is
  29. delx[j]. Each coordinate x[j] is rescaled to a variable y[j] in
  30. the range 0 to 1. The range is divided into bins with boundaries
  31. xi[i][j], where i=0 corresponds to y=0 and i=bins to y=1. The grid
  32. is refined (ie, bins are adjusted) using d[i][j] which is some
  33. variation on the squared sum. A third parameter used in defining
  34. the real coordinate using random numbers is called z. It ranges
  35. from 0 to bins. Its integer part gives the lower index of the bin
  36. into which a call is to be placed, and the remainder gives the
  37. location inside the bin.
  38. When stratified sampling is used the bins are grouped into boxes,
  39. and the algorithm allocates an equal number of function calls to
  40. each box.
  41. The variable alpha controls how "stiff" the rebinning algorithm is.
  42. alpha = 0 means never change the grid. Alpha is typically set between
  43. 1 and 2.
  44. */
  45. /* configuration headers */
  46. #include "gsl__config.h"
  47. /* standard headers */
  48. #include <math.h>
  49. #include <stdio.h>
  50. /* gsl headers */
  51. #include "gsl_math.h"
  52. #include "gsl_errno.h"
  53. #include "gsl_rng.h"
  54. #include "gsl_monte_vegas.h"
  55. /* lib-specific headers */
  56. #define BINS_MAX 50 /* even integer, will be divided by two */
  57. /* A separable grid with coordinates and values */
  58. #define COORD(s,i,j) ((s)->xi[(i)*(s)->dim + (j)])
  59. #define NEW_COORD(s,i) ((s)->xin[(i)])
  60. #define VALUE(s,i,j) ((s)->d[(i)*(s)->dim + (j)])
  61. /* predeclare functions */
  62. typedef int coord;
  63. static void init_grid (gsl_monte_vegas_state * s, double xl[], double xu[],
  64. size_t dim);
  65. static void reset_grid_values (gsl_monte_vegas_state * s);
  66. static void init_box_coord (gsl_monte_vegas_state * s, coord box[]);
  67. static int change_box_coord (gsl_monte_vegas_state * s, coord box[]);
  68. static void accumulate_distribution (gsl_monte_vegas_state * s, coord bin[],
  69. double y);
  70. static void random_point (double x[], coord bin[], double *bin_vol,
  71. const coord box[],
  72. const double xl[], const double xu[],
  73. gsl_monte_vegas_state * s, gsl_rng * r);
  74. static void resize_grid (gsl_monte_vegas_state * s, unsigned int bins);
  75. static void refine_grid (gsl_monte_vegas_state * s);
  76. static void print_lim (gsl_monte_vegas_state * state,
  77. double xl[], double xu[], unsigned long dim);
  78. static void print_head (gsl_monte_vegas_state * state,
  79. unsigned long num_dim, unsigned long calls,
  80. unsigned int it_num,
  81. unsigned int bins, unsigned int boxes);
  82. static void print_res (gsl_monte_vegas_state * state,
  83. unsigned int itr, double res, double err,
  84. double cum_res, double cum_err,
  85. double chi_sq);
  86. static void print_dist (gsl_monte_vegas_state * state, unsigned long dim);
  87. static void print_grid (gsl_monte_vegas_state * state, unsigned long dim);
  88. int
  89. gsl_monte_vegas_integrate (gsl_monte_function * f,
  90. double xl[], double xu[],
  91. size_t dim, size_t calls,
  92. gsl_rng * r,
  93. gsl_monte_vegas_state * state,
  94. double *result, double *abserr)
  95. {
  96. double cum_int, cum_sig;
  97. size_t i, k, it;
  98. if (dim != state->dim)
  99. {
  100. GSL_ERROR ("number of dimensions must match allocated size", GSL_EINVAL);
  101. }
  102. for (i = 0; i < dim; i++)
  103. {
  104. if (xu[i] <= xl[i])
  105. {
  106. GSL_ERROR ("xu must be greater than xl", GSL_EINVAL);
  107. }
  108. if (xu[i] - xl[i] > GSL_DBL_MAX)
  109. {
  110. GSL_ERROR ("Range of integration is too large, please rescale",
  111. GSL_EINVAL);
  112. }
  113. }
  114. if (state->stage == 0)
  115. {
  116. init_grid (state, xl, xu, dim);
  117. if (state->verbose >= 0)
  118. {
  119. print_lim (state, xl, xu, dim);
  120. }
  121. }
  122. if (state->stage <= 1)
  123. {
  124. state->wtd_int_sum = 0;
  125. state->sum_wgts = 0;
  126. state->chi_sum = 0;
  127. state->it_num = 1;
  128. state->samples = 0;
  129. }
  130. if (state->stage <= 2)
  131. {
  132. unsigned int bins = state->bins_max;
  133. unsigned int boxes = 1;
  134. if (state->mode != GSL_VEGAS_MODE_IMPORTANCE_ONLY)
  135. {
  136. /* shooting for 2 calls/box */
  137. boxes = floor (pow (calls / 2.0, 1.0 / dim));
  138. state->mode = GSL_VEGAS_MODE_IMPORTANCE;
  139. if (2 * boxes >= state->bins_max)
  140. {
  141. /* if bins/box < 2 */
  142. int box_per_bin = GSL_MAX (boxes / state->bins_max, 1);
  143. bins = GSL_MIN(boxes / box_per_bin, state->bins_max);
  144. boxes = box_per_bin * bins;
  145. state->mode = GSL_VEGAS_MODE_STRATIFIED;
  146. }
  147. }
  148. {
  149. double tot_boxes = pow ((double) boxes, (double) dim);
  150. state->calls_per_box = GSL_MAX (calls / tot_boxes, 2);
  151. calls = state->calls_per_box * tot_boxes;
  152. }
  153. /* total volume of x-space/(avg num of calls/bin) */
  154. state->jac = state->vol * pow ((double) bins, (double) dim) / calls;
  155. state->boxes = boxes;
  156. /* If the number of bins changes from the previous invocation, bins
  157. are expanded or contracted accordingly, while preserving bin
  158. density */
  159. if (bins != state->bins)
  160. {
  161. resize_grid (state, bins);
  162. if (state->verbose > 1)
  163. {
  164. print_grid (state, dim);
  165. }
  166. }
  167. if (state->verbose >= 0)
  168. {
  169. print_head (state,
  170. dim, calls, state->it_num, state->bins, state->boxes);
  171. }
  172. }
  173. state->it_start = state->it_num;
  174. cum_int = 0.0;
  175. cum_sig = 0.0;
  176. state->chisq = 0.0;
  177. for (it = 0; it < state->iterations; it++)
  178. {
  179. double intgrl = 0.0, intgrl_sq = 0.0;
  180. double sig = 0.0;
  181. double wgt;
  182. size_t calls_per_box = state->calls_per_box;
  183. double jacbin = state->jac;
  184. double *x = state->x;
  185. coord *bin = state->bin;
  186. state->it_num = state->it_start + it;
  187. reset_grid_values (state);
  188. init_box_coord (state, state->box);
  189. do
  190. {
  191. double m = 0, q = 0;
  192. double f_sq_sum = 0.0;
  193. for (k = 0; k < calls_per_box; k++)
  194. {
  195. double fval, bin_vol;
  196. random_point (x, bin, &bin_vol, state->box, xl, xu, state, r);
  197. fval = jacbin * bin_vol * GSL_MONTE_FN_EVAL (f, x);
  198. /* recurrence for mean and variance */
  199. {
  200. double d = fval - m;
  201. m += d / (k + 1.0);
  202. q += d * d * (k / (k + 1.0));
  203. }
  204. if (state->mode != GSL_VEGAS_MODE_STRATIFIED)
  205. {
  206. double f_sq = fval * fval;
  207. accumulate_distribution (state, bin, f_sq);
  208. }
  209. }
  210. intgrl += m * calls_per_box;
  211. f_sq_sum = q * calls_per_box ;
  212. sig += f_sq_sum ;
  213. if (state->mode == GSL_VEGAS_MODE_STRATIFIED)
  214. {
  215. accumulate_distribution (state, bin, f_sq_sum);
  216. }
  217. }
  218. while (change_box_coord (state, state->box));
  219. /* Compute final results for this iteration */
  220. sig = sig / (calls_per_box - 1.0) ;
  221. if (sig > 0)
  222. {
  223. wgt = 1.0 / sig;
  224. }
  225. else if (state->sum_wgts > 0)
  226. {
  227. wgt = state->sum_wgts / state->samples;
  228. }
  229. else
  230. {
  231. wgt = 0.0;
  232. }
  233. intgrl_sq = intgrl * intgrl;
  234. state->result = intgrl;
  235. state->sigma = sqrt(sig);
  236. if (wgt > 0.0)
  237. {
  238. state->samples++ ;
  239. state->sum_wgts += wgt;
  240. state->wtd_int_sum += intgrl * wgt;
  241. state->chi_sum += intgrl_sq * wgt;
  242. cum_int = state->wtd_int_sum / state->sum_wgts;
  243. cum_sig = sqrt (1 / state->sum_wgts);
  244. if (state->samples > 1)
  245. {
  246. state->chisq = (state->chi_sum - state->wtd_int_sum * cum_int) /
  247. (state->samples - 1.0);
  248. }
  249. }
  250. else
  251. {
  252. cum_int += (intgrl - cum_int) / (it + 1.0);
  253. cum_sig = 0.0;
  254. }
  255. if (state->verbose >= 0)
  256. {
  257. print_res (state,
  258. state->it_num, intgrl, sqrt (sig), cum_int, cum_sig,
  259. state->chisq);
  260. if (it + 1 == state->iterations && state->verbose > 0)
  261. {
  262. print_grid (state, dim);
  263. }
  264. }
  265. if (state->verbose > 1)
  266. {
  267. print_dist (state, dim);
  268. }
  269. refine_grid (state);
  270. if (state->verbose > 1)
  271. {
  272. print_grid (state, dim);
  273. }
  274. }
  275. /* By setting stage to 1 further calls will generate independent
  276. estimates based on the same grid, although it may be rebinned. */
  277. state->stage = 1;
  278. *result = cum_int;
  279. *abserr = cum_sig;
  280. return GSL_SUCCESS;
  281. }
  282. gsl_monte_vegas_state *
  283. gsl_monte_vegas_alloc (size_t dim)
  284. {
  285. gsl_monte_vegas_state *s =
  286. (gsl_monte_vegas_state *) malloc (sizeof (gsl_monte_vegas_state));
  287. if (s == 0)
  288. {
  289. GSL_ERROR_VAL ("failed to allocate space for vegas state struct",
  290. GSL_ENOMEM, 0);
  291. }
  292. s->delx = (double *) malloc (dim * sizeof (double));
  293. if (s->delx == 0)
  294. {
  295. free (s);
  296. GSL_ERROR_VAL ("failed to allocate space for delx", GSL_ENOMEM, 0);
  297. }
  298. s->d = (double *) malloc (BINS_MAX * dim * sizeof (double));
  299. if (s->d == 0)
  300. {
  301. free (s->delx);
  302. free (s);
  303. GSL_ERROR_VAL ("failed to allocate space for d", GSL_ENOMEM, 0);
  304. }
  305. s->xi = (double *) malloc ((BINS_MAX + 1) * dim * sizeof (double));
  306. if (s->xi == 0)
  307. {
  308. free (s->d);
  309. free (s->delx);
  310. free (s);
  311. GSL_ERROR_VAL ("failed to allocate space for xi", GSL_ENOMEM, 0);
  312. }
  313. s->xin = (double *) malloc ((BINS_MAX + 1) * sizeof (double));
  314. if (s->xin == 0)
  315. {
  316. free (s->xi);
  317. free (s->d);
  318. free (s->delx);
  319. free (s);
  320. GSL_ERROR_VAL ("failed to allocate space for xin", GSL_ENOMEM, 0);
  321. }
  322. s->weight = (double *) malloc (BINS_MAX * sizeof (double));
  323. if (s->weight == 0)
  324. {
  325. free (s->xin);
  326. free (s->xi);
  327. free (s->d);
  328. free (s->delx);
  329. free (s);
  330. GSL_ERROR_VAL ("failed to allocate space for xin", GSL_ENOMEM, 0);
  331. }
  332. s->box = (coord *) malloc (dim * sizeof (coord));
  333. if (s->box == 0)
  334. {
  335. free (s->weight);
  336. free (s->xin);
  337. free (s->xi);
  338. free (s->d);
  339. free (s->delx);
  340. free (s);
  341. GSL_ERROR_VAL ("failed to allocate space for box", GSL_ENOMEM, 0);
  342. }
  343. s->bin = (coord *) malloc (dim * sizeof (coord));
  344. if (s->bin == 0)
  345. {
  346. free (s->box);
  347. free (s->weight);
  348. free (s->xin);
  349. free (s->xi);
  350. free (s->d);
  351. free (s->delx);
  352. free (s);
  353. GSL_ERROR_VAL ("failed to allocate space for bin", GSL_ENOMEM, 0);
  354. }
  355. s->x = (double *) malloc (dim * sizeof (double));
  356. if (s->x == 0)
  357. {
  358. free (s->bin);
  359. free (s->box);
  360. free (s->weight);
  361. free (s->xin);
  362. free (s->xi);
  363. free (s->d);
  364. free (s->delx);
  365. free (s);
  366. GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
  367. }
  368. s->dim = dim;
  369. s->bins_max = BINS_MAX;
  370. gsl_monte_vegas_init (s);
  371. return s;
  372. }
  373. /* Set some default values and whatever */
  374. int
  375. gsl_monte_vegas_init (gsl_monte_vegas_state * state)
  376. {
  377. state->stage = 0;
  378. state->alpha = 1.5;
  379. state->verbose = -1;
  380. state->iterations = 5;
  381. state->mode = GSL_VEGAS_MODE_IMPORTANCE;
  382. state->chisq = 0;
  383. state->bins = state->bins_max;
  384. state->ostream = stdout;
  385. return GSL_SUCCESS;
  386. }
  387. void
  388. gsl_monte_vegas_free (gsl_monte_vegas_state * s)
  389. {
  390. free (s->x);
  391. free (s->delx);
  392. free (s->d);
  393. free (s->xi);
  394. free (s->xin);
  395. free (s->weight);
  396. free (s->box);
  397. free (s->bin);
  398. free (s);
  399. }
  400. static void
  401. init_box_coord (gsl_monte_vegas_state * s, coord box[])
  402. {
  403. size_t i;
  404. size_t dim = s->dim;
  405. for (i = 0; i < dim; i++)
  406. {
  407. box[i] = 0;
  408. }
  409. }
  410. /* change_box_coord steps through the box coord like
  411. {0,0}, {0, 1}, {0, 2}, {0, 3}, {1, 0}, {1, 1}, {1, 2}, ...
  412. */
  413. static int
  414. change_box_coord (gsl_monte_vegas_state * s, coord box[])
  415. {
  416. int j = s->dim - 1;
  417. int ng = s->boxes;
  418. while (j >= 0)
  419. {
  420. box[j] = (box[j] + 1) % ng;
  421. if (box[j] != 0)
  422. {
  423. return 1;
  424. }
  425. j--;
  426. }
  427. return 0;
  428. }
  429. static void
  430. init_grid (gsl_monte_vegas_state * s, double xl[], double xu[], size_t dim)
  431. {
  432. size_t j;
  433. double vol = 1.0;
  434. s->bins = 1;
  435. for (j = 0; j < dim; j++)
  436. {
  437. double dx = xu[j] - xl[j];
  438. s->delx[j] = dx;
  439. vol *= dx;
  440. COORD (s, 0, j) = 0.0;
  441. COORD (s, 1, j) = 1.0;
  442. }
  443. s->vol = vol;
  444. }
  445. static void
  446. reset_grid_values (gsl_monte_vegas_state * s)
  447. {
  448. size_t i, j;
  449. size_t dim = s->dim;
  450. size_t bins = s->bins;
  451. for (i = 0; i < bins; i++)
  452. {
  453. for (j = 0; j < dim; j++)
  454. {
  455. VALUE (s, i, j) = 0.0;
  456. }
  457. }
  458. }
  459. static void
  460. accumulate_distribution (gsl_monte_vegas_state * s, coord bin[], double y)
  461. {
  462. size_t j;
  463. size_t dim = s->dim;
  464. for (j = 0; j < dim; j++)
  465. {
  466. int i = bin[j];
  467. VALUE (s, i, j) += y;
  468. }
  469. }
  470. static void
  471. random_point (double x[], coord bin[], double *bin_vol,
  472. const coord box[], const double xl[], const double xu[],
  473. gsl_monte_vegas_state * s, gsl_rng * r)
  474. {
  475. /* Use the random number generator r to return a random position x
  476. in a given box. The value of bin gives the bin location of the
  477. random position (there may be several bins within a given box) */
  478. double vol = 1.0;
  479. size_t j;
  480. size_t dim = s->dim;
  481. size_t bins = s->bins;
  482. size_t boxes = s->boxes;
  483. DISCARD_POINTER(xu); /* prevent warning about unused parameter */
  484. for (j = 0; j < dim; ++j)
  485. {
  486. /* box[j] + ran gives the position in the box units, while z
  487. is the position in bin units. */
  488. double z = ((box[j] + gsl_rng_uniform_pos (r)) / boxes) * bins;
  489. int k = z;
  490. double y, bin_width;
  491. bin[j] = k;
  492. if (k == 0)
  493. {
  494. bin_width = COORD (s, 1, j);
  495. y = z * bin_width;
  496. }
  497. else
  498. {
  499. bin_width = COORD (s, k + 1, j) - COORD (s, k, j);
  500. y = COORD (s, k, j) + (z - k) * bin_width;
  501. }
  502. x[j] = xl[j] + y * s->delx[j];
  503. vol *= bin_width;
  504. }
  505. *bin_vol = vol;
  506. }
  507. static void
  508. resize_grid (gsl_monte_vegas_state * s, unsigned int bins)
  509. {
  510. size_t j, k;
  511. size_t dim = s->dim;
  512. /* weight is ratio of bin sizes */
  513. double pts_per_bin = (double) s->bins / (double) bins;
  514. for (j = 0; j < dim; j++)
  515. {
  516. double xold;
  517. double xnew = 0;
  518. double dw = 0;
  519. int i = 1;
  520. for (k = 1; k <= s->bins; k++)
  521. {
  522. dw += 1.0;
  523. xold = xnew;
  524. xnew = COORD (s, k, j);
  525. for (; dw > pts_per_bin; i++)
  526. {
  527. dw -= pts_per_bin;
  528. NEW_COORD (s, i) = xnew - (xnew - xold) * dw;
  529. }
  530. }
  531. for (k = 1 ; k < bins; k++)
  532. {
  533. COORD(s, k, j) = NEW_COORD(s, k);
  534. }
  535. COORD (s, bins, j) = 1;
  536. }
  537. s->bins = bins;
  538. }
  539. static void
  540. refine_grid (gsl_monte_vegas_state * s)
  541. {
  542. size_t i, j, k;
  543. size_t dim = s->dim;
  544. size_t bins = s->bins;
  545. for (j = 0; j < dim; j++)
  546. {
  547. double grid_tot_j, tot_weight;
  548. double * weight = s->weight;
  549. double oldg = VALUE (s, 0, j);
  550. double newg = VALUE (s, 1, j);
  551. VALUE (s, 0, j) = (oldg + newg) / 2;
  552. grid_tot_j = VALUE (s, 0, j);
  553. /* This implements gs[i][j] = (gs[i-1][j]+gs[i][j]+gs[i+1][j])/3 */
  554. for (i = 1; i < bins - 1; i++)
  555. {
  556. double rc = oldg + newg;
  557. oldg = newg;
  558. newg = VALUE (s, i + 1, j);
  559. VALUE (s, i, j) = (rc + newg) / 3;
  560. grid_tot_j += VALUE (s, i, j);
  561. }
  562. VALUE (s, bins - 1, j) = (newg + oldg) / 2;
  563. grid_tot_j += VALUE (s, bins - 1, j);
  564. tot_weight = 0;
  565. for (i = 0; i < bins; i++)
  566. {
  567. weight[i] = 0;
  568. if (VALUE (s, i, j) > 0)
  569. {
  570. oldg = grid_tot_j / VALUE (s, i, j);
  571. /* damped change */
  572. weight[i] = pow (((oldg - 1) / oldg / log (oldg)), s->alpha);
  573. }
  574. tot_weight += weight[i];
  575. #ifdef DEBUG
  576. printf("weight[%d] = %g\n", i, weight[i]);
  577. #endif
  578. }
  579. {
  580. double pts_per_bin = tot_weight / bins;
  581. double xold;
  582. double xnew = 0;
  583. double dw = 0;
  584. i = 1;
  585. for (k = 0; k < bins; k++)
  586. {
  587. dw += weight[k];
  588. xold = xnew;
  589. xnew = COORD (s, k + 1, j);
  590. for (; dw > pts_per_bin; i++)
  591. {
  592. dw -= pts_per_bin;
  593. NEW_COORD (s, i) = xnew - (xnew - xold) * dw / weight[k];
  594. }
  595. }
  596. for (k = 1 ; k < bins ; k++)
  597. {
  598. COORD(s, k, j) = NEW_COORD(s, k);
  599. }
  600. COORD (s, bins, j) = 1;
  601. }
  602. }
  603. }
  604. static void
  605. print_lim (gsl_monte_vegas_state * state,
  606. double xl[], double xu[], unsigned long dim)
  607. {
  608. unsigned long j;
  609. fprintf (state->ostream, "The limits of integration are:\n");
  610. for (j = 0; j < dim; ++j)
  611. fprintf (state->ostream, "\nxl[%lu]=%f xu[%lu]=%f", j, xl[j], j, xu[j]);
  612. fprintf (state->ostream, "\n");
  613. fflush (state->ostream);
  614. }
  615. static void
  616. print_head (gsl_monte_vegas_state * state,
  617. unsigned long num_dim, unsigned long calls,
  618. unsigned int it_num, unsigned int bins, unsigned int boxes)
  619. {
  620. fprintf (state->ostream,
  621. "\nnum_dim=%lu, calls=%lu, it_num=%d, max_it_num=%d ",
  622. num_dim, calls, it_num, state->iterations);
  623. fprintf (state->ostream,
  624. "verb=%d, alph=%.2f,\nmode=%d, bins=%d, boxes=%d\n",
  625. state->verbose, state->alpha, state->mode, bins, boxes);
  626. fprintf (state->ostream,
  627. "\n single.......iteration ");
  628. fprintf (state->ostream, "accumulated......results \n");
  629. fprintf (state->ostream,
  630. "iteration integral sigma integral ");
  631. fprintf (state->ostream, " sigma chi-sq/it\n\n");
  632. fflush (state->ostream);
  633. }
  634. static void
  635. print_res (gsl_monte_vegas_state * state,
  636. unsigned int itr,
  637. double res, double err,
  638. double cum_res, double cum_err,
  639. double chi_sq)
  640. {
  641. fprintf (state->ostream,
  642. "%4d %6.4e %10.2e %6.4e %8.2e %10.2e\n",
  643. itr, res, err, cum_res, cum_err, chi_sq);
  644. fflush (state->ostream);
  645. }
  646. static void
  647. print_dist (gsl_monte_vegas_state * state, unsigned long dim)
  648. {
  649. unsigned long i, j;
  650. int p = state->verbose;
  651. if (p < 1)
  652. return;
  653. for (j = 0; j < dim; ++j)
  654. {
  655. fprintf (state->ostream, "\n axis %lu \n", j);
  656. fprintf (state->ostream, " x g\n");
  657. for (i = 0; i < state->bins; i++)
  658. {
  659. fprintf (state->ostream, "weight [%11.2e , %11.2e] = ",
  660. COORD (state, i, j), COORD(state,i+1,j));
  661. fprintf (state->ostream, " %11.2e\n", VALUE (state, i, j));
  662. }
  663. fprintf (state->ostream, "\n");
  664. }
  665. fprintf (state->ostream, "\n");
  666. fflush (state->ostream);
  667. }
  668. static void
  669. print_grid (gsl_monte_vegas_state * state, unsigned long dim)
  670. {
  671. unsigned long i, j;
  672. int p = state->verbose;
  673. if (p < 1)
  674. return;
  675. for (j = 0; j < dim; ++j)
  676. {
  677. fprintf (state->ostream, "\n axis %lu \n", j);
  678. fprintf (state->ostream, " x \n");
  679. for (i = 0; i <= state->bins; i++)
  680. {
  681. fprintf (state->ostream, "%11.2e", COORD (state, i, j));
  682. if (i % 5 == 4)
  683. fprintf (state->ostream, "\n");
  684. }
  685. fprintf (state->ostream, "\n");
  686. }
  687. fprintf (state->ostream, "\n");
  688. fflush (state->ostream);
  689. }