Automatic Differentiation
 
Loading...
Searching...
No Matches
read_var.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_READ_VAR_HPP
2#define STAN_MATH_REV_CORE_READ_VAR_HPP
3
7
8namespace stan {
9namespace math {
10
17template <typename EigRev, typename EigVari, typename EigDbl>
19 const EigRev& var_mat;
20 EigVari& vi_mat;
21 EigDbl& val_mat;
22
23 public:
24 vi_val_adj_functor(const EigRev& arg1, EigVari& arg2, EigDbl& arg3)
25 : var_mat(arg1), vi_mat(arg2), val_mat(arg3) {}
26
27 inline decltype(auto) operator()(Eigen::Index row, Eigen::Index col) const {
28 vi_mat.coeffRef(row, col) = var_mat.coeffRef(row, col).vi_;
29 val_mat.coeffRef(row, col) = var_mat.coeffRef(row, col).vi_->val_;
30 return var_mat.coeffRef(row, col).vi_->adj_;
31 }
32
33 inline decltype(auto) operator()(Eigen::Index index) const {
34 vi_mat.coeffRef(index) = var_mat.coeffRef(index).vi_;
35 val_mat.coeffRef(index) = var_mat.coeffRef(index).vi_->val_;
36 return var_mat.coeffRef(index).vi_->adj_;
37 }
38};
39
44template <typename EigRev, typename EigDbl>
46 const EigRev& var_mat;
47 EigDbl& val_mat;
48
49 public:
50 val_adj_functor(const EigRev& arg1, EigDbl& arg2)
51 : var_mat(arg1), val_mat(arg2) {}
52
53 template <typename T = EigRev, require_st_same<T, var>* = nullptr>
54 inline decltype(auto) operator()(Eigen::Index row, Eigen::Index col) const {
55 val_mat.coeffRef(row, col) = var_mat.coeffRef(row, col).vi_->val_;
56 return var_mat.coeffRef(row, col).vi_->adj_;
57 }
58
59 template <typename T = EigRev, require_st_same<T, var>* = nullptr>
60 inline decltype(auto) operator()(Eigen::Index index) const {
61 val_mat.coeffRef(index) = var_mat.coeffRef(index).vi_->val_;
62 return var_mat.coeffRef(index).vi_->adj_;
63 }
64
65 template <typename T = EigRev, require_st_same<T, vari*>* = nullptr>
66 inline decltype(auto) operator()(Eigen::Index row, Eigen::Index col) const {
67 val_mat.coeffRef(row, col) = var_mat.coeffRef(row, col)->val_;
68 return var_mat.coeffRef(row, col)->adj_;
69 }
70
71 template <typename T = EigRev, require_st_same<T, vari*>* = nullptr>
72 inline decltype(auto) operator()(Eigen::Index index) const {
73 val_mat.coeffRef(index) = var_mat.coeffRef(index)->val_;
74 return var_mat.coeffRef(index)->adj_;
75 }
76};
77
82template <typename EigVar, typename EigVari>
84 const EigVar& var_mat;
85 EigVari& vi_mat;
86
87 public:
88 vi_val_functor(const EigVar& arg1, EigVari& arg2)
89 : var_mat(arg1), vi_mat(arg2) {}
90
91 inline decltype(auto) operator()(Eigen::Index row, Eigen::Index col) const {
92 vi_mat.coeffRef(row, col) = var_mat.coeffRef(row, col).vi_;
93 return var_mat.coeffRef(row, col).vi_->val_;
94 }
95
96 inline decltype(auto) operator()(Eigen::Index index) const {
97 vi_mat.coeffRef(index) = var_mat.coeffRef(index).vi_;
98 return var_mat.coeffRef(index).vi_->val_;
99 }
100};
101
106template <typename EigVar, typename EigVari>
108 const EigVar& var_mat;
109 EigVari& vi_mat;
110
111 public:
112 vi_adj_functor(const EigVar& arg1, EigVari& arg2)
113 : var_mat(arg1), vi_mat(arg2) {}
114
115 inline decltype(auto) operator()(Eigen::Index row, Eigen::Index col) const {
116 vi_mat.coeffRef(row, col) = var_mat.coeffRef(row, col).vi_;
117 return var_mat.coeffRef(row, col).vi_->adj_;
118 }
119
120 inline decltype(auto) operator()(Eigen::Index index) const {
121 vi_mat.coeffRef(index) = var_mat.coeffRef(index).vi_;
122 return var_mat.coeffRef(index).vi_->adj_;
123 }
124};
125
138template <typename EigVar, typename EigVari, typename EigDbl>
139inline void read_vi_val_adj(const EigVar& VarMat, EigVari& VariMat,
140 EigDbl& ValMat, EigDbl& AdjMat) {
141 AdjMat
142 = EigDbl::NullaryExpr(VarMat.rows(), VarMat.cols(),
144 VarMat, VariMat, ValMat));
145}
146
157template <typename EigRev, typename EigDbl>
158inline void read_val_adj(const EigRev& VarMat, EigDbl& ValMat, EigDbl& AdjMat) {
159 AdjMat = EigDbl::NullaryExpr(
160 VarMat.rows(), VarMat.cols(),
162}
163
174template <typename EigVar, typename EigVari, typename EigDbl>
175inline void read_vi_val(const EigVar& VarMat, EigVari& VariMat,
176 EigDbl& ValMat) {
177 ValMat = EigDbl::NullaryExpr(
178 VarMat.rows(), VarMat.cols(),
180}
181
192template <typename EigVar, typename EigVari, typename EigDbl>
193inline void read_vi_adj(const EigVar& VarMat, EigVari& VariMat,
194 EigDbl& AdjMat) {
195 AdjMat = EigDbl::NullaryExpr(
196 VarMat.rows(), VarMat.cols(),
198}
199
200} // namespace math
201} // namespace stan
202#endif
val_adj_functor(const EigRev &arg1, EigDbl &arg2)
Definition read_var.hpp:50
Functor for extracting the values and adjoints from a matrix of var or vari.
Definition read_var.hpp:45
vi_adj_functor(const EigVar &arg1, EigVari &arg2)
Definition read_var.hpp:112
Functor for extracting the varis and adjoints from a matrix of var.
Definition read_var.hpp:107
vi_val_adj_functor(const EigRev &arg1, EigVari &arg2, EigDbl &arg3)
Definition read_var.hpp:24
Functor for extracting the vari*, values, and adjoints from a matrix of var.
Definition read_var.hpp:18
vi_val_functor(const EigVar &arg1, EigVari &arg2)
Definition read_var.hpp:88
Functor for extracting the varis and values from a matrix of var.
Definition read_var.hpp:83
auto col(T_x &&x, size_t j)
Return the specified column of the specified kernel generator expression using start-at-1 indexing.
Definition col.hpp:23
auto row(T_x &&x, size_t j)
Return the specified row of the specified kernel generator expression using start-at-1 indexing.
Definition row.hpp:23
void read_val_adj(const EigRev &VarMat, EigDbl &ValMat, EigDbl &AdjMat)
Function applying the val_adj_functor to extract the values and adjoints of a given var or vari matri...
Definition read_var.hpp:158
void read_vi_val_adj(const EigVar &VarMat, EigVari &VariMat, EigDbl &ValMat, EigDbl &AdjMat)
Function applying the vi_val_adj_functor to extract the vari*, values, and adjoints of a given var ma...
Definition read_var.hpp:139
void read_vi_val(const EigVar &VarMat, EigVari &VariMat, EigDbl &ValMat)
Function applying the vi_val_functor to extract the varis and and values of a given var matrix into s...
Definition read_var.hpp:175
void read_vi_adj(const EigVar &VarMat, EigVari &VariMat, EigDbl &AdjMat)
Function applying the vi_adj_functor to extract the varis and and adjoints of a given var matrix into...
Definition read_var.hpp:193
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...