LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Random.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
32 #ifndef LSST_AFW_MATH_RANDOM_H
33 #define LSST_AFW_MATH_RANDOM_H
34 
35 #include "boost/shared_ptr.hpp"
36 
37 #include "gsl/gsl_rng.h"
38 
39 #include "lsst/pex/exceptions.h"
40 #include "lsst/pex/policy/Policy.h"
41 
42 
43 namespace lsst { namespace afw { namespace math {
44 
62 class Random {
63 public:
64 
66  enum Algorithm {
68  MT19937 = 0,
86  MRG,
95  };
96 
97  // -- Constructor --------
98  explicit Random(Algorithm algorithm = MT19937, unsigned long seed = 1);
99  explicit Random(std::string const & algorithm, unsigned long seed = 1);
100  explicit Random(lsst::pex::policy::Policy::Ptr const policy);
101  // Use compiler generated destructor and shallow copy constructor/assignment operator
102 
103  Random deepCopy() const;
104 
106 
116  typedef std::string State;
117  State getState() const;
118  void setState(State const & state);
119  std::size_t getStateSize() const;
121 
122  // -- Accessors --------
123  Algorithm getAlgorithm() const;
124  std::string getAlgorithmName() const;
125  static std::vector<std::string> const & getAlgorithmNames();
126  unsigned long getSeed() const;
127 
128  // -- Modifiers: generating random numbers --------
129  double uniform();
130  double uniformPos();
131  unsigned long uniformInt(unsigned long n);
132 
133  // -- Modifiers: computing random variates for various distributions --------
134  double flat(double const a, double const b);
135  double gaussian();
136  double chisq(double const nu);
137  double poisson(double const nu);
138 
139 private:
140  boost::shared_ptr< ::gsl_rng> _rng;
141  unsigned long _seed;
143 
144  static ::gsl_rng_type const * const _gslRngTypes[NUM_ALGORITHMS];
145  static char const * const _algorithmNames[NUM_ALGORITHMS];
146  static char const * const _algorithmEnvVarName;
147  static char const * const _seedEnvVarName;
148 
149  void initialize();
150  void initialize(std::string const &);
151 };
152 
153 /************************************************************************************************************/
154 /*
155  * Create Images containing random numbers
156  */
157 template<typename ImageT>
158 void randomUniformImage(ImageT *image, Random &rand);
159 
160 template<typename ImageT>
161 void randomUniformPosImage(ImageT *image, Random &rand);
162 
163 template<typename ImageT>
164 void randomUniformIntImage(ImageT *image, Random &rand, unsigned long n);
165 
166 template<typename ImageT>
167 void randomFlatImage(ImageT *image, Random &rand, double const a, double const b);
168 
169 template<typename ImageT>
170 void randomGaussianImage(ImageT *image, Random &rand);
171 
172 template<typename ImageT>
173 void randomChisqImage(ImageT *image, Random &rand, double const nu);
174 
175 template<typename ImageT>
176 void randomPoissonImage(ImageT *image, Random &rand, double const mu);
177 
178 
179 }}} // end of namespace lsst::afw::math
180 
181 #endif // LSST_AFW_MATH_RANDOM_H
182 
void randomUniformImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:112
std::string getAlgorithmName() const
Definition: Random.cc:263
static char const *const _seedEnvVarName
Definition: Random.h:147
Algorithm getAlgorithm() const
Definition: Random.cc:256
double chisq(double const nu)
Definition: Random.cc:380
unsigned long getSeed() const
Definition: Random.cc:285
double flat(double const a, double const b)
Definition: Random.cc:358
unsigned long _seed
Definition: Random.h:141
std::size_t getStateSize() const
Definition: Random.cc:247
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
std::string State
Definition: Random.h:116
::gsl_rng_type const *const _gslRngTypes[NUM_ALGORITHMS]
Definition: Random.h:144
void randomFlatImage(ImageT *image, Random &rand, double const a, double const b)
Definition: RandomImage.cc:143
double poisson(double const nu)
Definition: Random.cc:390
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
void randomUniformPosImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:122
void randomChisqImage(ImageT *image, Random &rand, double const nu)
Definition: RandomImage.cc:165
Random(Algorithm algorithm=MT19937, unsigned long seed=1)
Definition: Random.cc:151
unsigned long uniformInt(unsigned long n)
Definition: Random.cc:341
static char const *const _algorithmEnvVarName
Definition: Random.h:146
void randomGaussianImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:155
double uniformPos()
Definition: Random.cc:319
void setState(State const &state)
Definition: Random.cc:236
Random deepCopy() const
Definition: Random.cc:223
void randomUniformIntImage(ImageT *image, Random &rand, unsigned long n)
Definition: RandomImage.cc:132
static std::vector< std::string > const & getAlgorithmNames()
Definition: Random.cc:270
void randomPoissonImage(ImageT *image, Random &rand, double const mu)
Definition: RandomImage.cc:177
static char const *const _algorithmNames[NUM_ALGORITHMS]
Definition: Random.h:145
afw::table::Key< double > b
State getState() const
Definition: Random.cc:232
Include files required for standard LSST Exception handling.
boost::shared_ptr< ::gsl_rng > _rng
Definition: Random.h:140
Algorithm _algorithm
Definition: Random.h:142