TUnuranMultiContDist.cxx

Go to the documentation of this file.
00001 // @(#)root/unuran:$Id: TUnuranMultiContDist.cxx 35517 2010-09-21 09:43:15Z moneta $
00002 // Authors: L. Moneta, J. Leydold Wed Feb 28 2007
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Implementation file for class TUnuranMultiContDist
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    //Constructor from generic function interfaces
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    //Constructor from a TF1 objects
00035 } 
00036 
00037 
00038 
00039 TUnuranMultiContDist::TUnuranMultiContDist(const TUnuranMultiContDist & rhs) : 
00040    TUnuranBaseDist(), 
00041    fPdf(0)
00042 {
00043    // Implementation of copy ctor using assignment operator
00044    operator=(rhs);
00045 }
00046 
00047 TUnuranMultiContDist & TUnuranMultiContDist::operator = (const TUnuranMultiContDist &rhs) 
00048 {
00049    // Implementation of assignment operator (copy only the function pointer not the function itself)
00050    if (this == &rhs) return *this;  // time saving self-test
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    // destructor implementation
00067    if (fOwnFunc && fPdf) delete fPdf; 
00068 }
00069 
00070 
00071 double TUnuranMultiContDist::Pdf ( const double * x) const {  
00072    // evaluate the distribution 
00073    assert(fPdf != 0);
00074    return (*fPdf)(x); 
00075 }
00076 
00077 
00078 void TUnuranMultiContDist::Gradient( const double * x, double * grad) const { 
00079    // do numerical derivation and return gradient in vector grad
00080    // grad must point to a vector of at least ndim size
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     // do numerical derivation of gradient using 5 point rule
00090    // use 5 point rule 
00091 
00092    //double eps = 0.001; 
00093    //const double kC1 = 8*std::numeric_limits<double>::epsilon();
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    //compute the central differences
00107    double h2    = 1/(2.*h);
00108    double d0    = f1 - f2;
00109    double d2    = 2*(g1 - g2);
00110    //double error  = kC1*h2*fx;  //compute the error
00111    double deriv = h2*(4*d2 - d0)/3.;  
00112    return deriv;
00113 }
00114 
00115 
00116 

Generated on Tue Jul 5 14:43:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1