11#ifndef __MEAL_VectorRule_H
12#define __MEAL_VectorRule_H
14#include "MEAL/ProjectGradient.h"
15#include "MEAL/Composite.h"
27 typedef typename T::Result Result;
57 unsigned size ()
const {
return model.size(); }
67 {
return "VectorRule<" + std::string(T::Name)+
">"; }
72 void calculate (Result& result, std::vector<Result>* grad);
77 std::vector< Project<T> > model;
115 unsigned nmodel = copy.model.size();
116 for (
unsigned imodel=0; imodel < nmodel; imodel++)
119 model_index = copy.model_index;
128 std::cerr <<
get_name() +
"push_back" << std::endl;
130 model_index = model.size();
132 composite->map (model.back());
139 std::cerr <<
get_name() +
"assign" << std::endl;
147 if (model[model_index])
150 std::cerr <<
get_name() +
"assign unmap old" << std::endl;
152 composite->unmap (model[model_index]);
155 model[model_index] = x;
161 std::cerr <<
get_name() +
"assign map new" << std::endl;
163 composite->map (model[model_index]);
170 std::cerr <<
get_name() +
"erase" << std::endl;
176 std::cerr <<
get_name() +
"erase unmap" << std::endl;
178 composite->unmap (model[index]);
181 std::cerr <<
get_name() +
"erase vector::erase" << std::endl;
183 model.erase( model.begin() + index );
189 if (model_index >= model.size())
190 throw Error (InvalidRange,
"MEAL::"+
get_name()+
"::get_current",
191 "index=%d >= nmodel=%d", model_index, model.size());
193 if (!model[model_index])
194 throw Error (InvalidState,
"MEAL::"+
get_name()+
"::get_current",
195 "index=%d not set; nmodel=%d", model_index);
197 return model[model_index];
203 if (model_index >= model.size())
204 throw Error (InvalidRange,
"MEAL::"+
get_name()+
"::get_projection",
205 "index=%d >= nmodel=%d", model_index, model.size());
207 if (!model[model_index])
208 throw Error (InvalidState,
"MEAL::"+
get_name()+
"::get_projection",
209 "index=%d not set; nmodel=%d", model_index);
211 return model[model_index];
217 if (index == model_index)
220 if (index >= model.size())
221 throw Error (InvalidRange,
"MEAL::"+
get_name()+
"::set_index",
222 "index=%d >= nmodel=%d", index, model.size());
230 std::vector<Result>* grad)
232 unsigned nmodel = model.size();
234 std::cerr <<
get_name() +
"calculate nmodel=" << nmodel << std::endl;
237 throw Error (InvalidState,
"MEAL::"+
get_name()+
"::calculate",
"nmodel = 0");
240 std::vector<Result> comp_gradient;
243 std::vector<Result>* comp_gradient_ptr = 0;
246 comp_gradient_ptr = &comp_gradient;
248 if (this->get_verbose())
249 std::cerr <<
get_name() +
"calculate evaluate "
250 << model[model_index]->get_name() << std::endl;
255 result = model[model_index]->evaluate (comp_gradient_ptr);
257 if (this->get_verbose())
258 std::cerr <<
get_name() +
"calculate "
259 << model[model_index]->get_name()
260 <<
" result=" << result << std::endl;
263 catch (
Error& error) {
265 throw error <<
" model=" << model[model_index]->get_name();
270 if (model[model_index]->get_nparam() != comp_gradient.size())
271 throw Error (InvalidState, (
get_name() +
"calculate").c_str(),
272 "model[%d]=%s.get_nparam=%d != gradient.size=%d",
273 model_index, model[model_index]->
get_name().c_str(),
274 model[model_index]->get_nparam(), comp_gradient.size());
276 unsigned nparam = this->get_nparam();
278 grad->resize (nparam);
279 for (
unsigned iparam=0; iparam<nparam; iparam++)
280 (*grad)[iparam] = 0.0;
283 ProjectGradient (model[model_index], comp_gradient, *grad);
288 std::cerr <<
get_name() +
"calculate model index=" << model_index
289 <<
" result\n " << result << std::endl;
292 std::cerr <<
get_name() +
"calculate gradient" << std::endl;
293 for (
unsigned i=0; i<grad->size(); i++)
294 std::cerr <<
" " << i <<
":" << this->get_infit(i)
295 <<
"=" << (*grad)[i] << std::endl;
Parameter policy for composite functions.
Definition Composite.h:20
virtual void copy(const Function *model)
Does the work for operator =.
Definition Function.C:58
@ Evaluation
Function evaluation, as returned by the evaluate method.
Definition Function.h:171
Template combines a reference to a Component and its Projection.
Definition Projection.h:60
VectorRule implements a vector of Functions with a current index.
Definition VectorRule.h:23
unsigned size() const
Get the size of the array.
Definition VectorRule.h:57
void push_back(T *model)
Add an element to the array.
Definition VectorRule.h:125
std::string get_name() const
Return the name of the class.
Definition VectorRule.h:66
void assign(T *model)
Assign the current element of the array.
Definition VectorRule.h:136
void erase(unsigned index)
Erase an element from the array.
Definition VectorRule.h:167
~VectorRule()
Destructor.
Definition VectorRule.h:39
T * get_current()
Get the current element of the array.
Definition VectorRule.h:187
Project< T > & get_projection()
Get the projection of the current element of the array.
Definition VectorRule.h:201
VectorRule(Composite *policy=0)
Default constructor.
Definition VectorRule.h:90
unsigned get_index() const
Get the current element of the array.
Definition VectorRule.h:63
void calculate(Result &result, std::vector< Result > *grad)
Return the result and its gradient.
Definition VectorRule.h:229
void set_index(unsigned index)
Set the current element of the array.
Definition VectorRule.h:215
VectorRule & operator=(const VectorRule ©)
Assignment operator.
Definition VectorRule.h:110
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16