int main (void) { const gsl_rng_type * T; gsl_rng * r; gsl_histogram2d * h = gsl_histogram2d_alloc (10, 10); gsl_histogram2d_set_ranges_uniform (h, 0.0, 1.0, 0.0, 1.0); gsl_histogram2d_accumulate (h, 0.3, 0.3, 1); gsl_histogram2d_accumulate (h, 0.8, 0.1, 5); gsl_histogram2d_accumulate (h, 0.7, 0.9, 0.5); gsl_rng_env_setup (); T = gsl_rng_default; r = gsl_rng_alloc (T); { int i; gsl_histogram2d_pdf * p = gsl_histogram2d_pdf_alloc (h->nx, h->ny); gsl_histogram2d_pdf_init (p, h); for (i = 0; i < 1000; i++) { double x, y; double u = gsl_rng_uniform (r); double v = gsl_rng_uniform (r); gsl_histogram2d_pdf_sample (p, u, v, &x, &y); printf ("%g %g\n", x, y); } gsl_histogram2d_pdf_free (p); } gsl_histogram2d_free (h); gsl_rng_free (r); return 0; }
void test2d_resample (void) { size_t i, j; int status = 0; double total = 0; size_t N = 200000; gsl_histogram2d *h; gsl_ieee_env_setup (); h = gsl_histogram2d_calloc_uniform (10, 10, 0.0, 1.0, 0.0, 1.0); for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { double w = 10.0 * i + j; total += w; gsl_histogram2d_accumulate (h, 0.1 * i, 0.1 * i, w); } } { gsl_histogram2d_pdf *p = gsl_histogram2d_pdf_alloc (10,10); gsl_histogram2d *hh = gsl_histogram2d_calloc_uniform (20, 20, 0.0, 1.0, 0.0, 1.0); gsl_histogram2d_pdf_init (p, h); for (i = 0; i < N; i++) { double u = urand(); double v = urand(); double x, y; status = gsl_histogram2d_pdf_sample (p, u, v, &x, &y); status = gsl_histogram2d_increment (hh, x, y); } status = 0; for (i = 0; i < 20; i++) { for (j = 0; j < 20; j++) { double z = 4 * total * gsl_histogram2d_get (hh, i, j) / (double) N; size_t k1, k2; double ya; double x, xmax, y, ymax; gsl_histogram2d_get_xrange (hh, i, &x, &xmax); gsl_histogram2d_get_yrange (hh, j, &y, &ymax); gsl_histogram2d_find (h, x, y, &k1, &k2); ya = gsl_histogram2d_get (h, k1, k2); if (ya == 0) { if (z != 0) { status = 1; printf ("(%d,%d): %g vs %g\n", (int)i, (int)j, z, ya); } } else { double err = 1 / sqrt (gsl_histogram2d_get (hh, i, j)); double sigma = fabs ((z - ya) / (ya * err)); if (sigma > 3) { status = 1; printf ("%g vs %g err=%g sigma=%g\n", z, ya, err, sigma); } } } } gsl_histogram2d_pdf_free (p) ; gsl_histogram2d_free (hh) ; gsl_test (status, "gsl_histogram2d_pdf_sample within statistical errors"); } gsl_histogram2d_free (h) ; }
scope_ray *rays_initialize(int ray_setup, int *ray_status, double *overshoot){ /* Variable Declarations */ long i,j; scope_ray *rays,normal,g,det_plane; double angle=0.; printf("Initializing %0.3e rays...\n",N_RAYS); rays = (scope_ray *)malloc(N_RAYS * sizeof(scope_ray)); *ray_status = 2222; /* Initialize ray position randomly across the aperture */ /* Start GSL's RNG */ const gsl_rng_type *T; gsl_rng *r; gsl_rng_env_setup(); // T = gsl_rng_ranlux389; // Second slowest T = gsl_rng_taus2; r = gsl_rng_alloc(T); /* Assign random starting point for rays */ double radius = 1.0; double x,y; for(i=0,j=0;i<N_RAYS;i++,j++){ // j counts the # of times the loop executes x = gsl_rng_uniform(r)*2. - 1.; y = gsl_rng_uniform(r)*2. - 1.; if(x*x + y*y > 1.){ // If outside the circle, i--; // decriment i, continue; // and try again. } rays[i].x = x*radius; rays[i].y = y*radius; rays[i].z = +10.; // Start way up high rays[i].lost = false; // Not lost yet } *overshoot = (double)j/(double)i; /* Initialize ray direction based on setup criteria */ switch(ray_setup){ case(TARGET_POINT): printf("Serving up a single point source...\n"); for(i=0;i<N_RAYS;i++){ rays[i].vx = sin(angle); rays[i].vy = 0; rays[i].vz = -cos(angle); } break; case(TARGET_POINTS): break; case(TARGET_IMAGE): i=0; /* Variable Declaration */ int stat; gsl_histogram2d imghist; gsl_histogram2d_pdf imgpdf; double x,y; stat = gsl_histogram2d_pdf_sample(&imgpdf, gsl_rng_uniform(r), gsl_rng_uniform(r), &x, &y); rays[i].x = x; rays[i].y = y; rays[i].z = +10.; break; default: printf("I am defaulting on ray direction.\n"); } /* Clean up */ gsl_rng_free(r); return rays; }