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
17#include "complex_math.h"
18#include "Estimate.h"
19
21template<typename T>
23 typedef Estimate<T> type;
24};
25
26template<typename T>
27void add_variance (typename EstimateTraits<T>::type& result,
28 double var, const T& grad)
29{
30 result.var += var * grad * grad;
31}
32
33namespace MEAL {
34
36 template<typename T>
37 class Evaluable : public Function {
38
39 public:
40
42 typedef T Result;
43
45 static const char* Name;
46
49 {
50 evaluation_policy = default_evaluation_policy (this);
51 }
52
55 {
56 evaluation_policy = default_evaluation_policy (this);
57 }
58
61 {
63 return *this;
64 }
65
67 T evaluate (std::vector<T>* grad=0) const
68 {
69 T result = evaluation_policy->evaluate (grad);
70 if (!true_math::finite(result))
71 throw Error (InvalidState, "MEAL::Evaluable::evaluate for " + this->get_name(),
72 "non-finite result");
73 return result;
74 }
75
77 typename EstimateTraits<T>::type estimate () const
78 {
79 std::vector< T > gradient;
80 typename EstimateTraits<T>::type result = evaluate (&gradient);
81
82 const unsigned nparam = get_nparam();
83
84 if (gradient.size() != nparam)
85 throw Error (InvalidState, "MEAL::Evaluable::evaluate",
86 "gradient.size=%d != nparam=%d", gradient.size(), nparam);
87
88 for (unsigned iparam=0; iparam<nparam; iparam++)
89 add_variance( result, get_variance(iparam), gradient[iparam]);
90
91 return result;
92 }
93
94 protected:
95
96 template<class P> friend class CalculatePolicy;
97
99 virtual void calculate (T& result, std::vector<T>*) = 0;
100
103
104 void copy_evaluation_policy (const Evaluable* other)
105 {
107 }
108
110 void calculate (Evaluable* eval, T& result, std::vector<T>* grad)
111 {
112 eval->calculate (result, grad);
113 }
114
115 };
116
117}
118
119template<typename T>
120const char* MEAL::Evaluable<T>::Name = "Evaluable<T>";
121
122#endif
Abstract base class of Function parameter policies.
Definition CalculatePolicy.h:20
void calculate(Evaluable *eval, T &result, std::vector< T > *grad)
Use the calculate method of another Evaluable instance.
Definition Evaluable.h:110
Evaluable(const Evaluable &copy)
Copy constructor.
Definition Evaluable.h:54
Evaluable()
Default constructor.
Definition Evaluable.h:48
Reference::To< EvaluationPolicy< T > > evaluation_policy
The policy for managing function evaluation.
Definition Evaluable.h:102
T Result
The return type of the evaluate method.
Definition Evaluable.h:42
static const char * Name
The name of the class.
Definition Evaluable.h:45
T evaluate(std::vector< T > *grad=0) const
Return the Jones matrix and its gradient.
Definition Evaluable.h:67
EstimateTraits< T >::type estimate() const
Return the Jones Estimate matrix.
Definition Evaluable.h:77
virtual void calculate(T &result, std::vector< T > *)=0
Calculate the complex value and its gradient.
Evaluable & operator=(const Evaluable &copy)
Assignment operator.
Definition Evaluable.h:60
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
Function()
Default constructor.
Definition Function.C:24
virtual std::string get_name() const =0
Return the name of the class.
unsigned get_nparam() const
Return the number of parameters.
Definition Function.h:115
double get_variance(unsigned index) const
Return the variance of the specified parameter.
Definition Function.h:135
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16
Works for most scalar types.
Definition Evaluable.h:22

Generated using doxygen 1.14.0