Evaluable.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2009 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/More/MEAL/MEAL/Evaluable.h
10 
11 #ifndef __MEAL_Evaluable_H
12 #define __MEAL_Evaluable_H
13 
14 #include "MEAL/Function.h"
15 #include "MEAL/EvaluationPolicy.h"
16 #include "Estimate.h"
17 
19 template<typename T>
21  typedef Estimate<T> type;
22 };
23 
24 template<typename T>
25 void add_variance (typename EstimateTraits<T>::type& result,
26  double var, const T& grad)
27 {
28  result.var += var * grad * grad;
29 }
30 
31 namespace MEAL {
32 
34  template<typename T>
35  class Evaluable : public Function {
36 
37  public:
38 
40  typedef T Result;
41 
43  static const char* Name;
44 
47  {
48  evaluation_policy = default_evaluation_policy (this);
49  }
50 
53  {
54  evaluation_policy = default_evaluation_policy (this);
55  }
56 
59  {
61  return *this;
62  }
63 
65  T evaluate (std::vector<T>* grad=0) const
66  {
67  return evaluation_policy->evaluate (grad);
68  }
69 
71  typename EstimateTraits<T>::type estimate () const
72  {
73  std::vector< T > gradient;
74  typename EstimateTraits<T>::type result = evaluate (&gradient);
75 
76  const unsigned nparam = get_nparam();
77 
78  if (gradient.size() != nparam)
79  throw Error (InvalidState, "MEAL::Evaluable::evaluate",
80  "gradient.size=%d != nparam=%d", gradient.size(), nparam);
81 
82  for (unsigned iparam=0; iparam<nparam; iparam++)
83  add_variance( result, get_variance(iparam), gradient[iparam]);
84 
85  return result;
86  }
87 
88  protected:
89 
90  template<class P> friend class CalculatePolicy;
91 
93  virtual void calculate (T& result, std::vector<T>*) = 0;
94 
97 
98  void copy_evaluation_policy (const Evaluable* other)
99  {
101  }
102 
104  void calculate (Evaluable* eval, T& result, std::vector<T>* grad)
105  {
106  eval->calculate (result, grad);
107  }
108 
109  };
110 
111 }
112 
113 template<typename T>
114 const char* MEAL::Evaluable<T>::Name = "Evaluable<T>";
115 
116 #endif
T evaluate(std::vector< T > *grad=0) const
Return the Jones matrix and its gradient.
Definition: Evaluable.h:65
Abstract base class of Function parameter policies.
Definition: CalculatePolicy.h:25
EstimateTraits< T >::type estimate() const
Return the Jones Estimate matrix.
Definition: Evaluable.h:71
unsigned get_nparam() const
Return the number of parameters.
Definition: Function.h:115
Template base class of functions with an evaluate method.
Definition: Evaluable.h:35
void calculate(Evaluable *eval, T &result, std::vector< T > *grad)
Use the calculate method of another Evaluable instance.
Definition: Evaluable.h:104
virtual void calculate(T &result, std::vector< T > *)=0
Calculate the complex value and its gradient.
Namespace in which all modeling and calibration related code is declared.
Definition: ExampleComplex2.h:16
static const char * Name
The name of the class.
Definition: Evaluable.h:43
Evaluable()
Default constructor.
Definition: Evaluable.h:46
Pure virtual base class of all functions.
Definition: Function.h:49
Reference::To< EvaluationPolicy< T > > evaluation_policy
The policy for managing function evaluation.
Definition: Evaluable.h:96
double get_variance(unsigned index) const
Return the variance of the specified parameter.
Definition: Function.h:135
virtual void copy(const Function *model)
Does the work for operator =.
Definition: Function.C:58
Function & operator=(const Function &model)
Assignment operator.
Definition: Function.C:52
Works for most scalar types.
Definition: Evaluable.h:20
Evaluable(const Evaluable &copy)
Copy constructor.
Definition: Evaluable.h:52
Evaluable & operator=(const Evaluable &copy)
Assignment operator.
Definition: Evaluable.h:58
T Result
The return type of the evaluate method.
Definition: Evaluable.h:40

Generated using doxygen 1.8.17