Automatic Differentiation
 
Loading...
Searching...
No Matches
quad_form_sym.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_QUAD_FORM_SYM_HPP
2#define STAN_MATH_PRIM_FUN_QUAD_FORM_SYM_HPP
3
7
8namespace stan {
9namespace math {
10
25template <typename EigMat1, typename EigMat2,
26 require_all_eigen_t<EigMat1, EigMat2>* = nullptr,
27 require_not_eigen_col_vector_t<EigMat2>* = nullptr,
28 require_vt_same<EigMat1, EigMat2>* = nullptr,
29 require_all_vt_arithmetic<EigMat1, EigMat2>* = nullptr>
30inline plain_type_t<EigMat2> quad_form_sym(const EigMat1& A, const EigMat2& B) {
31 check_multiplicable("quad_form_sym", "A", A, "B", B);
32 const auto& A_ref = to_ref(A);
33 const auto& B_ref = to_ref(B);
34 check_symmetric("quad_form_sym", "A", A_ref);
35 return make_holder(
36 [](const auto& ret) { return 0.5 * (ret + ret.transpose()); },
37 (B_ref.transpose() * A_ref * B_ref).eval());
38}
39
52template <typename EigMat, typename ColVec, require_eigen_t<EigMat>* = nullptr,
53 require_eigen_col_vector_t<ColVec>* = nullptr,
54 require_vt_same<EigMat, ColVec>* = nullptr,
55 require_all_vt_arithmetic<EigMat, ColVec>* = nullptr>
56inline value_type_t<EigMat> quad_form_sym(const EigMat& A, const ColVec& B) {
57 check_multiplicable("quad_form_sym", "A", A, "B", B);
58 const auto& A_ref = to_ref(A);
59 const auto& B_ref = to_ref(B);
60 check_symmetric("quad_form_sym", "A", A_ref);
61 return B_ref.dot(A_ref * B_ref);
62}
63
64} // namespace math
65} // namespace stan
66
67#endif
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
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.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
promote_scalar_t< return_type_t< EigMat1, EigMat2 >, EigMat2 > quad_form_sym(const EigMat1 &A, const EigMat2 &B)
Return the quadratic form of a symmetric matrix.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9