Automatic Differentiation
 
Loading...
Searching...
No Matches
ode_store_sensitivities.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUNCTOR_ODE_STORE_SENSITIVITIES_HPP
2#define STAN_MATH_REV_FUNCTOR_ODE_STORE_SENSITIVITIES_HPP
3
7#include <ostream>
8#include <vector>
9
10namespace stan {
11namespace math {
12
32template <typename F, typename T_y0_t0, typename T_t0, typename T_t,
33 typename... Args,
34 require_any_autodiff_t<T_y0_t0, T_t0, T_t,
35 scalar_type_t<Args>...>* = nullptr>
36Eigen::Matrix<var, Eigen::Dynamic, 1> ode_store_sensitivities(
37 const F& f, const std::vector<double>& coupled_state,
38 const Eigen::Matrix<T_y0_t0, Eigen::Dynamic, 1>& y0, const T_t0& t0,
39 const T_t& t, std::ostream* msgs, const Args&... args) {
40 const size_t N = y0.size();
41 const size_t num_y0_vars = count_vars(y0);
42 const size_t num_args_vars = count_vars(args...);
43 const size_t num_t0_vars = count_vars(t0);
44 const size_t num_t_vars = count_vars(t);
45 Eigen::Matrix<var, Eigen::Dynamic, 1> yt(N);
46
47 Eigen::VectorXd y(N);
48 for (size_t n = 0; n < N; ++n) {
49 y.coeffRef(n) = coupled_state[n];
50 }
51
52 Eigen::VectorXd f_y_t;
54 f_y_t = f(value_of(t), y, msgs, eval(value_of(args))...);
55
56 Eigen::VectorXd f_y0_t0;
58 f_y0_t0
59 = f(value_of(t0), eval(value_of(y0)), msgs, eval(value_of(args))...);
60
61 const size_t total_vars
62 = num_y0_vars + num_args_vars + num_t0_vars + num_t_vars;
63
64 vari** varis
66
67 save_varis(varis, y0, args..., t0, t);
68
69 // memory for a column major jacobian
70 double* jacobian_mem
72 * total_vars);
73
74 Eigen::Map<Eigen::MatrixXd> jacobian(jacobian_mem, total_vars, N);
75
76 for (size_t j = 0; j < N; ++j) {
77 for (size_t k = 0; k < num_y0_vars; ++k) {
78 jacobian.coeffRef(k, j) = coupled_state[N + num_y0_vars * k + j];
79 }
80
81 for (size_t k = 0; k < num_args_vars; ++k) {
82 jacobian.coeffRef(num_y0_vars + k, j)
83 = coupled_state[N + N * num_y0_vars + N * k + j];
84 }
85
87 double dyt_dt0 = 0.0;
88 for (size_t k = 0; k < num_y0_vars; ++k) {
89 dyt_dt0 -= f_y0_t0.coeffRef(k) * coupled_state[N + num_y0_vars * k + j];
90 }
91 jacobian.coeffRef(num_y0_vars + num_args_vars, j) = dyt_dt0;
92 }
93
95 jacobian.coeffRef(num_y0_vars + num_args_vars + num_t0_vars, j)
96 = f_y_t.coeffRef(j);
97 }
98
99 // jacobian_mem + j * total_vars points to the jth column of jacobian
100 yt(j) = new precomputed_gradients_vari(y(j), total_vars, varis,
101 jacobian_mem + j * total_vars);
102 }
103
104 return yt;
105}
106
107} // namespace math
108} // namespace stan
109
110#endif
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
require_any_t< is_autodiff< std::decay_t< Types > >... > require_any_autodiff_t
Require any of the types satisfy is_autodiff.
void jacobian(const F &f, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, Eigen::Matrix< T, Eigen::Dynamic, 1 > &fx, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &J)
Definition jacobian.hpp:11
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
size_t count_vars(Pargs &&... args)
Count the number of vars in the input argument list.
vari ** save_varis(vari **dest, const var &x, Pargs &&... args)
Save the vari pointer in x into the memory pointed to by dest, increment the dest storage pointer,...
Eigen::VectorXd ode_store_sensitivities(const F &f, const std::vector< double > &coupled_state, const Eigen::Matrix< T_y0_t0, Eigen::Dynamic, 1 > &y0, T_t0 t0, T_t t, std::ostream *msgs, const Args &... args)
When all arguments are arithmetic, there are no sensitivities to store, so the function just returns ...
precomputed_gradients_vari_template< std::tuple<>, std::tuple<> > precomputed_gradients_vari
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Defines a static member named value which is defined to be false as the primitive scalar types cannot...
Definition is_var.hpp:14
static thread_local AutodiffStackStorage * instance_