| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

trace_gen_quad_form.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_TRACE_GEN_QUAD_FORM_HPP
2 #define STAN_MATH_REV_MAT_FUN_TRACE_GEN_QUAD_FORM_HPP
3
4 #include <boost/utility/enable_if.hpp>
5 #include <boost/type_traits.hpp>
6 #include <stan/math/prim/mat/fun/Eigen.hpp>
7 #include <stan/math/prim/mat/fun/typedefs.hpp>
8 #include <stan/math/rev/core.hpp>
9 #include <stan/math/rev/scal/fun/value_of.hpp>
10 #include <stan/math/prim/mat/fun/value_of.hpp>
11 #include <stan/math/rev/mat/fun/typedefs.hpp>
12 #include <stan/math/prim/mat/fun/trace_gen_quad_form.hpp>
13 #include <stan/math/prim/mat/err/check_multiplicable.hpp>
14 #include <stan/math/prim/mat/err/check_square.hpp>
15
16 namespace stan {
17 namespace math {
18 namespace {
19 template <typename TD, int RD, int CD,
20 typename TA, int RA, int CA,
21 typename TB, int RB, int CB>
22 class trace_gen_quad_form_vari_alloc : public chainable_alloc {
23 public:
24 trace_gen_quad_form_vari_alloc(const Eigen::Matrix<TD, RD, CD>& D,
25 const Eigen::Matrix<TA, RA, CA>& A,
26 const Eigen::Matrix<TB, RB, CB>& B)
27 : D_(D), A_(A), B_(B)
28 { }
29
30 double compute() {
31 return trace_gen_quad_form(value_of(D_),
32 value_of(A_),
33 value_of(B_));
34 }
35
36 Eigen::Matrix<TD, RD, CD> D_;
37 Eigen::Matrix<TA, RA, CA> A_;
38 Eigen::Matrix<TB, RB, CB> B_;
39 };
40
41 template <typename TD, int RD, int CD,
42 typename TA, int RA, int CA,
43 typename TB, int RB, int CB>
44 class trace_gen_quad_form_vari : public vari {
45 protected:
46 static inline void
47 computeAdjoints(double adj,
48 const Eigen::Matrix<double, RD, CD>& D,
49 const Eigen::Matrix<double, RA, CA>& A,
50 const Eigen::Matrix<double, RB, CB>& B,
51 Eigen::Matrix<var, RD, CD> *varD,
52 Eigen::Matrix<var, RA, CA> *varA,
53 Eigen::Matrix<var, RB, CB> *varB) {
54 Eigen::Matrix<double, CA, CB> AtB;
55 Eigen::Matrix<double, RA, CB> BD;
56 if (varB || varA)
57 BD.noalias() = B*D;
58 if (varB || varD)
59 AtB.noalias() = A.transpose()*B;
60
61 if (varB) {
62 Eigen::Matrix<double, RB, CB> adjB(adj*(A*BD + AtB*D.transpose()));
63 for (int j = 0; j < B.cols(); j++)
64 for (int i = 0; i < B.rows(); i++)
65 (*varB)(i, j).vi_->adj_ += adjB(i, j);
66 }
67 if (varA) {
68 Eigen::Matrix<double, RA, CA> adjA(adj*(B*BD.transpose()));
69 for (int j = 0; j < A.cols(); j++)
70 for (int i = 0; i < A.rows(); i++)
71 (*varA)(i, j).vi_->adj_ += adjA(i, j);
72 }
73 if (varD) {
74 Eigen::Matrix<double, RD, CD> adjD(adj*(B.transpose()*AtB));
75 for (int j = 0; j < D.cols(); j++)
76 for (int i = 0; i < D.rows(); i++)
77 (*varD)(i, j).vi_->adj_ += adjD(i, j);
78 }
79 }
80
81 public:
82 explicit
83 trace_gen_quad_form_vari(trace_gen_quad_form_vari_alloc
84 <TD, RD, CD, TA, RA, CA, TB, RB, CB> *impl)
85 : vari(impl->compute()), impl_(impl) { }
86
87 virtual void chain() {
88 computeAdjoints(adj_,
89 value_of(impl_->D_),
90 value_of(impl_->A_),
91 value_of(impl_->B_),
92 reinterpret_cast<Eigen::Matrix<var, RD, CD> *>
93 (boost::is_same<TD, var>::value?(&impl_->D_):NULL),
94 reinterpret_cast<Eigen::Matrix<var, RA, CA> *>
95 (boost::is_same<TA, var>::value?(&impl_->A_):NULL),
96 reinterpret_cast<Eigen::Matrix<var, RB, CB> *>
97 (boost::is_same<TB, var>::value?(&impl_->B_):NULL));
98 }
99
100 trace_gen_quad_form_vari_alloc<TD, RD, CD, TA, RA, CA, TB, RB, CB>
101 *impl_;
102 };
103 }
104
105 template <typename TD, int RD, int CD,
106 typename TA, int RA, int CA,
107 typename TB, int RB, int CB>
108 inline typename
109 boost::enable_if_c< boost::is_same<TD, var>::value ||
110 boost::is_same<TA, var>::value ||
111 boost::is_same<TB, var>::value,
112 var >::type
113 trace_gen_quad_form(const Eigen::Matrix<TD, RD, CD>& D,
114 const Eigen::Matrix<TA, RA, CA>& A,
115 const Eigen::Matrix<TB, RB, CB>& B) {
116 check_square("trace_gen_quad_form", "A", A);
117 check_square("trace_gen_quad_form", "D", D);
118 check_multiplicable("trace_gen_quad_form",
119 "A", A,
120 "B", B);
121 check_multiplicable("trace_gen_quad_form",
122 "B", B,
123 "D", D);
124
125 trace_gen_quad_form_vari_alloc<TD, RD, CD, TA, RA, CA, TB, RB, CB>
126 *baseVari
127 = new trace_gen_quad_form_vari_alloc<TD, RD, CD, TA, RA, CA, TB, RB, CB>
128 (D, A, B);
129
130 return var(new trace_gen_quad_form_vari
131 <TD, RD, CD, TA, RA, CA, TB, RB, CB>(baseVari));
132 }
133
134 }
135 }
136 #endif
B_
Eigen::Matrix< TB, RB, CB > B_
Definition: trace_gen_quad_form.hpp:38
core.hpp
check_multiplicable.hpp
A_
Eigen::Matrix< TA, RA, CA > A_
Definition: trace_gen_quad_form.hpp:37
stan
Definition: log_sum_exp.hpp:8
stan::math::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
D_
Eigen::Matrix< TD, RD, CD > D_
Definition: trace_gen_quad_form.hpp:36
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
stan::math::trace_gen_quad_form
fvar< T > trace_gen_quad_form(const Eigen::Matrix< fvar< T >, RD, CD > &D, const Eigen::Matrix< fvar< T >, RA, CA > &A, const Eigen::Matrix< fvar< T >, RB, CB > &B)
Definition: trace_gen_quad_form.hpp:16
trace_gen_quad_form.hpp
typedefs.hpp
stan::math::check_multiplicable
bool check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Return true if the matrices can be multiplied.
Definition: check_multiplicable.hpp:32
Eigen.hpp
value_of.hpp
impl_
trace_gen_quad_form_vari_alloc< TD, RD, CD, TA, RA, CA, TB, RB, CB > * impl_
Definition: trace_gen_quad_form.hpp:101
stan::math::check_square
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
Definition: check_square.hpp:28
value_of.hpp
typedefs.hpp
check_square.hpp

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