| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

quad_form.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_QUAD_FORM_HPP
2 #define STAN_MATH_REV_MAT_FUN_QUAD_FORM_HPP
3
4 #include <boost/utility/enable_if.hpp>
5 #include <boost/type_traits.hpp>
6 #include <stan/math/rev/core.hpp>
7 #include <stan/math/prim/mat/fun/Eigen.hpp>
8 #include <stan/math/prim/mat/fun/typedefs.hpp>
9 #include <stan/math/rev/mat/fun/typedefs.hpp>
10 #include <stan/math/prim/mat/fun/value_of.hpp>
11 #include <stan/math/prim/mat/fun/quad_form.hpp>
12 #include <stan/math/prim/mat/err/check_multiplicable.hpp>
13 #include <stan/math/prim/mat/err/check_square.hpp>
14 #include <stan/math/prim/mat/err/check_symmetric.hpp>
15
16 namespace stan {
17 namespace math {
18
19 namespace {
20 template <typename TA, int RA, int CA, typename TB, int RB, int CB>
21 class quad_form_vari_alloc : public chainable_alloc {
22 private:
23 inline void compute(const Eigen::Matrix<double, RA, CA>& A,
24 const Eigen::Matrix<double, RB, CB>& B) {
25 Eigen::Matrix<double, CB, CB> Cd(B.transpose()*A*B);
26 for (int j = 0; j < C_.cols(); j++) {
27 for (int i = 0; i < C_.rows(); i++) {
28 if (sym_) {
29 C_(i, j) = var(new vari(0.5*(Cd(i, j) + Cd(j, i)), false));
30 } else {
31 C_(i, j) = var(new vari(Cd(i, j), false));
32 }
33 }
34 }
35 }
36
37 public:
38 quad_form_vari_alloc(const Eigen::Matrix<TA, RA, CA>& A,
39 const Eigen::Matrix<TB, RB, CB>& B,
40 bool symmetric = false)
41 : A_(A), B_(B), C_(B_.cols(), B_.cols()), sym_(symmetric) {
42 compute(value_of(A), value_of(B));
43 }
44
45 Eigen::Matrix<TA, RA, CA> A_;
46 Eigen::Matrix<TB, RB, CB> B_;
47 Eigen::Matrix<var, CB, CB> C_;
48 bool sym_;
49 };
50
51 template <typename TA, int RA, int CA, typename TB, int RB, int CB>
52 class quad_form_vari : public vari {
53 protected:
54 inline void chainA(Eigen::Matrix<double, RA, CA>& A,
55 const Eigen::Matrix<double, RB, CB>& Bd,
56 const Eigen::Matrix<double, CB, CB>& adjC) {}
57 inline void chainB(Eigen::Matrix<double, RB, CB>& B,
58 const Eigen::Matrix<double, RA, CA>& Ad,
59 const Eigen::Matrix<double, RB, CB>& Bd,
60 const Eigen::Matrix<double, CB, CB>& adjC) {}
61
62 inline void chainA(Eigen::Matrix<var, RA, CA>& A,
63 const Eigen::Matrix<double, RB, CB>& Bd,
64 const Eigen::Matrix<double, CB, CB>& adjC) {
65 Eigen::Matrix<double, RA, CA> adjA(Bd*adjC*Bd.transpose());
66 for (int j = 0; j < A.cols(); j++) {
67 for (int i = 0; i < A.rows(); i++) {
68 A(i, j).vi_->adj_ += adjA(i, j);
69 }
70 }
71 }
72 inline void chainB(Eigen::Matrix<var, RB, CB>& B,
73 const Eigen::Matrix<double, RA, CA>& Ad,
74 const Eigen::Matrix<double, RB, CB>& Bd,
75 const Eigen::Matrix<double, CB, CB>& adjC) {
76 Eigen::Matrix<double, RA, CA> adjB(Ad * Bd * adjC.transpose()
77 + Ad.transpose()*Bd*adjC);
78 for (int j = 0; j < B.cols(); j++)
79 for (int i = 0; i < B.rows(); i++)
80 B(i, j).vi_->adj_ += adjB(i, j);
81 }
82
83 inline void chainAB(Eigen::Matrix<TA, RA, CA>& A,
84 Eigen::Matrix<TB, RB, CB>& B,
85 const Eigen::Matrix<double, RA, CA>& Ad,
86 const Eigen::Matrix<double, RB, CB>& Bd,
87 const Eigen::Matrix<double, CB, CB>& adjC) {
88 chainA(A, Bd, adjC);
89 chainB(B, Ad, Bd, adjC);
90 }
91
92 public:
93 quad_form_vari(const Eigen::Matrix<TA, RA, CA>& A,
94 const Eigen::Matrix<TB, RB, CB>& B,
95 bool symmetric = false)
96 : vari(0.0) {
97 impl_
98 = new quad_form_vari_alloc<TA, RA, CA, TB, RB, CB>(A, B, symmetric);
99 }
100
101 virtual void chain() {
102 Eigen::Matrix<double, CB, CB> adjC(impl_->C_.rows(),
103 impl_->C_.cols());
104
105 for (int j = 0; j < impl_->C_.cols(); j++)
106 for (int i = 0; i < impl_->C_.rows(); i++)
107 adjC(i, j) = impl_->C_(i, j).vi_->adj_;
108
109 chainAB(impl_->A_, impl_->B_,
110 value_of(impl_->A_), value_of(impl_->B_),
111 adjC);
112 }
113
114 quad_form_vari_alloc<TA, RA, CA, TB, RB, CB> *impl_;
115 };
116 }
117
118 template <typename TA, int RA, int CA, typename TB, int RB, int CB>
119 inline typename
120 boost::enable_if_c< boost::is_same<TA, var>::value ||
121 boost::is_same<TB, var>::value,
122 Eigen::Matrix<var, CB, CB> >::type
123 quad_form(const Eigen::Matrix<TA, RA, CA>& A,
124 const Eigen::Matrix<TB, RB, CB>& B) {
125 check_square("quad_form", "A", A);
126 check_multiplicable("quad_form",
127 "A", A,
128 "B", B);
129
130 quad_form_vari<TA, RA, CA, TB, RB, CB> *baseVari
131 = new quad_form_vari<TA, RA, CA, TB, RB, CB>(A, B);
132
133 return baseVari->impl_->C_;
134 }
135 template <typename TA, int RA, int CA, typename TB, int RB>
136 inline typename
137 boost::enable_if_c< boost::is_same<TA, var>::value ||
138 boost::is_same<TB, var>::value,
139 var >::type
140 quad_form(const Eigen::Matrix<TA, RA, CA>& A,
141 const Eigen::Matrix<TB, RB, 1>& B) {
142 check_square("quad_form", "A", A);
143 check_multiplicable("quad_form",
144 "A", A,
145 "B", B);
146
147 quad_form_vari<TA, RA, CA, TB, RB, 1> *baseVari
148 = new quad_form_vari<TA, RA, CA, TB, RB, 1>(A, B);
149
150 return baseVari->impl_->C_(0, 0);
151 }
152
153 }
154 }
155 #endif
core.hpp
check_multiplicable.hpp
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
impl_
quad_form_vari_alloc< TA, RA, CA, TB, RB, CB > * impl_
Definition: quad_form.hpp:114
B_
Eigen::Matrix< TB, RB, CB > B_
Definition: quad_form.hpp:46
check_symmetric.hpp
A_
Eigen::Matrix< TA, RA, CA > A_
Definition: quad_form.hpp:45
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
stan::math::cols
int cols(const Eigen::Matrix< T, R, C > &m)
Return the number of columns in the specified matrix, vector, or row vector.
Definition: cols.hpp:20
Eigen.hpp
value_of.hpp
C_
Eigen::Matrix< var, CB, CB > C_
Definition: quad_form.hpp:47
quad_form.hpp
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
sym_
bool sym_
Definition: quad_form.hpp:48
stan::math::quad_form
Eigen::Matrix< T, CB, CB > quad_form(const Eigen::Matrix< T, RA, CA > &A, const Eigen::Matrix< T, RB, CB > &B)
Compute B^T A B.
Definition: quad_form.hpp:21
typedefs.hpp
check_square.hpp

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