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 const vector < int > &
97 getFixedFlags () const
98 {
99  LinearSumFunction * p = const_cast < LinearSumFunction * > ( this );
100 
101  p -> m_fixed_flags.clear();
102  FunctionList_t::const_iterator it = m_functions.begin ();
103  for ( ; it != m_functions.end(); ++it ) {
104  const vector < int > & flags = (*it)->getFixedFlags ();
105  p -> m_fixed_flags.insert ( p -> m_fixed_flags.end(),
106  flags.begin(), flags.end() );
107  }
108  return m_fixed_flags;
109 }
110 
111 vector< double >::const_iterator
113 setParameters ( std::vector< double >::const_iterator it )
114 {
115  FunctionList_t::iterator fit = m_functions.begin();
116 
117  for ( ;fit != m_functions.end (); ++fit ) {
118  it = (*fit)->setParameters ( it );
119  }
120 
121  return it;
122 }
123 
124 
125 double LinearSumFunction::derivByParm ( int index, double x ) const
126 {
127  double value = 0;
128  unsigned int numf = m_functions.size ();
129  for ( unsigned int i = 0; i < numf; i++ ) {
130  int size = m_functions [i] -> size ();
131  if ( index < size ) {
132  value = m_functions [i] -> derivByParm ( index, x );
133  break;
134  }
135  else {
136  index -= size;
137  }
138  }
139 
140  return value;
141 }
142 
144 {
145  return m_functions.size();
146 }
147 
148 int
150 size () const
151 {
152  int number = 0;
153  unsigned int numf = m_functions.size ();
154 
155  for ( unsigned int i = 0; i < numf; i++ ) {
156  const FunctionBase * function = m_functions [i];
157  number += function -> size ();
158  }
159 
160  return number;
161 }
162 
164 {
165  return true;
166 }
167 
169 {
170  m_functions.push_back ( function );
171 }
172 
173 void
176 {
177  FunctionList_t::iterator it = m_functions.begin ();
178 
179  while ( it != m_functions.end () ) {
180  if ( (*it) == function ){
181  it = m_functions.erase ( it );
182  }
183  else{
184  it ++;
185  }
186  }
187 }
188 
189 double LinearSumFunction::operator () ( double x ) const
190 {
191  double sum = 0.0;
192  FunctionList_t::const_iterator it = m_functions.begin ();
193 
194  for ( ; it != m_functions.end (); ++it ) {
195  sum += (*it)->operator () ( x );
196  }
197  return sum;
198 }
199 
200 void
203 {
204  // does nothing
205 }
virtual bool isComposite() const
Returns true.
virtual const std::vector< int > & getFixedFlags() const
Returns the flags indicating which parameters should remain fixed during any minimization process...
virtual double operator()(double x) const
The function call operator.
virtual int size() const
Returns the number of parameters.
LinearSumFunction()
The default constructor.
virtual int count()
Returns the number of functions that are a part of this composite.
const std::string & name() const
Returns the name of the function.
virtual void initialize()
Initializes the function and parameter names.
virtual void removeFromComposite(FunctionBase *)
Removes the function from the linear sum of functions.
Namespace for HippoDraw.
Definition: AxesType.cxx:21
string convert(int i)
Converts an integer to a string.
std::string m_name
The name of the function.
Definition: FunctionBase.h:103
virtual void initialParameters(const FunctionHelper *helper)
Sets the FunctionHelper so that the function can calculate a reasonable set of initial parameter valu...
virtual FunctionBase * clone() const
Creates a new function object by copying an existing one.
A function that can be used with a fitter.
std::vector< int > m_fixed_flags
The flags to indicated which parameters are to be held fixed during minimization of this objective fu...
Definition: FunctionBase.h:117
std::vector< double > m_parms
The parameter values.
Definition: FunctionBase.h:109
const std::vector< std::string > & parmNames() const
Returns a reference to a vector of parameter names.
hippodraw::LinearSumFunction class interface
virtual const std::vector< double > & getParameters() const
Returns the values of the parameters as a vector.
std::vector< std::string > m_parm_names
The names of the function parameters.
Definition: FunctionBase.h:106
virtual FunctionBase * clone() const
Creates a new function object by copying an existing one.
A function that can be added to a DataRep and used in a fitter.
Definition: FunctionBase.h:90
An abstract base class to help FunctionBase objects perform some operations.
virtual void addToComposite(FunctionBase *)
Adds the function to the linear sum of functions.
virtual std::vector< double >::const_iterator setParameters(std::vector< double >::const_iterator it)
Sets the parameter values to the value pointed to by the iterator.
STL namespace.
virtual double derivByParm(int i, double x) const
Returns the function's derivative at the coordinate value x with respect to the i-th parameter...
The namespace for conversion to string.

Generated for HippoDraw Class Library by doxygen