00001 00012 #ifdef HAVE_CONFIG_H 00013 #include "config.h" 00014 #else 00015 #ifdef _MSC_VER 00016 #include "msdevstudio/MSconfig.h" 00017 #endif 00018 #endif 00019 00020 #include "LinearSumFunction.h" 00021 00022 #ifdef SSTREAM_DEFECT 00023 #include <strstream> 00024 using std::ostrstream; 00025 #else 00026 #include <sstream> 00027 using std::ostringstream; 00028 #endif 00029 00030 #include <cassert> 00031 00032 #ifdef ITERATOR_MEMBER_DEFECT 00033 using namespace std; 00034 #else 00035 using std::vector; 00036 using std::string; 00037 #endif 00038 00039 LinearSumFunction::LinearSumFunction ( ) 00040 { 00041 initialize (); 00042 } 00043 00044 LinearSumFunction::LinearSumFunction ( const LinearSumFunction & old ) 00045 { 00046 FunctionList_t::const_iterator it = old.m_functions.begin(); 00047 while ( it != old.m_functions.end() ) { 00048 FunctionBase * function = (*it)->clone (); 00049 m_functions.push_back ( function ); 00050 } 00051 00052 initialize (); 00053 } 00054 00055 void LinearSumFunction::initialize () 00056 { 00057 m_name = "Linear Sum"; 00058 } 00059 00060 FunctionBase * LinearSumFunction::clone () const 00061 { 00062 00063 return new LinearSumFunction ( *this ); 00064 } 00065 00066 const vector < string > & LinearSumFunction::parmNames() const 00067 { 00068 LinearSumFunction * self = const_cast < LinearSumFunction * > ( this ); 00069 00070 self->m_parm_names.clear(); 00071 00072 unsigned int f_size = m_functions.size (); 00073 for ( unsigned int i = 0; i < f_size; i++ ) { 00074 #ifdef SSTREAM_DEFECT 00075 ostrstream strm_text; 00076 #else 00077 std::ostringstream strm_text; 00078 #endif 00079 strm_text << "-" << i << std::ends; 00080 00081 const vector< string > & names = m_functions[i] -> parmNames (); 00082 unsigned int n_size = names.size (); 00083 for ( unsigned int j = 0; j < n_size; j++ ) { 00084 string name = names [j]; 00085 name += strm_text.str(); 00086 self -> m_parm_names.push_back ( name ); 00087 } 00088 } 00089 00090 return m_parm_names; 00091 } 00092 00093 const vector<double> & LinearSumFunction::getParameters () 00094 { 00095 m_parms.clear (); 00096 FunctionList_t::const_iterator it = m_functions.begin (); 00097 for ( ; it != m_functions.end (); ++it ) { 00098 const vector<double> & vals = (*it)->getParameters (); 00099 m_parms.insert ( m_parms.end (), vals.begin(), vals.end () ); 00100 } 00101 return m_parms; 00102 } 00103 00104 00105 vector< double >::const_iterator 00106 LinearSumFunction:: 00107 setParameters ( std::vector< double >::const_iterator it ) 00108 { 00109 FunctionList_t::iterator fit = m_functions.begin(); 00110 00111 for ( ;fit != m_functions.end (); ++fit ) { 00112 it = (*fit)->setParameters ( it ); 00113 } 00114 00115 return it; 00116 } 00117 00118 00119 double LinearSumFunction::derivByParm ( int index, double x ) const 00120 { 00121 double value = 0; 00122 unsigned int numf = m_functions.size (); 00123 for ( unsigned int i = 0; i < numf; i++ ) { 00124 int size = m_functions [i] -> size (); 00125 if ( index < size ) { 00126 value = m_functions [i] -> derivByParm ( index, x ); 00127 break; 00128 } 00129 else { 00130 index -= size; 00131 } 00132 } 00133 00134 return value; 00135 } 00136 00137 int LinearSumFunction::count () 00138 { 00139 return m_functions.size(); 00140 } 00141 00142 int 00143 LinearSumFunction:: 00144 size () const 00145 { 00146 int number = 0; 00147 unsigned int numf = m_functions.size (); 00148 00149 for ( unsigned int i = 0; i < numf; i++ ) { 00150 const FunctionBase * function = m_functions [i]; 00151 number += function -> size (); 00152 } 00153 00154 return number; 00155 } 00156 00157 bool LinearSumFunction::isComposite () const 00158 { 00159 return true; 00160 } 00161 00162 void LinearSumFunction:: addToComposite ( FunctionBase * function ) 00163 { 00164 // not sure why this was put in, but it breaks archiving and 00165 // dearchiving 00166 00167 // if ( m_functions.empty() ) { 00168 // m_name += " of "; 00169 // } 00170 // else { 00171 // m_name += " + "; 00172 // } 00173 // m_name += function -> name (); 00174 00175 m_functions.push_back ( function ); 00176 } 00177 00178 void 00179 LinearSumFunction:: 00180 removeFromComposite ( FunctionBase * function ) 00181 { 00182 FunctionList_t::iterator it = m_functions.begin (); 00183 00184 while ( it != m_functions.end () ) { 00185 if ( (*it) == function ){ 00186 it = m_functions.erase ( it ); 00187 } 00188 else{ 00189 it ++; 00190 } 00191 } 00192 } 00193 00194 double LinearSumFunction::operator () ( double x ) const 00195 { 00196 double sum = 0.0; 00197 FunctionList_t::const_iterator it = m_functions.begin (); 00198 00199 for ( ; it != m_functions.end (); ++it ) { 00200 sum += (*it)->operator () ( x ); 00201 } 00202 return sum; 00203 } 00204 00205 void 00206 LinearSumFunction:: 00207 initialParameters ( const FunctionHelper * helper ) 00208 { 00209 // does nothing 00210 }