IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39242 for trunk


Ignore:
Timestamp:
Dec 9, 2015, 3:42:26 PM (11 years ago)
Author:
eugene
Message:

move multple copies of gaussian deviates to libohana

Location:
trunk/Ohana/src
Files:
1 added
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/addstar/src/mkcmf.c

    r39121 r39242  
    1313# define FLAGS 0x1101
    1414
    15 void gauss_init (int Nbin);
    16 double rnd_gauss (double mean, double sigma);
    1715void writeStars_PS1_V5_Lensing (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
    1816void writeStars_PS1_V5 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
     
    282280  }
    283281   
    284   gauss_init (2048);
     282  gaussdev_init ();
    285283
    286284  // load test stars from a file:
     
    564562
    565563
    566 static int Ngaussint = 0;
    567 static double *gaussint;
    568 
    569 extern double drand48();
    570 
    571 double gaussian (double x, double mean, double sigma) {
    572 
    573   double f;
    574 
    575   f = exp (-0.5 * SQ(x - mean) / SQ(sigma)) / sqrt(2 * M_PI * SQ(sigma));
    576 
    577   return (f);
    578 
    579 }
    580 
    581 /* integrate a gaussian from -5 sigma to +5 sigma */
    582 void gauss_init (int Nbin) {
    583  
    584   int i;
    585   double val, x, dx, dx1, dx2, dx3, df;
    586   double mean, sigma;
    587  
    588   /* no need to generate this if it already exists */
    589   if (Ngaussint == Nbin) return;
    590 
    591   // A = time(NULL);
    592   // // XXX this is expensive if called a lot (1 sec min)
    593   // // for (B = 0; A == time(NULL); B++);
    594   // B = A + 10000;
    595   // srand48(B);
    596  
    597   Ngaussint = Nbin;
    598   ALLOCATE (gaussint, double, Ngaussint + 1);
    599 
    600   val = 0;
    601   dx = 1.0 / Ngaussint;
    602   dx1 = dx / 3.0;
    603   dx2 = 2.0*dx/3.0;
    604   dx3 = dx;
    605   mean = 0.0;
    606   sigma = 1.0;
    607  
    608   for (i = 0, x = -7.0; (i < Ngaussint) && (x < 7.0); x += dx)  {
    609     df = (3.0*gaussian(x    , mean, sigma) +
    610           9.0*gaussian(x+dx1, mean, sigma) +
    611           9.0*gaussian(x+dx2, mean, sigma) +
    612           3.0*gaussian(x+dx3, mean, sigma)) * (dx1/8.0);
    613     val += df;
    614     if (val > (i + 0.5) / (double) Ngaussint) {
    615       gaussint[i] = x + dx / 2.0;
    616       i++;
    617     }
    618   }
    619 }
    620 
    621 double rnd_gauss (double mean, double sigma) {
    622  
    623   int i;
    624   double y;
    625  
    626   y = drand48();
    627   i = Ngaussint*y;
    628   y = gaussint[i]*sigma + mean;
    629  
    630   return (y);
    631  
    632 }
    633  
    634 double int_gauss (int i) {
    635   double y;
    636   y = gaussint[i];
    637   return (y);
    638 }
    639  
    640564void writeStars_PS1_DEV_0 (FTable *ftable, double *X, double *Y, double *M, int Nstars) {
    641565
     
    654578
    655579    if (ADDNOISE) {
    656       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    657       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    658       M[i] += fSN*rnd_gauss(0.0, 1.0);
     580      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     581      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     582      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    659583      flux = pow (10.0, -0.4*M[i]);
    660584      fSN = 1.0 / sqrt(flux);
     
    700624
    701625    if (ADDNOISE) {
    702       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    703       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    704       M[i] += fSN*rnd_gauss(0.0, 1.0);
     626      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     627      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     628      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    705629      flux = pow (10.0, -0.4*M[i]);
    706630      fSN = 1.0 / sqrt(flux);
     
    749673
    750674    if (ADDNOISE) {
    751       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    752       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    753       M[i] += fSN*rnd_gauss(0.0, 1.0);
     675      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     676      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     677      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    754678      flux = pow (10.0, -0.4*M[i]);
    755679      fSN = 1.0 / sqrt(flux);
     
    800724
    801725    if (ADDNOISE) {
    802       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    803       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    804       M[i] += fSN*rnd_gauss(0.0, 1.0);
     726      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     727      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     728      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    805729      flux = pow (10.0, -0.4*M[i]);
    806730      fSN = 1.0 / sqrt(flux);
     
    857781
    858782    if (ADDNOISE) {
    859       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    860       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    861       M[i] += fSN*rnd_gauss(0.0, 1.0);
     783      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     784      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     785      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    862786      flux = pow (10.0, -0.4*M[i]);
    863787      fSN = 1.0 / sqrt(flux);
     
    918842
    919843    if (ADDNOISE) {
    920       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    921       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    922       M[i] += fSN*rnd_gauss(0.0, 1.0);
     844      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     845      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     846      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    923847      flux = pow (10.0, -0.4*M[i]);
    924848      fSN = 1.0 / sqrt(flux);
     
    987911
    988912    if (ADDNOISE) {
    989       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    990       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    991       M[i] += fSN*rnd_gauss(0.0, 1.0);
     913      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     914      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     915      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    992916      flux = pow (10.0, -0.4*M[i]);
    993917      fSN = 1.0 / sqrt(flux);
     
    10941018
    10951019    if (ADDNOISE) {
    1096       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    1097       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    1098       M[i] += fSN*rnd_gauss(0.0, 1.0);
     1020      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     1021      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     1022      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    10991023      flux = pow (10.0, -0.4*M[i]);
    11001024      fSN = 1.0 / sqrt(flux);
     
    11981122
    11991123    if (ADDNOISE) {
    1200       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    1201       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    1202       M[i] += fSN * rnd_gauss(0.0, 1.0);
     1124      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     1125      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     1126      M[i] += fSN * gaussdev_rnd(0.0, 1.0);
    12031127      flux = pow(10.0, -0.4 * M[i]);
    12041128      fSN = 1.0 / sqrt(flux);
     
    13241248
    13251249    if (ADDNOISE) {
    1326       X[i] += FX * fSN * rnd_gauss(0.0, 1.0);
    1327       Y[i] += FY * fSN * rnd_gauss(0.0, 1.0);
    1328       M[i] += fSN*rnd_gauss(0.0, 1.0);
     1250      X[i] += FX * fSN * gaussdev_rnd(0.0, 1.0);
     1251      Y[i] += FY * fSN * gaussdev_rnd(0.0, 1.0);
     1252      M[i] += fSN*gaussdev_rnd(0.0, 1.0);
    13291253      flux = pow (10.0, -0.4*M[i]);
    13301254      fSN = 1.0 / sqrt(flux);
  • trunk/Ohana/src/fakeastro/Makefile

    r37846 r39242  
    3838$(SRC)/fakestar_io.$(ARCH).o \
    3939$(SRC)/insert_fakestar.$(ARCH).o \
    40 $(SRC)/gaussian.$(ARCH).o \
    4140$(SRC)/fakeastro_images.$(ARCH).o \
    4241$(SRC)/fakeastro_images_region.$(ARCH).o \
  • trunk/Ohana/src/fakeastro/src/make_2mass_measures.c

    r38986 r39242  
    8080
    8181    // uR,uD in linear arcsec
    82     double dRsee = rnd_gauss (0.0, 1.0 / SN);
    83     double dDsee = rnd_gauss (0.0, 1.0 / SN);
     82    double dRsee = gaussdev_rnd (0.0, 1.0 / SN);
     83    double dDsee = gaussdev_rnd (0.0, 1.0 / SN);
    8484
    8585    double dRoff = (dRpm + dRsee) / 3600.0;
  • trunk/Ohana/src/fakeastro/src/make_fake_stars_catalog.c

    r37807 r39242  
    113113
    114114    // uR,uD in linear arcsec
    115     double dRsee = rnd_gauss (0.0, 1.0 / SN);
    116     double dDsee = rnd_gauss (0.0, 1.0 / SN);
     115    double dRsee = gaussdev_rnd (0.0, 1.0 / SN);
     116    double dDsee = gaussdev_rnd (0.0, 1.0 / SN);
    117117
    118118    double dRoff = (dRpm + dRsee) / 3600.0;
  • trunk/Ohana/src/fakeastro/src/make_fakeqsos.c

    r37807 r39242  
    5151
    5252    // Mr will be interpretted as m_r
    53     double Mr = rnd_gauss (18.0, 1.5);
     53    double Mr = gaussdev_rnd (18.0, 1.5);
    5454   
    5555    stars[i].R = R;
  • trunk/Ohana/src/fakeastro/src/make_fakestars.c

    r37807 r39242  
    4848      int inPatch = FALSE;
    4949      while (!inPatch) {
    50         z = rnd_gauss (0.0, FAKEASTRO_ZGAL);
     50        z = gaussdev_rnd (0.0, FAKEASTRO_ZGAL);
    5151        r = sqrt(drand48()) * FAKEASTRO_RGAL;
    5252        Lrad = drand48() * 2 * M_PI;
     
    9898    double Mr;
    9999    if (bigPeak) {
    100       Mr = rnd_gauss (11.25, 1.0);
     100      Mr = gaussdev_rnd (11.25, 1.0);
    101101    } else {
    102       Mr = rnd_gauss (10.00, 2.5);
     102      Mr = gaussdev_rnd (10.00, 2.5);
    103103    }
    104104   
  • trunk/Ohana/src/libohana/Makefile

    r39058 r39242  
    3535$(SRC)/sorts.$(ARCH).o           \
    3636$(SRC)/bisection.$(ARCH).o       \
     37$(SRC)/gaussdev.$(ARCH).o       \
    3738$(SRC)/string.$(ARCH).o          \
    3839$(SRC)/findexec.$(ARCH).o        \
  • trunk/Ohana/src/libohana/include/ohana.h

    r39040 r39242  
    486486int sprintf_double (char *output, double value);
    487487
    488 # endif
     488/* in gaussdev.c */
     489double gaussian (double x, double mean, double sigma);
     490void gaussdev_init (void);
     491void gaussdev_free (void);
     492double gaussdev_rnd (double mean, double sigma);
     493
     494# endif
  • trunk/Ohana/src/misc/src/fakedist.c

    r15487 r39242  
    11# include <ohana.h>
    2 
    3 # define NGAUSS 2048
    4 double drand48();
    5 double gaussint[NGAUSS];
    6 double gaussian();
    7 double rnd_gauss();
    82
    93# define MMIN 1.0
     
    4539  init_outmatrix ();
    4640 
    47   gauss_init ();
     41  gaussdev_init ();
    4842
    4943  read_datafiles ();
     
    349343        if (noise < dMo) {
    350344          *Noise = noise;
    351           *mag = rnd_gauss (v, noise);
    352           *color = rnd_gauss (uv, 1.4*noise);
     345          *mag = gaussdev_rnd (v, noise);
     346          *color = gaussdev_rnd (uv, 1.4*noise);
    353347          return;
    354348        }
     
    414408
    415409/*****************************************************************************/
    416 
    417 double
    418 rnd_gauss (mean, sigma)
    419 double mean, sigma;
    420 {
    421 
    422   int i;
    423   double y;
    424 
    425   y = drand48();
    426   i = NGAUSS*y;
    427   y = gaussint[i]*sigma + mean;
    428 
    429   return (y);
    430 
    431 }
    432 
    433 gauss_init ()
    434 {
    435 
    436   int i;
    437   long A, B;
    438   double val, x, dx, dx1, dx2, dx3, df;
    439   double mean, sigma;
    440 
    441   A = time(NULL);
    442   for (B = 0; A == time(NULL); B++);
    443   srand48(B);
    444 
    445   val = 0;
    446   dx = 0.001;
    447   dx1 = dx / 3.0;
    448   dx2 = 2.0*dx/3.0;
    449   dx3 = dx;
    450   mean = 0.0;
    451   sigma = 1.0;
    452 
    453   for (i = 0, x = -7.0; (i < NGAUSS) && (x < 7.0); x += dx)  {
    454     df = (3.0*gaussian(x    , mean, sigma) +
    455           9.0*gaussian(x+dx1, mean, sigma) +
    456           9.0*gaussian(x+dx2, mean, sigma) +
    457           3.0*gaussian(x+dx3, mean, sigma)) * (dx1/8.0);
    458     val += df;
    459     if (val > (i / (double) NGAUSS)) {
    460       gaussint[i] = x + dx / 2.0;
    461       i++;
    462     }
    463   }
    464   return (x + dx / 2.0);
    465 }
    466 
    467 double
    468 gaussian (x, mean, sigma)
    469 double x, mean, sigma;
    470 {
    471 
    472   double f, X;
    473 
    474   f = exp (-0.5 * SQ(x - mean) / SQ(sigma)) / sqrt(2 * M_PI * SQ(sigma));
    475 
    476   return (f);
    477 
    478 }
    479410
    480411/*****************************************************************************/
     
    505436      Ngood = 1.0;
    506437      for (k = 0; k < NTRY; k++) {
    507         v = rnd_gauss (mag, noise);
    508         uv = rnd_gauss (color, 1.4*noise);
     438        v = gaussdev_rnd (mag, noise);
     439        uv = gaussdev_rnd (color, 1.4*noise);
    509440        x = (uv - UV0 - 0.7*A_V) / DUV;
    510441        y = (v - V0 - Dist - A_V) / DV;
  • trunk/Ohana/src/misc/src/fakepop.c

    r7080 r39242  
    22# define MMIN 1.0
    33# define MMAX 120.0
    4 extern double drand48();
    54extern double rnd_mass();
    65double rnd_mass();
    76double term;
    8 extern double rnd_gauss();
    9 extern double rnd_integrate ();
    10 extern double gaussian ();
    11 
    12 double gaussian();
    13 double rnd_gauss();
    14 double rnd_integrate();
    15 
    16 # define NGAUSS 2048
    17 double gaussint[NGAUSS];
    187
    198double AgeS, AgeE, alpha, d, Av, dMo, dVo, dVref;
     
    4736  gfits_scan (&UV_h, "DEC_Y", "%lf", 1, &ldM);
    4837 
    49   gauss_init ();
     38  gaussdev_init ();
    5039
    5140  fprintf (stderr, "beginning main loop\n");
     
    6857        v = gfits_get_matrix_value (&V_i, X, Y) + d + Av;
    6958        noise = dVo*sqrt(1.0 + pow (10.0, (0.4*(v - dVref))));
    70         V = rnd_gauss (v, noise);
    71         UV = rnd_gauss (uv, 1.4*noise);
     59        V = gaussdev_rnd (v, noise);
     60        UV = gaussdev_rnd (uv, 1.4*noise);
    7261        if (noise < dMo) {
    7362          fprintf (stdout, "%f %f   %f %f   %f  %f\n", V, noise, V+UV, noise, mass, age);
     
    9685}
    9786
    98 
    99 double
    100 rnd_gauss (mean, sigma)
    101 double mean, sigma;
    102 {
    103 
    104   int i;
    105   double y;
    106 
    107   y = drand48();
    108   i = NGAUSS*y;
    109   y = gaussint[i]*sigma + mean;
    110 
    111   return (y);
    112 
    113 }
    114 
    115 
    116 gauss_init ()
    117 {
    118 
    119   int i;
    120   long A, B;
    121   double val, x, dx, dx1, dx2, dx3, df;
    122   double mean, sigma;
    123 
    124   A = time(NULL);
    125   for (B = 0; A == time(NULL); B++);
    126   srand48(B);
    127 
    128   val = 0;
    129   dx = 0.001;
    130   dx1 = dx / 3.0;
    131   dx2 = 2.0*dx/3.0;
    132   dx3 = dx;
    133   mean = 0.0;
    134   sigma = 1.0;
    135 
    136   for (i = 0, x = -7.0; (i < NGAUSS) && (x < 7.0); x += dx)  {
    137     df = (3.0*gaussian(x    , mean, sigma) +
    138           9.0*gaussian(x+dx1, mean, sigma) +
    139           9.0*gaussian(x+dx2, mean, sigma) +
    140           3.0*gaussian(x+dx3, mean, sigma)) * (dx1/8.0);
    141     val += df;
    142     if (val > (i / (double) NGAUSS)) {
    143       gaussint[i] = x + dx / 2.0;
    144       i++;
    145     }
    146   }
    147   return (x + dx / 2.0);
    148 }
    14987
    15088double
     
    172110  return (x + dx / 2.0);
    173111}
    174 
    175 double
    176 gaussian (x, mean, sigma)
    177 double x, mean, sigma;
    178 {
    179 
    180   double f, X;
    181 
    182   f = exp (-0.5 * SQ(x - mean) / SQ(sigma)) / sqrt(2 * M_PI * SQ(sigma));
    183 
    184   return (f);
    185 
    186 }
    187 
    188112
    189113/*****************************************************************************/
  • trunk/Ohana/src/misc/src/fakestars.c

    r27435 r39242  
    1717  int found;
    1818} Stars;
    19 
    20 void   gauss_init ();
    21 double gaussian (double x, double mean, double sigma);
    22 double rnd_gauss (double mean, double sigma);
    2319
    2420  /*
     
    4541  offset = atof (argv[4]);
    4642
    47   gauss_init ();
     43  gaussdev_init ();
    4844
    4945  /* load header, open file */
     
    9894    dMr = 0.001 * stars[i].dM;
    9995    dMo = hypot (dMs, dMr);
    100     dM  = rnd_gauss (0.0, dMo);
     96    dM  = gaussdev_rnd (0.0, dMo);
    10197    stars[i].M += dM + offset;
    10298  }
  • trunk/Ohana/src/misc/src/fitdist.c

    r8301 r39242  
    11# include <ohana.h>
    2 
    3 # define NGAUSS 2048
    4 double drand48();
    5 double gaussint[NGAUSS];
    6 double gaussian();
    7 double rnd_gauss();
    82
    93# define MMIN 1.0
     
    4539  fprintf (stderr, "read in parameters\n");
    4640
    47   gauss_init ();
     41  gaussdev_init ();
    4842
    4943  read_datafiles ();
     
    360354/*****************************************************************************/
    361355
    362 double
    363 rnd_gauss (mean, sigma)
    364 double mean, sigma;
    365 {
    366 
    367   int i;
    368   double y;
    369 
    370   y = drand48();
    371   i = NGAUSS*y;
    372   y = gaussint[i]*sigma + mean;
    373 
    374   return (y);
    375 
    376 }
    377 
    378 gauss_init ()
    379 {
    380 
    381   int i;
    382   long A, B;
    383   double val, x, dx, dx1, dx2, dx3, df;
    384   double mean, sigma;
    385 
    386   A = time(NULL);
    387   for (B = 0; A == time(NULL); B++);
    388   srand48(B);
    389 
    390   val = 0;
    391   dx = 0.001;
    392   dx1 = dx / 3.0;
    393   dx2 = 2.0*dx/3.0;
    394   dx3 = dx;
    395   mean = 0.0;
    396   sigma = 1.0;
    397 
    398   for (i = 0, x = -7.0; (i < NGAUSS) && (x < 7.0); x += dx)  {
    399     df = (3.0*gaussian(x    , mean, sigma) +
    400           9.0*gaussian(x+dx1, mean, sigma) +
    401           9.0*gaussian(x+dx2, mean, sigma) +
    402           3.0*gaussian(x+dx3, mean, sigma)) * (dx1/8.0);
    403     val += df;
    404     if (val > (i / (double) NGAUSS)) {
    405       gaussint[i] = x + dx / 2.0;
    406       i++;
    407     }
    408   }
    409   return (x + dx / 2.0);
    410 }
    411 
    412 double
    413 gaussian (x, mean, sigma)
    414 double x, mean, sigma;
    415 {
    416 
    417   double f, X;
    418 
    419   f = exp (-0.5 * SQ(x - mean) / SQ(sigma)) / sqrt(2 * M_PI * SQ(sigma));
    420 
    421   return (f);
    422 
    423 }
    424 
    425356/*****************************************************************************/
    426357
     
    453384      Ngood = 1.0;
    454385      for (k = 0; k < NTRY; k++) {
    455         v = rnd_gauss (Mag, Noise);
    456         uv = rnd_gauss (Color, 1.4*Noise);
     386        v = gaussdev_rnd (Mag, Noise);
     387        uv = gaussdev_rnd (Color, 1.4*Noise);
    457388        x = (uv - UV0 - 0.7*A_V) / DUV;
    458389        y = (v - V0 - Dist - A_V) / DV;
  • trunk/Ohana/src/misc/src/magtoage.c

    r7080 r39242  
    22# define MMIN 1.0
    33# define MMAX 120.0
    4 extern double drand48();
    5 extern double rnd_gauss();
    6 extern double rnd_integrate ();
    7 extern double gaussian ();
    8 
    9 double gaussian();
    10 double rnd_gauss();
    11 double rnd_integrate();
    124
    135void main (argc, argv)
     
    2921  long A, B;
    3022 
     23  gaussdev_init();
     24
    3125  lAo =   0.0;
    3226  ldA =   1.0;
     
    118112      }
    119113      else {
    120         v = rnd_gauss (V, dV);
    121         uv = rnd_gauss ((U-V), dUV);
     114        v = gaussdev_rnd (V, dV);
     115        uv = gaussdev_rnd ((U-V), dUV);
    122116      }
    123117      x = (uv - UV0 - 0.7*Av) / DUV;
     
    167161}
    168162
    169 double
    170 rnd_gauss (mean, sigma)
    171 double mean, sigma;
    172 {
    173 
    174   double range, x;
    175 
    176   range = drand48();
    177   x = rnd_integrate (*gaussian, range, mean, sigma);
    178 
    179   return (x);
    180 
    181 }
    182 
    183 
    184 double
    185 rnd_integrate (function, range, mean, sigma)
    186 double (*function) ();
    187 double range, mean, sigma;
    188 {
    189 
    190   double val, x, dx, dx1, dx2, dx3, df;
    191 
    192   range += 0.0001;
    193   val = 0;
    194   dx = sigma / 10.0;
    195   dx1 = dx / 3.0;
    196   dx2 = 2.0*dx/3.0;
    197   dx3 = dx;
    198 
    199   for (x = mean - 7*sigma; (val < range) && (x < mean + 7*sigma); x += dx)  {
    200     df = (3.0*function(x    , mean, sigma) +
    201           9.0*function(x+dx1, mean, sigma) +
    202           9.0*function(x+dx2, mean, sigma) +
    203           3.0*function(x+dx3, mean, sigma)) * (dx1/8.0);
    204     val += df;
    205   }
    206   return (x + dx / 2.0);
    207 }
    208 
    209 double
    210 gaussian (x, mean, sigma)
    211 double x, mean, sigma;
    212 {
    213 
    214   double f, X;
    215 
    216   f = exp (-0.5 * SQ(x - mean) / SQ(sigma)) / sqrt(2 * M_PI * SQ(sigma));
    217 
    218   return (f);
    219 
    220 }
    221 
  • trunk/Ohana/src/misc/src/magtomass.c

    r7080 r39242  
    11# include <ohana.h>
    2 extern double drand48();
    3 extern double rnd_gauss();
    4 extern double rnd_integrate ();
    5 extern double gaussian ();
    62# define NEWWAY 1
    7 
    8 double gaussian();
    9 double rnd_gauss();
    10 double rnd_integrate();
    11 
    12 # define NGAUSS 2048
    13 double gaussint[NGAUSS];
    143
    154void main (argc, argv)
     
    5241  fprintf (stderr, "using mags in columns %d & %d\n", col1, col2);
    5342
    54   gauss_init ();
     43  gaussdev_init ();
    5544
    5645  lAo = 0.1;
     
    147136        Ngood = 1.0;
    148137        for (k = 0; k < Ntry; k++) {
    149           v = rnd_gauss (V, dV);
    150           uv = rnd_gauss ((U-V), dUV);
     138          v = gaussdev_rnd (V, dV);
     139          uv = gaussdev_rnd ((U-V), dUV);
    151140          x = (uv - UV0 - 0.7*Av) / DUV;
    152141          y = (v - d - V0 - Av) / DV;
     
    202191*/
    203192
    204 double
    205 rnd_gauss (mean, sigma)
    206 double mean, sigma;
    207 {
    208 
    209   int i;
    210   double y;
    211 
    212   y = drand48();
    213   i = NGAUSS*y;
    214   y = gaussint[i]*sigma + mean;
    215 
    216   return (y);
    217 
    218 }
    219 
    220 
    221 gauss_init ()
    222 {
    223 
    224   int i;
    225   long A, B;
    226   double val, x, dx, dx1, dx2, dx3, df;
    227   double mean, sigma;
    228 
    229   A = time(NULL);
    230   for (B = 0; A == time(NULL); B++);
    231   srand48(B);
    232 
    233   val = 0;
    234   dx = 0.001;
    235   dx1 = dx / 3.0;
    236   dx2 = 2.0*dx/3.0;
    237   dx3 = dx;
    238   mean = 0.0;
    239   sigma = 1.0;
    240 
    241   for (i = 0, x = -7.0; (i < NGAUSS) && (x < 7.0); x += dx)  {
    242     df = (3.0*gaussian(x    , mean, sigma) +
    243           9.0*gaussian(x+dx1, mean, sigma) +
    244           9.0*gaussian(x+dx2, mean, sigma) +
    245           3.0*gaussian(x+dx3, mean, sigma)) * (dx1/8.0);
    246     val += df;
    247     if (val > (i / (double) NGAUSS)) {
    248       gaussint[i] = x + dx / 2.0;
    249       i++;
    250     }
    251   }
    252   return (x + dx / 2.0);
    253 }
    254 
    255 double
    256 rnd_integrate (function, range, mean, sigma)
    257 double (*function) ();
    258 double range, mean, sigma;
    259 {
    260 
    261   double val, x, dx, dx1, dx2, dx3, df;
    262 
    263   range += 0.0001;
    264   val = 0;
    265   dx = sigma / 100.0;
    266   dx1 = dx / 3.0;
    267   dx2 = 2.0*dx/3.0;
    268   dx3 = dx;
    269 
    270   for (x = mean - 7*sigma; (val < range) && (x < mean + 7*sigma); x += dx)  {
    271     df = (3.0*function(x    , mean, sigma) +
    272           9.0*function(x+dx1, mean, sigma) +
    273           9.0*function(x+dx2, mean, sigma) +
    274           3.0*function(x+dx3, mean, sigma)) * (dx1/8.0);
    275     val += df;
    276   }
    277   return (x + dx / 2.0);
    278 }
    279 
    280 double
    281 gaussian (x, mean, sigma)
    282 double x, mean, sigma;
    283 {
    284 
    285   double f, X;
    286 
    287   f = exp (-0.5 * SQ(x - mean) / SQ(sigma)) / sqrt(2 * M_PI * SQ(sigma));
    288 
    289   return (f);
    290 
    291 }
    292 
  • trunk/Ohana/src/misc/src/mkgauss.c

    r34088 r39242  
    11# include <ohana.h>
    2 
    3 void   gauss_init ();
    4 double gaussian (double x, double mean, double sigma);
    5 double rnd_gauss (double mean, double sigma);
    6 
    7 /* NGAUSS defines the resolution of the distribution */
    8 # define NGAUSS 2048
    9 static double gaussint[NGAUSS];
    102
    113int main (int argc, char **argv) {
     
    2315  Npts = atoi (argv[3]);
    2416
    25   gauss_init ();
     17  gaussdev_init ();
    2618
    2719  for (i = 0; i < Npts; i++) {
    28     f = rnd_gauss (mean, sigma);
     20    f = gaussdev_rnd (mean, sigma);
    2921    fprintf (stdout, "%f\n", f);
    3022  }
     
    3325}
    3426
    35 void gauss_init () {
    36 
    37   int i;
    38   long A, B;
    39   double val, x, dx, dx1, dx2, dx3, df;
    40 
    41   A = time(NULL);
    42   for (B = 0; A == time(NULL); B++);
    43   srand48(B);
    44 
    45   val = 0;
    46   dx = 0.001;
    47   dx1 = dx / 3.0;
    48   dx2 = 2.0*dx/3.0;
    49   dx3 = dx;
    50 
    51   for (i = 0, x = -7.0; (i < NGAUSS) && (x < 7.0); x += dx)  {
    52     df = (3.0*gaussian(x    , 0.0, 1.0) +
    53           9.0*gaussian(x+dx1, 0.0, 1.0) +
    54           9.0*gaussian(x+dx2, 0.0, 1.0) +
    55           3.0*gaussian(x+dx3, 0.0, 1.0)) * (dx1/8.0);
    56     val += df;
    57     if (val > (i / (double) NGAUSS)) {
    58       gaussint[i] = x + dx / 2.0;
    59       i++;
    60     }
    61   }
    62   return;
    63 }
    64 
    65 double gaussian (double x, double mean, double sigma) {
    66   double f;
    67 
    68   f = exp (-0.5 * SQ(x - mean) / SQ(sigma)) / sqrt(2 * M_PI * SQ(sigma));
    69   return (f);
    70 }
    71 
    72 double rnd_gauss (double mean, double sigma) {
    73 
    74   int i;
    75   double y;
    76 
    77   i = 0;
    78   while ((i == 0) || (i == NGAUSS - 1)) {
    79     y = drand48();
    80     i = MIN (NGAUSS - 1, MAX (0, NGAUSS*y));
    81   }
    82   y = gaussint[i]*sigma + mean;
    83 
    84   return (y);
    85 }
  • trunk/Ohana/src/opihi/cmd.data/gaussdeviate.c

    r34088 r39242  
    1717  ResetVector (vec, OPIHI_FLT, Npts);
    1818
    19   gauss_init (2048);
     19  gaussdev_init ();
    2020  for (i = 0; i < Npts; i++) {
    21     vec[0].elements.Flt[i] = rnd_gauss (mean, sigma);
     21    vec[0].elements.Flt[i] = gaussdev_rnd (mean, sigma);
    2222  }
    2323  return (TRUE);
     
    2828   
    2929}
    30 
    31 double int_gauss (int i);
    3230
    3331int gaussintegral (int argc, char **argv) {
     
    4644  ResetVector (vec, OPIHI_FLT, Npts);
    4745
    48   gauss_init (Npts);
     46  gaussdev_init ();
    4947  for (i = 0; i < Npts; i++) {
    50     vec[0].elements.Flt[i] = int_gauss (i);
     48    vec[0].elements.Flt[i] = gaussian_int (i);
    5149  }
    5250  return (TRUE);
  • trunk/Ohana/src/opihi/lib.data/Makefile

    r36679 r39242  
    3030$(SDIR)/starfuncs.$(ARCH).o             \
    3131$(SDIR)/hermitian.$(ARCH).o             \
    32 $(SDIR)/gaussian.$(ARCH).o              \
    3332$(SDIR)/graphtools.$(ARCH).o            \
    3433$(SDIR)/queues.$(ARCH).o                \
     
    3635$(SDIR)/open_kapa.$(ARCH).o             \
    3736$(SDIR)/style_args.$(ARCH).o
     37
     38# moved to libohana
     39# $(SDIR)/gaussian.$(ARCH).o            \
    3840
    3941# fix malloc
  • trunk/Ohana/src/opihi/mana/simsignal.c

    r7917 r39242  
    66  float *buf;
    77  double cvalue, dvalue, sigma, SN, period;
    8   double rnd_gauss ();
    98
    109  if (argc != 5) {
     
    2827    ivalue = scale * 0.5 * (sin (i*2*M_PI/period) + 1) + 0.5;
    2928    /*
    30     dvalue = rnd_gauss (cvalue, sigma);
     29    dvalue = gaussdev_rnd (cvalue, sigma);
    3130    cvalue = (dvalue + range) / (2.0*range);
    3231    dvalue = MAX (0, MIN (0.99999, cvalue));
     
    44438 bit = 2^8
    4544
    46   gauss_init (2*scale);
     45  gaussdev_init ();
    4746  sigma = 2.0 / SN;
    4847  range = 1 + 5*sigma;
Note: See TracChangeset for help on using the changeset viewer.