aboutsummaryrefslogtreecommitdiffstats
path: root/models/mul_model.hpp
blob: 463f39af971113eacb2bf50c93405e46cb950578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
   \file mul_model.hpp
   \brief the multiply of two models
   \author Junhua Gu
 */

#ifndef MUL_MODEL_HPP
#define MUL_MODEL_HPP
#define OPT_HEADER
#include <core/fitter.hpp>
#include <cmath>

namespace opt_utilities
{
  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  class mul_model
    :public model<Ty,Tx,Tp,Tstr>
  {
  private:
    model<Ty,Tx,Tp,Tstr>* do_clone()const
    {
      return new mul_model<Ty,Tx,Tp,Tstr>(*this);
    }
  private:
    mul_model()
    {
    }

  private:
    model<Ty,Tx,Tp,Tstr>* pm1;
    model<Ty,Tx,Tp,Tstr>* pm2;

  public:
    mul_model(const model<Ty,Tx,Tp,Tstr>& m1,
		 const model<Ty,Tx,Tp,Tstr>& m2)
      :pm1(m1.clone()),pm2(m2.clone())
    {
      int np1=m1.get_num_params();
      int np2=m2.get_num_params();
      for(int i=0;i<np1;++i)
	{
	  param_info<Tp,Tstr> p(m1.get_param_info(i));
	  param_info<Tp,Tstr> p1(p.get_name()+"1",p.get_value());
	  this->push_param_info(p1);
	}
      for(int i=0;i<np2;++i)
	{
	  param_info<Tp,Tstr> p(m2.get_param_info(i));
	  param_info<Tp,Tstr> p2(p.get_name()+"2",p.get_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<np1;++i)
	    {
	      param_info<Tp,Tstr> p(rhs.pm1->get_param_info(i));
	      param_info<Tp,Tstr> p1(p.get_name()+"1",p.get_value());
	      this->push_param_info(p1);
	    }
	}
      if(rhs.pm2)
	{
	  pm2=rhs.pm2->clone();
	  np2=rhs.pm2->get_num_params();
	  for(int i=0;i<np2;++i)
	    {
	      param_info<Tp,Tstr> p(rhs.pm2->get_param_info(i));
	      param_info<Tp,Tstr> p2(p.get_name()+"2",p.get_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<np1;++i)
	    {
	      param_info<Tp,Tstr> p(rhs.pm1->get_param_info(i));
	      param_info<Tp,Tstr> p1(p.get_name()+"1",p.get_value());
	      this->push_param_info(p1);
	    }
	}
      if(rhs.pm2)
	{
	  pm2=rhs.pm2->clone();
	  np2=rhs.pm2->get_num_params();
	  for(int i=0;i<np2;++i)
	    {
	      param_info<Tp,Tstr> p(rhs.pm2->get_param_info(i));
	      param_info<Tp,Tstr> p2(p.get_name()+"2",p.get_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;i<pm1->get_num_params();++i,++j)
	{
	  set_element(p1,i,get_element(param,j));
	}
      for(i=0;i<pm2->get_num_params();++i,++j)
	{
	  set_element(p2,i,get_element(param,j));
	}
      return pm1->eval(x,p1)*pm2->eval(x,p2);
    }
  };

  template <typename Ty,typename Tx,typename Tp,typename Tstr>
  mul_model<Ty,Tx,Tp,Tstr> operator*(const model<Ty,Tx,Tp,Tstr>& m1,
				     const model<Ty,Tx,Tp,Tstr>& m2)
  {
    return mul_model<Ty,Tx,Tp,Tstr>(m1,m2);
  }

}



#endif
//EOF