Automatic Differentiation
 
Loading...
Searching...
No Matches
map_rect_reduce.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUNCTOR_MAP_RECT_REDUCE_HPP
2#define STAN_MATH_REV_FUNCTOR_MAP_RECT_REDUCE_HPP
3
9#include <vector>
10
11namespace stan {
12namespace math {
13namespace internal {
14
15template <typename F>
16struct map_rect_reduce<F, var, var> {
17 matrix_d operator()(const vector_d& shared_params,
18 const vector_d& job_specific_params,
19 const std::vector<double>& x_r,
20 const std::vector<int>& x_i,
21 std::ostream* msgs = nullptr) const {
22 const size_type num_shared_params = shared_params.rows();
23 const size_type num_job_specific_params = job_specific_params.rows();
24 matrix_d out(1 + num_shared_params + num_job_specific_params, 0);
25
26 // Run nested autodiff in this scope
28
29 vector_v shared_params_v = to_var(shared_params);
30 vector_v job_specific_params_v = to_var(job_specific_params);
31
32 vector_v fx_v = F()(shared_params_v, job_specific_params_v, x_r, x_i, msgs);
33
34 const size_type size_f = fx_v.rows();
35
36 out.resize(Eigen::NoChange, size_f);
37
38 for (size_type i = 0; i < size_f; ++i) {
39 out(0, i) = fx_v(i).val();
40 nested.set_zero_all_adjoints();
41 fx_v(i).grad();
42 for (size_type j = 0; j < num_shared_params; ++j) {
43 out(1 + j, i) = shared_params_v(j).vi_->adj_;
44 }
45 for (size_type j = 0; j < num_job_specific_params; ++j) {
46 out(1 + num_shared_params + j, i) = job_specific_params_v(j).vi_->adj_;
47 }
48 }
49 return out;
50 }
51};
52
53template <typename F>
54struct map_rect_reduce<F, double, var> {
55 matrix_d operator()(const vector_d& shared_params,
56 const vector_d& job_specific_params,
57 const std::vector<double>& x_r,
58 const std::vector<int>& x_i,
59 std::ostream* msgs = nullptr) const {
60 const size_type num_job_specific_params = job_specific_params.rows();
61 matrix_d out(1 + num_job_specific_params, 0);
62
63 // Run nested autodiff in this scope
65
66 vector_v job_specific_params_v = to_var(job_specific_params);
67
68 vector_v fx_v = F()(shared_params, job_specific_params_v, x_r, x_i, msgs);
69
70 const size_type size_f = fx_v.rows();
71
72 out.resize(Eigen::NoChange, size_f);
73
74 for (size_type i = 0; i < size_f; ++i) {
75 out(0, i) = fx_v(i).val();
76 nested.set_zero_all_adjoints();
77 fx_v(i).grad();
78 for (size_type j = 0; j < num_job_specific_params; ++j) {
79 out(1 + j, i) = job_specific_params_v(j).vi_->adj_;
80 }
81 }
82 return out;
83 }
84};
85
86template <typename F>
87struct map_rect_reduce<F, var, double> {
88 matrix_d operator()(const vector_d& shared_params,
89 const vector_d& job_specific_params,
90 const std::vector<double>& x_r,
91 const std::vector<int>& x_i,
92 std::ostream* msgs = nullptr) const {
93 const size_type num_shared_params = shared_params.rows();
94 matrix_d out(1 + num_shared_params, 0);
95
96 // Run nested autodiff in this scope
98
99 vector_v shared_params_v = to_var(shared_params);
100
101 vector_v fx_v = F()(shared_params_v, job_specific_params, x_r, x_i, msgs);
102
103 const size_type size_f = fx_v.rows();
104
105 out.resize(Eigen::NoChange, size_f);
106
107 for (size_type i = 0; i < size_f; ++i) {
108 out(0, i) = fx_v(i).val();
109 nested.set_zero_all_adjoints();
110 fx_v(i).grad();
111 for (size_type j = 0; j < num_shared_params; ++j) {
112 out(1 + j, i) = shared_params_v(j).vi_->adj_;
113 }
114 }
115
116 return out;
117 }
118};
119
120} // namespace internal
121} // namespace math
122} // namespace stan
123
124#ifdef STAN_REGISTER_MPI_MAP_RECT_ALL
125
126#undef STAN_REGISTER_MPI_MAP_RECT_ALL
127
128#define STAN_REGISTER_MPI_MAP_RECT_ALL(CALLID, FUNCTOR) \
129 STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, double, double) \
130 STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, double, var) \
131 STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, var, double) \
132 STAN_REGISTER_MPI_MAP_RECT(CALLID, FUNCTOR, var, var)
133
134#endif
135
136#endif
void set_zero_all_adjoints()
Reset all adjoint values in this nested stack to zero.
A class following the RAII idiom to start and recover nested autodiff scopes.
var to_var(double x)
Converts argument to an automatic differentiation variable.
Definition to_var.hpp:22
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Type for (column) vector of double values.
Definition typedefs.hpp:24
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double elements.
Definition typedefs.hpp:11
Eigen::Matrix< var, Eigen::Dynamic, 1 > vector_v
The type of a (column) vector holding var values.
Definition typedefs.hpp:24
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_d
Type for matrix of double values.
Definition typedefs.hpp:19
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
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
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
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