UnuranDistr.h

Go to the documentation of this file.
00001 // @(#)root/unuran:$Id: UnuranDistr.h 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: L. Moneta Wed Sep 27 11:22:07 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Header file for class UnuranDistr
00012 
00013 #ifndef ROOT_Math_UnuranDistr
00014 #define ROOT_Math_UnuranDistr
00015 
00016 #include "unuran.h"
00017 #include <iostream>
00018 
00019 #include <cmath>
00020 
00021 /** 
00022    UnuranDistr 
00023    Provides free function based on TF1 to be called by unuran 
00024 */ 
00025 
00026 
00027 template<class Function> 
00028 struct UnuranDistr {
00029 
00030    /// evaluate the probal
00031    static double Pdf(double x, const UNUR_DISTR * dist) {  
00032       const Function * func = reinterpret_cast<const Function *> (  unur_distr_get_extobj(dist) ); 
00033       return func->operator()(x);      
00034    }   
00035 
00036    static double Dpdf(double x,  const UNUR_DISTR * dist) { 
00037       const Function * func = reinterpret_cast<const Function *> (  unur_distr_get_extobj(dist) ); 
00038       return func->Derivative(x);            
00039    }
00040 
00041    static double Cdf(double x,  const UNUR_DISTR * dist) { 
00042       const Function * func = reinterpret_cast<const Function *> (  unur_distr_get_extobj(dist) ); 
00043       return func->Cdf(x);            
00044    }
00045 
00046 }; 
00047 
00048 /**
00049    free functions for multidimensional functions
00050    needed bby UNURAN
00051 */
00052 template<class Function> 
00053 struct UnuranDistrMulti {
00054 
00055    /// evaluate the probality density function
00056    static double Pdf(const double * x, UNUR_DISTR * dist) {  
00057       const Function * func = reinterpret_cast<const Function *> (  unur_distr_get_extobj(dist) ); 
00058       assert( func != 0); 
00059       return func->operator()(x);      
00060    }   
00061 
00062 
00063    static int Dpdf(double * grad, const double * x,  UNUR_DISTR * dist) { 
00064       const Function * func = reinterpret_cast<const Function *> (  unur_distr_get_extobj(dist) ); 
00065       func->Gradient(x,grad);
00066       return 0; 
00067    }
00068    
00069    // provides the gradient componets separatly
00070    static double Pdpdf(const double * x, int coord, UNUR_DISTR * dist) { 
00071       const Function * func = reinterpret_cast<const Function *> (  unur_distr_get_extobj(dist) ); 
00072       return func->Gradient(x,coord);
00073    }
00074 
00075    static double Logpdf(const double * x, UNUR_DISTR * dist) {  
00076 //       const Function * func = reinterpret_cast<const Function *> (  unur_distr_get_extobj(dist) ); 
00077 //       assert( func != 0); 
00078 //       double y  =  func->operator()(x);      
00079 // //       if ( y < std::numeric_limits<double>::min() ) 
00080       return  std::log( Pdf(x,dist) ); 
00081    }   
00082 
00083    static int Dlogpdf(double * grad, const double * x,  UNUR_DISTR * dist) {    
00084       int dim = unur_distr_get_dim(dist);
00085       double pdf = Pdf(x,dist); 
00086       int ret = Dpdf(grad,x,dist); 
00087       for (int i = 0; i < dim; ++i) { 
00088          grad[i] /= pdf; 
00089       }
00090       return ret; 
00091    }
00092 
00093    static double Pdlogpdf(const double * x, int coord, UNUR_DISTR * dist) { 
00094       return  Pdpdf(x,coord,dist)/ Pdf(x,dist);
00095    }
00096 
00097 }; 
00098 
00099 
00100 
00101 #endif /* ROOT_Math_UnuranDistr */

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