11#ifndef __MEAL_ChainRule_H
12#define __MEAL_ChainRule_H
14#include "MEAL/ProjectGradient.h"
15#include "MEAL/Composite.h"
16#include "MEAL/Scalar.h"
21 class ConstrainedParameter {
24 ConstrainedParameter (
unsigned iparam,
bool infit,
Scalar* func)
63 typedef typename T::Result Result;
76 T* get_model () {
return model; }
80 bool has_constraint (
unsigned iparam);
81 Scalar* get_constraint (
unsigned iparam);
95 void calculate (Result& result, std::vector<Result>* gradient);
117 std::vector<unsigned>& imap,
118 std::vector< std::vector<double> >& covar);
125 return "ChainRule<" + std::string(T::Name)+
">";
137 for (
unsigned ic=0; ic < rule.
constraints.size(); ic++)
148 for (
unsigned ifunc=0; ifunc<
constraints.size(); ifunc++)
161 if (this->get_verbose())
162 std::cerr <<
get_name() +
"::set_constraint iparam=" << iparam
171 if (this->get_verbose())
172 std::cerr <<
get_name() +
"::set_constraint"
173 " remove param=" << iparam << std::endl;
175 composite.unmap (existing->
scalar);
189 infit =
model->get_infit (iparam);
193 if (this->get_verbose())
194 std::cerr <<
get_name() +
"::set_constraint"
195 " add param=" << iparam << std::endl;
200 model->set_infit (iparam,
false);
204MEAL::Scalar* MEAL::ChainRule<T>::get_constraint (
unsigned iparam)
208 throw Error (InvalidParam,
"MEAL::ChainRule<T>::get_constraint",
209 "iparam=%u is not constrained", iparam);
215bool MEAL::ChainRule<T>::has_constraint (
unsigned iparam)
217 return find_constraint (iparam) != 0;
228 if (this->get_verbose())
229 std::cerr <<
get_name() +
"::set_model"
230 " unmap old model" << std::endl;
231 composite.unmap (
model);
236 if (this->get_verbose())
237 std::cerr <<
get_name() +
"::set_model"
238 " map new model" << std::endl;
240 composite.map (
model);
242 for (
unsigned ifunc=0; ifunc<
constraints.size(); ifunc++)
246 model->set_infit (iparam,
false);
252 std::vector<Result>* grad)
255 throw Error (InvalidState,
get_name() +
"::calculate",
"no model");
257 if (this->get_verbose())
258 std::cerr <<
get_name() +
"::calculate" << std::endl;
260 for (
unsigned ifunc=0; ifunc<
constraints.size(); ifunc++)
263 std::cerr <<
get_name() +
"::calculate constraint on iparam="
265 <<
constraints[ifunc].scalar->get_name () << std::endl;
267 std::vector<double>* fgrad = 0;
276 std::vector<Result> model_grad;
277 std::vector<Result>* model_grad_ptr = 0;
279 model_grad_ptr = & model_grad;
281 result =
model->evaluate (model_grad_ptr);
285 unsigned ngrad = this->get_nparam();
286 grad->resize (ngrad);
289 for (igrad=0; igrad<ngrad; igrad++)
290 (*grad)[igrad] = 0.0;
293 ProjectGradient (
model, model_grad, *(grad));
296 std::vector<Result> fgrad;
298 for (
unsigned ifunc=0; ifunc<
constraints.size(); ifunc++)
303 fgrad.resize (ngrad);
306 for (igrad=0; igrad<ngrad; igrad++)
307 fgrad[igrad] = model_grad[iparam] *
constraints[ifunc].gradient[igrad];
309 ProjectGradient (
constraints[ifunc].scalar, fgrad, *(grad));
313 if (this->get_verbose())
315 std::cerr <<
get_name() +
"::calculate result\n"
316 " " << result << std::endl;
319 std::cerr <<
get_name() +
"::calculate gradient\n";
320 for (
unsigned i=0; i<grad->size(); i++)
321 std::cerr <<
" " << i <<
":" << this->get_infit(i) <<
"="
322 << (*grad)[i] << std::endl;
Parameterizes a Function by one or more Scalar ordinates.
Definition ChainRule.h:59
ChainRule & operator=(const ChainRule &rule)
Assignment operator.
Definition ChainRule.h:130
ChainRule()
Default constructor.
Definition ChainRule.h:66
std::vector< ConstrainedParameter > constraints
Scalars used to constrain the parameters.
Definition ChainRule.h:98
void set_constraint(unsigned iparam, Scalar *scalar)
Set the Scalar instance used to constrain the specified parameter.
Definition ChainRule.h:159
Project< MEAL::Scalar > model
Definition ChainRule.h:104
ConstrainedParameter * find_constraint(unsigned iparam)
Find the matching constraint.
Definition ChainRule.h:146
std::string get_name() const
Return the name of the class.
Definition ChainRule.h:123
ChainRule(const ChainRule &rule)
Copy constructor.
Definition ChainRule.h:69
void calculate(Result &result, std::vector< Result > *gradient)
Return the Result and its gradient.
Definition ChainRule.h:251
void set_model(T *model)
Set the Function to be constrained by Scalar ordinates.
Definition ChainRule.h:221
Parameter policy for composite functions.
Definition Composite.h:20
A parameter and its constraining Scalar instance.
Definition ChainRule.h:21
unsigned parameter
The index of the Function parameter constrained by scalar.
Definition ChainRule.h:32
Project< Scalar > scalar
The constraining Scalar instance.
Definition ChainRule.h:38
std::vector< double > gradient
The last calculated gradient of the Scalar.
Definition ChainRule.h:41
bool previously_infit
The value of the fit flag before the parameter was constrained.
Definition ChainRule.h:35
unsigned index
Index within ChainRule vector.
Definition ChainRule.h:44
Template combines a reference to a Component and its Projection.
Definition Projection.h:60
Pure virtual base class of scalar functions.
Definition Scalar.h:20
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16
void covariance(Scalar *function, unsigned index, std::vector< unsigned > &imap, std::vector< std::vector< double > > &covar)
Definition ChainRule.C:12