queso-0.53.0
Functions | Variables
rand.cpp File Reference
#include "rand.h"
Include dependency graph for rand.cpp:

Go to the source code of this file.

Functions

double annRan0 ()
 
static int annRanInt (int n)
 
static double annRanUnif (double lo, double hi)
 
static double annRanGauss ()
 
static double annRanLaplace ()
 
void annUniformPts (ANNpointArray pa, int n, int dim)
 
void annGaussPts (ANNpointArray pa, int n, int dim, double std_dev)
 
void annLaplacePts (ANNpointArray pa, int n, int dim)
 
void annCoGaussPts (ANNpointArray pa, int n, int dim, double correlation)
 
void annCoLaplacePts (ANNpointArray pa, int n, int dim, double correlation)
 
void annClusGaussPts (ANNpointArray pa, int n, int dim, int n_clus, ANNbool new_clust, double std_dev)
 
static void genOrthFlat (ANNpointArray pa, int n, int dim, double *control, double std_dev)
 
void annClusOrthFlats (ANNpointArray pa, int n, int dim, int n_clus, ANNbool new_clust, double std_dev, int max_dim)
 
static void genGauss (ANNpointArray pa, int n, int dim, double *center, double *std_dev)
 
void annClusEllipsoids (ANNpointArray pa, int n, int dim, int n_clus, ANNbool new_clust, double std_dev_small, double std_dev_lo, double std_dev_hi, int max_dim)
 
void annPlanted (ANNpointArray pa, int n, int dim, ANNpointArray src, int n_src, double std_dev)
 

Variables

int annIdum = 0
 
const double CO_FLAG = 999
 

Function Documentation

void annClusEllipsoids ( ANNpointArray  pa,
int  n,
int  dim,
int  n_clus,
ANNbool  new_clust,
double  std_dev_small,
double  std_dev_lo,
double  std_dev_hi,
int  max_dim 
)

Definition at line 503 of file rand.cpp.

References annAllocPts(), annDeallocPts(), annRan0(), annRanInt(), annRanUnif(), dim, genGauss(), and std_dev.

Referenced by generatePts().

513 {
514  static ANNpointArray centers = NULL; // cluster centers
515  static ANNpointArray std_dev = NULL; // standard deviations
516 
517  if (centers == NULL || new_clust) { // need new cluster centers
518  if (centers != NULL) // clusters already exist
519  annDeallocPts(centers); // get rid of them
520  if (std_dev != NULL) // std deviations already exist
521  annDeallocPts(std_dev); // get rid of them
522 
523  centers = annAllocPts(n_clus, dim); // alloc new clusters and devs
524  std_dev = annAllocPts(n_clus, dim);
525 
526  for (int i = 0; i < n_clus; i++) { // gen cluster center coords
527  for (int d = 0; d < dim; d++) {
528  centers[i][d] = (ANNcoord) annRanUnif(-1,1);
529  }
530  }
531  for (int c = 0; c < n_clus; c++) { // generate cluster std dev
532  int n_dim = 1 + annRanInt(max_dim); // number of dimensions in flat
533  for (int d = 0; d < dim; d++) { // generate std dev's
534  // prob. of picking next dim
535  double Prob = ((double) n_dim)/((double) (dim-d));
536  if (annRan0() < Prob) { // add this one to ellipse
537  // generate random std dev
538  std_dev[c][d] = annRanUnif(std_dev_lo, std_dev_hi);
539  n_dim--; // one fewer dim to fill
540  }
541  else { // don't take this one
542  std_dev[c][d] = std_dev_small;// use small std dev
543  }
544  }
545  }
546  }
547 
548  int offset = 0; // next slot to fill
549  for (int c = 0; c < n_clus; c++) { // generate clusters
550  int pick = (n+c)/n_clus; // number of points to pick
551  // generate the points
552  genGauss(pa+offset, pick, dim, centers[c], std_dev[c]);
553  offset += pick; // increment offset in array
554  }
555 }
double ANNcoord
Definition: ANN.h:158
double std_dev_lo
Definition: ann_test.cpp:481
DLL_API void annDeallocPts(ANNpointArray &pa)
Definition: ANN.cpp:133
ANNpoint * ANNpointArray
Definition: ANN.h:376
int max_dim
Definition: ann_test.cpp:477
static int annRanInt(int n)
Definition: rand.cpp:112
int dim
Definition: ann2fig.cpp:81
ANNbool new_clust
Definition: ann_test.cpp:476
double annRan0()
Definition: rand.cpp:59
double std_dev
Definition: ann_test.cpp:480
static double annRanUnif(double lo, double hi)
Definition: rand.cpp:124
DLL_API ANNpointArray annAllocPts(int n, int dim)
Definition: ANN.cpp:117
double std_dev_hi
Definition: ann_test.cpp:482
static void genGauss(ANNpointArray pa, int n, int dim, double *center, double *std_dev)
Definition: rand.cpp:489
void annClusGaussPts ( ANNpointArray  pa,
int  n,
int  dim,
int  n_clus,
ANNbool  new_clust,
double  std_dev 
)

