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
18namespace 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
98
100 std::vector<double> offsets;
101
103 unsigned igradient;
104
105 };
106
107}
108
109
110template<typename T>
112 : parameters (this)
113{
114 resize (ncoef);
115 igradient = 0;
116}
117
119template<typename T>
121 : parameters (this)
122{
123 operator = (copy);
124}
125
127template<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
144template<typename T>
146{
147 return new PhaseGradients( *this );
148}
149
151template<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
166template<typename T>
168{
169 return igradient;
170}
171
173template<typename T>
174void 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
184template<typename T>
186{
187 return offsets.at(igradient);
188}
189
191template<typename T>
192void 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
213template<typename T>
218
220template<typename T>
225
227template<typename T>
229{
230 return parameters.get_nparam();
231}
232
234template<typename T>
236{
237 return "PhaseGradients";
238}
239
241template<typename T>
242void 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
Abstract base class implements parameter storage and access.
Definition Parameters.h:23
Multiple phase gradients parameterized by their slopes.
Definition PhaseGradients.h:42
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
std::string get_name() const
Return the name of the class.
Definition PhaseGradients.h:235
void resize(unsigned ngradient)
Set the number of gradients.
Definition PhaseGradients.h:192
unsigned get_igradient() const
Get the current phase gradient index.
Definition PhaseGradients.h:167
void remove_gradient()
Remove the last gradient from the set.
Definition PhaseGradients.h:221
void add_gradient()
Add another gradient to the set.
Definition PhaseGradients.h:214
PhaseGradients(unsigned ngradient=0)
Default constructor.
Definition PhaseGradients.h:111
double get_offset(unsigned igradient) const
Get the current phase gradient index.
Definition PhaseGradients.h:185
void calculate(typename T::Result &result, std::vector< typename T::Result > *gradient)
Calculate the Jones matrix and its gradient.
Definition PhaseGradients.h:242
unsigned igradient
The current phase gradient.
Definition PhaseGradients.h:103
std::vector< double > offsets
The phase offsets.
Definition PhaseGradients.h:100
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 * clone() const
Clone operator.
Definition PhaseGradients.h:145
Parameters parameters
Parameter policy.
Definition PhaseGradients.h:97
double get_abscissa() const
Get the abscissa value.
Definition Univariate.h:46
Univariate()
Default constructor.
Definition Univariate.h:28
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16
const ScalarMath sin(const ScalarMath &x)
Return a ScalarMath instance representing sin(x)
Definition ScalarMath.C:160
const ScalarMath cos(const ScalarMath &x)
Return a ScalarMath instance representing cos(x)
Definition ScalarMath.C:168

Generated using doxygen 1.14.0