1 #ifndef STAN_MATH_REV_FUN_QUAD_FORM_SYM_HPP
2 #define STAN_MATH_REV_FUN_QUAD_FORM_SYM_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
6 #include <stan/math/prim/err.hpp>
7 #include <stan/math/prim/fun/Eigen.hpp>
8 #include <stan/math/prim/fun/to_ref.hpp>
9 #include <stan/math/rev/fun/quad_form.hpp>
10 #include <type_traits>
11 
12 namespace stan {
13 namespace math {
14 
15 /**
16  * Return the quadratic form \f$ B^T A B \f$ of a symmetric matrix.
17  *
18  * Symmetry of the resulting matrix is guaranteed.
19  *
20  * @tparam EigMat1 type of the first (symmetric) matrix
21  * @tparam EigMat2 type of the second matrix
22  *
23  * @param A symmetric matrix
24  * @param B second matrix
25  * @return The quadratic form, which is a symmetric matrix of size Cb.
26  * If \c B is a column vector returns a scalar.
27  * @throws std::invalid_argument if A is not symmetric, or if A cannot be
28  * multiplied by B
29  */
30 template <typename EigMat1, typename EigMat2,
31           require_all_eigen_t<EigMat1, EigMat2>* = nullptr,
32           require_any_vt_var<EigMat1, EigMat2>* = nullptr>
quad_form_sym(const EigMat1 & A,const EigMat2 & B)33 inline auto quad_form_sym(const EigMat1& A, const EigMat2& B) {
34   check_multiplicable("quad_form_sym", "A", A, "B", B);
35   const auto& A_ref = to_ref(A);
36   check_symmetric("quad_form_sym", "A", A_ref);
37   return quad_form(A_ref, B, true);
38 }
39 
40 }  // namespace math
41 }  // namespace stan
42 #endif
43