Definition at line 314 of file rand.cpp.

References annAllocPts(), annDeallocPts(), annRanGauss(), annRanInt(), annRanUnif(), and dim.

Referenced by generatePts().

321 {
322  static ANNpointArray clusters = NULL;// cluster storage
323 
324  if (clusters == NULL || new_clust) {// need new cluster centers
325  if (clusters != NULL) // clusters already exist
326  annDeallocPts(clusters); // get rid of them
327  clusters = annAllocPts(n_clus, dim);
328  // generate cluster center coords
329  for (int i = 0; i < n_clus; i++) {
330  for (int d = 0; d < dim; d++) {
331  clusters[i][d] = (ANNcoord) annRanUnif(-1,1);
332  }
333  }
334  }
335 
336  for (int i = 0; i < n; i++) {
337  int c = annRanInt(n_clus); // generate cluster index
338  for (int d = 0; d < dim; d++) {
339  pa[i][d] = (ANNcoord) (std_dev*annRanGauss() + clusters[c][d]);
340  }
341  }
342 }
static double annRanGauss()
Definition: rand.cpp:137
double ANNcoord
Definition: ANN.h:158
DLL_API void annDeallocPts(ANNpointArray &pa)
Definition: ANN.cpp:133
ANNpoint * ANNpointArray
Definition: ANN.h:376
static int annRanInt(int n)
Definition: rand.cpp:112
int dim
Definition: ann2fig.cpp:81
ANNbool new_clust
Definition: ann_test.cpp:476
double std_dev
Definition: ann_test.cpp:480
static double annRanUnif(double lo, double hi)
Definition: rand.cpp:124
DLL_API ANNpointArray annAllocPts(int n, int dim)
Definition: ANN.cpp:117
void annClusOrthFlats ( ANNpointArray  pa,
int  n,
int  dim,
int  n_clus,
ANNbool  new_clust,
double  std_dev,
int  max_dim 
)

Definition at line 408 of file rand.cpp.

References annAllocPts(), annDeallocPts(), annRan0(), annRanInt(), annRanUnif(), CO_FLAG, dim, and genOrthFlat().

Referenced by generatePts().

416 {
417  static ANNpointArray control = NULL; // control vectors
418 
419  if (control == NULL || new_clust) { // need new cluster centers
420  if (control != NULL) { // clusters already exist
421  annDeallocPts(control); // get rid of them
422  }
423  control = annAllocPts(n_clus, dim);
424 
425  for (int c = 0; c < n_clus; c++) { // generate clusters
426  int n_dim = 1 + annRanInt(max_dim); // number of dimensions in flat
427  for (int d = 0; d < dim; d++) { // generate side locations
428  // prob. of picking next dim
429  double Prob = ((double) n_dim)/((double) (dim-d));
430  if (annRan0() < Prob) { // add this one to flat
431  control[c][d] = CO_FLAG; // flag this entry
432  n_dim--; // one fewer dim to fill
433  }
434  else { // don't take this one
435  control[c][d] = annRanUnif(-1,1);// random value in [-1,1]
436  }
437  }
438  }
439  }
440  int offset = 0; // offset in pa array
441  for (int c = 0; c < n_clus; c++) { // generate clusters
442  int pick = (n+c)/n_clus; // number of points to pick
443  // generate the points
444  genOrthFlat(pa+offset, pick, dim, control[c], std_dev);
445  offset += pick; // increment offset
446  }
447 }
DLL_API void annDeallocPts(ANNpointArray &pa)
Definition: ANN.cpp:133
static void genOrthFlat(ANNpointArray pa, int n, int dim, double *control, double std_dev)
Definition: rand.cpp:391
ANNpoint * ANNpointArray
Definition: ANN.h:376
const double CO_FLAG
Definition: rand.cpp:389
int max_dim
Definition: ann_test.cpp:477
static int annRanInt(int n)
Definition: rand.cpp:112
int dim
Definition: ann2fig.cpp:81
ANNbool new_clust
Definition: ann_test.cpp:476
double annRan0()
Definition: rand.cpp:59
double std_dev
Definition: ann_test.cpp:480
static double annRanUnif(double lo, double hi)
Definition: rand.cpp:124
DLL_API ANNpointArray annAllocPts(int n, int dim)
Definition: ANN.cpp:117
void annCoGaussPts ( ANNpointArray  pa,
int  n,
int  dim,
double  correlation 
)

