Stan Math Library: stan/math/rev/mat/fun/quad_form.hpp Source File

| Stan Math Library 2.10.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 namespace {
19 template <typename TA, int RA, int CA, typename TB, int RB, int CB>
20 class quad_form_vari_alloc : public chainable_alloc {
21 private:
22 inline void compute(const Eigen::Matrix<double, RA, CA>& A,
23 const Eigen::Matrix<double, RB, CB>& B) {
24 Eigen::Matrix<double, CB, CB> Cd(B.transpose()*A*B);
25 for (int j = 0; j < C_.cols(); j++) {
26 for (int i = 0; i < C_.rows(); i++) {
27 if (_sym) {
28 C_(i, j) = var(new vari(0.5*(Cd(i, j) + Cd(j, i)), false));
29 } else {
30 C_(i, j) = var(new vari(Cd(i, j), false));
31 }
32 }
33 }
34 }
35
36 public:
37 quad_form_vari_alloc(const Eigen::Matrix<TA, RA, CA>& A,
38 const Eigen::Matrix<TB, RB, CB>& B,
39 bool symmetric = false)
40 : A_(A), B_(B), C_(B_.cols(), B_.cols()), _sym(symmetric) {
41 using stan::math::value_of;
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 using stan::math::value_of;
103 Eigen::Matrix<double, CB, CB> adjC(_impl->C_.rows(),
104 _impl->C_.cols());
105
106 for (int j = 0; j < _impl->C_.cols(); j++)
107 for (int i = 0; i < _impl->C_.rows(); i++)
108 adjC(i, j) = _impl->C_(i, j).vi_->adj_;
109
110 chainAB(_impl->A_, _impl->B_,
111 value_of(_impl->A_), value_of(_impl->B_),
112 adjC);
113 }
114
115 quad_form_vari_alloc<TA, RA, CA, TB, RB, CB> *_impl;
116 };
117 }
118
119 template <typename TA, int RA, int CA, typename TB, int RB, int CB>
120 inline typename
121 boost::enable_if_c< boost::is_same<TA, var>::value ||
122 boost::is_same<TB, var>::value,
123 Eigen::Matrix<var, CB, CB> >::type
124 quad_form(const Eigen::Matrix<TA, RA, CA>& A,
125 const Eigen::Matrix<TB, RB, CB>& B) {
126 stan::math::check_square("quad_form", "A", A);
127 stan::math::check_multiplicable("quad_form",
128 "A", A,
129 "B", B);
130
131 quad_form_vari<TA, RA, CA, TB, RB, CB> *baseVari
132 = new quad_form_vari<TA, RA, CA, TB, RB, CB>(A, B);
133
134 return baseVari->_impl->C_;
135 }
136 template <typename TA, int RA, int CA, typename TB, int RB>
137 inline typename
138 boost::enable_if_c< boost::is_same<TA, var>::value ||
139 boost::is_same<TB, var>::value,
140 var >::type
141 quad_form(const Eigen::Matrix<TA, RA, CA>& A,
142 const Eigen::Matrix<TB, RB, 1>& B) {
143 stan::math::check_square("quad_form", "A", A);
144 stan::math::check_multiplicable("quad_form",
145 "A", A,
146 "B", B);
147
148 quad_form_vari<TA, RA, CA, TB, RB, 1> *baseVari
149 = new quad_form_vari<TA, RA, CA, TB, RB, 1>(A, B);
150
151 return baseVari->_impl->C_(0, 0);
152 }
153
154 }
155 }
156
157 #endif
_sym
bool _sym
Definition: quad_form.hpp:48
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
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:33
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
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
_impl
quad_form_vari_alloc< TA, RA, CA, TB, RB, CB > * _impl
Definition: quad_form.hpp:115
typedefs.hpp
check_square.hpp

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