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 #
28 ###############################################################################
29 */
30 
31 
37 #ifndef EGS_ALIAS_TABLE_
38 #define EGS_ALIAS_TABLE_
39 
40 #include "egs_libconfig.h"
41 #include "egs_rndm.h"
42 #include <vector>
43 
44 typedef EGS_Float(*EGS_AtFunction)(EGS_Float,void *);
45 
64 
65 public:
66 
72  EGS_AliasTable() : n(0) {};
73 
75  EGS_AliasTable(const EGS_AliasTable &t) : n(0) {
76  copy(t);
77  };
78 
93  EGS_AliasTable(int N, const EGS_Float *x, const EGS_Float *f,
94  int Type = 1) : n(0) {
95  initialize(N,x,f,Type);
96  };
97 
98 
109  EGS_AliasTable(EGS_Float xmin, EGS_Float xmax, EGS_Float accu, int nmax,
110  EGS_AtFunction func, void *data) : n(0) {
111  initialize(xmin,xmax,accu,nmax,func,data);
112  };
113 
118  void initialize(int N, const EGS_Float *x, const EGS_Float *f,
119  int Type = 1);
120 
130  int initialize(EGS_Float xmin, EGS_Float xmax, EGS_Float accu, int nmax,
131  EGS_AtFunction func, void *data);
132 
134  EGS_Float sample(EGS_RandomGenerator *rndm) const;
135 
137  int sampleBin(EGS_RandomGenerator *rndm) const;
138 
141  EGS_Float getAverage() const {
142  return average;
143  };
144 
146  EGS_Float getMaximum() const {
147  return xi[n-1];
148  };
149 
150 private:
151 
152  int n;
153  int np;
154  EGS_Float *fi;
155  EGS_Float *xi;
156  EGS_Float *wi;
157  EGS_Float average;
158  int *bin;
159  int type;
162  void copy(const EGS_AliasTable &t);
163  void clear();
164  void allocate(int N, int Type);
165  void make();
166 
167 
168 };
169 
183 
184 public:
185 
191  EGS_SimpleAliasTable(int N, const EGS_Float *f);
192 
195 
200  int sample(EGS_RandomGenerator *rndm) const {
201  int bin = (int)(rndm->getUniform()*n);
202  return rndm->getUniform() < wi[bin] ? bin : bins[bin];
203  };
204 
205 private:
206 
207  int n;
208  EGS_Float *wi;
209  int *bins;
210 
211 };
212 
213 
214 #endif
215 
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
A class for sampling random values from a given probability distribution using the alias table techni...
EGS_Float getAverage() const
Get the average of the probability distribution represented by this alias table object.
int sample(EGS_RandomGenerator *rndm) const
Sample a random bin.
Base random number generator class. All random number generators should be derived from this class...
Definition: egs_rndm.h:67
EGS_RandomGenerator class header file.
A class for sampling random bins from a given probability distribution using the alias table techniqu...
EGS_Float getUniform()
Returns a random number uniformly distributed between zero (inclusive) and 1 (exclusive).
Definition: egs_rndm.h:103
EGS_AliasTable()
Construct an empty (uninitialized) alias table.
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...
Defines the EGS_EXPORT and EGS_LOCAL macros.
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...
EGS_AliasTable(const EGS_AliasTable &t)
Copy constructor. Performs a deep copy.
EGS_Float getMaximum() const
Get the maximum abscissa of this alias table object.