Definition at line 250 of file rand.cpp.

References annRanGauss(), and dim.

Referenced by generatePts().

255 {
256  double std_dev_w = sqrt(1.0 - correlation * correlation);
257  for (int i = 0; i < n; i++) {
258  double previous = annRanGauss();
259  pa[i][0] = (ANNcoord) previous;
260  for (int d = 1; d < dim; d++) {
261  previous = correlation*previous + std_dev_w*annRanGauss();
262  pa[i][d] = (ANNcoord) previous;
263  }
264  }
265 }
static double annRanGauss()
Definition: rand.cpp:137
double ANNcoord
Definition: ANN.h:158
int dim
Definition: ann2fig.cpp:81
void annCoLaplacePts ( ANNpointArray  pa,
int  n,
int  dim,
double  correlation 
)

Definition at line 273 of file rand.cpp.

References annRan0(), annRanLaplace(), and dim.

Referenced by generatePts().

278 {
279  double wn;
280  double corr_sq = correlation * correlation;
281 
282  for (int i = 0; i < n; i++) {
283  double previous = annRanLaplace();
284  pa[i][0] = (ANNcoord) previous;
285  for (int d = 1; d < dim; d++) {
286  double temp = annRan0();
287  if (temp < corr_sq)
288  wn = 0.0;
289  else
290  wn = annRanLaplace();
291  previous = correlation * previous + wn;
292  pa[i][d] = (ANNcoord) previous;
293  }
294  }
295 }
double ANNcoord
Definition: ANN.h:158
static double annRanLaplace()
Definition: rand.cpp:181
int dim
Definition: ann2fig.cpp:81
double annRan0()
Definition: rand.cpp:59
void annGaussPts ( ANNpointArray  pa,
int  n,
int  dim,
double  std_dev 
)

Definition at line 214 of file rand.cpp.

References annRanGauss(), dim, and std_dev.

Referenced by generatePts().

219 {
220  for (int i = 0; i < n; i++) {
221  for (int d = 0; d < dim; d++) {
222  pa[i][d] = (ANNcoord) (annRanGauss() * std_dev);
223  }
224  }
225 }
static double annRanGauss()
Definition: rand.cpp:137
double ANNcoord
Definition: ANN.h:158
int dim
Definition: ann2fig.cpp:81
double std_dev
Definition: ann_test.cpp:480
void annLaplacePts ( ANNpointArray  pa,
int  n,
int  dim 
)

Definition at line 232 of file rand.cpp.

References annRanLaplace(), and dim.

Referenced by generatePts().

236 {
237  for (int i = 0; i < n; i++) {
238  for (int d = 0; d < dim; d++) {
239  pa[i][d] = (ANNcoord) annRanLaplace();
240  }
241  }
242 }
double ANNcoord
Definition: ANN.h:158
static double annRanLaplace()
Definition: rand.cpp:181
int dim
Definition: ann2fig.cpp:81
void annPlanted ( ANNpointArray  pa,
int  n,
int  dim,
ANNpointArray  src,
int  n_src,
double  std_dev 
)

Definition at line 580 of file rand.cpp.

References annRanGauss(), annRanInt(), and dim.

