LinearSumFunction.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "LinearSumFunction.h"
17 
18 #include "pattern/string_convert.h"
19 
20 #include <cassert>
21 
22 #ifdef ITERATOR_MEMBER_DEFECT
23 using namespace std;
24 #else
25 using std::vector;
26 using std::string;
27 #endif
28 
29 using namespace hippodraw;
30 
31 LinearSumFunction::LinearSumFunction ( )
32 {
33  initialize ();
34 }
35 
36 LinearSumFunction::
37 LinearSumFunction ( const LinearSumFunction & old )
38  : FunctionBase ()
39 {
40  FunctionList_t::const_iterator it = old.m_functions.begin();
41  while ( it != old.m_functions.end() ) {
42  FunctionBase * function = (*it)->clone ();
43  m_functions.push_back ( function );
44  }
45 
46  initialize ();
47 }
48 
50 {
51  m_name = "Linear Sum";
52 }
53 
55 {
56 
57  return new LinearSumFunction ( *this );
58 }
59 
60 const vector < string > & LinearSumFunction::parmNames() const
61 {
62  LinearSumFunction * self = const_cast < LinearSumFunction * > ( this );
63 
64  self->m_parm_names.clear();
65 
66  unsigned int f_size = m_functions.size ();
67  for ( unsigned int i = 0; i < f_size; i++ ) {
68  string suffix ( "-" );
69  suffix += String::convert ( i );
70 
71  const vector< string > & names = m_functions[i] -> parmNames ();
72  unsigned int n_size = names.size ();
73  for ( unsigned int j = 0; j < n_size; j++ ) {
74  string name = names [j];
75  name += suffix;
76  self -> m_parm_names.push_back ( name );
77  }
78  }
79 
80  return m_parm_names;
81 }
82 
83 const vector<double> & LinearSumFunction::getParameters () const
84 {
85  LinearSumFunction * p = const_cast < LinearSumFunction * > ( this );
86  p->m_parms.clear ();
87  FunctionList_t::const_iterator it = m_functions.begin ();
88  for ( ; it != m_functions.end (); ++it ) {
89  const vector<double> & vals = (*it)->getParameters ();
90  p->m_parms.insert ( p->m_parms.end (), vals.begin(), vals.end () );
91  }
92  return m_parms;
93 }
94 
95 
96 vector< double >::const_iterator
98 setParameters ( std::vector< double >::const_iterator it )
99 {
100  FunctionList_t::iterator fit = m_functions.begin();
101 
102  for ( ;fit != m_functions.end (); ++fit ) {
103  it = (*fit)->setParameters ( it );
104  }
105 
106  return it;
107 }
108 
109 
110 double LinearSumFunction::derivByParm ( int index, double x ) const
111 {
112  double value = 0;
113  unsigned int numf = m_functions.size ();
114  for ( unsigned int i = 0; i < numf; i++ ) {
115  int size = m_functions [i] -> size ();
116  if ( index < size ) {
117  value = m_functions [i] -> derivByParm ( index, x );
118  break;
119  }
120  else {
121  index -= size;
122  }
123  }
124 
125  return value;
126 }
127 
129 {
130  return m_functions.size();
131 }
132 
133 int
135 size () const
136 {
137  int number = 0;
138  unsigned int numf = m_functions.size ();
139 
140  for ( unsigned int i = 0; i < numf; i++ ) {
141  const FunctionBase * function = m_functions [i];
142  number += function -> size ();
143  }
144 
145  return number;
146 }
147 
149 {
150  return true;
151 }
152 
154 {
155  m_functions.push_back ( function );
156 }
157 
158 void
161 {
162  FunctionList_t::iterator it = m_functions.begin ();
163 
164  while ( it != m_functions.end () ) {
165  if ( (*it) == function ){
166  it = m_functions.erase ( it );
167  }
168  else{
169  it ++;
170  }
171  }
172 }
173 
174 double LinearSumFunction::operator () ( double x ) const
175 {
176  double sum = 0.0;
177  FunctionList_t::const_iterator it = m_functions.begin ();
178 
179  for ( ; it != m_functions.end (); ++it ) {
180  sum += (*it)->operator () ( x );
181  }
182  return sum;
183 }
184 
185 void
188 {
189  // does nothing
190 }

Generated for HippoDraw Class Library by doxygen