| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

trace_quad_form.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_TRACE_QUAD_FORM_HPP
2 #define STAN_MATH_REV_MAT_FUN_TRACE_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/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_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 TA, int RA, int CA, typename TB, int RB, int CB>
20 class trace_quad_form_vari_alloc : public chainable_alloc {
21 public:
22 trace_quad_form_vari_alloc(const Eigen::Matrix<TA, RA, CA>& A,
23 const Eigen::Matrix<TB, RB, CB>& B)
24 : A_(A), B_(B)
25 { }
26
27 double compute() {
28 return trace_quad_form(value_of(A_),
29 value_of(B_));
30 }
31
32 Eigen::Matrix<TA, RA, CA> A_;
33 Eigen::Matrix<TB, RB, CB> B_;
34 };
35
36 template <typename TA, int RA, int CA, typename TB, int RB, int CB>
37 class trace_quad_form_vari : public vari {
38 protected:
39 static inline void chainA(Eigen::Matrix<double, RA, CA>& A,
40 const Eigen::Matrix<double, RB, CB>& Bd,
41 double adjC) {}
42 static inline void chainB(Eigen::Matrix<double, RB, CB>& B,
43 const Eigen::Matrix<double, RA, CA>& Ad,
44 const Eigen::Matrix<double, RB, CB>& Bd,
45 double adjC) {}
46
47 static inline void chainA(Eigen::Matrix<var, RA, CA>& A,
48 const Eigen::Matrix<double, RB, CB>& Bd,
49 double adjC) {
50 Eigen::Matrix<double, RA, CA> adjA(adjC*Bd*Bd.transpose());
51 for (int j = 0; j < A.cols(); j++)
52 for (int i = 0; i < A.rows(); i++)
53 A(i, j).vi_->adj_ += adjA(i, j);
54 }
55 static inline void chainB(Eigen::Matrix<var, RB, CB>& B,
56 const Eigen::Matrix<double, RA, CA>& Ad,
57 const Eigen::Matrix<double, RB, CB>& Bd,
58 double adjC) {
59 Eigen::Matrix<double, RA, CA> adjB(adjC*(Ad + Ad.transpose())*Bd);
60 for (int j = 0; j < B.cols(); j++)
61 for (int i = 0; i < B.rows(); i++)
62 B(i, j).vi_->adj_ += adjB(i, j);
63 }
64
65 inline void chainAB(Eigen::Matrix<TA, RA, CA>& A,
66 Eigen::Matrix<TB, RB, CB>& B,
67 const Eigen::Matrix<double, RA, CA>& Ad,
68 const Eigen::Matrix<double, RB, CB>& Bd,
69 double adjC) {
70 chainA(A, Bd, adjC);
71 chainB(B, Ad, Bd, adjC);
72 }
73
74 public:
75 explicit
76 trace_quad_form_vari
77 (trace_quad_form_vari_alloc<TA, RA, CA, TB, RB, CB> *impl)
78 : vari(impl->compute()), impl_(impl) { }
79
80 virtual void chain() {
81 chainAB(impl_->A_, impl_->B_,
82 value_of(impl_->A_), value_of(impl_->B_),
83 adj_);
84 }
85
86 trace_quad_form_vari_alloc<TA, RA, CA, TB, RB, CB> *impl_;
87 };
88 }
89
90 template <typename TA, int RA, int CA, typename TB, int RB, int CB>
91 inline typename
92 boost::enable_if_c< boost::is_same<TA, var>::value ||
93 boost::is_same<TB, var>::value,
94 var >::type
95 trace_quad_form(const Eigen::Matrix<TA, RA, CA>& A,
96 const Eigen::Matrix<TB, RB, CB>& B) {
97 check_square("trace_quad_form", "A", A);
98 check_multiplicable("trace_quad_form",
99 "A", A,
100 "B", B);
101
102 trace_quad_form_vari_alloc<TA, RA, CA, TB, RB, CB> *baseVari
103 = new trace_quad_form_vari_alloc<TA, RA, CA, TB, RB, CB>(A, B);
104
105 return var(new trace_quad_form_vari<TA, RA, CA, TB, RB, CB>(baseVari));
106 }
107
108 }
109 }
110 #endif
core.hpp
check_multiplicable.hpp
stan::math::trace_quad_form
fvar< T > trace_quad_form(const Eigen::Matrix< fvar< T >, RA, CA > &A, const Eigen::Matrix< fvar< T >, RB, CB > &B)
Definition: trace_quad_form.hpp:18
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
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
A_
Eigen::Matrix< TA, RA, CA > A_
Definition: trace_quad_form.hpp:32
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
trace_quad_form.hpp
B_
Eigen::Matrix< TB, RB, CB > B_
Definition: trace_quad_form.hpp:33
Eigen.hpp
value_of.hpp
impl_
trace_quad_form_vari_alloc< TA, RA, CA, TB, RB, CB > * impl_
Definition: trace_quad_form.hpp:86
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.