1 #ifndef STAN_MATH_REV_ARR_FUNCTOR_COUPLED_ODE_SYSTEM_HPP 2 #define STAN_MATH_REV_ARR_FUNCTOR_COUPLED_ODE_SYSTEM_HPP 64 const std::vector<double>&
x_;
84 const std::vector<var>& theta,
85 const std::vector<double>& x,
86 const std::vector<int>& x_int, std::ostream* msgs)
96 for (
const var& p : theta)
97 theta_nochain_.emplace_back(
var(
new vari(p.val(),
false)));
114 void operator()(
const std::vector<double>& z, std::vector<double>& dz_dt,
121 const vector<var> y_vars(z.begin(), z.begin() + N_);
123 vector<var> dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_);
128 for (
size_t i = 0; i < N_; i++) {
129 dz_dt[i] = dy_dt_vars[i].val();
130 dy_dt_vars[i].grad();
132 for (
size_t j = 0; j < M_; j++) {
136 double temp_deriv = theta_nochain_[j].adj();
137 const size_t offset = N_ + N_ * j;
138 for (
size_t k = 0; k < N_; k++)
139 temp_deriv += z[offset + k] * y_vars[k].adj();
141 dz_dt[offset + i] = temp_deriv;
149 for (
size_t j = 0; j < M_; ++j)
150 theta_nochain_[j].vi_->set_zero_adjoint();
152 }
catch (
const std::exception&
e) {
164 size_t size()
const {
return size_; }
182 std::vector<double> state(size_, 0.0);
183 for (
size_t n = 0; n < N_; n++)
184 state[n] = y0_dbl_[n];
216 template <
typename F>
219 const std::vector<var>&
y0_;
221 const std::vector<double>&
x_;
241 const std::vector<double>& theta,
242 const std::vector<double>& x,
243 const std::vector<int>& x_int, std::ostream* msgs)
252 size_(N_ + N_ * N_) {}
268 void operator()(
const std::vector<double>& z, std::vector<double>& dz_dt,
275 const vector<var> y_vars(z.begin(), z.begin() + N_);
277 vector<var> dy_dt_vars = f_(t, y_vars, theta_dbl_, x_, x_int_, msgs_);
282 for (
size_t i = 0; i < N_; i++) {
283 dz_dt[i] = dy_dt_vars[i].val();
284 dy_dt_vars[i].grad();
286 for (
size_t j = 0; j < N_; j++) {
291 double temp_deriv = 0;
292 const size_t offset = N_ + N_ * j;
293 for (
size_t k = 0; k < N_; k++)
294 temp_deriv += z[offset + k] * y_vars[k].adj();
296 dz_dt[offset + i] = temp_deriv;
301 }
catch (
const std::exception&
e) {
313 size_t size()
const {
return size_; }
329 std::vector<double> initial(size_, 0.0);
330 for (
size_t i = 0; i < N_; i++)
332 for (
size_t i = 0; i < N_; i++)
333 initial[N_ + i * N_ + i] = 1.0;
386 template <
typename F>
389 const std::vector<var>&
y0_;
392 const std::vector<double>&
x_;
412 const std::vector<var>& theta,
413 const std::vector<double>& x,
414 const std::vector<int>& x_int, std::ostream* msgs)
422 size_(N_ + N_ * (N_ + M_)),
424 for (
const var& p : theta)
425 theta_nochain_.emplace_back(
var(
new vari(p.val(),
false)));
442 void operator()(
const std::vector<double>& z, std::vector<double>& dz_dt,
449 const vector<var> y_vars(z.begin(), z.begin() + N_);
451 vector<var> dy_dt_vars = f_(t, y_vars, theta_nochain_, x_, x_int_, msgs_);
456 for (
size_t i = 0; i < N_; i++) {
457 dz_dt[i] = dy_dt_vars[i].val();
458 dy_dt_vars[i].grad();
460 for (
size_t j = 0; j < N_; j++) {
464 double temp_deriv = 0;
465 const size_t offset = N_ + N_ * j;
466 for (
size_t k = 0; k < N_; k++)
467 temp_deriv += z[offset + k] * y_vars[k].adj();
469 dz_dt[offset + i] = temp_deriv;
472 for (
size_t j = 0; j < M_; j++) {
473 double temp_deriv = theta_nochain_[j].adj();
474 const size_t offset = N_ + N_ * N_ + N_ * j;
475 for (
size_t k = 0; k < N_; k++)
476 temp_deriv += z[offset + k] * y_vars[k].adj();
478 dz_dt[offset + i] = temp_deriv;
486 for (
size_t j = 0; j < M_; ++j)
487 theta_nochain_[j].vi_->set_zero_adjoint();
489 }
catch (
const std::exception&
e) {
501 size_t size()
const {
return size_; }
522 std::vector<double> initial(size_, 0.0);
523 for (
size_t i = 0; i < N_; i++)
525 for (
size_t i = 0; i < N_; i++)
526 initial[N_ + i * N_ + i] = 1.0;
const std::vector< double > & x_
T value_of(const fvar< T > &v)
Return the value of the specified variable.
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.
The variable implementation base class.
const std::vector< var > & y0_
const std::vector< var > & y0_
Independent (input) and dependent (output) variables for gradients.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
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 from the base system function, initial state of the base system...
std::vector< double > initial_state() const
Returns the initial state of the coupled system.
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t) const
Calculates the derivative of the coupled ode system with respect to time.
const std::vector< int > & x_int_
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t) const
Calculates the derivative of the coupled ode system with respect to time.
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t) const
Calculates the derivative of the coupled ode system with respect to time.
const std::vector< double > & y0_dbl_
const std::vector< var > & theta_
const std::vector< double > & theta_dbl_
size_t size() const
Returns the size of the coupled system.
const std::vector< var > & theta_
double e()
Return the base of the natural logarithm.
const std::vector< int > & x_int_
size_t size() const
Returns the size of the coupled system.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
The coupled_ode_system represents the coupled ode system, which is the base ode and the sensitivities...
std::vector< var > theta_nochain_
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 from the base system function, initial state of the base system...
std::vector< double > initial_state() const
Returns the initial state of the coupled system.
const std::vector< double > & x_
std::vector< double > initial_state() const
Returns the initial state of the coupled system.
const std::vector< double > & x_
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 from the base system function, initial state of the base system...
static void start_nested()
Record the current position so that recover_memory_nested() can find it.
size_t size() const
Returns the size of the coupled system.
std::vector< var > theta_nochain_