void print_ip_steady(int line, int col) { if (!line) { if (!col) { print_lim(); fprintf(stderr, WC "|" FC "Ver: " VC "%c ", tok[0]); fprintf(stderr, WC "|" FC "IHL: " VC "%c ", tok[1]); ip.hlen = c2hex(tok[1]); /* if header length is > 5 (in 32bit words) then we must take into account to view the additional options in the ip header */ if (ip.hlen > 5) { ip.options++; ip.left = (ip.hlen - 5) << 1; } else ip.options = 0; fprintf(stderr, WC "|" FC " ToS: " VC "%s ", &tok[2]); } else if (col == 1) { /* copy the next portion of XXXX inside buf take into account that there is a space between each of the 4-char hex pairs that get formed */ ip.total_len = ((((c2hex(tok[0]) & 0x0f) << 12) | ((c2hex(tok[1]) & 0x0f) << 8)) | ((c2hex(tok[2]) & 0x0f) << 4)) | (c2hex(tok[3]) & 0x0f); fprintf(stderr, WC "|" FC " Total Length: " VC "%s " WC "|", tok); print_lim(); } else if (col == 2) { fprintf(stderr, WC "|" FC " Identification: " VC "%s ", tok); } else if (col == 3) { /* this (not really)obfu thing prints the 3bit flags of the ip header */ char c = c2hex(tok[0]); fprintf(stderr, WC "|" FC "F: " VC "%x ", (c & 0x0e) >> 1); /* print the 13bit fragment offset */ unsigned int t; t = ((((c & 0x01) << 12) | ((c2hex(tok[1]) & 0x0f) << 8)) | ((c2hex(tok[2]) & 0x0f) << 4)) | (c2hex(tok[3]) & 0x0f); fprintf(stderr, WC "|" FC " Fragment Offset:" VC "%4x " WC "|", t); print_lim(); } else if (col == 4) {
void print_ip_options(void) { static int i = 0; if (!i) fprintf(stderr, WC "|" FC "\t\t Options: " VC "%s", tok); else { fprintf(stderr, VC " %s " WC "|", tok); print_lim(); } ip.left--; if (!i) i++; else i = 0; }
int gsl_monte_vegas_integrate (gsl_monte_function * f, double xl[], double xu[], size_t dim, size_t calls, gsl_rng * r, gsl_monte_vegas_state * state, double *result, double *abserr) { double cum_int, cum_sig; size_t i, k, it; if (dim != state->dim) { GSL_ERROR ("number of dimensions must match allocated size", GSL_EINVAL); } for (i = 0; i < dim; i++) { if (xu[i] <= xl[i]) { GSL_ERROR ("xu must be greater than xl", GSL_EINVAL); } if (xu[i] - xl[i] > GSL_DBL_MAX) { GSL_ERROR ("Range of integration is too large, please rescale", GSL_EINVAL); } } if (state->stage == 0) { init_grid (state, xl, xu, dim); if (state->verbose >= 0) { print_lim (state, xl, xu, dim); } } if (state->stage <= 1) { state->wtd_int_sum = 0; state->sum_wgts = 0; state->chi_sum = 0; state->it_num = 1; state->samples = 0; state->chisq = 0; } if (state->stage <= 2) { unsigned int bins = state->bins_max; unsigned int boxes = 1; if (state->mode != GSL_VEGAS_MODE_IMPORTANCE_ONLY) { /* shooting for 2 calls/box */ boxes = floor (pow (calls / 2.0, 1.0 / dim)); state->mode = GSL_VEGAS_MODE_IMPORTANCE; if (2 * boxes >= state->bins_max) { /* if bins/box < 2 */ int box_per_bin = GSL_MAX (boxes / state->bins_max, 1); bins = GSL_MIN(boxes / box_per_bin, state->bins_max); boxes = box_per_bin * bins; state->mode = GSL_VEGAS_MODE_STRATIFIED; } } { double tot_boxes = gsl_pow_int ((double) boxes, dim); state->calls_per_box = GSL_MAX (calls / tot_boxes, 2); calls = state->calls_per_box * tot_boxes; } /* total volume of x-space/(avg num of calls/bin) */ state->jac = state->vol * pow ((double) bins, (double) dim) / calls; state->boxes = boxes; /* If the number of bins changes from the previous invocation, bins are expanded or contracted accordingly, while preserving bin density */ if (bins != state->bins) { resize_grid (state, bins); if (state->verbose > 1) { print_grid (state, dim); } } if (state->verbose >= 0) { print_head (state, dim, calls, state->it_num, state->bins, state->boxes); } } state->it_start = state->it_num; cum_int = 0.0; cum_sig = 0.0; for (it = 0; it < state->iterations; it++) { double intgrl = 0.0, intgrl_sq = 0.0; double tss = 0.0; double wgt, var, sig; size_t calls_per_box = state->calls_per_box; double jacbin = state->jac; double *x = state->x; coord *bin = state->bin; state->it_num = state->it_start + it; reset_grid_values (state); init_box_coord (state, state->box); do { volatile double m = 0, q = 0; double f_sq_sum = 0.0; for (k = 0; k < calls_per_box; k++) { volatile double fval; double bin_vol; random_point (x, bin, &bin_vol, state->box, xl, xu, state, r); fval = jacbin * bin_vol * GSL_MONTE_FN_EVAL (f, x); /* recurrence for mean and variance (sum of squares) */ { double d = fval - m; m += d / (k + 1.0); q += d * d * (k / (k + 1.0)); } if (state->mode != GSL_VEGAS_MODE_STRATIFIED) { double f_sq = fval * fval; accumulate_distribution (state, bin, f_sq); } } intgrl += m * calls_per_box; f_sq_sum = q * calls_per_box; tss += f_sq_sum; if (state->mode == GSL_VEGAS_MODE_STRATIFIED) { accumulate_distribution (state, bin, f_sq_sum); } } while (change_box_coord (state, state->box)); /* Compute final results for this iteration */ var = tss / (calls_per_box - 1.0) ; if (var > 0) { wgt = 1.0 / var; } else if (state->sum_wgts > 0) { wgt = state->sum_wgts / state->samples; } else { wgt = 0.0; } intgrl_sq = intgrl * intgrl; sig = sqrt (var); state->result = intgrl; state->sigma = sig; if (wgt > 0.0) { double sum_wgts = state->sum_wgts; double wtd_int_sum = state->wtd_int_sum; double m = (sum_wgts > 0) ? (wtd_int_sum / sum_wgts) : 0; double q = intgrl - m; state->samples++ ; state->sum_wgts += wgt; state->wtd_int_sum += intgrl * wgt; state->chi_sum += intgrl_sq * wgt; cum_int = state->wtd_int_sum / state->sum_wgts; cum_sig = sqrt (1 / state->sum_wgts); #if USE_ORIGINAL_CHISQ_FORMULA /* This is the chisq formula from the original Lepage paper. It computes the variance from <x^2> - <x>^2 and can suffer from catastrophic cancellations, e.g. returning negative chisq. */ if (state->samples > 1) { state->chisq = (state->chi_sum - state->wtd_int_sum * cum_int) / (state->samples - 1.0); } #else /* The new formula below computes exactly the same quantity as above but using a stable recurrence */ if (state->samples == 1) { state->chisq = 0; } else { state->chisq *= (state->samples - 2.0); state->chisq += (wgt / (1 + (wgt / sum_wgts))) * q * q; state->chisq /= (state->samples - 1.0); } #endif } else { cum_int += (intgrl - cum_int) / (it + 1.0); cum_sig = 0.0; } if (state->verbose >= 0) { print_res (state, state->it_num, intgrl, sig, cum_int, cum_sig, state->chisq); if (it + 1 == state->iterations && state->verbose > 0) { print_grid (state, dim); } } if (state->verbose > 1) { print_dist (state, dim); } refine_grid (state); if (state->verbose > 1) { print_grid (state, dim); } } /* By setting stage to 1 further calls will generate independent estimates based on the same grid, although it may be rebinned. */ state->stage = 1; *result = cum_int; *abserr = cum_sig; return GSL_SUCCESS; }