YAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parameter.h
Go to the documentation of this file.
1 /* YAP - Yet another PWA toolkit
2  Copyright 2015, Technische Universitaet Muenchen,
3  Authors: Daniel Greenwald, Johannes Rauch
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
20 
21 #ifndef yap_Parameter_h
22 #define yap_Parameter_h
23 
24 #include "fwd/Parameter.h"
25 
26 #include "Exceptions.h"
27 #include "VariableStatus.h"
28 
29 #include <algorithm>
30 #include <complex>
31 #include <memory>
32 #include <numeric>
33 
34 namespace yap {
35 
41 {
42 protected:
43 
46 
47 public:
48 
51  { return VariableStatus_; }
52 
55  { return VariableStatus_; }
56 
58  virtual const size_t size() const = 0;
59 
61  virtual void setValue(const std::vector<double>& V) = 0;
62 
63 private:
64 
67 
68 };
69 
71 template <typename IterType>
72 constexpr VariableStatus variable_status(IterType first, IterType last)
73 {
74  return std::any_of(first, last,
75  [](const std::shared_ptr<ParameterBase>& p)
76  {return p->variableStatus() == VariableStatus::changed;})
78 }
79 
85 template <typename T>
86 class Parameter : public ParameterBase
87 {
88 public:
89 
91  Parameter() = default;
92 
94  explicit Parameter(T t) : ParameterBase(), ParameterValue_(t)
95  {}
96 
98  ~Parameter() = default;
99 
101  Parameter(const Parameter&) = default;
102 
104  Parameter(Parameter&&) = default;
105 
107  Parameter& operator=(const Parameter&) = default;
108 
110  Parameter& operator=(Parameter&&) = default;
111 
113  virtual typename std::conditional<std::is_fundamental<T>::value, const T, const T&>::type
114  value() const
115  { return ParameterValue_; }
116 
118 
120  virtual void setValue(typename std::conditional<std::is_fundamental<T>::value, const T, const T&>::type val)
121  {
123  throw exceptions::ParameterIsFixed("", "Parameter::setValue");
124  if (ParameterValue_ == val)
125  return;
126  ParameterValue_ = val;
128  }
129 
131  Parameter& operator=(typename std::conditional<std::is_fundamental<T>::value, const T, const T&>::type t)
132  { setValue(t); return *this; }
133 
134 
135 private:
136 
139 
140 };
141 
143 template <typename T>
144 inline std::string to_string(const Parameter<T>& P)
145 { using std::to_string; return to_string(P.value()) + " (" + to_string(P.variableStatus()) + ")"; }
146 
149 class RealParameter : public Parameter<double>
150 {
151 public:
152 
154  RealParameter(double t = 0) : Parameter(t) {}
155 
157  const size_t size() const {return 1;}
158 
159  using Parameter::setValue;
160 
162  void setValue(const std::vector<double>& V)
163  { setValue(V[0]); }
164 
165  using Parameter::operator=;
166 };
167 
170 class ComplexParameter : public Parameter<std::complex<double> >
171 {
172 public:
173 
175  ComplexParameter(const std::complex<double>& t = 0) : Parameter(t) {}
176 
178  const size_t size() const {return 2;}
179 
180  using Parameter::setValue;
181 
183  void setValue(const std::vector<double>& V)
184  { setValue(std::complex<double>(V[0], V[1])); }
185 
186  using Parameter::operator=;
187 };
188 
189 
194 {
195 public:
198  ComplexComponentParameter(std::shared_ptr<ComplexParameter> par);
199 
201  const double value() const override
202  { return component(Parent_->value()); }
203 
205 
207  void setValue(double val) override;
208 
210  std::shared_ptr<ComplexParameter> parent()
211  { return Parent_; }
212 
213  using RealParameter::operator=;
214 
215 protected:
216 
218  virtual double component(const std::complex<double>& c) const = 0;
219 
221  virtual std::complex<double> setComponent(const std::complex<double>& c, double v) const = 0;
222 
223 private:
224 
226  std::shared_ptr<ComplexParameter> Parent_;
227 
228 };
229 
234 {
235 public:
236 
239  RealComponentParameter(std::shared_ptr<ComplexParameter> par)
240  : ComplexComponentParameter(par) {}
241 
242  using ComplexComponentParameter::operator=;
243 
244 protected:
245 
247  double component(const std::complex<double>& c) const override
248  { return real(c); }
249 
251  virtual std::complex<double> setComponent(const std::complex<double>& c, double v) const override
252  { return std::complex<double>(v, imag(c)); }
253 
254 };
255 
260 {
261 public:
262 
265  ImaginaryComponentParameter(std::shared_ptr<ComplexParameter> par)
266  : ComplexComponentParameter(par) {}
267 
268  using ComplexComponentParameter::operator=;
269 
270 protected:
271 
273  double component(const std::complex<double>& c) const override
274  { return imag(c); }
275 
277  virtual std::complex<double> setComponent(const std::complex<double>& c, double v) const override
278  { return std::complex<double>(real(c), v); }
279 
280 };
281 
283 inline const size_t size(const ParameterVector& V)
284 { return std::accumulate(V.begin(), V.end(), size_t(0), [](size_t& s, const ParameterVector::value_type & p) {return s += p->size();}); }
285 
289 template <class InputIt>
290 std::vector<double>::const_iterator set_value(ParameterBase& P, InputIt first)
291 { std::vector<double> V(first, first + P.size()); P.setValue(V); return first += (P.size() - 1); }
292 
294 template <class InputIt>
295 void set_values(ParameterVector::iterator first_par, ParameterVector::iterator last_par, InputIt first_val, InputIt last_val)
296 {
297  while (first_par != last_par and first_val != last_val) {
298  first_val = set_value(**first_par, first_val);
299  ++first_par;
300  ++first_val;
301  }
302  if (first_par != last_par)
303  throw exceptions::Exception("insufficient number of values provided", "set_values");
304 }
305 
307 inline void set_values(ParameterVector& pars, const std::vector<double>& vals)
308 { set_values(pars.begin(), pars.end(), vals.begin(), vals.end()); }
309 
310 }
311 
312 #endif
ComplexComponentParameter(std::shared_ptr< ComplexParameter > par)
Definition: Parameter.cxx:6
RealIntegralElement real(const ComplexIntegralElement &Z)
Definition: IntegralElement.h:144
double component(const std::complex< double > &c) const override
Definition: Parameter.h:273
VariableStatus VariableStatus_
Status of variable.
Definition: Parameter.h:66
const size_t size() const
Definition: Parameter.h:157
Class holding basic properties of a parameter, but not a value!
Definition: Parameter.h:40
ComplexParameter(const std::complex< double > &t=0)
constructor
Definition: Parameter.h:175
std::string to_string(const Parameter< T > &P)
Definition: Parameter.h:144
VariableStatus
Definition: VariableStatus.h:29
const double value() const override
Definition: Parameter.h:201
Variable is free and has been changed.
const size_t size() const
Definition: Parameter.h:178
Parameter & operator=(typename std::conditional< std::is_fundamental< T >::value, const T, const T & >::type t)
set value by operator
Definition: Parameter.h:131
virtual std::conditional< std::is_fundamental< T >::value, const T, const T & >::type value() const
Definition: Parameter.h:114
virtual double component(const std::complex< double > &c) const =0
T ParameterValue_
Value stored.
Definition: Parameter.h:138
constexpr VariableStatus variable_status(IterType first, IterType last)
Definition: Parameter.h:72
virtual std::complex< double > setComponent(const std::complex< double > &c, double v) const override
Definition: Parameter.h:251
Abstract base allowing access to the components of a ComplexParameter as a RealParameter.
Definition: Parameter.h:193
double component(const std::complex< double > &c) const override
Definition: Parameter.h:247
const VariableStatus variableStatus() const
Definition: Parameter.h:54
RealParameter(double t=0)
constructor
Definition: Parameter.h:154
ImaginaryComponentParameter(std::shared_ptr< ComplexParameter > par)
Definition: Parameter.h:265
void setValue(double val) override
set value by accessing parent
Definition: Parameter.cxx:16
void setValue(const std::vector< double > &V)
Set value from vector.
Definition: Parameter.h:183
const size_t size(const ParameterVector &V)
Definition: Parameter.h:283
std::shared_ptr< ComplexParameter > Parent_
ComplexParameter this object points to a component of.
Definition: Parameter.h:226
RealComponentParameter(std::shared_ptr< ComplexParameter > par)
Definition: Parameter.h:239
std::shared_ptr< ComplexParameter > parent()
Definition: Parameter.h:210
virtual void setValue(typename std::conditional< std::is_fundamental< T >::value, const T, const T & >::type val)
set value
Definition: Parameter.h:120
std::vector< double >::const_iterator set_value(ParameterBase &P, InputIt first)
Definition: Parameter.h:290
Base class for handling YAP exceptions.
Definition: Exceptions.h:39
virtual const size_t size() const =0
ImaginaryParameter accessing imaginary component of ComplexParameter.
Definition: Parameter.h:259
void setValue(const std::vector< double > &V)
Set value from vector.
Definition: Parameter.h:162
virtual void setValue(const std::vector< double > &V)=0
Set value from vector.
virtual std::complex< double > setComponent(const std::complex< double > &c, double v) const =0
Variable is free but has not been changed.
VariableStatus & variableStatus()
Definition: Parameter.h:50
virtual std::complex< double > setComponent(const std::complex< double > &c, double v) const override
Definition: Parameter.h:277
std::string to_string(const CachedValue::Status &S)
streaming operator for CachedValue::Status
Definition: CachedValue.cxx:27
Definition: Parameter.h:170
RealParameter accessing real component of ComplexParameter.
Definition: Parameter.h:233
void set_values(ParameterVector::iterator first_par, ParameterVector::iterator last_par, InputIt first_val, InputIt last_val)
set values in ParameterVector from iterators
Definition: Parameter.h:295
Definition: Parameter.h:149
ParameterBase()
Constructor.
Definition: Parameter.h:45
Variable is fixed.
Template class holding also a value for a parameter.
Definition: Parameter.h:86
Definition: Exceptions.h:99
Parameter & operator=(const Parameter &)=default
copy assignment defaulted
~Parameter()=default
virtual destructor defaulted
RealIntegralElement imag(const ComplexIntegralElement &Z)
Definition: IntegralElement.h:148
Parameter(T t)
Value-assigning constructor.
Definition: Parameter.h:94
Parameter()=default
Default constructor.