PhaseGradients.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2004 - 2016 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/More/MEAL/MEAL/PhaseGradients.h
10 
11 #ifndef __MEAL_PhaseGradients_H
12 #define __MEAL_PhaseGradients_H
13 
14 #include "MEAL/Univariate.h"
15 #include "MEAL/Complex2.h"
16 #include "MEAL/Parameters.h"
17 
18 namespace MEAL {
19 
21 
40  template<typename T>
41  class PhaseGradients : public Univariate<T>
42  {
43 
44  public:
45 
47  PhaseGradients (unsigned ngradient = 0);
48 
51 
54 
56  PhaseGradients* clone () const;
57 
59  unsigned get_ngradient () const;
60 
62  void set_igradient (unsigned igradient);
63 
65  unsigned get_igradient () const;
66 
68  void set_offset (unsigned igradient, double offset);
69 
71  double get_offset (unsigned igradient) const;
72 
74  void add_gradient ();
75 
77  void remove_gradient ();
78 
80  void resize (unsigned ngradient);
81 
82  // ///////////////////////////////////////////////////////////////////
83  //
84  // Function implementation
85  //
86  // ///////////////////////////////////////////////////////////////////
87 
89  std::string get_name () const;
90 
91  protected:
92 
94  void calculate (typename T::Result& result, std::vector<typename T::Result>* gradient);
95 
97  Parameters parameters;
98 
100  std::vector<double> offsets;
101 
103  unsigned igradient;
104 
105  };
106 
107 }
108 
109 
110 template<typename T>
112  : parameters (this)
113 {
114  resize (ncoef);
115  igradient = 0;
116 }
117 
119 template<typename T>
121  : parameters (this)
122 {
123  operator = (copy);
124 }
125 
127 template<typename T>
130 {
131  if (&copy == this)
132  return *this;
133 
134  parameters = copy.parameters;
135  offsets = copy.offsets;
136  igradient = copy.igradient;
137 
138  this->set_evaluation_changed();
139 
140  return *this;
141 }
142 
144 template<typename T>
146 {
147  return new PhaseGradients( *this );
148 }
149 
151 template<typename T>
153 {
154  if (i == igradient)
155  return;
156 
157  if (i >= get_ngradient())
158  throw Error (InvalidParam, "MEAL::PhaseGradients<T>::set_igradient",
159  "invalid index=%d >= ngradient=%d", i, get_ngradient());
160 
161  igradient = i;
162  this->set_evaluation_changed ();
163 }
164 
166 template<typename T>
168 {
169  return igradient;
170 }
171 
173 template<typename T>
174 void MEAL::PhaseGradients<T>::set_offset (unsigned igradient, double offset)
175 {
176 #if 0
177  cerr << "MEAL::PhaseGradients<T>::set_offset"
178  " i=" << igradient << " offset=" << offset << endl;
179 #endif
180  offsets.at(igradient) = offset;
181 }
182 
184 template<typename T>
185 double MEAL::PhaseGradients<T>::get_offset (unsigned igradient) const
186 {
187  return offsets.at(igradient);
188 }
189 
191 template<typename T>
192 void MEAL::PhaseGradients<T>::resize (unsigned ngradient)
193 {
194  unsigned current = get_ngradient();
195 
196  if (current == ngradient)
197  return;
198 
199  parameters.resize( ngradient );
200  offsets.resize (ngradient);
201 
202  for (unsigned i=current; i<ngradient; i++)
203  {
204  parameters.set_name (i, "phi_" + tostring(i));
205  offsets[i] = 0.0;
206  }
207 
208  if (ngradient)
209  set_igradient (ngradient-1);
210 }
211 
213 template<typename T>
215 {
216  resize ( get_ngradient() + 1 );
217 }
218 
220 template<typename T>
222 {
223  resize ( get_ngradient() - 1 );
224 }
225 
227 template<typename T>
229 {
230  return parameters.get_nparam();
231 }
232 
234 template<typename T>
236 {
237  return "PhaseGradients";
238 }
239 
241 template<typename T>
242 void MEAL::PhaseGradients<T>::calculate (typename T::Result& result,
243  std::vector<typename T::Result>* grad)
244 {
245  double x = this->get_abscissa();
246 
247  double phase = (this->get_param(igradient) + this->get_offset(igradient)) * x;
248 
249  if (this->verbose)
250  std::cerr << "MEAL::PhaseGradients<T>::calculate phase=" << phase
251  << std::endl;
252 
253  double cos_phase = ::cos(phase);
254  double sin_phase = ::sin(phase);
255 
256  result = std::complex<double>(cos_phase, sin_phase);
257 
258  if (grad)
259  {
260  for (unsigned i=0; i<grad->size(); i++)
261  (*grad)[i] = 0.0;
262 
263  (*grad)[igradient] = x * std::complex<double>(-sin_phase, cos_phase);
264 
265  if (this->verbose)
266  std::cerr << "MEAL::PhaseGradients<T>::calculate gradient" << std::endl
267  << " " << (*grad)[igradient] << std::endl;
268  }
269 }
270 
271 #endif // __MEAL_PhaseGradients_H
PhaseGradients * clone() const
Clone operator.
Definition: PhaseGradients.h:145
unsigned igradient
The current phase gradient.
Definition: PhaseGradients.h:113
void remove_gradient()
Remove the last gradient from the set.
Definition: PhaseGradients.h:221
Parameters parameters
Parameter policy.
Definition: PhaseGradients.h:107
void add_gradient()
Add another gradient to the set.
Definition: PhaseGradients.h:214
void calculate(typename T::Result &result, std::vector< typename T::Result > *gradient)
Calculate the Jones matrix and its gradient.
Definition: PhaseGradients.h:242
const ScalarMath sin(const ScalarMath &x)
Return a ScalarMath instance representing sin(x)
Definition: ScalarMath.C:160
void resize(unsigned ngradient)
Set the number of gradients.
Definition: PhaseGradients.h:192
void set_igradient(unsigned igradient)
Set the current phase gradient index.
Definition: PhaseGradients.h:152
PhaseGradients & operator=(const PhaseGradients &)
Assignment operator.
Definition: PhaseGradients.h:129
PhaseGradients(unsigned ngradient=0)
Default constructor.
Definition: PhaseGradients.h:111
Namespace in which all modeling and calibration related code is declared.
Definition: ExampleComplex2.h:16
void set_offset(unsigned igradient, double offset)
Set the current phase gradient index.
Definition: PhaseGradients.h:174
unsigned get_ngradient() const
Get the number of gradients.
Definition: PhaseGradients.h:228
const ScalarMath cos(const ScalarMath &x)
Return a ScalarMath instance representing cos(x)
Definition: ScalarMath.C:168
double get_offset(unsigned igradient) const
Get the current phase gradient index.
Definition: PhaseGradients.h:185
unsigned get_igradient() const
Get the current phase gradient index.
Definition: PhaseGradients.h:167
std::string get_name() const
Return the name of the class.
Definition: PhaseGradients.h:235
Multiple phase gradients parameterized by their slopes.
Definition: PhaseGradients.h:46
std::vector< double > offsets
The phase offsets.
Definition: PhaseGradients.h:110

Generated using doxygen 1.8.17