GSLFunctionWrapper.h

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: GSLFunctionWrapper.h 20882 2007-11-19 11:31:26Z rdm $
00002 // Authors: L. Moneta, A. Zsenei   08/2005 
00003 
00004  /**********************************************************************
00005   *                                                                    *
00006   * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
00007   *                                                                    *
00008   * This library is free software; you can redistribute it and/or      *
00009   * modify it under the terms of the GNU General Public License        *
00010   * as published by the Free Software Foundation; either version 2     *
00011   * of the License, or (at your option) any later version.             *
00012   *                                                                    *
00013   * This library is distributed in the hope that it will be useful,    *
00014   * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00015   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00016   * General Public License for more details.                           *
00017   *                                                                    *
00018   * You should have received a copy of the GNU General Public License  *
00019   * along with this library (see file COPYING); if not, write          *
00020   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00021   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00022   *                                                                    *
00023   **********************************************************************/
00024 
00025 // Header file for class GSLFunctionWrapper
00026 // 
00027 // Created by: moneta  at Sat Nov 13 14:54:41 2004
00028 // 
00029 // Last update: Sat Nov 13 14:54:41 2004
00030 // 
00031 #ifndef ROOT_Math_GSLFunctionWrapper
00032 #define ROOT_Math_GSLFunctionWrapper
00033 
00034 #include "gsl/gsl_math.h"
00035 
00036 #include "Math/GSLFunctionAdapter.h"
00037 
00038 #include <cassert>
00039 
00040 namespace ROOT {
00041 namespace Math {
00042 
00043 
00044 
00045 typedef double ( * GSLFuncPointer ) ( double, void *);
00046 typedef void ( * GSLFdfPointer ) ( double, void *, double *, double *);
00047 
00048 
00049 /**
00050    Wrapper class to the gsl_function C structure. 
00051    This class to fill the GSL C structure  gsl_function with 
00052    the C++ function objcet. 
00053    Use the class ROOT::Math::GSLFunctionAdapter to adapt the 
00054    C++ function object to the right signature (function pointer type) 
00055    requested by GSL 
00056 */
00057 class GSLFunctionWrapper { 
00058 
00059 public: 
00060 
00061    GSLFunctionWrapper() 
00062    {
00063       fFunc.function = 0; 
00064       fFunc.params = 0;
00065    }
00066 
00067    /// set in the GSL C struct the pointer to the function evaluation 
00068    void SetFuncPointer( GSLFuncPointer f) { fFunc.function = f; } 
00069 
00070    /// set in the GSL C struct the extra-object pointer
00071    void SetParams ( void * p) { fFunc.params = p; }
00072 
00073    /// fill the GSL C struct from a generic C++ callable object 
00074    /// implementing operator() 
00075    template<class FuncType> 
00076    void SetFunction(const FuncType &f) { 
00077       const void * p = &f;
00078       assert (p != 0); 
00079       SetFuncPointer(&GSLFunctionAdapter<FuncType >::F);
00080       SetParams(const_cast<void *>(p));
00081    }
00082     
00083    gsl_function * GetFunc() { return &fFunc; } 
00084 
00085    GSLFuncPointer FunctionPtr() { return fFunc.function; }
00086 
00087    // evaluate the function 
00088    double operator() (double x) {  return GSL_FN_EVAL(&fFunc, x); }
00089 
00090    /// check if function is valid (has been set) 
00091    bool IsValid() { 
00092       return (fFunc.function != 0) ? true : false;  
00093    }
00094 
00095 private: 
00096    gsl_function fFunc; 
00097 
00098 
00099 };
00100 
00101 
00102    /**
00103      class to wrap a gsl_function_fdf (with derivatives)   
00104    */
00105   class GSLFunctionDerivWrapper { 
00106 
00107   public: 
00108 
00109      GSLFunctionDerivWrapper() 
00110      {
00111         fFunc.f = 0; 
00112         fFunc.df = 0; 
00113         fFunc.fdf = 0; 
00114         fFunc.params = 0;
00115      }
00116 
00117 
00118     void SetFuncPointer( GSLFuncPointer f) { fFunc.f = f; } 
00119     void SetDerivPointer( GSLFuncPointer f) { fFunc.df = f; } 
00120     void SetFdfPointer( GSLFdfPointer f) { fFunc.fdf = f; } 
00121     void SetParams ( void * p) { fFunc.params = p; }
00122 
00123     
00124     gsl_function_fdf * GetFunc() { return &fFunc; } 
00125 
00126     // evaluate the function and derivatives
00127     double operator() (double x) {  return GSL_FN_FDF_EVAL_F(&fFunc, x); }
00128 
00129     double Derivative (double x) { return GSL_FN_FDF_EVAL_DF(&fFunc, x); } 
00130 
00131     void Fdf(double x, double & f, double & df) { 
00132       return GSL_FN_FDF_EVAL_F_DF(&fFunc, x, &f, &df);
00133     }
00134 
00135    /// check if function is valid (has been set) 
00136    bool IsValid() { 
00137       return (fFunc.f != 0 ) ? true : false;  
00138    }
00139 
00140   private: 
00141     gsl_function_fdf fFunc; 
00142 
00143   };
00144 
00145 
00146 
00147 } // namespace Math
00148 } // namespace ROOT
00149 
00150 #endif /* ROOT_Math_GSLFunctionWrapper */

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