00001 00012 #ifdef _MSC_VER 00013 #include "msdevstudio/MSconfig.h" 00014 #endif 00015 00016 #include "StatedFCN.h" 00017 00018 #include "functions/FunctionBase.h" 00019 00020 #include <stdexcept> 00021 00022 using std::string; 00023 using std::vector; 00024 00025 using namespace hippodraw; 00026 00027 StatedFCN:: 00028 StatedFCN ( ) 00029 : m_function ( 0 ), 00030 m_needs_derivs ( false ) 00031 { 00032 } 00033 00034 StatedFCN:: 00035 StatedFCN ( const StatedFCN & fcn ) 00036 : FCNBase (), 00037 m_function ( fcn.m_function ), 00038 m_needs_derivs ( fcn.m_needs_derivs ) 00039 { 00040 } 00041 00042 void 00043 StatedFCN:: 00044 copyFrom ( const StatedFCN * other ) 00045 { 00046 // m_fixed_flags = other -> m_fixed_flags; 00047 m_function = other -> m_function; 00048 00049 } 00050 00051 bool 00052 StatedFCN:: 00053 hasFunction () const 00054 { 00055 return m_function != 0; 00056 } 00057 00061 void 00062 StatedFCN:: 00063 setFunction ( FunctionBase * function ) 00064 { 00065 bool yes = isCompatible ( function ); 00066 if ( yes == false ) { 00067 string what ( "StatedFCN: The function `" ); 00068 what += function -> name (); 00069 what += "' can not provide partial\n"; 00070 what += "derivatives needed by this fitter."; 00071 throw std::runtime_error ( what ); 00072 } 00073 00074 m_function = function; 00075 } 00076 00077 const vector < string > & 00078 StatedFCN:: 00079 getParmNames () const 00080 { 00081 return m_function -> parmNames (); 00082 } 00083 00084 const vector < double > & 00085 StatedFCN:: 00086 getParameters ( ) const 00087 { 00088 return m_function -> getParameters ( ); 00089 } 00090 00091 void 00092 StatedFCN:: 00093 setParameters ( const std::vector < double > & parms ) 00094 { 00095 m_function -> setParameters ( parms ); 00096 } 00097 00098 void 00099 StatedFCN:: 00100 fillFreeParameters ( std::vector < double > & free_parms ) const 00101 { 00102 free_parms.clear(); 00103 const vector < double > & parms = m_function -> getParameters (); 00104 const vector < int > & flags = m_function -> getFixedFlags (); 00105 unsigned int size = parms.size (); 00106 for ( unsigned int i = 0; i < size; i++ ) { 00107 if ( flags[ i ] == 0 ) { 00108 free_parms.push_back ( parms[ i ] ); 00109 } 00110 } 00111 } 00112 00113 unsigned int 00114 StatedFCN:: 00115 getNumberFreeParms () const 00116 { 00117 unsigned int number = 0; 00118 const vector < int > & flags = m_function -> getFixedFlags (); 00119 // unsigned int size = m_fixed_flags.size (); 00120 unsigned int size = flags.size (); 00121 for ( unsigned int i = 0; i < size; i++ ) { 00122 // if ( m_fixed_flags[i] == 0 ) number++; 00123 if ( flags[i] == 0 ) number++; 00124 } 00125 00126 return number; 00127 } 00128 00129 const vector < int > & 00130 StatedFCN:: 00131 getFixedFlags () const 00132 { 00133 return m_function -> getFixedFlags (); 00134 00135 } 00136 00137 void 00138 StatedFCN:: 00139 setFixedFlags ( const std::vector < int > & flags ) 00140 { 00141 // m_fixed_flags = flags; 00142 m_function -> setFixedFlags ( flags ); 00143 } 00144 00145 void 00146 StatedFCN:: 00147 setFreeParameters ( const std::vector < double > & free_parms ) 00148 { 00149 // make a copy... 00150 vector < double > parms = m_function -> getParameters (); 00151 const vector < int > & flags = m_function -> getFixedFlags (); 00152 unsigned int size = parms.size (); 00153 unsigned int j = 0; 00154 00155 for ( unsigned int i = 0; i < size; i++ ) { 00156 if ( flags [i] == 0 ) { 00157 parms[i] = free_parms[j]; 00158 j++; 00159 } 00160 } 00161 00162 m_function -> setParameters ( parms ); 00163 } 00164 00165 void 00166 StatedFCN:: 00167 fillFreeDerivatives ( std::vector < double > & derives, double x ) 00168 { 00169 derives.clear(); 00170 const vector < double > & parms = m_function -> getParameters (); 00171 const vector < int > & flags = m_function -> getFixedFlags (); 00172 unsigned int size = parms.size(); 00173 00174 for ( unsigned int i = 0; i < size; i++ ) { 00175 // if ( m_fixed_flags [i] == 0 ) { 00176 if ( flags [i] == 0 ) { 00177 double value = m_function -> derivByParm ( i, x ); 00178 derives.push_back ( value ); 00179 } 00180 } 00181 } 00182 00183 double 00184 StatedFCN:: 00185 operator () ( const std::vector < double > & parms ) const 00186 { 00187 m_function -> setParameters ( parms ); 00188 00189 return objectiveValue (); 00190 } 00191 00192 void 00193 StatedFCN:: 00194 setNeedsDerivatives ( bool yes ) 00195 { 00196 m_needs_derivs = yes; 00197 } 00198 00199 bool 00200 StatedFCN:: 00201 isCompatible ( const FunctionBase * function ) const 00202 { 00203 bool yes = true; 00204 if ( m_needs_derivs && 00205 ( function -> hasDerivatives () == false ) ) { 00206 yes = false; 00207 } 00208 00209 return yes; 00210 }