GSLFunctionAdapter.h

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: GSLFunctionAdapter.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 GSLFunctionAdapter
00026 //
00027 // Generic adapter for gsl_function signature 
00028 // usable for any c++ class which defines operator( ) 
00029 // 
00030 // Created by: Lorenzo Moneta  at Fri Nov 12 16:58:51 2004
00031 // 
00032 // Last update: Fri Nov 12 16:58:51 2004
00033 // 
00034 #ifndef ROOT_Math_GSLFunctionAdapter
00035 #define ROOT_Math_GSLFunctionAdapter
00036 
00037 
00038 namespace ROOT {
00039 namespace Math {
00040 
00041   /**
00042      Function pointer corresponding to gsl_function signature
00043    */
00044 
00045   typedef double ( * GSLFuncPointer ) ( double, void *);
00046 
00047 
00048   /**
00049      Class for adapting any C++ functor class to C function pointers used by GSL. 
00050      The templated C++ function class must implement: 
00051 
00052     <em> double operator( double x)</em>
00053     and if the derivatives are required: 
00054     <em> double Gradient( double x)</em>
00055     
00056     This class defines static methods with will be used to fill the 
00057     \a gsl_function and \a gsl_function_fdf structs used by GSL. 
00058     See for examples the <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_32.html#SEC432">GSL online manual</A>
00059   */ 
00060      
00061 
00062   template<class UserFunc> 
00063   class GSLFunctionAdapter {
00064 
00065   public: 
00066 
00067     GSLFunctionAdapter() {}
00068     virtual ~GSLFunctionAdapter() {} 
00069     
00070     static double F( double x, void * p) { 
00071       
00072       UserFunc * function = reinterpret_cast< UserFunc *> (p);       
00073       return (*function)( x ); 
00074     }
00075 
00076     
00077     static double Df( double x, void * p) { 
00078       
00079       UserFunc * function = reinterpret_cast< UserFunc *> (p);       
00080       return (*function).Derivative( x ); 
00081     }
00082 
00083     static void Fdf( double x, void * p, double *f, double *df ) { 
00084       
00085       UserFunc * function = reinterpret_cast< UserFunc *> (p);      
00086       *f  = (*function) ( x ); 
00087       *df = (*function).Derivative( x ); 
00088     }
00089 
00090   };
00091 
00092 
00093 } // namespace Math
00094 } // namespace ROOT
00095 
00096 
00097 #endif /* ROOT_Math_GSLFunctionAdapter */

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