1 #ifndef STAN_MATH_REV_ARR_FUNCTOR_COUPLED_ODE_SYSTEM_HPP
2 #define STAN_MATH_REV_ARR_FUNCTOR_COUPLED_ODE_SYSTEM_HPP
34 std::vector<std::vector<var> >& y) {
35 for (
size_t n = 0; n < y.size(); n++)
36 for (
size_t m = 0; m < y0.size(); m++)
67 const std::vector<double>&
x_;
87 const std::vector<double>& y0,
88 const std::vector<var>& theta,
89 const std::vector<double>& x,
90 const std::vector<int>& x_int,
95 theta_dbl_(theta.
size(), 0.0),
102 for (
size_t m = 0; m <
M_; m++)
124 std::vector<double>& dz_dt,
128 vector<double> y(z.begin(), z.begin() +
N_);
129 dz_dt = f_(t, y, theta_dbl_, x_, x_int_, msgs_);
131 "dz_dt", dz_dt.size(),
N_);
133 vector<double> coupled_sys(N_ * M_);
134 vector<double>
grad(N_ + M_);
140 z_vars.reserve(N_ + M_);
142 vector<var> y_vars(y.begin(), y.end());
143 z_vars.insert(z_vars.end(), y_vars.begin(), y_vars.end());
145 vector<var> theta_vars(theta_dbl_.begin(), theta_dbl_.end());
146 z_vars.insert(z_vars.end(), theta_vars.begin(), theta_vars.end());
148 vector<var> dy_dt_vars = f_(t, y_vars, theta_vars, x_, x_int_, msgs_);
150 for (
size_t i = 0; i <
N_; i++) {
152 dy_dt_vars[i].grad(z_vars, grad);
154 for (
size_t j = 0; j <
M_; j++) {
158 double temp_deriv = grad[N_ + j];
159 for (
size_t k = 0; k <
N_; k++)
160 temp_deriv += z[N_ + N_ * j + k] * grad[k];
162 coupled_sys[i + j *
N_] = temp_deriv;
165 }
catch (
const std::exception&
e) {
171 dz_dt.insert(dz_dt.end(), coupled_sys.begin(), coupled_sys.end());
197 std::vector<double> state(size_, 0.0);
198 for (
size_t n = 0; n <
N_; n++)
199 state[n] = y0_dbl_[n];
209 std::vector<std::vector<var> >
211 std::vector<var> temp_vars(N_);
212 std::vector<double> temp_gradients(M_);
213 std::vector<std::vector<var> > y_return(y.size());
215 for (
size_t i = 0; i < y.size(); i++) {
217 for (
size_t j = 0; j <
N_; j++) {
219 for (
size_t k = 0; k <
M_; k++)
220 temp_gradients[k] = y[i][y0_dbl_.size() + y0_dbl_.size() * k + j];
226 y_return[i] = temp_vars;
258 template <
typename F>
261 const std::vector<var>&
y0_;
264 const std::vector<double>&
x_;
285 const std::vector<var>& y0,
286 const std::vector<double>& theta,
287 const std::vector<double>& x,
288 const std::vector<int>& x_int,
292 y0_dbl_(y0.
size(), 0.0),
299 size_(N_ + N_ * N_) {
300 for (
size_t n = 0; n <
N_; n++)
321 std::vector<double>& dz_dt,
325 std::vector<double> y(z.begin(), z.begin() +
N_);
326 for (
size_t n = 0; n <
N_; n++)
329 dz_dt = f_(t, y, theta_dbl_, x_, x_int_, msgs_);
331 "dz_dt", dz_dt.size(),
N_);
333 std::vector<double> coupled_sys(N_ * N_);
334 std::vector<double>
grad(N_);
342 vector<var> y_vars(y.begin(), y.end());
343 z_vars.insert(z_vars.end(), y_vars.begin(), y_vars.end());
345 vector<var> dy_dt_vars = f_(t, y_vars, theta_dbl_, x_, x_int_, msgs_);
347 for (
size_t i = 0; i <
N_; i++) {
349 dy_dt_vars[i].grad(z_vars, grad);
351 for (
size_t j = 0; j <
N_; j++) {
355 double temp_deriv = grad[j];
356 for (
size_t k = 0; k <
N_; k++)
357 temp_deriv += z[N_ + N_ * j + k] * grad[k];
359 coupled_sys[i + j *
N_] = temp_deriv;
362 }
catch (
const std::exception&
e) {
368 dz_dt.insert(dz_dt.end(), coupled_sys.begin(), coupled_sys.end());
395 return std::vector<double>(
size_, 0.0);
405 std::vector<std::vector<var> >
409 vector<var> temp_vars(N_);
410 vector<double> temp_gradients(N_);
411 vector<vector<var> > y_return(y.size());
413 for (
size_t i = 0; i < y.size(); i++) {
415 for (
size_t j = 0; j <
N_; j++) {
417 for (
size_t k = 0; k <
N_; k++)
418 temp_gradients[k] = y[i][y0_.size() + y0_.size() * k + j];
421 y0_, temp_gradients);
423 y_return[i] = temp_vars;
467 template <
typename F>
470 const std::vector<var>&
y0_;
474 const std::vector<double>&
x_;
495 const std::vector<var>& y0,
496 const std::vector<var>& theta,
497 const std::vector<double>& x,
498 const std::vector<int>& x_int,
502 y0_dbl_(y0.
size(), 0.0),
504 theta_dbl_(theta.
size(), 0.0),
509 size_(N_ + N_ * (N_ + M_)),
511 for (
size_t n = 0; n <
N_; n++)
514 for (
size_t m = 0; m <
M_; m++)
535 std::vector<double>& dz_dt,
539 vector<double> y(z.begin(), z.begin() +
N_);
540 for (
size_t n = 0; n <
N_; n++)
543 dz_dt = f_(t, y, theta_dbl_, x_, x_int_, msgs_);
545 "dz_dt", dz_dt.size(),
N_);
547 vector<double> coupled_sys(N_ * (N_ + M_));
548 vector<double>
grad(N_ + M_);
554 z_vars.reserve(N_ + M_);
556 vector<var> y_vars(y.begin(), y.end());
557 z_vars.insert(z_vars.end(), y_vars.begin(), y_vars.end());
559 vector<var> theta_vars(theta_dbl_.begin(), theta_dbl_.end());
560 z_vars.insert(z_vars.end(), theta_vars.begin(), theta_vars.end());
562 vector<var> dy_dt_vars = f_(t, y_vars, theta_vars, x_, x_int_, msgs_);
564 for (
size_t i = 0; i <
N_; i++) {
566 dy_dt_vars[i].grad(z_vars, grad);
568 for (
size_t j = 0; j < N_ +
M_; j++) {
572 double temp_deriv = grad[j];
573 for (
size_t k = 0; k <
N_; k++)
574 temp_deriv += z[N_ + N_ * j + k] * grad[k];
576 coupled_sys[i + j *
N_] = temp_deriv;
579 }
catch (
const std::exception&
e) {
585 dz_dt.insert(dz_dt.end(), coupled_sys.begin(), coupled_sys.end());
609 return std::vector<double>(
size_, 0.0);
619 std::vector<std::vector<var> >
623 vector<var> vars = y0_;
624 vars.insert(vars.end(), theta_.begin(), theta_.end());
626 vector<var> temp_vars(N_);
627 vector<double> temp_gradients(N_ + M_);
628 vector<vector<var> > y_return(y.size());
630 for (
size_t i = 0; i < y.size(); i++) {
632 for (
size_t j = 0; j <
N_; j++) {
634 for (
size_t k = 0; k < N_ +
M_; k++)
635 temp_gradients[k] = y[i][N_ + N_ * k + j];
638 vars, temp_gradients);
640 y_return[i] = temp_vars;
var precomputed_gradients(const double value, const std::vector< var > &operands, const std::vector< double > &gradients)
This function returns a var for an expression that has the specified value, vector of operands...
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t)
Populates the derivative vector with derivatives of the coupled ODE system state with respect to time...
std::vector< double > initial_state()
Returns the initial state of the coupled system.
size_t size() const
Returns the size of the coupled system.
std::vector< double > y0_dbl_
const std::vector< var > & y0_
const std::vector< double > & x_
std::vector< double > initial_state()
Returns the initial state of the coupled system.
std::vector< double > y0_dbl_
T value_of(const fvar< T > &v)
Return the value of the specified variable.
std::vector< double > theta_dbl_
const std::vector< int > & x_int_
static void set_zero_all_adjoints_nested()
Reset all adjoint values in the top nested portion of the stack to zero.
size_t size() const
Returns the size of the coupled system.
size_t size() const
Returns the size of the coupled system.
const std::vector< var > & y0_
Independent (input) and dependent (output) variables for gradients.
coupled_ode_system(const F &f, const std::vector< var > &y0, const std::vector< var > &theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
Construct a coupled ODE system with unknown initial value and known parameters, given the base ODE sy...
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
coupled_ode_system(const F &f, const std::vector< double > &y0, const std::vector< var > &theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
Construct a coupled ODE system with the specified base ODE system, base initial state, parameters, data, and a message stream.
std::vector< std::vector< var > > decouple_states(const std::vector< std::vector< double > > &y)
Return the solutions to the basic ODE system, including appropriate autodiff partial derivatives...
std::vector< double > theta_dbl_
std::vector< std::vector< var > > decouple_states(const std::vector< std::vector< double > > &y)
Returns the base ODE system state corresponding to the specified coupled system state.
const std::vector< int > & x_int_
const std::vector< double > & y0_dbl_
const std::vector< var > & theta_
bool check_equal(const char *function, const char *name, const T_y &y, const T_eq &eq)
Return true if y is equal to eq.
const std::vector< double > & theta_dbl_
double e()
Return the base of the natural logarithm.
std::vector< double > initial_state()
Returns the initial state of the coupled system.
const std::vector< int > & x_int_
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Base template class for a coupled ordinary differential equation system, which adds sensitivities to ...
static void recover_memory_nested()
Recover only the memory used for the top nested call.
coupled_ode_system(const F &f, const std::vector< var > &y0, const std::vector< double > &theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
Construct a coupled ODE system for an unknown initial state and known parameters givne the specified ...
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t)
Calculates the derivative of the coupled ode system with respect to the state y at time t...
const std::vector< double > & x_
void add_initial_values(const std::vector< var > &y0, std::vector< std::vector< var > > &y)
Increment the state derived from the coupled system in the with the original initial state...
std::vector< std::vector< var > > decouple_states(const std::vector< std::vector< double > > &y)
Return the basic ODE solutions given the specified coupled system solutions, including the partials v...
const std::vector< double > & x_
static void start_nested()
Record the current position so that recover_memory_nested() can find it.
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t)
Assign the derivative vector with the system derivatives at the specified state and time...
const std::vector< var > & theta_