npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
GridRandomizer.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_GRIDRANDOMIZER_HH_
2 #define NPSTAT_GRIDRANDOMIZER_HH_
3 
4 /*!
5 // \file GridRandomizer.hh
6 //
7 // \brief Generate random numbers according to a multivariate distribution
8 // tabulated on a grid
9 //
10 // Author: I. Volobouev
11 //
12 // March 2010
13 */
14 
15 #include "npstat/nm/ArrayND.hh"
16 #include "npstat/nm/BoxND.hh"
17 
18 namespace npstat {
19  /**
20  // Class for generating random numbers according to a multivariate
21  // distribution tabulated on a rectangular grid.
22  */
24  {
25  public:
26  /**
27  // Main constructor. "interpolationDegree" may be either 0 or 1.
28  // The function values are assumed to be given in the centers
29  // of the bins. The density represented by "gridData" will be
30  // internally normalized using grid boundaries to determine the
31  // support of the density.
32  */
33  template <class Array>
34  inline GridRandomizer(const Array& gridData,
35  const BoxND<double>& gridBoundary,
36  const unsigned interpolationDegree)
37  : grid_(gridData), boundary_(gridBoundary),
38  iDeg_(interpolationDegree), dim_(gridData.rank()) {initialize();}
39 
41  GridRandomizer& operator=(const GridRandomizer&);
42 
43  inline ~GridRandomizer() {clearCdfs();}
44 
45  bool operator==(const GridRandomizer& r) const;
46  inline bool operator!=(const GridRandomizer& r) const
47  {return !(*this == r);}
48 
49  //@{
50  /** Inspect the properties of this object */
51  inline const ArrayND<double>& gridData() const {return grid_;}
52  inline const BoxND<double>& gridBoundary() const {return boundary_;}
53  inline unsigned interpolationDegree() const {return iDeg_;}
54  inline unsigned dim() const {return dim_;}
55  //@}
56 
57  /** Probability density */
58  double density(const double* x, unsigned xLen) const;
59 
60  /**
61  // Generate random numbers according to the density provided
62  // in the constructor.
63  //
64  // In this method, "bufLen" should not necessarily be equal
65  // to the grid dimensionality. When "bufLen" is smaller,
66  // the random numbers are efficiently generated for dimensions
67  // 0, 1, ..., bufLen - 1. When "bufLen" is larger, only the
68  // first "dim()" elements of the "resultBuf" array are filled.
69  */
70  void generate(const double* uniformRandomInput, unsigned bufLen,
71  double* resultBuf) const;
72  private:
74 
75  void initialize();
76  void clearCdfs();
77  void copyCdfs(const GridRandomizer&);
78 
79  void generateFlat(const double* randNum, unsigned bufLen,
80  double* result) const;
81  void generateInterpolated(const double* randNum, unsigned bufLen,
82  double* result) const;
83  ArrayND<double> grid_;
84  BoxND<double> boundary_;
85  unsigned iDeg_;
86  unsigned dim_;
87 
88  std::vector<ArrayND<double>*> cdfs_;
89  std::vector<ArrayND<double>*> norms_;
90  std::vector<double> binwidth_;
91  double densityNorm_;
92 
93  mutable std::vector<unsigned> genBins_;
94  mutable std::vector<double> work_;
95  mutable std::vector<double> memoizeIn_;
96  mutable std::vector<double> memoizeOut_;
97  };
98 }
99 
100 #endif // NPSTAT_GRIDRANDOMIZER_HH_
Arbitrary-dimensional array template.
Template to represent rectangles, boxes, and hyperboxes.
Definition: GridRandomizer.hh:24
double density(const double *x, unsigned xLen) const
void generate(const double *uniformRandomInput, unsigned bufLen, double *resultBuf) const
const ArrayND< double > & gridData() const
Definition: GridRandomizer.hh:51
GridRandomizer(const Array &gridData, const BoxND< double > &gridBoundary, const unsigned interpolationDegree)
Definition: GridRandomizer.hh:34
Definition: AbsArrayProjector.hh:14