Logo ROOT   6.08/07
Reference Guide
TUnuranMultiContDist.h
Go to the documentation of this file.
1 // @(#)root/unuran:$Id$
2 // Authors: L. Moneta, J. Leydold Wed Feb 28 2007
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class TUnuranMultiContDist
12 
13 #ifndef ROOT_Math_TUnuranMultiContDist
14 #define ROOT_Math_TUnuranMultiContDist
15 
16 #ifndef ROOT_Math_TUnuranBaseDist
17 #include "TUnuranBaseDist.h"
18 #endif
19 
20 #ifndef ROOT_Math_IFunction
21 #include "Math/IFunction.h"
22 #endif
23 
24 
25 
26 #include <vector>
27 
28 class TF1;
29 
30 
31 
32 /**
33  TUnuranMultiContDist class describing multi dimensional continuous distributions.
34  It is used by TUnuran to generate a set of random numbers according to this distribution via
35  TUnuran::Sample(double *).
36  The class can be constructed from a multi-dimensional function (TF1 pointer, which can be actually also a
37  TF2 or a TF3).
38  It provides a method to set the domain of the distribution ( SetDomain ) which will correspond to the range
39  of the generated random numbers. By default the domain is [(-inf,-inf,...)(+inf,+inf,...)], indipendently of the
40  range set in the TF1 class used to construct the distribution.
41 
42  The derivatives of the pdf which are used by some UNURAN methods are estimated numerically in the
43  Derivative() method. Some extra information (like distribution mode) can be set using SetMode.
44  Some methods require instead of the pdf the log of the pdf.
45  This can also be controlled by setting a flag when constructing this class.
46 
47  \ingroup Unuran
48 
49 */
50 
52 
53 public:
54 
55 
56  /**
57  Constructor from a TF1 object representing the Probability density function.
58  The derivatives of the Pdf are estimated, when required by the UNURAN algorithm,
59  using numerical derivation.
60  If a value of dim 0 is passed , the dimension of the function is taken from TF1::GetNdim().
61  This works only for 2D and 3D (for TF2 and TF3 objects).
62  */
63  TUnuranMultiContDist (TF1 * func = 0, unsigned int dim = 0, bool isLogPdf = false);
64 
65 
66  /**
67  Constructor as before but from a generic function object interface for multi-dim functions
68  */
69  TUnuranMultiContDist (const ROOT::Math::IMultiGenFunction & pdf, bool isLogPdf = false);
70 
71  /**
72  Destructor
73  */
74  virtual ~TUnuranMultiContDist ();
75 
76 
77  /**
78  Copy constructor
79  */
81 
82  /**
83  Assignment operator
84  */
86 
87  /**
88  Clone (required by base class)
89  */
90  virtual TUnuranMultiContDist * Clone() const { return new TUnuranMultiContDist(*this); }
91 
92 
93  /**
94  get number of dimension of the distribution
95  */
96  unsigned int NDim() const {
97  return fPdf->NDim();
98  }
99 
100  /**
101  set the domain of the distribution giving an array of minimum and maximum values
102  By default otherwise the domain is undefined, i.e. is [-inf,+inf]
103  To remove the domain do a SetDomain(0,0).
104  There is no possibility to have a domain defined in only one coordinate. Use instead inf or DOUBLE_MAX to
105  specify un infinite domain in that coordinate
106  */
107  void SetDomain(const double *xmin, const double *xmax) {
108  if (xmin == 0 || xmax == 0) return;
109  fXmin = std::vector<double>(xmin,xmin+NDim());
110  fXmax = std::vector<double>(xmax,xmax+NDim());
111  }
112 
113  /**
114  set the mode of the distribution (coordinates of the distribution maximum values)
115  */
116  void SetMode(const double * x) {
117  fMode = std::vector<double>(x,x+NDim());
118  }
119 
120  /**
121  get the distribution lower domain values. Return a null pointer if domain is not defined
122  */
123  const double * GetLowerDomain() const {
124  if (fXmin.size() == 0 || ( fXmin.size() != fXmax.size() ) ) return 0;
125  return &fXmin[0];
126  }
127  /**
128  get the distribution upper domain values. Return a null pointer if domain is not defined
129  */
130  const double * GetUpperDomain() const {
131  if (fXmax.size() == 0 || ( fXmin.size() != fXmax.size() ) ) return 0;
132  return &fXmax[0];
133  }
134 
135 
136  /**
137  get the mode (vector of coordinate positions of the maxima of the distribution)
138  If a mode has not defined return a NULL pointer
139  */
140  const double * GetMode( ) const {
141  if (fMode.size() == 0 ) return 0;
142  return &fMode.front();
143  }
144 
145 
146  /**
147  flag to control if given function represent the log of a pdf
148  */
149  bool IsLogPdf() const { return fIsLogPdf; }
150 
151  /**
152  evaluate the probability density function, used by UnuRan
153  */
154  double Pdf ( const double * x) const;
155 
156  /**
157  evaluate the gradient vector of the Pdf. Used by UnuRan
158  */
159  void Gradient( const double * x, double * grad) const;
160 
161  /**
162  evaluate the partial derivative for the given coordinate. Used by UnuRan
163  */
164  double Derivative( const double * x, int icoord) const;
165 
166 
167 
168 private:
169 
170  const ROOT::Math::IMultiGenFunction * fPdf; //pointer to the pdf
171 
172  std::vector<double> fXmin; //vector with lower x values of the domain
173  std::vector<double> fXmax; //vector with upper x values of the domain
174  std::vector<double> fMode; //vector representing the x coordinates of the maximum of the pdf
175 
176  bool fIsLogPdf; //flag to control if function pointer represent log of pdf
177  bool fOwnFunc; // flag to indicate if class manages the function pointers
178 
179 
180  ClassDef(TUnuranMultiContDist,1) //Wrapper class for multi dimensional continuous distribution
181 
182 
183 };
184 
185 
186 
187 #endif /* ROOT_Math_TUnuranMultiContDist */
TUnuranBaseDist, base class for Unuran distribution classees such as TUnuranContDist (for one-dimensi...
float xmin
Definition: THbookFile.cxx:93
double Pdf(const double *x) const
evaluate the probability density function, used by UnuRan
const double * GetMode() const
get the mode (vector of coordinate positions of the maxima of the distribution) If a mode has not def...
TUnuranMultiContDist(TF1 *func=0, unsigned int dim=0, bool isLogPdf=false)
Constructor from a TF1 object representing the Probability density function.
const double * GetLowerDomain() const
get the distribution lower domain values.
unsigned int NDim() const
get number of dimension of the distribution
std::vector< double > fMode
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual TUnuranMultiContDist * Clone() const
Clone (required by base class)
virtual ~TUnuranMultiContDist()
Destructor.
const ROOT::Math::IMultiGenFunction * fPdf
bool IsLogPdf() const
flag to control if given function represent the log of a pdf
TUnuranMultiContDist & operator=(const TUnuranMultiContDist &rhs)
Assignment operator.
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
void SetDomain(const double *xmin, const double *xmax)
set the domain of the distribution giving an array of minimum and maximum values By default otherwise...
float xmax
Definition: THbookFile.cxx:93
TUnuranMultiContDist class describing multi dimensional continuous distributions. ...
double func(double *x, double *p)
Definition: stressTF1.cxx:213
const double * GetUpperDomain() const
get the distribution upper domain values.
double Derivative(const double *x, int icoord) const
evaluate the partial derivative for the given coordinate.
1-Dim function class
Definition: TF1.h:149
std::vector< double > fXmin
void SetMode(const double *x)
set the mode of the distribution (coordinates of the distribution maximum values) ...
std::vector< double > fXmax
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
void Gradient(const double *x, double *grad) const
evaluate the gradient vector of the Pdf.