Stan Math Library  2.20.0
reverse mode automatic differentiation
map_rect_reduce.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUNCTOR_MAP_RECT_REDUCE_HPP
2 #define STAN_MATH_REV_MAT_FUNCTOR_MAP_RECT_REDUCE_HPP
3 
4 #include <stan/math/rev/meta.hpp>
10 
11 #include <vector>
12 
13 namespace stan {
14 namespace math {
15 namespace internal {
16 
17 template <typename F>
18 struct map_rect_reduce<F, var, var> {
19  matrix_d operator()(const vector_d& shared_params,
20  const vector_d& job_specific_params,
21  const std::vector<double>& x_r,
22  const std::vector<int>& x_i,
23  std::ostream* msgs = nullptr) const {
24  const size_type num_shared_params = shared_params.rows();
25  const size_type num_job_specific_params = job_specific_params.rows();
26  matrix_d out(1 + num_shared_params + num_job_specific_params, 0);
27 
28  try {
29  start_nested();
30  vector_v shared_params_v = to_var(shared_params);
31  vector_v job_specific_params_v = to_var(job_specific_params);
32 
33  vector_v fx_v
34  = F()(shared_params_v, job_specific_params_v, x_r, x_i, msgs);
35 
36  const size_type size_f = fx_v.rows();
37 
38  out.resize(Eigen::NoChange, size_f);
39 
40  for (size_type i = 0; i < size_f; ++i) {
41  out(0, i) = fx_v(i).val();
43  fx_v(i).grad();
44  for (size_type j = 0; j < num_shared_params; ++j)
45  out(1 + j, i) = shared_params_v(j).vi_->adj_;
46  for (size_type j = 0; j < num_job_specific_params; ++j)
47  out(1 + num_shared_params + j, i)
48  = job_specific_params_v(j).vi_->adj_;
49  }
51  } catch (const std::exception& e) {
53  throw;
54  }
55  return out;
56  }
57 };
58 
59 template <typename F>
60 struct map_rect_reduce<F, double, var> {
61  matrix_d operator()(const vector_d& shared_params,
62  const vector_d& job_specific_params,
63  const std::vector<double>& x_r,
64  const std::vector<int>& x_i,
65  std::ostream* msgs = nullptr) const {
66  const size_type num_job_specific_params = job_specific_params.rows();
67  matrix_d out(1 + num_job_specific_params, 0);
68 
69  try {
70  start_nested();
71  vector_v job_specific_params_v = to_var(job_specific_params);
72 
73  vector_v fx_v = F()(shared_params, job_specific_params_v, x_r, x_i, msgs);
74 
75  const size_type size_f = fx_v.rows();
76 
77  out.resize(Eigen::NoChange, size_f);
78 
79  for (size_type i = 0; i < size_f; ++i) {
80  out(0, i) = fx_v(i).val();
82  fx_v(i).grad();
83  for (size_type j = 0; j < num_job_specific_params; ++j)
84  out(1 + j, i) = job_specific_params_v(j).vi_->adj_;
85  }
87  } catch (const std::exception& e) {
89  throw;
90  }
91  return out;
92  }
93 };
94 
95 template <typename F>
96 struct map_rect_reduce<F, var, double> {
97  matrix_d operator()(const vector_d& shared_params,
98  const vector_d& job_specific_params,
99  const std::vector<double>& x_r,
100  const std::vector<int>& x_i,
101  std::ostream* msgs = nullptr) const {
102  const size_type num_shared_params = shared_params.rows();
103  matrix_d out(1 + num_shared_params, 0);
104 
105  try {
106  start_nested();
107  vector_v shared_params_v = to_var(shared_params);
108 
109  vector_v fx_v = F()(shared_params_v, job_specific_params, x_r, x_i, msgs);
110 
111  const size_type size_f = fx_v.rows();
112 
113  out.resize(Eigen::NoChange, size_f);
114 
115  for (size_type i = 0; i < size_f; ++i) {
116  out(0, i) = fx_v(i).val();
118  fx_v(i).grad();
119  for (size_type j = 0; j < num_shared_params; ++j)
120  out(1 + j, i) = shared_params_v(j).vi_->adj_;
121  }
123  } catch (const std::exception& e) {
125  throw;
126  }
127  return out;
128  }
129 };
130 
131 } // namespace internal
132 } // namespace math
133 } // namespace stan
134 
135 #ifdef STAN_REGISTER_MPI_MAP_RECT_ALL
136 
137 #undef STAN_REGISTER_MPI_MAP_RECT_ALL
138 
139 #define STAN_REGISTER_MPI_MAP_RECT_ALL(CALLID, FUNCTOR) \
140  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, double, double) \
141  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, double, var) \
142  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, var, double) \
143  STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, var, var)
144 
145 #endif
146 
147 #endif
Eigen::Matrix< var, Eigen::Dynamic, 1 > vector_v
The type of a (column) vector holding var values.
Definition: typedefs.hpp:23
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Type for (column) vector of double values.
Definition: typedefs.hpp:24
static void set_zero_all_adjoints_nested()
Reset all adjoint values in the top nested portion of the stack to zero.
matrix_d operator()(const vector_d &shared_params, const vector_d &job_specific_params, const std::vector< double > &x_r, const std::vector< int > &x_i, std::ostream *msgs=nullptr) const
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:11
matrix_d operator()(const vector_d &shared_params, const vector_d &job_specific_params, const std::vector< double > &x_r, const std::vector< int > &x_i, std::ostream *msgs=nullptr) const
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_d
Type for matrix of double values.
Definition: typedefs.hpp:19
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:87
static void recover_memory_nested()
Recover only the memory used for the top nested call.
matrix_d operator()(const vector_d &shared_params, const vector_d &job_specific_params, const std::vector< double > &x_r, const std::vector< int > &x_i, std::ostream *msgs=nullptr) const
std::vector< var > to_var(const std::vector< double > &v)
Converts argument to an automatic differentiation variable.
Definition: to_var.hpp:19
static void start_nested()
Record the current position so that recover_memory_nested() can find it.

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