Cached.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2005 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/More/MEAL/MEAL/Cached.h
10 
11 #ifndef __Cached_H
12 #define __Cached_H
13 
14 #include "MEAL/CalculatePolicy.h"
15 #include <iostream>
16 
17 namespace MEAL {
18 
20 
23  template<class T>
24  class Cached : public CalculatePolicy<T>
25  {
26  public:
27 
28  typedef typename T::Result Result;
29 
31  Cached (T* context) : CalculatePolicy<T> (context)
32  { gradient_is_cached = false; }
33 
35  Result evaluate (std::vector<Result>* gradient = 0) const try
36  {
37  if (this->get_context()->get_verbose())
38  std::cerr << class_name() + "evaluate" << std::endl;
39 
40  update (gradient != 0);
41 
42  if (gradient)
43  {
44  gradient->resize (cached_gradient.size());
45  for (unsigned i=0; i<cached_gradient.size(); i++)
46  (*gradient)[i] = cached_gradient[i];
47  }
48 
49  return cached_result;
50  }
51  catch (Error& error)
52  {
53  throw error += class_name() + "evaluate";
54  }
55 
57  std::string class_name() const
58  { return "MEAL::Cached[" + this->get_context()->get_name() + "]::"; }
59 
60  protected:
61 
63  void update (bool need_gradient) const
64  {
65  const_cast<Cached*>(this)->update (need_gradient);
66  }
67 
68  void update (bool need_gradient)
69  {
70  if (! (this->get_context()->get_evaluation_changed()
71  || (need_gradient && !gradient_is_cached)))
72  {
73  if (this->get_context()->get_verbose())
74  std::cerr << class_name() + "update no change" << std::endl;
75  return;
76  }
77 
78  std::vector<Result>* grad_ptr = 0;
79 
80  if (need_gradient)
81  {
82  cached_gradient.resize (this->get_context()->get_nparam());
83  grad_ptr = &cached_gradient;
84  gradient_is_cached = true;
85  }
86 
87  this->calculate (cached_result, grad_ptr);
88 
89  this->get_context()->set_evaluation_changed (false);
90  }
91 
93  Result cached_result;
94 
96  std::vector<Result> cached_gradient;
97 
98  private:
99 
101  bool gradient_is_cached;
102 
103  };
104 
105 }
106 
107 #endif
Abstract base class of Function parameter policies.
Definition: CalculatePolicy.h:25
std::vector< Result > cached_gradient
The gradient cached in the last call to calculate.
Definition: Cached.h:106
Function * get_context() const
Return the Function for whom this policy operates.
Definition: FunctionPolicy.h:37
Namespace in which all modeling and calibration related code is declared.
Definition: ExampleComplex2.h:16
void update(bool need_gradient) const
Updates the result (and gradient when needed)
Definition: Cached.h:73
void set_evaluation_changed(bool _changed=true)
Set true if the Function evaluation has changed.
Definition: Function.C:129
virtual std::string get_name() const =0
Return the name of the class.
Result evaluate(std::vector< Result > *gradient=0) const
Implement the evaluate method of the Function.
Definition: Cached.h:45
Cached(T *context)
Default constructor.
Definition: Cached.h:41
Result cached_result
The result cached in the last call to calculate.
Definition: Cached.h:103
std::string class_name() const
Return the name of the class for debugging.
Definition: Cached.h:67

Generated using doxygen 1.8.17