ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ranfill.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2009-2012 ADMB Foundation
6  */
11 #include <fvar.hpp>
12 
13 
14 #ifdef __TURBOC__
15  #pragma hdrstop
16  #include <iostream.h>
17 #endif
18 
19 #ifdef __ZTC__
20  #include <iostream.hpp>
21 #endif
22 
23 #include <math.h>
24 
25 #if (defined(_MSC_VER) && (_MSC_VER < 1900))\
26  || (defined(__cplusplus) && (__cplusplus < 201103L))
27 #define M1 714025
28 #else
29 constexpr int M1 = 714025;
30 #endif
31 
32 #define IA1 1366
33 #define IC1 150889
34 
35 #if (defined(_MSC_VER) && (_MSC_VER < 1900))\
36  || (defined(__cplusplus) && (__cplusplus < 201103L))
37 #define RM1 (1.0f/M1)
38 #else
39 constexpr float RM1 = 1.0f / M1;
40 #endif
41 
42 #define M3 134456
43 #define IA3 8121
44 #define IC3 28411
45 
46 #if (defined(_MSC_VER) && (_MSC_VER < 1900))\
47  || (defined(__cplusplus) && (__cplusplus < 201103L))
48 #define M2 243000
49 #else
50 constexpr int M2 = 243000;
51 #endif
52 
53 #define IA2 4561
54 #define IC2 51349
55 
56 #if (defined(_MSC_VER) && (_MSC_VER < 1900))\
57  || (defined(__cplusplus) && (__cplusplus < 201103L))
58 #define RM2 (1.0f/M2)
59 #else
60 constexpr float RM2 = 1.0f / M2;
61 #endif
62 
63 double auto_rand(long int& idum, int reset);
65 double randn(long int& n);
66 
72 {
73  long int One = 1;
74  auto_rand(One, -1);
75 }
76 
84 double auto_rand(long int& idum, int reset)
85 {
86  static long ix1,ix2,ix3;
87  static float r[108];
88  static int iff=0;
89 
90  if (reset < 0)
91  {
92  iff = 0;
93  return .5;
94  }
95 
96  if (idum < 0 || iff == 0)
97  {
98  iff=2;
99  ix1=(IC1-(idum))%M1;
100  ix1=ix1 % M1;
101 
102 
103  ix1=(IA1*ix1+IC1);
104  ix1=ix1 % M1;
105 
106  ix2=ix1 % M2;
107  ix1=(IA1*ix1+IC1);
108  ix1=ix1 % M1;
109  ix3=ix1 % M3;
110  for (int j=1;j<=107;j++)
111  {
112  ix2=(IA2*ix2+IC2) % M2;
113  ix1=(IA1*ix1+IC1);
114  ix1=ix1 % M1;
115 
116  long int iu = (long int)(ix2 * RM2);
117  r[j]=(ix1+iu)*RM1;
118  }
119  idum =6;
120  }
121 
122  ix3=(IA3*ix3+IC3) % M3;
123  ix3=ix3 % M3;
124  ix1=(IA1*ix1+IC1) % M1;
125  ix1=ix1 % M1;
126  ix2=(IA2*ix2+IC2) % M2;
127  ix2=ix2 % M2;
128  long j = 1 + ((107*ix3)/M3);
129  if (j > 107 || j < 1) cerr << " Error in random number generator\n";
130  double temp = r[j];
131  r[j]=ix2*RM2;
132  r[j]=(ix1+r[j]);
133  r[j]=r[j]*RM1;
134  return temp;
135 }
136 
137 #undef IA1
138 #undef IC1
139 #undef IA2
140 #undef IC2
141 #undef M3
142 #undef IA3
143 #undef IC3
144 
149 double randn(long int& n)
150 {
151  long int nn=n;
152  double x=auto_rand(nn,1);
153  double y=auto_rand(nn,1);
154  double u=sqrt(-2*log(x))*cos(2*PI*y);
155  return(u);
156 }
164 void dvector::fill_randbi(long int& n, double p)
165 {
166  if (p < 0.0 || p > 1.0)
167  {
168  cerr << "Error in dvar_vector::fill_randbi proportions of"
169  " successes must lie between 0 and 1.\n";
170  ad_exit(1);
171  }
172  long int nn = n;
173  for (int i = indexmin(); i <= indexmax(); ++i)
174  {
175  if (auto_rand(nn, 1) <= p)
176  {
177  elem(i) = 1.0;
178  }
179  else
180  {
181  elem(i) = 0.0;
182  }
183  }
185 }
190 void dvector::fill_randu(long int& n)
191 {
192  long int nn=n;
193  for (int i = indexmin(); i <= indexmax(); ++i)
194  {
195  elem(i) = auto_rand(nn, 1);
196  }
198 }
204 void dmatrix::colfill_randu(const int& j, long int& n)
205 {
206  long int nn = n;
207  for (int i = rowmin(); i <= rowmax(); ++i)
208  {
209  elem(i, j) = auto_rand(nn, 1);
210  }
212 }
218 void dmatrix::rowfill_randu(const int& i, long int& n)
219 {
220  long int nn = n;
221  for (int j = colmin(); j <= colmax(); ++j)
222  {
223  elem(i, j) = auto_rand(nn, 1);
224  }
226 }
231 void dvector::fill_randn(long int& n)
232 {
233  long int nn = n;
234  for (int i = indexmin(); i <= indexmax(); ++i)
235  {
236  (*this)(i) = randn(nn);
237  }
239 }
244 void dmatrix::fill_randn(long int& n)
245 {
246  long int nn = n;
247  for (int i = rowmin(); i <= rowmax(); ++i)
248  {
249  elem(i).fill_randn_ni(nn);
250  nn += 2;
251  }
253 }
258 void d3_array::fill_randn(long int& n)
259 {
260  long int nn = n;
261  for (int i = slicemin(); i <= slicemax(); ++i)
262  {
263  elem(i).fill_randn_ni(nn);
264  nn += 2;
265  }
267 }
272 void d3_array::fill_randu(long int& n)
273 {
274  long int nn = n;
275  for (int i = slicemin(); i <= slicemax(); ++i)
276  {
277  elem(i).fill_randu_ni(nn);
278  nn += 2;
279  }
281 }
286 void dmatrix::fill_randu(long int& n)
287 {
288  long int nn = n;
289  for (int i = rowmin(); i <= rowmax(); ++i)
290  {
291  elem(i).fill_randu_ni(nn);
292  nn += 2;
293  }
295 }
301 void dmatrix::colfill_randn(const int& j,long int& n)
302 {
303  long int nn=n;
304  for (int i = rowmin(); i <= rowmax(); ++i)
305  {
306  elem(i, j) = randn(nn);
307  }
309 }
315 void dmatrix::rowfill_randn(const int& i, long int& n)
316 {
317  long int nn = n;
318  for (int j = colmin(); j <= colmax(); ++j)
319  {
320  elem(i, j) = randn(nn);
321  }
323 }
constexpr float RM1
Definition: ranfill.cpp:39
#define IC1
Definition: ranfill.cpp:33
#define IA3
Definition: ranfill.cpp:43
double & elem(int i)
Definition: dvector.h:152
#define IC2
Definition: ranfill.cpp:54
double auto_rand(long int &idum, int reset)
Random number generator.
Definition: ranfill.cpp:84
void fill_randn_ni(long int &n)
Description not yet available.
Definition: ranf_ni.cpp:116
void reinitialize_auto_rand()
Description not yet available.
Definition: ranfill.cpp:71
void fill_randn(const random_number_generator &rng)
Description not yet available.
Definition: rngen.cpp:292
#define IC3
Definition: ranfill.cpp:44
#define x
constexpr float RM2
Definition: ranfill.cpp:60
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void fill_randn(long int &n)
Fill matrix with random numbers.
Definition: ranfill.cpp:244
constexpr int M2
Definition: ranfill.cpp:50
exitptr ad_exit
Definition: gradstrc.cpp:53
constexpr int M1
Definition: ranfill.cpp:29
void fill_randu_ni(long int &n)
Description not yet available.
Definition: ranf_ni.cpp:160
void fill_randu(long int &n)
Fill vector with random numbers.
Definition: ranfill.cpp:190
void colfill_randn(const int &j, long int &n)
Fill random numbers into specified column j of matrix.
Definition: ranfill.cpp:301
int slicemax() const
Definition: fvar.hpp:3830
void fill_randn(long int &n)
Fill vector with random numbers.
Definition: ranfill.cpp:231
#define IA1
Definition: ranfill.cpp:32
int rowmax() const
Definition: fvar.hpp:2929
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
void fill_randu(long int &n)
Fill matrix with random numbers.
Definition: ranfill.cpp:286
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void fill_randbi(long int &n, double)
Fill vector with binary random numbers.
Definition: ranfill.cpp:164
int slicemin() const
Definition: fvar.hpp:3826
int colmin(void) const
Definition: fvar.hpp:2939
dvector & elem(int i)
Definition: fvar.hpp:3011
#define M3
Definition: ranfill.cpp:42
d3_array cos(const d3_array &arr3)
Returns d3_array results with computed cos from elements in arr3.
Definition: d3arr2a.cpp:58
#define IA2
Definition: ranfill.cpp:53
double randn(const random_number_generator &rng)
Normal number generator.
Definition: rngen.cpp:183
void fill_randn_ni(long int &n)
Description not yet available.
Definition: ranf_ni.cpp:102
void colfill_randu(const int &j, long int &n)
Fill random numbers into specified column j of matrix.
Definition: ranfill.cpp:204
void rowfill_randn(const int &i, long int &n)
Fill random numbers into specified row i of matrix.
Definition: ranfill.cpp:315
dmatrix & elem(int k)
Definition: fvar.hpp:3870
#define PI
Definition: fvar.hpp:95
void fill_randu(const random_number_generator &rng)
Description not yet available.
Definition: rngen.cpp:304
int rowmin() const
Definition: fvar.hpp:2925
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
int colmax(void) const
Definition: fvar.hpp:2943
void rowfill_randu(const int &i, long int &n)
Fill random numbers into specified row i of matrix.
Definition: ranfill.cpp:218
void fill_randu_ni(long int &n)
Description not yet available.
Definition: ranf_ni.cpp:60