1 #ifndef STAN_MATH_REV_MAT_FUN_TRACE_GEN_QUAD_FORM_HPP
2 #define STAN_MATH_REV_MAT_FUN_TRACE_GEN_QUAD_FORM_HPP
4 #include <boost/utility/enable_if.hpp>
5 #include <boost/type_traits.hpp>
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 {
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)
36 Eigen::Matrix<TD, RD, CD>
D_;
37 Eigen::Matrix<TA, RA, CA>
A_;
38 Eigen::Matrix<TB, RB, CB>
B_;
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 {
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;
59 AtB.noalias() = A.transpose()*B;
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);
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);
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);
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) { }
87 virtual void chain() {
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));
100 trace_gen_quad_form_vari_alloc<TD, RD, CD, TA, RA, CA, TB, RB, CB>
105 template <
typename TD,
int RD,
int CD,
106 typename TA,
int RA,
int CA,
107 typename TB,
int RB,
int CB>
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,
114 const Eigen::Matrix<TA, RA, CA>& A,
115 const Eigen::Matrix<TB, RB, CB>& B) {
125 trace_gen_quad_form_vari_alloc<TD, RD, CD, TA, RA, CA, TB, RB, CB>
127 =
new trace_gen_quad_form_vari_alloc<TD, RD, CD, TA, RA, CA, TB, RB, CB>
130 return var(
new trace_gen_quad_form_vari
131 <TD, RD, CD, TA, RA, CA, TB, RB, CB>(baseVari));
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Independent (input) and dependent (output) variables for gradients.
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)
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.
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.