Referenced by generatePts().

587 {
588  for (int i = 0; i < n; i++) {
589  int c = annRanInt(n_src); // generate source index
590  for (int d = 0; d < dim; d++) {
591  pa[i][d] = (ANNcoord) (std_dev*annRanGauss() + src[c][d]);
592  }
593  }
594 }
static double annRanGauss()
Definition: rand.cpp:137
double ANNcoord
Definition: ANN.h:158
static int annRanInt(int n)
Definition: rand.cpp:112
int dim
Definition: ann2fig.cpp:81
double std_dev
Definition: ann_test.cpp:480
double annRan0 ( )

Definition at line 59 of file rand.cpp.

References ANN_RAND, ANN_RAND_MAX, ANN_SRAND, and annIdum.

Referenced by annClusEllipsoids(), annClusOrthFlats(), annCoLaplacePts(), annRanInt(), annRanLaplace(), and annRanUnif().

60 {
61  const int TAB_SIZE = 97; // table size: any large number
62  int j;
63 
64  static double y, v[TAB_SIZE];
65  static int iff = 0;
66  const double RAN_DIVISOR = double(ANN_RAND_MAX + 1UL);
67  if (RAN_DIVISOR < 0) {
68  cout << "RAN_DIVISOR " << RAN_DIVISOR << endl;
69  exit(0);
70  }
71 
72  //--------------------------------------------------------------------
73  // As a precaution against misuse, we will always initialize on the
74  // first call, even if "annIdum" is not set negative. Determine
75  // "maxran", the next integer after the largest representable value
76  // of type int. We assume this is a factor of 2 smaller than the
77  // corresponding value of type unsigned int.
78  //--------------------------------------------------------------------
79 
80  if (annIdum < 0 || iff == 0) { // initialize
81  iff = 1;
82  ANN_SRAND(annIdum); // (re)seed the generator
83  annIdum = 1;
84 
85  for (j = 0; j < TAB_SIZE; j++) // exercise the system routine
86  ANN_RAND(); // (values intentionally ignored)
87 
88  for (j = 0; j < TAB_SIZE; j++) // then save TAB_SIZE-1 values
89  v[j] = ANN_RAND();
90  y = ANN_RAND(); // generate starting value
91  }
92 
93  //--------------------------------------------------------------------
94  // This is where we start if not initializing. Use the previously
95  // saved random number y to get an index j between 1 and TAB_SIZE-1.
96  // Then use the corresponding v[j] for both the next j and as the
97  // output number.
98  //--------------------------------------------------------------------
99 
100  j = int(TAB_SIZE * (y / RAN_DIVISOR));
101  y = v[j];
102  v[j] = ANN_RAND(); // refill the table entry
103  return y / RAN_DIVISOR;
104 }
#define ANN_RAND_MAX
Definition: rand.h:54
#define ANN_SRAND
Definition: rand.h:53
#define ANN_RAND
Definition: rand.h:52
int annIdum
Definition: rand.cpp:42
static double annRanGauss ( )
static

Definition at line 137 of file rand.cpp.

References annRanUnif().

Referenced by annClusGaussPts(), annCoGaussPts(), annGaussPts(), annPlanted(), genGauss(), and genOrthFlat().

138 {
139  static int iset=0;
140  static double gset;
141 
142  if (iset == 0) { // we don't have a deviate handy
143  double v1, v2;
144  double r = 2.0;
145  while (r >= 1.0) {
146  //------------------------------------------------------------
147  // Pick two uniform numbers in the square extending from -1 to
148  // +1 in each direction, see if they are in the circle of radius
149  // 1. If not, try again
150  //------------------------------------------------------------
151  v1 = annRanUnif(-1, 1);
152  v2 = annRanUnif(-1, 1);
153  r = v1 * v1 + v2 * v2;
154  }
155  double fac = sqrt(-2.0 * log(r) / r);
156  //-----------------------------------------------------------------
157  // Now make the Box-Muller transformation to get two normal
158  // deviates. Return one and save the other for next time.
159  //-----------------------------------------------------------------
160  gset = v1 * fac;
161  iset = 1; // set flag
162  return v2 * fac;
163  }
164  else { // we have an extra deviate handy
165  iset = 0; // so unset the flag
166  return gset; // and return it
167  }
168 }
static double annRanUnif(double lo, double hi)
Definition: rand.cpp:124
static int annRanInt ( int  n)
static

