Automatic Differentiation
 
Loading...
Searching...
No Matches
quad_form.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_QUAD_FORM_HPP
2#define STAN_MATH_PRIM_FUN_QUAD_FORM_HPP
3
7
8namespace stan {
9namespace math {
10
26template <typename EigMat1, typename EigMat2,
27 require_all_eigen_t<EigMat1, EigMat2>* = nullptr,
28 require_not_eigen_col_vector_t<EigMat2>* = nullptr,
29 require_vt_same<EigMat1, EigMat2>* = nullptr,
30 require_all_vt_arithmetic<EigMat1, EigMat2>* = nullptr>
31inline auto quad_form(const EigMat1& A, const EigMat2& B) {
32 check_square("quad_form", "A", A);
33 check_multiplicable("quad_form", "A", A, "B", B);
34 return make_holder(
35 [](const auto& b, const auto& a) { return b.transpose() * a * b; },
36 to_ref(B), to_ref(A));
37}
38
51template <typename EigMat, typename ColVec, require_eigen_t<EigMat>* = nullptr,
52 require_eigen_col_vector_t<ColVec>* = nullptr,
53 require_vt_same<EigMat, ColVec>* = nullptr,
54 require_all_vt_arithmetic<EigMat, ColVec>* = nullptr>
55inline value_type_t<EigMat> quad_form(const EigMat& A, const ColVec& B) {
56 check_square("quad_form", "A", A);
57 check_multiplicable("quad_form", "A", A, "B", B);
58 const auto& B_ref = to_ref(B);
59 return B_ref.dot(A * B_ref);
60}
61
62} // namespace math
63} // namespace stan
64
65#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
auto make_holder(const F &func, Args &&... args)
Constructs an expression from given arguments using given functor.
Definition holder.hpp:352
void check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the matrices can be multiplied.
promote_scalar_t< return_type_t< EigMat1, EigMat2 >, EigMat2 > quad_form(const EigMat1 &A, const EigMat2 &B)
Return the quadratic form .
Definition quad_form.hpp:31
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...