00001 // @(#)root/mathmore:$Id: GSLMultiMinFunctionWrapper.h 30749 2009-10-15 16:33:04Z brun $ 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 GSLMultiMinFunctionWrapper 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_GSLMultiMinFunctionWrapper 00032 #define ROOT_Math_GSLMultiMinFunctionWrapper 00033 00034 #include "gsl/gsl_multimin.h" 00035 00036 #include "GSLMultiMinFunctionAdapter.h" 00037 00038 00039 #include <cassert> 00040 00041 namespace ROOT { 00042 namespace Math { 00043 00044 00045 00046 typedef double ( * GSLMultiMinFuncPointer ) ( const gsl_vector *, void *); 00047 typedef void ( * GSLMultiMinDfPointer ) ( const gsl_vector *, void *, gsl_vector *); 00048 typedef void ( * GSLMultiMinFdfPointer ) ( const gsl_vector *, void *, double *, gsl_vector *); 00049 00050 00051 /** 00052 wrapper to a multi-dim function withtout derivatives for multi-dimensional 00053 minimization algorithm 00054 00055 @ingroup MultiMin 00056 */ 00057 00058 class GSLMultiMinFunctionWrapper { 00059 00060 public: 00061 00062 GSLMultiMinFunctionWrapper() 00063 { 00064 fFunc.f = 0; 00065 fFunc.n = 0; 00066 fFunc.params = 0; 00067 } 00068 00069 void SetFuncPointer( GSLMultiMinFuncPointer f) { fFunc.f = f; } 00070 void SetDim ( unsigned int n ) { fFunc.n = n; } 00071 void SetParams ( void * p) { fFunc.params = p; } 00072 00073 /// Fill gsl function structure from a C++ Function class 00074 template<class FuncType> 00075 void SetFunction(const FuncType &f) { 00076 const void * p = &f; 00077 assert (p != 0); 00078 SetFuncPointer(&GSLMultiMinFunctionAdapter<FuncType >::F); 00079 SetDim( f.NDim() ); 00080 SetParams(const_cast<void *>(p)); 00081 } 00082 00083 gsl_multimin_function * GetFunc() { return &fFunc; } 00084 00085 bool IsValid() { 00086 return (fFunc.f != 0) ? true : false; 00087 } 00088 00089 00090 private: 00091 00092 gsl_multimin_function fFunc; 00093 00094 }; 00095 00096 00097 /** 00098 Wrapper for a multi-dimensional function with derivatives used in GSL multidim 00099 minimization algorithm 00100 00101 @ingroup MultiMin 00102 00103 */ 00104 class GSLMultiMinDerivFunctionWrapper { 00105 00106 public: 00107 00108 GSLMultiMinDerivFunctionWrapper() 00109 { 00110 fFunc.f = 0; 00111 fFunc.df = 0; 00112 fFunc.fdf = 0; 00113 fFunc.n = 0; 00114 fFunc.params = 0; 00115 } 00116 00117 00118 void SetFuncPointer( GSLMultiMinFuncPointer f) { fFunc.f = f; } 00119 void SetDerivPointer( GSLMultiMinDfPointer f) { fFunc.df = f; } 00120 void SetFdfPointer( GSLMultiMinFdfPointer f) { fFunc.fdf = f; } 00121 void SetDim ( unsigned int n ) { fFunc.n = n; } 00122 void SetParams ( void * p) { fFunc.params = p; } 00123 00124 /// Fill gsl function structure from a C++ Function class 00125 template<class FuncType> 00126 void SetFunction(const FuncType &f) { 00127 const void * p = &f; 00128 assert (p != 0); 00129 SetFuncPointer(&GSLMultiMinFunctionAdapter<FuncType >::F); 00130 SetDerivPointer(&GSLMultiMinFunctionAdapter<FuncType >::Df); 00131 SetFdfPointer(&GSLMultiMinFunctionAdapter<FuncType >::Fdf); 00132 SetDim( f.NDim() ); 00133 SetParams(const_cast<void *>(p)); 00134 } 00135 00136 gsl_multimin_function_fdf * GetFunc() { return &fFunc; } 00137 00138 #ifdef NEEDED_LATER 00139 // evaluate the function 00140 double operator() (const double * x) { 00141 // vx must be a gsl_vector 00142 return GSL_MULTIMIN_FN_EVAL(&fFunc, vx); 00143 } 00144 #endif 00145 00146 /// check if function is valid (has been set) 00147 bool IsValid() { 00148 return (fFunc.f != 0) ? true : false; 00149 } 00150 00151 private: 00152 00153 gsl_multimin_function_fdf fFunc; 00154 00155 }; 00156 00157 00158 00159 00160 } // namespace Math 00161 } // namespace ROOT 00162 00163 #endif /* ROOT_Math_GSLMultiMinFunctionWrapper */