BasisRule.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2004 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/More/MEAL/MEAL/BasisRule.h
10
11#ifndef __MEAL_BasisRule_H
12#define __MEAL_BasisRule_H
13
14//#include "MEAL/ProjectGradient.h"
15#include "MEAL/Composite.h"
16#include "MEAL/Null.h"
17#include "Matrix.h"
18#include "tostring.h"
19
20namespace MEAL {
21
23
27
28 template<unsigned N, class T>
29 class BasisRule : public T
30 {
31
32 public:
33
34 typedef typename T::Result Result;
35
37 BasisRule () : composite (this) { init(); }
38
40 BasisRule (const BasisRule& rule) : composite (this) { init(); }
41
43 BasisRule& operator = (const BasisRule& rule);
44
46 void set_model (T* model);
47
49 void set_transformation (const Matrix<N,N,double>& xform);
50
51 // ///////////////////////////////////////////////////////////////////
52 //
53 // Function implementation
54 //
55 // ///////////////////////////////////////////////////////////////////
56
58 std::string get_name () const;
59
60 protected:
61
63 void calculate (Result& result, std::vector<Result>* gradient);
64
67
70
73
75 Matrix<N,N,double> transformation;
76
77 private:
78
80 Composite composite;
81
82 void init ()
83 { parameters.resize(N); project = &parameters;
84 composite.map (project); matrix_identity (transformation); }
85
86 };
87
88}
89
90template<unsigned N, class T>
92{
93 return "BasisRule<" + tostring(N) + "," + std::string(T::Name)+ ">";
94}
95
96template<unsigned N, class T>
99{
100 if (this == &rule)
101 return *this;
102
103 set_model (rule.model);
105
106 return *this;
107}
108
109template<unsigned N, class T>
111{
112 if (model)
113 {
114 if (T::verbose)
115 std::cerr << "MEAL::BasisRule::set_model"
116 " unmap old model" << std::endl;
117 composite.unmap (model);
118 }
119
120 model = _model;
121
122 if (!_model)
123 return;
124
125 if (T::verbose)
126 std::cerr << "MEAL::BasisRule::set_model"
127 " map new model" << std::endl;
128
129 composite.map (model);
130
131 Vector<N,double> params;
132
133 for (unsigned i=0; i<N; i++)
134 {
135 model->set_infit (i, false);
136 params[i] = model->get_param(i);
137 }
138
139 params = inv(transformation) * params;
140
141 for (unsigned i=0; i<N; i++)
142 parameters.set_param(i, params[i]);
143
144}
145
146template<unsigned N, class T>
147void MEAL::BasisRule<N,T>::set_transformation (const Matrix<N,N,double>& xform)
148{
149 transformation = xform;
150
151 if (!model)
152 return;
153
154 Vector<N,double> params;
155
156 for (unsigned i=0; i<N; i++)
157 params[i] = model->get_param(i);
158
159 params = inv(transformation) * params;
160
161 for (unsigned i=0; i<N; i++)
162 parameters.set_param(i, params[i]);
163}
164
165template<unsigned N, class T>
167 std::vector<Result>* grad)
168{
169 if (!model)
170 throw Error (InvalidState, "MEAL::BasisRule::calculate","no model");
171
172 if (T::verbose)
173 std::cerr << "MEAL::BasisRule::calculate" << std::endl;
174
175 Vector<N,double> params;
176 for (unsigned i=0; i<N; i++)
177 params[i] = parameters.get_param(i);
178
179 params = transformation * params;
180
181 for (unsigned i=0; i<N; i++)
182 model->set_param (i, params[i]);
183
184 std::vector<Result> model_grad;
185 std::vector<Result>* model_grad_ptr = 0;
186 if (grad)
187 model_grad_ptr = & model_grad;
188
189 result = model->evaluate (model_grad_ptr);
190
191 if (grad) {
192
193 unsigned ngrad = this->get_nparam();
194 grad->resize (ngrad);
195
196 unsigned igrad;
197 for (igrad=0; igrad<ngrad; igrad++)
198 (*grad)[igrad] = 0.0;
199
200 // map the model gradient
201 ProjectGradient (model, model_grad, *(grad));
202
203 // map the scalar gradients
204 std::vector<Result> fgrad (N, 0);
205
206 for (igrad=0; igrad<N; igrad++)
207 for (unsigned idep=0; idep<N; idep++)
208 fgrad[igrad] += model_grad[idep] * transformation[idep][igrad];
209
210 ProjectGradient (project, fgrad, *(grad));
211
212 }
213
214 if (T::verbose) {
215 std::cerr << "MEAL::BasisRule::calculate result\n"
216 " " << result << std::endl;
217 if (grad) {
218 std::cerr << "MEAL::BasisRule::calculate gradient\n";
219 for (unsigned i=0; i<grad->size(); i++)
220 std::cerr << " " << i << ":" << this->get_infit(i) << "="
221 << (*grad)[i] << std::endl;
222 }
223 }
224
225}
226
227#endif
Changes the basis of the parameterization.
Definition BasisRule.h:30
Project< T > model
The Function with the parameters to be transformed.
Definition BasisRule.h:66
BasisRule(const BasisRule &rule)
Copy constructor.
Definition BasisRule.h:40
Matrix< N, N, double > transformation
The basis transformation.
Definition BasisRule.h:75
BasisRule()
Default constructor.
Definition BasisRule.h:37
void set_transformation(const Matrix< N, N, double > &xform)
Set the basis transformation.
Definition BasisRule.h:147
std::string get_name() const
Return the name of the class.
Definition BasisRule.h:91
void set_model(T *model)
Set the Function with the parameters to be transformed.
Definition BasisRule.h:110
Null parameters
The free parameters.
Definition BasisRule.h:69
Project< Null > project
The projection of the free parameters.
Definition BasisRule.h:72
void calculate(Result &result, std::vector< Result > *gradient)
Return the Result and its gradient.
Definition BasisRule.h:166
BasisRule & operator=(const BasisRule &rule)
Assignment operator.
Definition BasisRule.h:98
Parameter policy for composite functions.
Definition Composite.h:20
Null function holds an arbitrary number of parameters.
Definition Null.h:20
Template combines a reference to a Component and its Projection.
Definition Projection.h:60
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16

Generated using doxygen 1.14.0