EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_alias_table.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ alias table headers
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors: Ernesto Mainegra-Hing
27 # Marc Chamberland
28 #
29 ###############################################################################
30 */
31 
32 
38 #ifndef EGS_ALIAS_TABLE_
39 #define EGS_ALIAS_TABLE_
40 
41 #include "egs_libconfig.h"
42 #include "egs_rndm.h"
43 #include <vector>
44 
45 typedef EGS_Float(*EGS_AtFunction)(EGS_Float,void *);
46 
65 
66 public:
67 
73  EGS_AliasTable() : n(0) {};
74 
76  EGS_AliasTable(const EGS_AliasTable &t) : n(0) {
77  copy(t);
78  };
79 
94  EGS_AliasTable(int N, const EGS_Float *x, const EGS_Float *f,
95  int Type = 1) : n(0) {
96  initialize(N,x,f,Type);
97  };
98 
99 
110  EGS_AliasTable(EGS_Float xmin, EGS_Float xmax, EGS_Float accu, int nmax,
111  EGS_AtFunction func, void *data) : n(0) {
112  initialize(xmin,xmax,accu,nmax,func,data);
113  };
114 
119  void initialize(int N, const EGS_Float *x, const EGS_Float *f,
120  int Type = 1);
121 
131  int initialize(EGS_Float xmin, EGS_Float xmax, EGS_Float accu, int nmax,
132  EGS_AtFunction func, void *data);
133 
135  EGS_Float sample(EGS_RandomGenerator *rndm) const;
136 
138  int sampleBin(EGS_RandomGenerator *rndm) const;
139 
142  EGS_Float getAverage() const {
143  return average;
144  };
145 
147  EGS_Float getMaximum() const {
148  return xi[n-1];
149  };
150 
151 private:
152 
153  int n;
154  int np;
155  EGS_Float *fi;
156  EGS_Float *xi;
157  EGS_Float *wi;
158  EGS_Float average;
159  int *bin;
160  int type;
163  void copy(const EGS_AliasTable &t);
164  void clear();
165  void allocate(int N, int Type);
166  void make();
167 
168 
169 };
170 
184 
185 public:
186 
192  EGS_SimpleAliasTable(int N, const EGS_Float *f);
193 
196 
201  int sample(EGS_RandomGenerator *rndm) const {
202  int bin = (int)(rndm->getUniform()*n);
203  return rndm->getUniform() < wi[bin] ? bin : bins[bin];
204  };
205 
206 private:
207 
208  int n;
209  EGS_Float *wi;
210  int *bins;
211 
212 };
213 
214 
215 #endif
A class for sampling random values from a given probability distribution using the alias table techni...
EGS_Float getMaximum() const
Get the maximum abscissa of this alias table object.
EGS_AliasTable()
Construct an empty (uninitialized) alias table.
EGS_AliasTable(const EGS_AliasTable &t)
Copy constructor. Performs a deep copy.
EGS_AliasTable(int N, const EGS_Float *x, const EGS_Float *f, int Type=1)
Construct an alias table from the tabulated distribution with N bins with probabilities f at the bins...
EGS_Float getAverage() const
Get the average of the probability distribution represented by this alias table object.
EGS_AliasTable(EGS_Float xmin, EGS_Float xmax, EGS_Float accu, int nmax, EGS_AtFunction func, void *data)
Construct an alias table for sampling values in the interval xmin ... xmax for the function func.
Base random number generator class. All random number generators should be derived from this class.
Definition: egs_rndm.h:67
EGS_Float getUniform()
Returns a random number uniformly distributed between zero (inclusive) and 1 (exclusive).
Definition: egs_rndm.h:103
A class for sampling random bins from a given probability distribution using the alias table techniqu...
int sample(EGS_RandomGenerator *rndm) const
Sample a random bin.
Defines the EGS_EXPORT and EGS_LOCAL macros.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
EGS_RandomGenerator class header file.