Loading [MathJax]/extensions/TeX/AMSsymbols.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
coupled_ode_system.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUNCTOR_COUPLED_ODE_SYSTEM_HPP
2#define STAN_MATH_PRIM_FUNCTOR_COUPLED_ODE_SYSTEM_HPP
3
9#include <ostream>
10#include <vector>
11
12namespace stan {
13namespace math {
14
15template <bool ArithmeticArguments, typename F, typename T_y0, typename... Args>
17
28template <typename F, typename T_y0, typename... Args>
29struct coupled_ode_system_impl<true, F, T_y0, Args...> {
30 const F& f_;
31 const Eigen::VectorXd& y0_;
32 std::tuple<const Args&...> args_tuple_;
33 const size_t N_;
34 std::ostream* msgs_;
35
46 coupled_ode_system_impl(const F& f, const Eigen::VectorXd& y0,
47 std::ostream* msgs, const Args&... args)
48 : f_(f), y0_(y0), args_tuple_(args...), N_(y0.size()), msgs_(msgs) {}
49
63 void operator()(const std::vector<double>& z, std::vector<double>& dz_dt,
64 double t) const {
65 Eigen::VectorXd y(z.size());
66 for (size_t n = 0; n < z.size(); ++n)
67 y.coeffRef(n) = z[n];
68
69 dz_dt.resize(y.size());
70
71 Eigen::VectorXd f_y_t = math::apply(
72 [&](const Args&... args) { return f_(t, y, msgs_, args...); },
73 args_tuple_);
74
75 check_size_match("coupled_ode_system", "dy_dt", f_y_t.size(), "states",
76 y.size());
77
78 Eigen::Map<Eigen::VectorXd>(dz_dt.data(), dz_dt.size()) = f_y_t;
79 }
80
86 size_t size() const { return N_; }
87
94 std::vector<double> initial_state() const {
95 std::vector<double> initial(size(), 0.0);
96
97 for (size_t i = 0; i < N_; i++) {
98 initial[i] = value_of(y0_(i));
99 }
100
101 return initial;
102 }
103};
104
105template <typename F, typename T_y0, typename... Args>
108 std::is_arithmetic<return_type_t<T_y0, Args...>>::value, F, T_y0,
109 Args...> {
111 const Eigen::Matrix<T_y0, Eigen::Dynamic, 1>& y0,
112 std::ostream* msgs, const Args&... args)
114 std::is_arithmetic<return_type_t<T_y0, Args...>>::value, F, T_y0,
115 Args...>(f, y0, msgs, args...) {}
116};
117
118} // namespace math
119} // namespace stan
120#endif
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
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.
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
std::is_arithmetic< scalar_type_t< T > > is_arithmetic
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.
coupled_ode_system_impl(const F &f, const Eigen::VectorXd &y0, std::ostream *msgs, const Args &... args)
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.
size_t size() const
Returns the size of the coupled system.
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t) const
Evaluate the right hand side of the coupled system at state z and time t, and store the sensitivities...
coupled_ode_system(const F &f, const Eigen::Matrix< T_y0, Eigen::Dynamic, 1 > &y0, std::ostream *msgs, const Args &... args)