Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
chainable.hpp
Go to the documentation of this file.
1 #ifndef __STAN__AGRAD__REV__CHAINABLE_HPP__
2 #define __STAN__AGRAD__REV__CHAINABLE_HPP__
3 
4 #include <vector>
6 
7 namespace stan {
8  namespace agrad {
9 
14  class chainable {
15 
16  public:
17 
22  chainable() { }
23 
29  // handled automatically
30  }
31 
37  virtual void chain() {
38  }
39 
44  virtual void init_dependent() {
45  }
46 
51  virtual void set_zero_adjoint() {
52  }
53 
63  static inline void* operator new(size_t nbytes) {
64  return memalloc_.alloc(nbytes);
65  }
66  };
67 
68 
69 
73  static void set_zero_all_adjoints() {
74  for (size_t i = 0; i < var_stack_.size(); ++i)
75  var_stack_[i]->set_zero_adjoint();
76  for (size_t i = 0; i < var_nochain_stack_.size(); ++i)
77  var_nochain_stack_[i]->set_zero_adjoint();
78  }
79 
92  static void grad(chainable* vi) {
93  std::vector<chainable*>::reverse_iterator it;
94 
95  vi->init_dependent();
96  // propagate derivates for vars
97  for (it = var_stack_.rbegin(); it < var_stack_.rend(); ++it)
98  (*it)->chain();
99  }
100 
101  }
102 }
103 #endif

     [ Stan Home Page ] © 2011–2013, Stan Development Team.