Definition at line 112 of file rand.cpp.

References annRan0().

Referenced by annClusEllipsoids(), annClusGaussPts(), annClusOrthFlats(), and annPlanted().

114 {
115  int r = (int) (annRan0()*n);
116  if (r == n) r--; // (in case annRan0() == 1 or n == 0)
117  return r;
118 }
double annRan0()
Definition: rand.cpp:59
static double annRanLaplace ( )
static

Definition at line 181 of file rand.cpp.

References annRan0().

Referenced by annCoLaplacePts(), and annLaplacePts().

182 {
183  const double b = 1.4142136;
184 
185  double laprand = -log(annRan0()) / b;
186  double sign = annRan0();
187  if (sign < 0.5) laprand = -laprand;
188  return(laprand);
189 }
double annRan0()
Definition: rand.cpp:59
static double annRanUnif ( double  lo,
double  hi 
)
static

Definition at line 124 of file rand.cpp.

References annRan0().

Referenced by annClusEllipsoids(), annClusGaussPts(), annClusOrthFlats(), annRanGauss(), annUniformPts(), and genOrthFlat().

127 {
128  return annRan0()*(hi-lo) + lo;
129 }
double annRan0()
Definition: rand.cpp:59
void annUniformPts ( ANNpointArray  pa,
int  n,
int  dim 
)

Definition at line 196 of file rand.cpp.

References annRanUnif(), and dim.

Referenced by generatePts().

200 {
201  for (int i = 0; i < n; i++) {
202  for (int d = 0; d < dim; d++) {
203  pa[i][d] = (ANNcoord) (annRanUnif(-1,1));
204  }
205  }
206 }
double ANNcoord
Definition: ANN.h:158
int dim
Definition: ann2fig.cpp:81
static double annRanUnif(double lo, double hi)
Definition: rand.cpp:124
static void genGauss ( ANNpointArray  pa,
int  n,
int  dim,
double *  center,
double *  std_dev 
)
static

Definition at line 489 of file rand.cpp.

References annRanGauss(), and dim.

Referenced by annClusEllipsoids().

495 {
496  for (int i = 0; i < n; i++) {
497  for (int d = 0; d < dim; d++) {
498  pa[i][d] = (ANNcoord) (std_dev[d]*annRanGauss() + center[d]);
499  }
500  }
501 }
static double annRanGauss()
Definition: rand.cpp:137
double ANNcoord
Definition: ANN.h:158
int dim
Definition: ann2fig.cpp:81
double std_dev
Definition: ann_test.cpp:480
static void genOrthFlat ( ANNpointArray  pa,
int  n,
int  dim,
double *  control,
double  std_dev 
)
static

Definition at line 391 of file rand.cpp.

References annRanGauss(), annRanUnif(), CO_FLAG, and dim.

Referenced by annClusOrthFlats().

397 {
398  for (int i = 0; i < n; i++) { // generate each point
399  for (int d = 0; d < dim; d++) { // generate each coord
400  if (control[d] == CO_FLAG) // dimension on flat
401  pa[i][d] = (ANNcoord) annRanUnif(-1,1);
402  else // dimension off flat
403  pa[i][d] = (ANNcoord) (std_dev*annRanGauss() + control[d]);
404  }
405  }
406 }
static double annRanGauss()
Definition: rand.cpp:137
double ANNcoord
Definition: ANN.h:158
const double CO_FLAG
Definition: rand.cpp:389
int dim
Definition: ann2fig.cpp:81
double std_dev
Definition: ann_test.cpp:480
static double annRanUnif(double lo, double hi)
Definition: rand.cpp:124

Variable Documentation

int annIdum = 0

Definition at line 42 of file rand.cpp.

Referenced by annRan0(), generatePts(), initGlobals(), and main().

const double CO_FLAG = 999

Definition at line 389 of file rand.cpp.

Referenced by annClusOrthFlats(), and genOrthFlat().


Generated on Thu Jun 11 2015 13:52:33 for queso-0.53.0 by  doxygen 1.8.5