From 1f4a944064bc42284c33e6b755353d191cf288e8 Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Mon, 15 Dec 2008 07:26:12 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@1 ed2142bd-67ad-457f-ba7c-d818d4011675 --- models/mul_model.hpp | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 models/mul_model.hpp (limited to 'models/mul_model.hpp') diff --git a/models/mul_model.hpp b/models/mul_model.hpp new file mode 100644 index 0000000..0e64c1b --- /dev/null +++ b/models/mul_model.hpp @@ -0,0 +1,171 @@ +#ifndef MUL_MODEL_H_ +#define MUL_MODEL_H_ +#include +#include + +namespace opt_utilities +{ + template + class mul_model + :public model + { + private: + model* do_clone()const + { + return new mul_model(*this); + } + private: + mul_model() + { + } + + private: + model* pm1; + model* pm2; + + public: + mul_model(const model& m1, + const model& m2) + :pm1(m1.clone()),pm2(m2.clone()) + { + int np1=m1.get_num_params(); + int np2=m2.get_num_params(); + for(int i=0;i p(m1.get_param_info(i)); + param_info p1(p.get_name()+"1",p.get_default_value()); + this->push_param_info(p1); + } + for(int i=0;i p(m2.get_param_info(i)); + param_info p2(p.get_name()+"2",p.get_default_value()); + this->push_param_info(p2); + } + } + + mul_model(const mul_model& rhs) + :pm1(NULL),pm2(NULL) + { + int np1(0),np2(0); + if(rhs.pm1) + { + pm1=rhs.pm1->clone(); + np1=rhs.pm1->get_num_params(); + for(int i=0;i p(rhs.pm1->get_param_info(i)); + param_info p1(p.get_name()+"1",p.get_default_value()); + this->push_param_info(p1); + } + } + if(rhs.pm2) + { + pm2=rhs.pm2->clone(); + np2=rhs.pm2->get_num_params(); + for(int i=0;i p(rhs.pm2->get_param_info(i)); + param_info p2(p.get_name()+"2",p.get_default_value()); + this->push_param_info(p2); + } + } + } + + mul_model& operator=(const mul_model& rhs) + { + if(this==&rhs) + { + return *this; + } + if(!pm1) + { + //delete pm1; + pm1->destroy(); + } + if(!pm2) + { + //delete pm2; + pm2->destroy(); + } + int np1(0),np2(0); + if(rhs.pm1) + { + pm1=rhs.pm1->clone(); + np1=rhs.pm1->get_num_params(); + for(int i=0;i p(rhs.pm1->get_param_info(i)); + param_info p1(p.get_name()+"1",p.get_default_value()); + this->push_param_info(p1); + } + } + if(rhs.pm2) + { + pm2=rhs.pm2->clone(); + np2=rhs.pm2->get_num_params(); + for(int i=0;i p(rhs.pm2->get_param_info(i)); + param_info p2(p.get_name()+"2",p.get_default_value()); + this->push_param_info(p2); + } + } + return *this; + } + + + ~mul_model() + { + if(!pm1) + { + //delete pm1; + pm1->destroy(); + } + if(!pm2) + { + //delete pm2; + pm2->destroy(); + } + } + + public: + Ty do_eval(const Tx& x,const Tp& param) + { + if(!pm1) + { + throw opt_exception("incomplete model!"); + } + if(!pm2) + { + throw opt_exception("incomplete model!"); + } + Tp p1(pm1->get_num_params()); + Tp p2(pm2->get_num_params()); + int i=0; + int j=0; + for(i=0;iget_num_params();++i,++j) + { + set_element(p1,i,get_element(param,j)); + } + for(i=0;iget_num_params();++i,++j) + { + set_element(p2,i,get_element(param,j)); + } + return pm1->eval(x,p1)*pm2->eval(x,p2); + } + }; + + template + mul_model operator*(const model& m1, + const model& m2) + { + return mul_model(m1,m2); + } + +}; + + + +#endif +//EOF -- cgit v1.2.2