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 }
unsigned int i
virtual void addToComposite(FunctionBase *)
Adds the function to the linear sum of functions.
const std::vector< std::string > & parmNames() const
Returns a reference to a vector of parameter names.
virtual void initialize()
Initializes the function and parameter names.
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
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.
virtual double derivByParm(int i, double x) const
Returns the function&#39;s derivative at the coordinate value x with respect to the i-th parameter...
LinearSumFunction()
The default constructor.
virtual bool isComposite() const
Returns true.
virtual FunctionBase * clone() const
Creates a new function object by copying an existing one.
virtual const std::vector< double > & getParameters() const
Returns the values of the parameters as a vector.
virtual double operator()(double x) const
The function call operator.
The namespace for conversion to string.
std::vector< double > m_parms
The parameter values.
Definition: FunctionBase.h:109
A function that can be used with a fitter.
virtual FunctionBase * clone() const
Creates a new function object by copying an existing one.
string convert(int i)
Converts an integer to a string.
virtual int count()
Returns the number of functions that are a part of this composite.
A function that can be added to a DataRep and used in a fitter.
Definition: FunctionBase.h:90
hippodraw::LinearSumFunction class interface
An abstract base class to help FunctionBase objects perform some operations.
std::string m_name
The name of the function.
Definition: FunctionBase.h:103
virtual const std::vector< int > & getFixedFlags() const
Returns the flags indicating which parameters should remain fixed during any minimization process...
virtual void removeFromComposite(FunctionBase *)
Removes the function from the linear sum of functions.
std::vector< std::string > m_parm_names
The names of the function parameters.
Definition: FunctionBase.h:106
return index
Definition: PickTable.cxx:182
virtual void initialParameters(const FunctionHelper *helper)
Sets the FunctionHelper so that the function can calculate a reasonable set of initial parameter valu...
virtual int size() const
Returns the number of parameters.
list< QAction * >::iterator it
const std::string & name() const
Returns the name of the function.

Generated for HippoDraw Class Library by doxygen