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