Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
quad_form.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__MATRIX__QUAD_FORM_HPP
2 #define STAN__MATH__MATRIX__QUAD_FORM_HPP
3 
4 #include <boost/utility/enable_if.hpp>
5 #include <boost/type_traits.hpp>
13 
14 namespace stan {
15  namespace math {
19  template<int RA,int CA,int RB,int CB,typename T>
20  inline Eigen::Matrix<T,CB,CB>
21  quad_form(const Eigen::Matrix<T,RA,CA>& A,
22  const Eigen::Matrix<T,RB,CB>& B)
23  {
25  stan::math::check_square("quad_form(%1%)",A,"A",(double*)0);
26  stan::math::check_multiplicable("quad_form(%1%)",A,"A",
27  B,"B",(double*)0);
28  return multiply(stan::math::transpose(B),multiply(A,B));
29  }
30 
31  template<int RA,int CA,int RB,typename T>
32  inline T
33  quad_form(const Eigen::Matrix<T,RA,CA>& A,
34  const Eigen::Matrix<T,RB,1>& B)
35  {
38 
39  stan::math::check_square("quad_form(%1%)",A,"A",(double*)0);
40  stan::math::check_multiplicable("quad_form(%1%)",A,"A",
41  B,"B",(double*)0);
42  return dot_product(B,multiply(A,B));
43  }
44 
45  template<int RA,int CA,int RB,int CB,typename T>
46  inline Eigen::Matrix<T,CB,CB>
47  quad_form_sym(const Eigen::Matrix<T,RA,CA>& A,
48  const Eigen::Matrix<T,RB,CB>& B)
49  {
51 
52  stan::math::check_square("quad_form_sym(%1%)",A,"A",(double*)0);
53  stan::math::check_multiplicable("quad_form_sym(%1%)",A,"A",
54  B,"B",(double*)0);
55  stan::math::check_symmetric("quad_form_sym(%1%)",A,"A",(double*)0);
56  Eigen::Matrix<T,CB,CB> ret(multiply(stan::math::transpose(B),multiply(A,B)));
57  return 0.5*(ret + stan::math::transpose(ret));
58  }
59 
60  template<int RA,int CA,int RB,typename T>
61  inline T
62  quad_form_sym(const Eigen::Matrix<T,RA,CA>& A,
63  const Eigen::Matrix<T,RB,1>& B)
64  {
67 
68  stan::math::check_square("quad_form_sym(%1%)",A,"A",(double*)0);
69  stan::math::check_multiplicable("quad_form_sym(%1%)",A,"A",
70  B,"B",(double*)0);
71  stan::math::check_symmetric("quad_form_sym(%1%)",A,"A",(double*)0);
72  return dot_product(B,multiply(A,B));
73  }
74  }
75 }
76 
77 #endif
78 
Eigen::Matrix< T, CB, CB > quad_form_sym(const Eigen::Matrix< T, RA, CA > &A, const Eigen::Matrix< T, RB, CB > &B)
Definition: quad_form.hpp:47
bool check_symmetric(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is symmetric.
bool check_multiplicable(const char *function, const T1 &y1, const char *name1, const T2 &y2, const char *name2, T_result *result)
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
Definition: transpose.hpp:12
boost::enable_if_c< boost::is_arithmetic< T >::value, Eigen::Matrix< double, R, C > >::type multiply(const Eigen::Matrix< double, R, C > &m, T c)
Return specified matrix multiplied by specified scalar.
Definition: multiply.hpp:25
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
bool check_square(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is square.
double dot_product(const Eigen::Matrix< double, R1, C1 > &v1, const Eigen::Matrix< double, R2, C2 > &v2)
Returns the dot product of the specified vectors.
Definition: dot_product.hpp:22

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