00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "TUnuranMultiContDist.h"
00014 #include "Math/WrappedMultiTF1.h"
00015
00016 #include "TF1.h"
00017 #include <cassert>
00018
00019
00020 TUnuranMultiContDist::TUnuranMultiContDist (const ROOT::Math::IMultiGenFunction & pdf, bool isLogPdf) :
00021 fPdf(&pdf),
00022 fIsLogPdf(isLogPdf),
00023 fOwnFunc(false)
00024 {
00025
00026 }
00027
00028
00029 TUnuranMultiContDist::TUnuranMultiContDist (TF1 * func, unsigned int dim, bool isLogPdf) :
00030 fPdf( new ROOT::Math::WrappedMultiTF1( *func, dim) ),
00031 fIsLogPdf(isLogPdf),
00032 fOwnFunc(true)
00033 {
00034
00035 }
00036
00037
00038
00039 TUnuranMultiContDist::TUnuranMultiContDist(const TUnuranMultiContDist & rhs) :
00040 TUnuranBaseDist(),
00041 fPdf(0)
00042 {
00043
00044 operator=(rhs);
00045 }
00046
00047 TUnuranMultiContDist & TUnuranMultiContDist::operator = (const TUnuranMultiContDist &rhs)
00048 {
00049
00050 if (this == &rhs) return *this;
00051 fXmin = rhs.fXmin;
00052 fXmax = rhs.fXmax;
00053 fMode = rhs.fMode;
00054 fIsLogPdf = rhs.fIsLogPdf;
00055 fOwnFunc = rhs.fOwnFunc;
00056 if (!fOwnFunc)
00057 fPdf = rhs.fPdf;
00058 else {
00059 if (fPdf) delete fPdf;
00060 fPdf = (rhs.fPdf) ? rhs.fPdf->Clone() : 0;
00061 }
00062 return *this;
00063 }
00064
00065 TUnuranMultiContDist::~TUnuranMultiContDist() {
00066
00067 if (fOwnFunc && fPdf) delete fPdf;
00068 }
00069
00070
00071 double TUnuranMultiContDist::Pdf ( const double * x) const {
00072
00073 assert(fPdf != 0);
00074 return (*fPdf)(x);
00075 }
00076
00077
00078 void TUnuranMultiContDist::Gradient( const double * x, double * grad) const {
00079
00080
00081 unsigned int ndim = NDim();
00082 for (unsigned int i = 0; i < ndim; ++i)
00083 grad[i] = Derivative(x,i);
00084
00085 return;
00086 }
00087
00088 double TUnuranMultiContDist::Derivative( const double * x, int coord) const {
00089
00090
00091
00092
00093
00094 assert(fPdf != 0);
00095
00096 double h = 0.001;
00097
00098 std::vector<double> xx(NDim() );
00099
00100 xx[coord] = x[coord]+h; double f1 = (*fPdf)(&xx.front());
00101 xx[coord] = x[coord]-h; double f2 = (*fPdf)(&xx.front());
00102
00103 xx[coord] = x[coord]+h/2; double g1 = (*fPdf)(&xx.front());
00104 xx[coord] = x[coord]-h/2; double g2 = (*fPdf)(&xx.front());
00105
00106
00107 double h2 = 1/(2.*h);
00108 double d0 = f1 - f2;
00109 double d2 = 2*(g1 - g2);
00110
00111 double deriv = h2*(4*d2 - d0)/3.;
00112 return deriv;
00113 }
00114
00115
00116