Function.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2004-2009 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/More/MEAL/MEAL/Function.h
10 
26 #ifndef __MEAL_Function_H
27 #define __MEAL_Function_H
28 
29 #include "MEAL/Argument.h"
30 #include "MEAL/ArgumentPolicy.h"
31 #include "MEAL/ParameterPolicy.h"
32 #include "Callback.h"
33 #include "Estimate.h"
34 
35 #include <string>
36 
37 // forward declaration of text interface
38 namespace TextInterface { class Parser; };
39 
41 
42 namespace MEAL {
43 
45 
49  class Function : public Reference::Able {
50 
51  public:
52 
54  static bool verbose;
55 
57  static bool very_verbose;
58 
60  static bool check_zero;
61 
63  static bool check_variance;
64 
66  static bool cache_results;
67 
69  template<class Model>
70  static Model* load (const std::string& filename);
71 
73  static Function* load_Function (const std::string& filename);
74 
76  static Function* factory (const std::string& text);
77 
79  Function ();
80 
82  Function (const Function& model);
83 
85  Function& operator = (const Function& model);
86 
88  virtual ~Function ();
89 
91  virtual Function* clone () const;
92 
94  virtual void copy (const Function* model);
95 
97  class Interface;
98 
101 
103  virtual void parse (const std::string& text);
104 
106  virtual void print (std::string& text) const;
107 
109  virtual std::string get_name () const = 0;
110 
112  virtual std::string get_description () const { return ""; }
113 
115  unsigned get_nparam () const
116  { if (parameter_policy) return parameter_policy->get_nparam (); return 0; }
117 
119  std::string get_param_name (unsigned index) const
120  { return parameter_policy->get_name (index); }
121 
123  std::string get_param_description (unsigned index) const
124  { return parameter_policy->get_description (index); }
125 
127  double get_param (unsigned index) const
128  { return parameter_policy->get_param (index); }
129 
131  void set_param (unsigned index, double value)
132  { parameter_policy->set_param (index,value); }
133 
135  double get_variance (unsigned index) const
136  { return parameter_policy->get_variance (index); }
137 
139  void set_variance (unsigned index, double value)
140  { parameter_policy->set_variance (index,value); }
141 
143  bool get_infit (unsigned index) const
144  { return parameter_policy->get_infit (index); }
145 
147  void set_infit (unsigned index, bool flag)
148  { parameter_policy->set_infit (index,flag); }
149 
151  void set_argument (unsigned dimension, Argument* axis)
152  { if (argument_policy) argument_policy->set_argument (dimension, axis); }
153 
155  Estimate<double> get_Estimate (unsigned index) const;
156 
158  void set_Estimate (unsigned index, const Estimate<double>& param);
159 
161  void set_verbose (bool);
162 
164  bool get_verbose () const;
165 
167  enum Attribute {
172  };
173 
176 
178  void set_evaluation_changed (bool _changed = true);
179 
181  bool get_evaluation_changed () const { return evaluation_changed; }
182 
185  { return parameter_policy; }
186 
188  bool has_parameter_policy () const { return parameter_policy; }
189 
191  virtual void print_parameters (std::string& text,
192  const std::string& separator) const;
193 
194  protected:
195 
196  friend class ParameterPolicy;
197 
200 
201  friend class ArgumentPolicy;
202 
205 
207  void copy_evaluation_changed (const Function& model)
209 
211  void copy_parameter_policy (const Function*);
212 
214  void set_parameter_policy (ParameterPolicy* policy);
215 
218 
221 
222  private:
223 
225  bool evaluation_changed;
226 
228  void init ();
229 
230  };
231 
232 }
233 
234 template<class Model>
235 Model* MEAL::Function::load (const std::string& filename)
236 {
237  Function* function = load_Function (filename);
238 
239  Model* model = dynamic_cast<Model*> (function);
240  if (!model)
241  throw Error (InvalidState, "MEAL::Function::load",
242  "function is not a %s", Model::Name);
243 
244  return model;
245 }
246 
247 #endif
void set_verbose(bool)
Set the verbosity of this instance.
Definition: Function.C:117
void set_param(unsigned index, double value)
Set the value of the specified parameter.
Definition: Function.h:131
bool has_parameter_policy() const
Some wrappers may not have a parameter policy.
Definition: Function.h:188
virtual TextInterface::Parser * get_interface()
Return a text interface that can be used to access this instance.
Definition: Function.C:148
Reference::To< ParameterPolicy > parameter_policy
The policy for managing function parameters.
Definition: Function.h:199
bool get_evaluation_changed() const
Return true if the Function evaluation has changed.
Definition: Function.h:181
unsigned get_nparam() const
Return the number of parameters.
Definition: Function.h:115
virtual void print_parameters(std::string &text, const std::string &separator) const
Prints the values of model parameters and fit flags to a string.
Definition: Function_print.C:22
Reference::To< ArgumentPolicy > argument_policy
The policy for managing function arguments.
Definition: Function.h:204
const ParameterPolicy * get_parameter_policy() const
Provide access to the parameter_policy attribute.
Definition: Function.h:184
virtual std::string get_description() const
Return the description of the class.
Definition: Function.h:112
virtual ~Function()
Virtual destructor.
Definition: Function.C:45
Pure virtual base class of function arguments.
Definition: Argument.h:30
Managers of Function arguments.
Definition: ArgumentPolicy.h:26
virtual void print(std::string &text) const
Prints the values of model parameters and fit flags to a string.
Definition: Function_print.C:14
void set_parameter_policy(ParameterPolicy *policy)
Set the parameter policy.
Definition: Function.C:97
Provides a text interface to get and set Function attributes.
Definition: FunctionInterface.h:26
static Function * factory(const std::string &text)
Construct a new Function instance from a string.
Definition: Function_factory.C:40
static bool check_zero
When set, some Functions will throw an Error if they evaluate to zero.
Definition: Function.h:60
void set_Estimate(unsigned index, const Estimate< double > &param)
Set the Estimate of the specified parameter.
Definition: Function.C:109
Namespace in which all modeling and calibration related code is declared.
Definition: ExampleComplex2.h:16
static bool verbose
Verbosity flag.
Definition: Function.h:54
void copy_evaluation_changed(const Function &model)
Copy the evaluation changed state of another model instance.
Definition: Function.h:207
static bool very_verbose
Low-level verbosity flag.
Definition: Function.h:57
void set_evaluation_changed(bool _changed=true)
Set true if the Function evaluation has changed.
Definition: Function.C:129
double get_param(unsigned index) const
Return the value of the specified parameter.
Definition: Function.h:127
void set_variance(unsigned index, double value)
Set the variance of the specified parameter.
Definition: Function.h:139
@ ParameterCount
Number of Function parameters, as returned by get_nparam.
Definition: Function.h:169
std::string get_param_description(unsigned index) const
Return the description of the specified parameter.
Definition: Function.h:123
static bool check_variance
When set, some Functions will throw an Error if input variance <= 0.
Definition: Function.h:63
virtual std::string get_name() const =0
Return the name of the class.
static Model * load(const std::string &filename)
Construct a new Model instance from a file.
Definition: Function.h:235
bool get_infit(unsigned index) const
Return true if parameter at index is to be fitted.
Definition: Function.h:143
std::string get_param_name(unsigned index) const
Return the name of the specified parameter.
Definition: Function.h:119
static bool cache_results
When set, use the Cached evaluation policy and callbacks.
Definition: Function.h:66
Managers of Function parameters.
Definition: ParameterPolicy.h:24
Pure virtual base class of all functions.
Definition: Function.h:49
bool get_verbose() const
Get the verbosity of this instance.
Definition: Function.C:123
virtual void parse(const std::string &text)
Parses the values of model parameters and fit flags from a string.
Definition: Function_parse.C:16
Attribute
Function attributes that require the attention of Composite models.
Definition: Function.h:167
bool set_parameter_policy_context
Set the parameter policy context.
Definition: Function.h:217
static Function * load_Function(const std::string &filename)
Construct a new Function instance from a file.
Definition: Function_load.C:12
Callback< Attribute > changed
Callback executed when a Function Attribute has been changed.
Definition: Function.h:175
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
void copy_parameter_policy(const Function *)
Copy the parameter policy of another instance.
Definition: Function.C:91
void set_argument(unsigned dimension, Argument *axis)
Set the independent variable of the specified dimension.
Definition: Function.h:151
Function & operator=(const Function &model)
Assignment operator.
Definition: Function.C:52
Function()
Default constructor.
Definition: Function.C:24
Estimate< double > get_Estimate(unsigned index) const
Return an Estimate of the specified parameter.
Definition: Function.C:104
virtual Function * clone() const
Clone.
Definition: Function.C:86
bool this_verbose
The verbosity of this instance.
Definition: Function.h:220
@ Evaluation
Function evaluation, as returned by the evaluate method.
Definition: Function.h:171
void set_infit(unsigned index, bool flag)
Set flag for parameter at index to be fitted.
Definition: Function.h:147

Generated using doxygen 1.8.17