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 00096 vector< double >::const_iterator 00097 LinearSumFunction:: 00098 setParameters ( std::vector< double >::const_iterator it ) 00099 { 00100 FunctionList_t::iterator fit = m_functions.begin(); 00101 00102 for ( ;fit != m_functions.end (); ++fit ) { 00103 it = (*fit)->setParameters ( it ); 00104 } 00105 00106 return it; 00107 } 00108 00109 00110 double LinearSumFunction::derivByParm ( int index, double x ) const 00111 { 00112 double value = 0; 00113 unsigned int numf = m_functions.size (); 00114 for ( unsigned int i = 0; i < numf; i++ ) { 00115 int size = m_functions [i] -> size (); 00116 if ( index < size ) { 00117 value = m_functions [i] -> derivByParm ( index, x ); 00118 break; 00119 } 00120 else { 00121 index -= size; 00122 } 00123 } 00124 00125 return value; 00126 } 00127 00128 int LinearSumFunction::count () 00129 { 00130 return m_functions.size(); 00131 } 00132 00133 int 00134 LinearSumFunction:: 00135 size () const 00136 { 00137 int number = 0; 00138 unsigned int numf = m_functions.size (); 00139 00140 for ( unsigned int i = 0; i < numf; i++ ) { 00141 const FunctionBase * function = m_functions [i]; 00142 number += function -> size (); 00143 } 00144 00145 return number; 00146 } 00147 00148 bool LinearSumFunction::isComposite () const 00149 { 00150 return true; 00151 } 00152 00153 void LinearSumFunction:: addToComposite ( FunctionBase * function ) 00154 { 00155 m_functions.push_back ( function ); 00156 } 00157 00158 void 00159 LinearSumFunction:: 00160 removeFromComposite ( FunctionBase * function ) 00161 { 00162 FunctionList_t::iterator it = m_functions.begin (); 00163 00164 while ( it != m_functions.end () ) { 00165 if ( (*it) == function ){ 00166 it = m_functions.erase ( it ); 00167 } 00168 else{ 00169 it ++; 00170 } 00171 } 00172 } 00173 00174 double LinearSumFunction::operator () ( double x ) const 00175 { 00176 double sum = 0.0; 00177 FunctionList_t::const_iterator it = m_functions.begin (); 00178 00179 for ( ; it != m_functions.end (); ++it ) { 00180 sum += (*it)->operator () ( x ); 00181 } 00182 return sum; 00183 } 00184 00185 void 00186 LinearSumFunction:: 00187 initialParameters ( const FunctionHelper * ) 00188 { 00189 // does nothing 00190 }