StatedFCN.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "StatedFCN.h"
17 
18 #include "functions/FunctionBase.h"
19 
20 #include <stdexcept>
21 
22 using std::string;
23 using std::vector;
24 
25 using namespace hippodraw;
26 
29  : m_function ( 0 ),
30  m_needs_derivs ( false )
31 {
32 }
33 
35 StatedFCN ( const StatedFCN & fcn )
36  : FCNBase (),
37  m_function ( fcn.m_function ),
38  m_needs_derivs ( fcn.m_needs_derivs )
39 {
40 }
41 
42 void
44 copyFrom ( const StatedFCN * other )
45 {
46 // m_fixed_flags = other -> m_fixed_flags;
47  m_function = other -> m_function;
48 
49 }
50 
51 bool
53 hasFunction () const
54 {
55  return m_function != 0;
56 }
57 
61 void
63 setFunction ( FunctionBase * function )
64 {
65  bool yes = isCompatible ( function );
66  if ( yes == false ) {
67  string what ( "StatedFCN: The function `" );
68  what += function -> name ();
69  what += "' can not provide partial\n";
70  what += "derivatives needed by this fitter.";
71  throw std::runtime_error ( what );
72  }
73 
74  m_function = function;
75 }
76 
77 const vector < string > &
79 getParmNames () const
80 {
81  return m_function -> parmNames ();
82 }
83 
84 const vector < double > &
86 getParameters ( ) const
87 {
88  return m_function -> getParameters ( );
89 }
90 
91 void
93 setParameters ( const std::vector < double > & parms )
94 {
95  m_function -> setParameters ( parms );
96 }
97 
98 void
100 fillFreeParameters ( std::vector < double > & free_parms ) const
101 {
102  free_parms.clear();
103  const vector < double > & parms = m_function -> getParameters ();
104  const vector < int > & flags = m_function -> getFixedFlags ();
105  unsigned int size = parms.size ();
106  for ( unsigned int i = 0; i < size; i++ ) {
107  if ( flags[ i ] == 0 ) {
108  free_parms.push_back ( parms[ i ] );
109  }
110  }
111 }
112 
113 unsigned int
116 {
117  unsigned int number = 0;
118  const vector < int > & flags = m_function -> getFixedFlags ();
119 // unsigned int size = m_fixed_flags.size ();
120  unsigned int size = flags.size ();
121  for ( unsigned int i = 0; i < size; i++ ) {
122 // if ( m_fixed_flags[i] == 0 ) number++;
123  if ( flags[i] == 0 ) number++;
124  }
125 
126  return number;
127 }
128 
129 const vector < int > &
132 {
133  return m_function -> getFixedFlags ();
134 
135 }
136 
137 void
139 setFixedFlags ( const std::vector < int > & flags )
140 {
141 // m_fixed_flags = flags;
142  m_function -> setFixedFlags ( flags );
143 }
144 
145 void
147 setFreeParameters ( const std::vector < double > & free_parms )
148 {
149  // make a copy...
150  vector < double > parms = m_function -> getParameters ();
151  const vector < int > & flags = m_function -> getFixedFlags ();
152  unsigned int size = parms.size ();
153  unsigned int j = 0;
154 
155  for ( unsigned int i = 0; i < size; i++ ) {
156  if ( flags [i] == 0 ) {
157  parms[i] = free_parms[j];
158  j++;
159  }
160  }
161 
162  m_function -> setParameters ( parms );
163 }
164 
165 void
167 fillFreeDerivatives ( std::vector < double > & derives, double x )
168 {
169  derives.clear();
170  const vector < double > & parms = m_function -> getParameters ();
171  const vector < int > & flags = m_function -> getFixedFlags ();
172  unsigned int size = parms.size();
173 
174  for ( unsigned int i = 0; i < size; i++ ) {
175 // if ( m_fixed_flags [i] == 0 ) {
176  if ( flags [i] == 0 ) {
177  double value = m_function -> derivByParm ( i, x );
178  derives.push_back ( value );
179  }
180  }
181 }
182 
183 double
185 operator () ( const std::vector < double > & parms ) const
186 {
187  m_function -> setParameters ( parms );
188 
189  return objectiveValue ();
190 }
191 
192 void
195 {
197 }
198 
199 bool
201 isCompatible ( const FunctionBase * function ) const
202 {
203  bool yes = true;
204  if ( m_needs_derivs &&
205  ( function -> hasDerivatives () == false ) ) {
206  yes = false;
207  }
208 
209  return yes;
210 }
virtual void copyFrom(const StatedFCN *other)
Makes a copy of the relevant attributes from other object.
Definition: StatedFCN.cxx:44
FunctionBase * m_function
The flags to indicated which parameters are to be held fixed during minimization of this object...
Definition: StatedFCN.h:90
unsigned int i
void fillFreeDerivatives(std::vector< double > &, double x)
Clears and fills the vector with the derivatives of the function a coordinate value x...
Definition: StatedFCN.cxx:167
virtual double operator()(const std::vector< double > &parms) const
Sets the model function parameters to parms and returns the objective value.
Definition: StatedFCN.cxx:185
virtual double objectiveValue() const =0
Returns the value of the objective function.
bool isCompatible(const FunctionBase *) const
Returns true if function is compatible this objective function.
Definition: StatedFCN.cxx:201
virtual void setFixedFlags(const std::vector< int > &flags)
Returns the vector of which of the model function&#39;s parameters are considered fixed and not to be cha...
Definition: StatedFCN.cxx:139
virtual void setFreeParameters(const std::vector< double > &parms)
Sets the free parameters from the vector parms.
Definition: StatedFCN.cxx:147
void setParameters(const std::vector< double > &parms)
Sets the values of the model function&#39;s parameters.
Definition: StatedFCN.cxx:93
return yes
Definition: CanvasView.cxx:883
bool m_needs_derivs
Needs derivatives flag.
Definition: StatedFCN.h:95
intp size(numeric::array arr)
Definition: num_util.cpp:296
unsigned int getNumberFreeParms() const
Returns the number of free parameters.
Definition: StatedFCN.cxx:115
StatedFCN()
The default constructor.
Definition: StatedFCN.cxx:28
A function that can be added to a DataRep and used in a fitter.
Definition: FunctionBase.h:90
const std::vector< double > & getParameters() const
Returns the current state of the model function&#39;s parameters.
Definition: StatedFCN.cxx:86
void setNeedsDerivatives(bool yes)
Sets the needs derivatives flag.
Definition: StatedFCN.cxx:194
const std::vector< std::string > & getParmNames() const
Returns the names of the model function&#39;s parameters.
Definition: StatedFCN.cxx:79
void setFunction(FunctionBase *function)
Sets the model function.
Definition: StatedFCN.cxx:63
hippodraw::FunctionBase class interface
hippodraw::StatedFCN class interface
void fillFreeParameters(std::vector< double > &free_parms) const
Fills the vector with the values of the free parameters.
Definition: StatedFCN.cxx:100
const std::vector< int > & getFixedFlags() const
Returns a vector of flags for parameters which are held fixed by the function.
Definition: StatedFCN.cxx:131
A derived class for FCNBase class.
Definition: StatedFCN.h:70
bool hasFunction() const
Returns true if model function has been set, otherwise returns false.
Definition: StatedFCN.cxx:53

Generated for HippoDraw Class Library by doxygen