ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rngen.cpp
Go to the documentation of this file.
1 /*
2  $Id$
3 
4  ADMB adaptations Copyright (c) 2009-2012 ADMB Foundation
5 
6  A C-program for MT19937, with initialization improved 2002/1/26.
7  Coded by Takuji Nishimura and Makoto Matsumoto.
8 
9  Before using, initialize the state by using init_genrand(seed)
10  or init_by_array(init_key, key_length).
11 
12  Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
13  All rights reserved.
14 
15  Redistribution and use in source and binary forms, with or without
16  modification, are permitted provided that the following conditions
17  are met:
18 
19  1. Redistributions of source code must retain the above copyright
20  notice, this list of conditions and the following disclaimer.
21 
22  2. Redistributions in binary form must reproduce the above copyright
23  notice, this list of conditions and the following disclaimer in the
24  documentation and/or other materials provided with the distribution.
25 
26  3. The names of its contributors may not be used to endorse or promote
27  products derived from this software without specific prior written
28  permission.
29 
30  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 
42 
43  Any feedback is very welcome.
44  http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
45  email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
46 
47  Modified for AD Model Builder by Kasper Kristensen <kkr@aqua.dtu.dk> and
48  Anders Nielsen <an@aqua.dtu.dk> 2009
49 */
54 #include "fvar.hpp"
55 
56 #define N 624
57 #define M 397
58 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
59 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
60 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
61 
71 {
72  unsigned long s=seed;
73  mt=new unsigned long [N]; /* the array for the state vector */
74  mti=N+1; /* mti==N+1 means mt[N] is not initialized */
75 
76  mt[0]= s & 0xffffffffUL;
77  for (mti=1; mti<N; mti++) {
78  mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
79  /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
80  /* In the previous versions, MSBs of the seed affect */
81  /* only MSBs of the array mt[]. */
82  /* 2002/01/09 modified by Makoto Matsumoto */
83  mt[mti] &= 0xffffffffUL;
84  /* for >32 bit machines */
85  }
86  better_rand();
87 }
88 
93 {
94  delete [] mt;
95  mt = 0;
96 }
97 
107 {
108  unsigned long s=seed;
109  mt[0]= s & 0xffffffffUL;
110  for (mti=1; mti<N; mti++) {
111  mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
112  /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
113  /* In the previous versions, MSBs of the seed affect */
114  /* only MSBs of the array mt[]. */
115  /* 2002/01/09 modified by Makoto Matsumoto */
116  mt[mti] &= 0xffffffffUL;
117  /* for >32 bit machines */
118  }
119  better_rand();
120 }
121 
135 {
136  unsigned long y;
137  static unsigned long mag01[2]={0x0UL, MATRIX_A};
138  /* mag01[x] = x * MATRIX_A for x=0,1 */
139 
140  if (mti >= N) { /* generate N words at one time */
141  int kk = 0;
142 
143  //if (mti == N+1) /* if init_genrand() has not been called, */
144  // init_genrand(5489UL); /* a default initial seed is used */
145 
146  for (;kk<N-M;kk++) {
147  y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
148  mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
149  }
150  for (;kk<N-1;kk++) {
151  y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
152  mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
153  }
154  y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
155  mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
156 
157  mti = 0;
158  }
159 
160  y = mt[mti++];
161 
162  /* Tempering */
163  y ^= (y >> 11);
164  y ^= (y << 7) & 0x9d2c5680UL;
165  y ^= (y << 15) & 0xefc60000UL;
166  y ^= (y >> 18);
167 
168  return (((double)y) + 0.5)*(1.0/4294967296.0);
169 }
170 
171 #undef N
172 #undef M
173 #undef MATRIX_A
174 #undef UPPER_MASK
175 #undef LOWER_MASK
176 
183 double randn(const random_number_generator& rng)
184 {
185  double x=((random_number_generator&) rng).better_rand();
186  double y=((random_number_generator&) rng).better_rand();
187  double u=sqrt(-2*log(x))*cos(2*PI*y);
188  return u;
189 }
190 
198 double randu(const random_number_generator& rng)
199 {
200  return ((random_number_generator&)rng).better_rand();
201 }
202 
208 {
209  if ( p<0 || p>1)
210  {
211  cerr << "Error in dvar_vector::fill_randbi proportions of"
212  " successes must lie between 0 and 1\n";
213  ad_exit(1);
214  }
215  for (int i=indexmin(); i<=indexmax(); i++)
216  {
217  if ( ((random_number_generator&) rng).better_rand()<=p)
218  {
219  elem(i)=1;
220  }
221  else
222  {
223  elem(i)=0;
224  }
225  }
226 }
227 
233 {
234  for (int i=indexmin(); i<=indexmax(); i++)
235  {
236  elem(i)=((random_number_generator&) rng).better_rand();
237  }
238 }
239 
244 void dmatrix::colfill_randu(const int&j, const random_number_generator& rng)
245 {
246  for (int i=rowmin(); i<=rowmax(); i++)
247  {
248  elem(i,j)=((random_number_generator&) rng).better_rand();
249  }
250 }
251 
256 void dmatrix::rowfill_randu(const int& i, const random_number_generator& rng)
257 {
258  for (int j=colmin(); j<=colmax(); j++)
259  {
260  elem(i,j)=((random_number_generator&) rng).better_rand();
261  }
262 }
263 
269 {
270  for (int i=indexmin(); i<=indexmax(); i++)
271  {
272  (*this)(i)=randn(rng);
273  }
274 }
275 
281 {
282  for (int i=rowmin(); i<=rowmax(); i++)
283  {
284  elem(i).fill_randn(rng);
285  }
286 }
287 
293 {
294  for (int i=slicemin(); i<=slicemax(); i++)
295  {
296  elem(i).fill_randn(rng);
297  }
298 }
299 
305 {
306  for (int i=slicemin(); i<=slicemax(); i++)
307  {
308  elem(i).fill_randu(rng);
309  }
310 }
311 
317 {
318  for (int i=rowmin(); i<=rowmax(); i++)
319  {
320  elem(i).fill_randu(rng);
321  }
322 }
323 
328 void dmatrix::colfill_randn(const int&j, const random_number_generator& rng)
329 {
330  for (int i=rowmin(); i<=rowmax(); i++)
331  {
332  elem(i,j)=randn(rng);
333  }
334 }
335 
340 void dmatrix::rowfill_randn(const int& i, const random_number_generator& rng)
341 {
342  for (int j=colmin(); j<=colmax(); j++)
343  {
344  elem(i,j)=randn(rng);
345  }
346 }
void reinitialize(const int seed)
Reinitialize random number seed.
Definition: rngen.cpp:106
double & elem(int i)
Definition: dvector.h:152
void fill_randn(const random_number_generator &rng)
Description not yet available.
Definition: rngen.cpp:292
#define UPPER_MASK
Definition: rngen.cpp:59
#define N
Definition: rngen.cpp:56
~random_number_generator()
Destructor.
Definition: rngen.cpp:92
#define x
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
#define LOWER_MASK
Definition: rngen.cpp:60
void fill_randn(long int &n)
Fill matrix with random numbers.
Definition: ranfill.cpp:244
exitptr ad_exit
Definition: gradstrc.cpp:53
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
double better_rand()
Random number generator.
Definition: rngen.cpp:134
double randu(const random_number_generator &rng)
Uniform random number generator.
Definition: rngen.cpp:198
void fill_randn(long int &n)
Fill vector with random numbers.
Definition: ranfill.cpp:231
Description not yet available.
Definition: fvar.hpp:7951
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
#define M
Definition: rngen.cpp:57
int slicemin() const
Definition: fvar.hpp:3826
int colmin(void) const
Definition: fvar.hpp:2939
unsigned long * mt
the array for the state vector
Definition: fvar.hpp:7953
dvector & elem(int i)
Definition: fvar.hpp:3011
random_number_generator(const int seed)
Constructor for random_number_generator class.
Definition: rngen.cpp:70
d3_array cos(const d3_array &arr3)
Returns d3_array results with computed cos from elements in arr3.
Definition: d3arr2a.cpp:58
double randn(const random_number_generator &rng)
Normal number generator.
Definition: rngen.cpp:183
int mti
mti==N+1 means mt[N] is not initialized
Definition: fvar.hpp:7954
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
#define MATRIX_A
Definition: rngen.cpp:58
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