Automatic Differentiation
 
Loading...
Searching...
No Matches
quad_form_sym.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_QUAD_FORM_SYM_HPP
2#define STAN_MATH_FWD_FUN_QUAD_FORM_SYM_HPP
3
8
9namespace stan {
10namespace math {
11
26template <typename EigMat1, typename EigMat2,
27 require_all_eigen_t<EigMat1, EigMat2>* = nullptr,
28 require_not_eigen_col_vector_t<EigMat2>* = nullptr,
29 require_any_vt_fvar<EigMat1, EigMat2>* = nullptr>
31 EigMat1&& A, EigMat2&& B) {
33 check_multiplicable("quad_form_sym", "A", A, "B", B);
34 decltype(auto) A_ref = to_ref(std::forward<EigMat1>(A));
35 check_symmetric("quad_form_sym", "A", A_ref);
36 decltype(auto) B_ref = to_ref(std::forward<EigMat2>(B));
38 multiply(B_ref.transpose(),
39 multiply(std::forward<decltype(A_ref)>(A_ref), B_ref)));
40 return make_holder(
41 [](auto&& ret) { return T_ret(0.5) * (ret + ret.transpose()); },
42 std::move(ret));
43}
44
57template <typename EigMat, typename ColVec, require_eigen_t<EigMat>* = nullptr,
58 require_eigen_col_vector_t<ColVec>* = nullptr,
59 require_any_vt_fvar<EigMat, ColVec>* = nullptr>
60inline return_type_t<EigMat, ColVec> quad_form_sym(EigMat&& A, ColVec&& B) {
61 check_multiplicable("quad_form_sym", "A", A, "B", B);
62 decltype(auto) A_ref = to_ref(std::forward<EigMat>(A));
63 check_symmetric("quad_form_sym", "A", A_ref);
64 decltype(auto) B_ref = to_ref(std::forward<ColVec>(B));
65 return make_holder(
66 [](auto&& B_ref_, auto&& A_ref_) {
67 return dot_product(
68 B_ref_, multiply(std::forward<decltype(A_ref_)>(A_ref_), B_ref_));
69 },
70 std::forward<decltype(B_ref)>(B_ref),
71 std::forward<decltype(A_ref)>(A_ref));
72}
73
74} // namespace math
75} // namespace stan
76
77#endif
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
auto multiply(Mat1 &&m1, Mat2 &&m2)
Return the product of the specified matrices.
Definition multiply.hpp:20
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.
typename promote_scalar_type< std::decay_t< T >, std::decay_t< S > >::type promote_scalar_t
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:481
promote_scalar_t< return_type_t< EigMat1, EigMat2 >, EigMat2 > quad_form_sym(EigMat1 &&A, EigMat2 &&B)
Return the quadratic form of a symmetric matrix.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...