Stan Math Library: stan/math/rev/mat/functor/ode_system.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

ode_system.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUNCTOR_ODE_SYSTEM_HPP
2 #define STAN_MATH_REV_MAT_FUNCTOR_ODE_SYSTEM_HPP
3
4 #include <stan/math/rev/core.hpp>
5 #include <stan/math/prim/arr/fun/value_of.hpp>
6 #include <iostream>
7 #include <vector>
8
9 namespace stan {
10 namespace math {
11
20 template <typename F>
21 class ode_system {
22 const F& f_;
23 const std::vector<double> theta_;
24 const std::vector<double>& x_;
25 const std::vector<int>& x_int_;
26 std::ostream* msgs_;
27
28 public:
39 ode_system(const F& f, const std::vector<double> theta,
40 const std::vector<double>& x, const std::vector<int>& x_int,
41 std::ostream* msgs)
42 : f_(f), theta_(theta), x_(x), x_int_(x_int), msgs_(msgs) { }
43
51 inline void operator()(const double t, const std::vector<double>& y,
52 std::vector<double>& dy_dt) const {
53 dy_dt = f_(t, y, theta_, x_, x_int_, msgs_);
54 }
55
66 template <typename Derived1, typename Derived2>
67 void jacobian(const double t, const std::vector<double>& y,
68 Eigen::MatrixBase<Derived1>& dy_dt,
69 Eigen::MatrixBase<Derived2>& Jy) const {
70 using Eigen::Matrix;
71 using Eigen::Map;
72 using Eigen::RowVectorXd;
73 using stan::math::var;
74 using std::vector;
75 vector<double> grad(y.size());
76 Map<RowVectorXd> grad_eig(&grad[0], y.size());
77 try {
78 start_nested();
79 vector<var> y_var(y.begin(), y.end());
80 vector<var> dy_dt_var = f_(t, y_var, theta_, x_, x_int_, msgs_);
81 for (size_t i = 0; i < dy_dt_var.size(); ++i) {
82 dy_dt(i) = dy_dt_var[i].val();
83 set_zero_all_adjoints_nested();
84 dy_dt_var[i].grad(y_var, grad);
85 Jy.row(i) = grad_eig;
86 }
87 } catch (const std::exception& e) {
88 recover_memory_nested();
89 throw;
90 }
91 recover_memory_nested();
92 }
93
106 template <typename Derived1, typename Derived2>
107 void jacobian(const double t, const std::vector<double>& y,
108 Eigen::MatrixBase<Derived1>& dy_dt,
109 Eigen::MatrixBase<Derived2>& Jy,
110 Eigen::MatrixBase<Derived2>& Jtheta) const {
111 using Eigen::Dynamic;
112 using Eigen::Map;
113 using Eigen::Matrix;
114 using Eigen::RowVectorXd;
115 using stan::math::var;
116 using std::vector;
117 vector<double> grad(y.size() + theta_.size());
118 Map<RowVectorXd> grad_eig(&grad[0], y.size() + theta_.size());
119 try {
120 start_nested();
121 vector<var> y_var(y.begin(), y.end());
122 vector<var> theta_var(theta_.begin(), theta_.end());
123 vector<var> z_var;
124 z_var.reserve(y.size() + theta_.size());
125 z_var.insert(z_var.end(), y_var.begin(), y_var.end());
126 z_var.insert(z_var.end(), theta_var.begin(), theta_var.end());
127 vector<var> dy_dt_var = f_(t, y_var, theta_var, x_, x_int_, msgs_);
128 for (size_t i = 0; i < dy_dt_var.size(); ++i) {
129 dy_dt(i) = dy_dt_var[i].val();
130 set_zero_all_adjoints_nested();
131 dy_dt_var[i].grad(z_var, grad);
132 Jy.row(i) = grad_eig.leftCols(y.size());
133 Jtheta.row(i) = grad_eig.rightCols(theta_.size());
134 }
135 } catch (const std::exception& e) {
136 recover_memory_nested();
137 throw;
138 }
139 stan::math::recover_memory_nested();
140 }
141 };
142
143 }
144 }
145 #endif
core.hpp
stan::math::ode_system::operator()
void operator()(const double t, const std::vector< double > &y, std::vector< double > &dy_dt) const
Calculate the RHS of the ODE.
Definition: ode_system.hpp:51
value_of.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::set_zero_all_adjoints_nested
static void set_zero_all_adjoints_nested()
Reset all adjoint values in the top nested portion of the stack to zero.
Definition: set_zero_all_adjoints_nested.hpp:17
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
stan::math::ode_system::jacobian
void jacobian(const double t, const std::vector< double > &y, Eigen::MatrixBase< Derived1 > &dy_dt, Eigen::MatrixBase< Derived2 > &Jy) const
Calculate the Jacobian of the ODE RHS wrt to states y.
Definition: ode_system.hpp:67
stan::math::grad
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
Definition: grad.hpp:30
stan::math::ode_system::ode_system
ode_system(const F &f, const std::vector< double > theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
Construct an ODE model with the specified base ODE system, parameters, data, and a message stream...
Definition: ode_system.hpp:39
stan::math::ode_system::jacobian
void jacobian(const double t, const std::vector< double > &y, Eigen::MatrixBase< Derived1 > &dy_dt, Eigen::MatrixBase< Derived2 > &Jy, Eigen::MatrixBase< Derived2 > &Jtheta) const
Calculate the Jacobian of the ODE RHS wrt to states y and parameters theta.
Definition: ode_system.hpp:107
stan::math::ode_system
Internal representation of an ODE model object which provides convenient Jacobian functions to obtain...
Definition: ode_system.hpp:21
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
stan::math::recover_memory_nested
static void recover_memory_nested()
Recover only the memory used for the top nested call.
Definition: recover_memory_nested.hpp:20
stan::math::start_nested
static void start_nested()
Record the current position so that recover_memory_nested() can find it.
Definition: start_nested.hpp:13

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