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_fixed_flags ( fcn.m_fixed_flags ),
38  m_function ( fcn.m_function ),
39  m_needs_derivs ( fcn.m_needs_derivs )
40 {
41 }
42 
43 void
45 copyFrom ( const StatedFCN * other )
46 {
47  m_fixed_flags = other -> m_fixed_flags;
48  m_function = other -> m_function;
49 
50 }
51 
52 bool
54 hasFunction () const
55 {
56  return m_function != 0;
57 }
58 
62 void
64 setFunction ( FunctionBase * function )
65 {
66  bool yes = isCompatible ( function );
67  if ( yes == false ) {
68  string what ( "StatedFCN: The function `" );
69  what += function -> name ();
70  what += "' can not provide partial\n";
71  what += "derivatives needed by this fitter.";
72  throw std::runtime_error ( what );
73  }
74 
75  m_function = function;
76  unsigned int size = m_function -> size();
77  m_fixed_flags.resize ( size );
78 
79  for ( unsigned int i = 0; i < size; i++ ) {
80  m_fixed_flags [i] = 0;
81  }
82 }
83 
84 const vector < string > &
86 getParmNames () const
87 {
88  return m_function -> parmNames ();
89 }
90 
91 const vector < double > &
93 getParameters ( ) const
94 {
95  return m_function -> getParameters ( );
96 }
97 
98 void
100 setParameters ( const std::vector < double > & parms )
101 {
102  m_function -> setParameters ( parms );
103 }
104 
105 void
107 fillFreeParameters ( std::vector < double > & free_parms ) const
108 {
109  free_parms.clear();
110  const vector < double > & parms = m_function -> getParameters ();
111  unsigned int size = parms.size ();
112  for ( unsigned int i = 0; i < size; i++ ) {
113  if ( m_fixed_flags[ i ] == 0 ) {
114  free_parms.push_back ( parms[ i ] );
115  }
116  }
117 }
118 
119 unsigned int
122 {
123  unsigned int number = 0;
124  unsigned int size = m_fixed_flags.size ();
125  for ( unsigned int i = 0; i < size; i++ ) {
126  if ( m_fixed_flags[i] == 0 ) number++;
127  }
128 
129  return number;
130 }
131 
132 const vector < int > &
135 {
136  return m_fixed_flags;
137 }
138 
139 void
141 setFixedFlags ( const std::vector < int > & flags )
142 {
143  m_fixed_flags = flags;
144 }
145 
146 void
148 setFreeParameters ( const std::vector < double > & free_parms )
149 {
150  vector < double > parms = m_function -> getParameters ();
151  unsigned int size = parms.size ();
152  unsigned int j = 0;
153 
154  for ( unsigned int i = 0; i < size; i++ ) {
155  if ( m_fixed_flags [i] == 0 ) {
156  parms[i] = free_parms[j];
157  j++;
158  }
159  }
160 
161  m_function -> setParameters ( parms );
162 }
163 
164 void
166 fillFreeDerivatives ( std::vector < double > & derives, double x )
167 {
168  derives.clear();
169  const vector < double > & parms = m_function -> getParameters ();
170  unsigned int size = parms.size();
171 
172  for ( unsigned int i = 0; i < size; i++ ) {
173  if ( m_fixed_flags [i] == 0 ) {
174  double value = m_function -> derivByParm ( i, x );
175  derives.push_back ( value );
176  }
177  }
178 }
179 
180 double
182 operator () ( const std::vector < double > & parms ) const
183 {
184  m_function -> setParameters ( parms );
185 
186  return objectiveValue ();
187 }
188 
189 void
192 {
193  m_needs_derivs = yes;
194 }
195 
196 bool
198 isCompatible ( const FunctionBase * function ) const
199 {
200  bool yes = true;
201  if ( m_needs_derivs &&
202  ( function -> hasDerivatives () == false ) ) {
203  yes = false;
204  }
205 
206  return yes;
207 }

Generated for HippoDraw Class Library by doxygen