00001 // @(#)root/mathmore:$Id: GSLMultiMinFunctionAdapter.h 20882 2007-11-19 11:31:26Z rdm $ 00002 // Authors: L. Moneta, 12/2006 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 GSLMultiMinFunctionAdapter 00026 // 00027 // Generic adapter for gsl_multimin_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_GSLMultiMinFunctionAdapter 00035 #define ROOT_Math_GSLMultiMinFunctionAdapter 00036 00037 #include "gsl/gsl_vector.h" 00038 00039 namespace ROOT { 00040 namespace Math { 00041 00042 00043 00044 00045 /** 00046 Class for adapting any multi-dimension C++ functor class to C function pointers used by 00047 GSL MultiMin algorithms. 00048 The templated C++ function class must implement: 00049 00050 <em> double operator( const double * x)</em> 00051 and if the derivatives are required: 00052 <em> void Gradient( const double * x, double * g)</em> 00053 00054 This class defines static methods with will be used to fill the 00055 \a gsl_multimin_function and 00056 \a gsl_multimin_function_fdf structs used by GSL. 00057 See for examples the 00058 <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Providing-a-function-to-minimize.html#Providing-a-function-to-minimize">GSL online manual</A> 00059 00060 @ingroup MultiMin 00061 00062 */ 00063 00064 00065 template<class UserFunc> 00066 struct GSLMultiMinFunctionAdapter { 00067 00068 static double F( const gsl_vector * x, void * p) { 00069 00070 UserFunc * function = reinterpret_cast< UserFunc *> (p); 00071 // get pointer to data from gsl_vector 00072 return (*function)( x->data ); 00073 } 00074 00075 00076 static void Df( const gsl_vector * x, void * p, gsl_vector * g) { 00077 00078 UserFunc * function = reinterpret_cast< UserFunc *> (p); 00079 (*function).Gradient( x->data, g->data ); 00080 00081 } 00082 00083 static void Fdf( const gsl_vector * x, void * p, double *f, gsl_vector * g ) { 00084 00085 UserFunc * function = reinterpret_cast< UserFunc *> (p); 00086 // *f = (*function) ( x ); 00087 // *df = (*function).Gradient( x ); 00088 00089 (*function).FdF( x->data, *f, g->data); 00090 } 00091 00092 }; 00093 00094 00095 } // namespace Math 00096 } // namespace ROOT 00097 00098 00099 #endif /* ROOT_Math_GSLMultiMinFunctionAdapter */