Eigen::Vector3d npRandom::dir3d() { Eigen::Vector3d out; double x, y, z; gsl_ran_dir_3d(ran, &x, &y, &z); out << x,y,z; return out; }
double test_dir3dzx (void) { double x=0, y=0, z=0, theta; gsl_ran_dir_3d (r_global, &x, &y, &z); theta = atan2(z,x); return theta; }
CAMLprim value ml_gsl_ran_dir_3d(value rng) { double x,y,z; gsl_ran_dir_3d(Rng_val(rng), &x, &y, &z); { CAMLparam0(); CAMLlocal1(r); r=alloc_tuple(3); Store_field(r, 0, copy_double(x)); Store_field(r, 1, copy_double(y)); Store_field(r, 2, copy_double(z)); CAMLreturn(r); } }
void test_dir(void){ double x,y,z, nd[SIZE] = { 0,0,0,0 }; gsl_ran_dir_2d(rng, &x, &y); printf("gsl_ran_dir_2d\t[%.12f,%.12f]\n", x, y); gsl_ran_dir_2d_trig_method(rng, &x, &y); printf("gsl_ran_dir_2d_trig_method\t[%.12f,%.12f]\n", x, y); gsl_ran_dir_3d(rng, &x, &y, &z); printf("gsl_ran_dir_3d\t[%.12f,%.12f,%.12f]\n", x, y, z); /* perl needs to pass either outpdl or dims */ gsl_ran_dir_nd(rng, SIZE, nd); printf("gsl_ran_dir_nd\t%d\t[%.12f,%.12f,%.12f,%.12f]\n", SIZE, nd[0], nd[1], nd[2], nd[3]); }
int main (int argc, char *argv[]) { size_t i,j; size_t n = 0; double mu = 0, nu = 0, nu1 = 0, nu2 = 0, sigma = 0, a = 0, b = 0, c = 0; double zeta = 0, sigmax = 0, sigmay = 0, rho = 0; double p = 0; double x = 0, y =0, z=0 ; unsigned int N = 0, t = 0, n1 = 0, n2 = 0 ; unsigned long int seed = 0 ; const char * name ; gsl_rng * r ; if (argc < 4) { printf ( "Usage: gsl-randist seed n DIST param1 param2 ...\n" "Generates n samples from the distribution DIST with parameters param1,\n" "param2, etc. Valid distributions are,\n" "\n" " beta\n" " binomial\n" " bivariate-gaussian\n" " cauchy\n" " chisq\n" " dir-2d\n" " dir-3d\n" " dir-nd\n" " erlang\n" " exponential\n" " exppow\n" " fdist\n" " flat\n" " gamma\n" " gaussian-tail\n" " gaussian\n" " geometric\n" " gumbel1\n" " gumbel2\n" " hypergeometric\n" " laplace\n" " landau\n" " levy\n" " levy-skew\n" " logarithmic\n" " logistic\n" " lognormal\n" " negative-binomial\n" " pareto\n" " pascal\n" " poisson\n" " rayleigh-tail\n" " rayleigh\n" " tdist\n" " ugaussian-tail\n" " ugaussian\n" " weibull\n") ; exit (0); } argv++ ; seed = atol (argv[0]); argc-- ; argv++ ; n = atol (argv[0]); argc-- ; argv++ ; name = argv[0] ; argc-- ; argc-- ; gsl_rng_env_setup() ; if (gsl_rng_default_seed != 0) { fprintf(stderr, "overriding GSL_RNG_SEED with command line value, seed = %ld\n", seed) ; } gsl_rng_default_seed = seed ; r = gsl_rng_alloc(gsl_rng_default) ; #define NAME(x) !strcmp(name,(x)) #define OUTPUT(x) for (i = 0; i < n; i++) { printf("%g\n", (x)) ; } #define OUTPUT1(a,x) for(i = 0; i < n; i++) { a ; printf("%g\n", x) ; } #define OUTPUT2(a,x,y) for(i = 0; i < n; i++) { a ; printf("%g %g\n", x, y) ; } #define OUTPUT3(a,x,y,z) for(i = 0; i < n; i++) { a ; printf("%g %g %g\n", x, y, z) ; } #define INT_OUTPUT(x) for (i = 0; i < n; i++) { printf("%d\n", (x)) ; } #define ARGS(x,y) if (argc != x) error(y) ; #define DBL_ARG(x) if (argc) { x=atof((++argv)[0]);argc--;} else {error( #x);}; #define INT_ARG(x) if (argc) { x=atoi((++argv)[0]);argc--;} else {error( #x);}; if (NAME("bernoulli")) { ARGS(1, "p = probability of success"); DBL_ARG(p) INT_OUTPUT(gsl_ran_bernoulli (r, p)); } else if (NAME("beta")) { ARGS(2, "a,b = shape parameters"); DBL_ARG(a) DBL_ARG(b) OUTPUT(gsl_ran_beta (r, a, b)); } else if (NAME("binomial")) { ARGS(2, "p = probability, N = number of trials"); DBL_ARG(p) INT_ARG(N) INT_OUTPUT(gsl_ran_binomial (r, p, N)); } else if (NAME("cauchy")) { ARGS(1, "a = scale parameter"); DBL_ARG(a) OUTPUT(gsl_ran_cauchy (r, a)); } else if (NAME("chisq")) { ARGS(1, "nu = degrees of freedom"); DBL_ARG(nu) OUTPUT(gsl_ran_chisq (r, nu)); } else if (NAME("erlang")) { ARGS(2, "a = scale parameter, b = order"); DBL_ARG(a) DBL_ARG(b) OUTPUT(gsl_ran_erlang (r, a, b)); } else if (NAME("exponential")) { ARGS(1, "mu = mean value"); DBL_ARG(mu) ; OUTPUT(gsl_ran_exponential (r, mu)); } else if (NAME("exppow")) { ARGS(2, "a = scale parameter, b = power (1=exponential, 2=gaussian)"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_exppow (r, a, b)); } else if (NAME("fdist")) { ARGS(2, "nu1, nu2 = degrees of freedom parameters"); DBL_ARG(nu1) ; DBL_ARG(nu2) ; OUTPUT(gsl_ran_fdist (r, nu1, nu2)); } else if (NAME("flat")) { ARGS(2, "a = lower limit, b = upper limit"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_flat (r, a, b)); } else if (NAME("gamma")) { ARGS(2, "a = order, b = scale"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_gamma (r, a, b)); } else if (NAME("gaussian")) { ARGS(1, "sigma = standard deviation"); DBL_ARG(sigma) ; OUTPUT(gsl_ran_gaussian (r, sigma)); } else if (NAME("gaussian-tail")) { ARGS(2, "a = lower limit, sigma = standard deviation"); DBL_ARG(a) ; DBL_ARG(sigma) ; OUTPUT(gsl_ran_gaussian_tail (r, a, sigma)); } else if (NAME("ugaussian")) { ARGS(0, "unit gaussian, no parameters required"); OUTPUT(gsl_ran_ugaussian (r)); } else if (NAME("ugaussian-tail")) { ARGS(1, "a = lower limit"); DBL_ARG(a) ; OUTPUT(gsl_ran_ugaussian_tail (r, a)); } else if (NAME("bivariate-gaussian")) { ARGS(3, "sigmax = x std.dev., sigmay = y std.dev., rho = correlation"); DBL_ARG(sigmax) ; DBL_ARG(sigmay) ; DBL_ARG(rho) ; OUTPUT2(gsl_ran_bivariate_gaussian (r, sigmax, sigmay, rho, &x, &y), x, y); } else if (NAME("dir-2d")) { OUTPUT2(gsl_ran_dir_2d (r, &x, &y), x, y); } else if (NAME("dir-3d")) { OUTPUT3(gsl_ran_dir_3d (r, &x, &y, &z), x, y, z); } else if (NAME("dir-nd")) { double *xarr; ARGS(1, "n1 = number of dimensions of hypersphere"); INT_ARG(n1) ; xarr = (double *)malloc(n1*sizeof(double)); for(i = 0; i < n; i++) { gsl_ran_dir_nd (r, n1, xarr) ; for (j = 0; j < n1; j++) { if (j) putchar(' '); printf("%g", xarr[j]) ; } putchar('\n'); } ; free(xarr); } else if (NAME("geometric")) { ARGS(1, "p = bernoulli trial probability of success"); DBL_ARG(p) ; INT_OUTPUT(gsl_ran_geometric (r, p)); } else if (NAME("gumbel1")) { ARGS(2, "a = order, b = scale parameter"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_gumbel1 (r, a, b)); } else if (NAME("gumbel2")) { ARGS(2, "a = order, b = scale parameter"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_gumbel2 (r, a, b)); } else if (NAME("hypergeometric")) { ARGS(3, "n1 = tagged population, n2 = untagged population, t = number of trials"); INT_ARG(n1) ; INT_ARG(n2) ; INT_ARG(t) ; INT_OUTPUT(gsl_ran_hypergeometric (r, n1, n2, t)); } else if (NAME("laplace")) { ARGS(1, "a = scale parameter"); DBL_ARG(a) ; OUTPUT(gsl_ran_laplace (r, a)); } else if (NAME("landau")) { ARGS(0, "no arguments required"); OUTPUT(gsl_ran_landau (r)); } else if (NAME("levy")) { ARGS(2, "c = scale, a = power (1=cauchy, 2=gaussian)"); DBL_ARG(c) ; DBL_ARG(a) ; OUTPUT(gsl_ran_levy (r, c, a)); } else if (NAME("levy-skew")) { ARGS(3, "c = scale, a = power (1=cauchy, 2=gaussian), b = skew"); DBL_ARG(c) ; DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_levy_skew (r, c, a, b)); } else if (NAME("logarithmic")) { ARGS(1, "p = probability"); DBL_ARG(p) ; INT_OUTPUT(gsl_ran_logarithmic (r, p)); } else if (NAME("logistic")) { ARGS(1, "a = scale parameter"); DBL_ARG(a) ; OUTPUT(gsl_ran_logistic (r, a)); } else if (NAME("lognormal")) { ARGS(2, "zeta = location parameter, sigma = scale parameter"); DBL_ARG(zeta) ; DBL_ARG(sigma) ; OUTPUT(gsl_ran_lognormal (r, zeta, sigma)); } else if (NAME("negative-binomial")) { ARGS(2, "p = probability, a = order"); DBL_ARG(p) ; DBL_ARG(a) ; INT_OUTPUT(gsl_ran_negative_binomial (r, p, a)); } else if (NAME("pareto")) { ARGS(2, "a = power, b = scale parameter"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_pareto (r, a, b)); } else if (NAME("pascal")) { ARGS(2, "p = probability, n = order (integer)"); DBL_ARG(p) ; INT_ARG(N) ; INT_OUTPUT(gsl_ran_pascal (r, p, N)); } else if (NAME("poisson")) { ARGS(1, "mu = scale parameter"); DBL_ARG(mu) ; INT_OUTPUT(gsl_ran_poisson (r, mu)); } else if (NAME("rayleigh")) { ARGS(1, "sigma = scale parameter"); DBL_ARG(sigma) ; OUTPUT(gsl_ran_rayleigh (r, sigma)); } else if (NAME("rayleigh-tail")) { ARGS(2, "a = lower limit, sigma = scale parameter"); DBL_ARG(a) ; DBL_ARG(sigma) ; OUTPUT(gsl_ran_rayleigh_tail (r, a, sigma)); } else if (NAME("tdist")) { ARGS(1, "nu = degrees of freedom"); DBL_ARG(nu) ; OUTPUT(gsl_ran_tdist (r, nu)); } else if (NAME("weibull")) { ARGS(2, "a = scale parameter, b = exponent"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_weibull (r, a, b)); } else { fprintf(stderr,"Error: unrecognized distribution: %s\n", name) ; } return 0 ; }
int Particle::Move(Objects &opWorld_a) { if((TTL != PARTICLE_IMMORTAL)||(TTL != 0)) TTL--; int iCollision = CheckCollision(opWorld_a); switch(iCollision) { case PARTICLE_NO_COLLISION: x += vx; y += vy; z += vz; break; case PARTICLE_SENSOR_COLLISION: { double persy, persz; persy = y;// + vx/(vy*x); persz = z;// + vx/(vz*x); double size = opWorld_a.isSensor.Size; double res = ((double)opWorld_a.isSensor.Resolution)/size; if ((persy < size)&&(persy > 0)) { if ((persz < size)&&(persz > 0)) { opWorld_a.isSensor[((int)(persy*res))*opWorld_a.isSensor.Resolution + (int)(persz*res)] += opWorld_a.ParticleEnergy; } } x += vx; y += vy; z += vz; break; return PARTICLE_TO_DELETE; } break; case PARTICLE_LENS_COLLISION: { //фотон двигается до линзы long double time = (long double)abs(opWorld_a.lLens.x - x)/(long double)vx; y += (long double)time*(long double)vy; z += (long double)time*(long double)vz; long double Focus = 1/(long double)opWorld_a.lLens.Focus; long double LenCenterY = (long double)opWorld_a.lLens.y*Focus; long double LenCenterZ = (long double)opWorld_a.lLens.z*Focus; long double tempy, tempz; tempy = (long double)vy/(long double)vx + LenCenterY; tempz = (long double)vz/(long double)vx + LenCenterZ; vx = (vx>=0?1:-1); vy = tempy - (long double)y*Focus; vz = tempz - (long double)z*Focus; /**/ NormalizeSpeed(); //фотон двигается с новым вектором после линзы x = (long double)opWorld_a.lLens.x + (1-time)*(long double)vx; y += (1-time)*(long double)vy; z += (1-time)*(long double)vz; //salt double SaltScale = 0.2*PARTICLE_SPEED; y += (long double)((long double)rand()/(long double)RAND_MAX - 0.5)*SaltScale; z += (long double)((long double)rand()/(long double)RAND_MAX - 0.5)*SaltScale; /**/ break; } break; case PARTICLE_WALL_COLLISION: { //фотон двигается до стены long double time = (long double)abs(opWorld_a.wWall.y - y)/(long double)vy; x += (long double)time*(long double)vx; z += (long double)time*(long double)vz; //глянец //vy = -vy; //матовая double temp = (vy>=0?-1:1); gsl_ran_dir_3d(randNumGen, &vx, &vy, &vz); vy = temp*abs(vy); vx *= PARTICLE_SPEED; vy *= PARTICLE_SPEED; vz *= PARTICLE_SPEED; /**/ //фотон двигается с новым вектором после стены y = (long double)opWorld_a.wWall.y + (1-time)*(long double)vy; x += (1-time)*(long double)vx; z += (1-time)*(long double)vz; }break; case PARTICLE_TO_DELETE: break; } return iCollision; }
scalar sasfit_ff_RNDMultiLamellarVesicle2(scalar q, sasfit_param * param) { scalar sum, sumt; scalar Delta_s, DRi, Fi, Fj, rij; scalar xc, yc, zc; scalar t_sh, s_tsh, R_c, s_Rc, n, s_n, t_sol, s_tsol, Dt_sol, Delta_eta; int i, j, N, p, Nav = 200; static int idum = -1; static gsl_rng * r; static const gsl_rng_type * T; // static float o_R_c=-1., o_t_sh=-1., o_t_sol=-1., o_Dt_sol=-1.,o_n=-1.; static scalar r_i[100][3], R_i[100], tsh_i[100]; sasfit_param subParam; SASFIT_ASSERT_PTR(param); sasfit_get_param(param, 10, &t_sh, &s_tsh, &R_c, &s_Rc, &n, &s_n, &t_sol, &s_tsol, &Dt_sol, &Delta_eta); SASFIT_CHECK_COND1((q < 0.0), param, "q(%lg) < 0",q); SASFIT_CHECK_COND1((R_c <= 0.0), param, "R_c(%lg) <= 0",R_c); SASFIT_CHECK_COND1((s_Rc < 0.0), param, "s_Rc(%lg) < 0",s_Rc); SASFIT_CHECK_COND1((t_sol < 0.0), param, "t_sol(%lg) < 0",t_sol); SASFIT_CHECK_COND1((s_tsol < 0.0), param, "s_tsol(%lg) < 0",s_tsol); SASFIT_CHECK_COND1((t_sh < 0.0), param, "t_sh(%lg) < 0",t_sh); SASFIT_CHECK_COND1((s_tsh < 0.0), param, "s_tsh(%lg) < 0",s_tsh); SASFIT_CHECK_COND1((n < 1.0), param, "n(%lg) < 1",n); SASFIT_CHECK_COND1((n > 100), param, "n(%lg) > 100",n); SASFIT_CHECK_COND1((s_n < 0.0), param, "s_n(%lg) < 0",s_n); if (idum < 0) { idum = 1; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc(T); } if (20 < sasfit_eps_get_iter_4_mc()) { Nav = sasfit_eps_get_iter_4_mc(); } else { Nav =20; } sasfit_init_param( &subParam ); sumt = 0.0; for (p=0; p < Nav ;p++) { // do { // N = (int) (gsl_ran_gaussian (r,s_n)+n); // } while ((N>n+3*s_n) || (N<1)); if (s_n== 0.0) { N = (int) n; } else { do { N = (int) gsl_ran_lognormal(r,log(n),s_n); } while ((N>200) || (N<1)); } r_i[0][0] = 0.0; r_i[0][1] = 0.0; r_i[0][2] = 0.0; for (i=0; i < N ;i++) { if (s_tsh == 0.0) { tsh_i[i] = t_sh; } else { do { tsh_i[i] = gsl_ran_gaussian(r,s_tsh)+t_sh; } while (tsh_i[i] <= 0); } } // do { // R_i[0] = gsl_ran_gaussian(r,s_Rc)+R_c; // } while (R_i[0] <= 0); if (s_Rc == 0.0) { R_i[0] = R_c; } else { do { R_i[0] = gsl_ran_lognormal(r,log(R_c),s_Rc); } while (R_i[0] <= 0); } for (i=1; i < N ;i++) { gsl_ran_dir_3d(r,&xc,&yc,&zc); if (s_tsol == 0.0) { DRi = t_sol; } else { do { DRi = gsl_ran_gaussian(r,s_tsol)+t_sol; } while (DRi < 0); } R_i[i] = R_i[i-1]+tsh_i[i-1]+DRi; if (Dt_sol <= 0) { Delta_s = Dt_sol*DRi; } else { do { Delta_s = Dt_sol*DRi*gsl_rng_uniform(r); } while (Delta_s < 0); } r_i[i][0] = r_i[i-1][0] + Delta_s*xc; r_i[i][1] = r_i[i-1][1] + Delta_s*yc; r_i[i][2] = r_i[i-1][2] + Delta_s*zc; } sum=0.0; for (i=0; i < N ;i++) { /* Fi = K(interp,q,R_i[i] ,-Delta_eta,error) + K(interp,q,R_i[i]+tsh_i[i], Delta_eta,error); */ subParam.p[0] = R_i[i]; subParam.p[3] = -Delta_eta; Fi = sasfit_ff_sphere_f(q, &subParam); subParam.p[0] = R_i[i] + tsh_i[i]; subParam.p[3] = Delta_eta; Fi += sasfit_ff_sphere_f(q, &subParam); sum = sum + Fi*Fi; for (j=i+1; j < N ;j++) { /* Fj = K(interp,q,R_i[j] ,-Delta_eta,error) + K(interp,q,R_i[j]+tsh_i[j], Delta_eta,error); */ subParam.p[0] = R_i[j]; subParam.p[3] = -Delta_eta; Fj = sasfit_ff_sphere_f(q, &subParam); subParam.p[0] = R_i[j] + tsh_i[j]; subParam.p[3] = Delta_eta; Fj += sasfit_ff_sphere_f(q, &subParam); rij = sqrt( pow(r_i[i][0]-r_i[j][0],2) +pow(r_i[i][1]-r_i[j][1],2) +pow(r_i[i][2]-r_i[j][2],2) ); if (rij == 0) { sum = sum + 2.0*Fi*Fj; } else { sum = sum + 2.0*Fi*Fj*sin(q*rij)/(q*rij); } } } sumt = sumt+sum; } SASFIT_CHECK_SUB_ERR(param, subParam); return sumt/Nav; }